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.assertTrue;
15 import com.google.common.primitives.UnsignedLong;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import java.math.BigInteger;
22 import org.junit.Test;
24 public class Uint64Test {
26 public void testValueOf() {
27 assertEquals(127, Uint64.valueOf(Byte.MAX_VALUE).byteValue());
28 assertEquals(32767, Uint64.valueOf(Short.MAX_VALUE).shortValue());
29 assertEquals(2147483647, Uint64.valueOf(Integer.MAX_VALUE).intValue());
30 assertEquals(9223372036854775807L, Uint64.valueOf(Long.MAX_VALUE).longValue());
31 assertEquals(0, Uint64.valueOf("0").intValue());
35 public void testCompareTo() {
36 final Uint64 five = Uint64.valueOf(5);
37 final Uint64 zero = Uint64.valueOf(0);
38 final Uint64 max = Uint64.valueOf(4294967295L);
40 assertEquals(0, zero.compareTo(zero));
41 assertEquals(-1, zero.compareTo(five));
42 assertEquals(-1, zero.compareTo(max));
44 assertEquals(1, five.compareTo(zero));
45 assertEquals(0, five.compareTo(five));
46 assertEquals(-1, five.compareTo(max));
48 assertEquals(1, max.compareTo(zero));
49 assertEquals(1, max.compareTo(five));
50 assertEquals(0, max.compareTo(max));
54 public void testEquals() {
55 final Uint64 five = Uint64.valueOf(5);
56 final Uint64 zero = Uint64.valueOf(0);
57 final Uint64 max = Uint64.valueOf(4294967295L);
59 final Uint64 test = new Uint64(5);
60 assertFalse(test.equals(zero));
61 assertFalse(test.equals(new Object()));
62 assertFalse(test.equals(max));
63 assertTrue(test.equals(test));
64 assertTrue(test.equals(five));
65 assertTrue(five.equals(test));
69 public void testToString() {
70 assertEquals("0", Uint64.valueOf(0).toString());
71 assertEquals("2147483647", Uint64.valueOf(2147483647L).toString());
72 assertEquals("2147483648", Uint64.valueOf(2147483648L).toString());
73 assertEquals("4294967295", Uint64.valueOf(4294967295L).toString());
77 public void testHashCode() {
78 assertEquals(Long.hashCode(-63), Uint64.fromLongBits(-63L).hashCode());
82 public void testFloatValue() {
83 assertEquals(0, Uint64.valueOf(0).floatValue(), 0);
87 public void testDoubleValue() {
88 assertEquals(0, Uint64.valueOf(0).doubleValue(), 0);
92 public void testConversions() {
93 assertSame(Uint64.valueOf(5), Uint64.valueOf(Uint8.valueOf(5)));
94 assertSame(Uint64.valueOf(10), Uint64.valueOf(Uint16.valueOf(10)));
95 assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
96 assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
98 assertSame(Uint64.valueOf(5), Uint64.fromUnsignedLong(UnsignedLong.fromLongBits(5)));
99 assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toUnsignedLong());
103 public void testSerialization() throws IOException, ClassNotFoundException {
104 final Uint64 source = Uint64.valueOf(255);
105 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
106 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
107 oos.writeObject(source);
111 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
112 read = ois.readObject();
115 assertSame(source, read);
118 @Test(expected = IllegalArgumentException.class)
119 public void testNegativeByte() {
120 Uint64.valueOf((byte)-1);
123 @Test(expected = IllegalArgumentException.class)
124 public void testNegativeShort() {
125 Uint64.valueOf((short)-1);
128 @Test(expected = IllegalArgumentException.class)
129 public void testNegativeInt() {
133 @Test(expected = IllegalArgumentException.class)
134 public void testNegativeLong() {
138 @Test(expected = IllegalArgumentException.class)
139 public void testNegativeBigInteger() {
140 Uint64.valueOf(new BigInteger("-1"));
143 @Test(expected = IllegalArgumentException.class)
144 public void testBigBigInteger() {
145 Uint64.valueOf(new BigInteger("0x10000000000000000"));