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 testSaturatedOf() {
39 assertEquals(127, Uint64.saturatedOf((byte) 127).byteValue());
40 assertEquals(127, Uint64.saturatedOf((short) 127).byteValue());
41 assertEquals(127, Uint64.saturatedOf(127).byteValue());
42 assertEquals(127, Uint64.saturatedOf(127L).byteValue());
44 assertEquals(255, Uint64.saturatedOf((short) 255).intValue());
45 assertEquals(255, Uint64.saturatedOf(255).intValue());
46 assertEquals(255L, Uint64.saturatedOf(255L).longValue());
47 assertEquals(2170205184637009920L, Uint64.saturatedOf(new BigInteger("2170205184637009920")).longValue());
51 public void testCompareTo() {
52 final Uint64 five = Uint64.valueOf(5);
53 final Uint64 zero = Uint64.valueOf(0);
54 final Uint64 max = Uint64.valueOf(4294967295L);
56 assertEquals(0, zero.compareTo(zero));
57 assertEquals(-1, zero.compareTo(five));
58 assertEquals(-1, zero.compareTo(max));
60 assertEquals(1, five.compareTo(zero));
61 assertEquals(0, five.compareTo(five));
62 assertEquals(-1, five.compareTo(max));
64 assertEquals(1, max.compareTo(zero));
65 assertEquals(1, max.compareTo(five));
66 assertEquals(0, max.compareTo(max));
70 public void testEquals() {
71 final Uint64 five = Uint64.valueOf(5);
72 final Uint64 zero = Uint64.valueOf(0);
73 final Uint64 max = Uint64.valueOf(4294967295L);
75 final Uint64 test = new Uint64(five);
76 assertFalse(test.equals(zero));
77 assertFalse(test.equals(new Object()));
78 assertFalse(test.equals(max));
79 assertTrue(test.equals(test));
80 assertTrue(test.equals(five));
81 assertTrue(five.equals(test));
85 public void testToString() {
86 assertEquals("0", Uint64.valueOf(0).toString());
87 assertEquals("2147483647", Uint64.valueOf(2147483647L).toString());
88 assertEquals("2147483648", Uint64.valueOf(2147483648L).toString());
89 assertEquals("4294967295", Uint64.valueOf(4294967295L).toString());
93 public void testHashCode() {
94 assertEquals(Long.hashCode(-63), Uint64.fromLongBits(-63L).hashCode());
98 public void testFloatValue() {
99 assertEquals(0, Uint64.valueOf(0).floatValue(), 0);
103 public void testDoubleValue() {
104 assertEquals(0, Uint64.valueOf(0).doubleValue(), 0);
108 public void testConversions() {
109 assertSame(Uint64.valueOf(5), Uint64.valueOf(Uint8.valueOf(5)));
110 assertSame(Uint64.valueOf(10), Uint64.valueOf(Uint16.valueOf(10)));
111 assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
112 assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
114 assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
115 assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
117 assertEquals(Uint8.TEN, Uint64.TEN.toUint8());
118 assertEquals(Uint16.TEN, Uint64.TEN.toUint16());
119 assertEquals(Uint32.TEN, Uint64.TEN.toUint32());
123 public void testToUint8() {
124 assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint8());
128 public void testToUint16() {
129 assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint16());
133 public void testToUint32() {
134 assertThrows(IllegalArgumentException.class, () -> Uint64.MAX_VALUE.toUint32());
138 public void testSerialization() throws IOException, ClassNotFoundException {
139 final Uint64 source = Uint64.valueOf(255);
140 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
141 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
142 oos.writeObject(source);
146 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
147 read = ois.readObject();
150 assertSame(source, read);
154 public void testNegativeValues() {
155 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((byte)-1));
156 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf((short)-1));
157 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1));
158 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(-1L));
159 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(new BigInteger("-1")));
161 assertEquals(Uint64.ZERO, Uint64.saturatedOf((byte)-1));
162 assertEquals(Uint64.ZERO, Uint64.saturatedOf((short)-1));
163 assertEquals(Uint64.ZERO, Uint64.saturatedOf(-1));
164 assertEquals(Uint64.ZERO, Uint64.saturatedOf(-1L));
165 assertEquals(Uint64.ZERO, Uint64.saturatedOf(new BigInteger("-1")));
169 public void testLargeValues() {
170 final BigInteger big = new BigInteger("10000000000000000", 16);
171 assertThrows(IllegalArgumentException.class, () -> Uint64.valueOf(big));
173 assertEquals(Uint64.MAX_VALUE, Uint64.saturatedOf(big));
177 public void testNullValueOf() {
178 assertThrows(NullPointerException.class, () -> Uint64.valueOf((String) null));
179 assertThrows(NullPointerException.class, () -> Uint64.valueOf((BigInteger) null));