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.UnsignedInteger;
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 org.junit.Test;
23 public class Uint32Test {
25 public void testValueOf() {
26 assertEquals(127, Uint32.valueOf(Byte.MAX_VALUE).byteValue());
27 assertEquals(32767, Uint32.valueOf(Short.MAX_VALUE).shortValue());
28 assertEquals(2147483647, Uint32.valueOf(Integer.MAX_VALUE).intValue());
29 assertEquals(4294967295L, Uint32.valueOf(4294967295L).longValue());
30 assertEquals(0, Uint32.valueOf("0").intValue());
34 public void testCompareTo() {
35 final Uint32 five = Uint32.valueOf(5);
36 final Uint32 zero = Uint32.valueOf(0);
37 final Uint32 max = Uint32.valueOf(4294967295L);
39 assertEquals(0, zero.compareTo(zero));
40 assertEquals(-1, zero.compareTo(five));
41 assertEquals(-1, zero.compareTo(max));
43 assertEquals(1, five.compareTo(zero));
44 assertEquals(0, five.compareTo(five));
45 assertEquals(-1, five.compareTo(max));
47 assertEquals(1, max.compareTo(zero));
48 assertEquals(1, max.compareTo(five));
49 assertEquals(0, max.compareTo(max));
53 public void testEquals() {
54 final Uint32 five = Uint32.valueOf(5);
55 final Uint32 zero = Uint32.valueOf(0);
56 final Uint32 max = Uint32.valueOf(4294967295L);
58 final Uint32 test = new Uint32(five);
59 assertFalse(test.equals(zero));
60 assertFalse(test.equals(new Object()));
61 assertFalse(test.equals(max));
62 assertTrue(test.equals(test));
63 assertTrue(test.equals(five));
64 assertTrue(five.equals(test));
68 public void testToString() {
69 assertEquals("0", Uint32.valueOf(0).toString());
70 assertEquals("2147483647", Uint32.valueOf(2147483647L).toString());
71 assertEquals("2147483648", Uint32.valueOf(2147483648L).toString());
72 assertEquals("4294967295", Uint32.valueOf(4294967295L).toString());
76 public void testHashCode() {
77 assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode());
81 public void testFloatValue() {
82 assertEquals(0, Uint32.valueOf(0).floatValue(), 0);
86 public void testDoubleValue() {
87 assertEquals(0, Uint32.valueOf(0).doubleValue(), 0);
91 public void testConversions() {
92 assertSame(Uint32.valueOf(5), Uint32.valueOf(Uint8.valueOf(5)));
93 assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
94 assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
96 assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
97 assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
99 assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
100 assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
101 assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
104 @Test(expected = IllegalArgumentException.class)
105 public void testToUint8() {
106 Uint32.MAX_VALUE.toUint8();
109 @Test(expected = IllegalArgumentException.class)
110 public void testToUint16() {
111 Uint32.MAX_VALUE.toUint16();
115 public void testSerialization() throws IOException, ClassNotFoundException {
116 final Uint32 source = Uint32.valueOf(255);
117 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
118 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
119 oos.writeObject(source);
123 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
124 read = ois.readObject();
127 assertSame(source, read);
130 @Test(expected = IllegalArgumentException.class)
131 public void testNegativeByte() {
132 Uint32.valueOf((byte)-1);
135 @Test(expected = IllegalArgumentException.class)
136 public void testNegativeShort() {
137 Uint32.valueOf((short)-1);
140 @Test(expected = IllegalArgumentException.class)
141 public void testNegativeInt() {
145 @Test(expected = IllegalArgumentException.class)
146 public void testNegativeLong() {
150 @Test(expected = IllegalArgumentException.class)
151 public void testBigLong() {
152 Uint32.valueOf(4294967296L);
155 @Test(expected = NullPointerException.class)
156 public void testNullValueOfString() {
157 Uint32.valueOf((String) null);