2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.common;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.primitives.UnsignedLong;
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.math.BigInteger;
23 import org.junit.Test;
25 public class Uint64Test {
27 public void testValueOf() {
28 assertEquals(127, Uint64.valueOf(Byte.MAX_VALUE).byteValue());
29 assertEquals(32767, Uint64.valueOf(Short.MAX_VALUE).shortValue());
30 assertEquals(2147483647, Uint64.valueOf(Integer.MAX_VALUE).intValue());
31 assertEquals(9223372036854775807L, Uint64.valueOf(Long.MAX_VALUE).longValue());
32 assertEquals(0, Uint64.valueOf("0").intValue());
33 assertEquals(2170205184637009920L, Uint64.valueOf(2170205184637009920L).longValue());
34 assertEquals(2170205184637009920L, Uint64.valueOf(new BigInteger("2170205184637009920")).longValue());
38 public void testCompareTo() {
39 final Uint64 five = Uint64.valueOf(5);
40 final Uint64 zero = Uint64.valueOf(0);
41 final Uint64 max = Uint64.valueOf(4294967295L);
43 assertEquals(0, zero.compareTo(zero));
44 assertEquals(-1, zero.compareTo(five));
45 assertEquals(-1, zero.compareTo(max));
47 assertEquals(1, five.compareTo(zero));
48 assertEquals(0, five.compareTo(five));
49 assertEquals(-1, five.compareTo(max));
51 assertEquals(1, max.compareTo(zero));
52 assertEquals(1, max.compareTo(five));
53 assertEquals(0, max.compareTo(max));
57 public void testEquals() {
58 final Uint64 five = Uint64.valueOf(5);
59 final Uint64 zero = Uint64.valueOf(0);
60 final Uint64 max = Uint64.valueOf(4294967295L);
62 final Uint64 test = new Uint64(five);
63 assertFalse(test.equals(zero));
64 assertFalse(test.equals(new Object()));
65 assertFalse(test.equals(max));
66 assertTrue(test.equals(test));
67 assertTrue(test.equals(five));
68 assertTrue(five.equals(test));
72 public void testToString() {
73 assertEquals("0", Uint64.valueOf(0).toString());
74 assertEquals("2147483647", Uint64.valueOf(2147483647L).toString());
75 assertEquals("2147483648", Uint64.valueOf(2147483648L).toString());
76 assertEquals("4294967295", Uint64.valueOf(4294967295L).toString());
80 public void testHashCode() {
81 assertEquals(Long.hashCode(-63), Uint64.fromLongBits(-63L).hashCode());
85 public void testFloatValue() {
86 assertEquals(0, Uint64.valueOf(0).floatValue(), 0);
90 public void testDoubleValue() {
91 assertEquals(0, Uint64.valueOf(0).doubleValue(), 0);
95 public void testConversions() {
96 assertSame(Uint64.valueOf(5), Uint64.valueOf(Uint8.valueOf(5)));
97 assertSame(Uint64.valueOf(10), Uint64.valueOf(Uint16.valueOf(10)));
98 assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
99 assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
101 assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
102 assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
104 assertEquals(Uint8.TEN, Uint64.TEN.toUint8());
105 assertEquals(Uint16.TEN, Uint64.TEN.toUint16());
106 assertEquals(Uint32.TEN, Uint64.TEN.toUint32());
110 public void testToUint8() {
111 assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint8());
115 public void testToUint16() {
116 assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint16());
120 public void testToUint32() {
121 assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint32());
125 public void testSerialization() throws IOException, ClassNotFoundException {
126 final Uint64 source = Uint64.valueOf(255);
127 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
128 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
129 oos.writeObject(source);
133 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
134 read = ois.readObject();
137 assertSame(source, read);
141 public void testNegativeValues() {
142 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((byte)-1));
143 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((short)-1));
144 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1));
145 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1L));
146 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(new BigInteger("-1")));
150 public void testLargeValues() {
151 final BigInteger big = new BigInteger("10000000000000000", 16);
152 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(big));
156 public void testNullValueOf() {
157 assertThrows(NullPointerException.class, () -> Uint64.valueOf((String) null));
158 assertThrows(NullPointerException.class, () -> Uint64.valueOf((BigInteger) null));