b1a122647a1f007c9ca31e23749614842a0817d0
[yangtools.git] / yang / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / Uint32Test.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
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;
15
16 import com.google.common.primitives.UnsignedInteger;
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 org.junit.Test;
23
24 public class Uint32Test {
25     @Test
26     public void testValueOf() {
27         assertEquals(127, Uint32.valueOf(Byte.MAX_VALUE).byteValue());
28         assertEquals(32767, Uint32.valueOf(Short.MAX_VALUE).shortValue());
29         assertEquals(2147483647, Uint32.valueOf(Integer.MAX_VALUE).intValue());
30         assertEquals(4294967295L, Uint32.valueOf(4294967295L).longValue());
31         assertEquals(0, Uint32.valueOf("0").intValue());
32     }
33
34     @Test
35     public void testCompareTo() {
36         final Uint32 five = Uint32.valueOf(5);
37         final Uint32 zero = Uint32.valueOf(0);
38         final Uint32 max = Uint32.valueOf(4294967295L);
39
40         assertEquals(0, zero.compareTo(zero));
41         assertEquals(-1, zero.compareTo(five));
42         assertEquals(-1, zero.compareTo(max));
43
44         assertEquals(1, five.compareTo(zero));
45         assertEquals(0, five.compareTo(five));
46         assertEquals(-1, five.compareTo(max));
47
48         assertEquals(1, max.compareTo(zero));
49         assertEquals(1, max.compareTo(five));
50         assertEquals(0, max.compareTo(max));
51     }
52
53     @Test
54     public void testEquals() {
55         final Uint32 five = Uint32.valueOf(5);
56         final Uint32 zero = Uint32.valueOf(0);
57         final Uint32 max = Uint32.valueOf(4294967295L);
58
59         final Uint32 test = new Uint32(five);
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));
66     }
67
68     @Test
69     public void testToString() {
70         assertEquals("0", Uint32.valueOf(0).toString());
71         assertEquals("2147483647", Uint32.valueOf(2147483647L).toString());
72         assertEquals("2147483648", Uint32.valueOf(2147483648L).toString());
73         assertEquals("4294967295", Uint32.valueOf(4294967295L).toString());
74     }
75
76     @Test
77     public void testHashCode() {
78         assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode());
79     }
80
81     @Test
82     public void testFloatValue() {
83         assertEquals(0, Uint32.valueOf(0).floatValue(), 0);
84     }
85
86     @Test
87     public void testDoubleValue() {
88         assertEquals(0, Uint32.valueOf(0).doubleValue(), 0);
89     }
90
91     @Test
92     public void testConversions() {
93         assertSame(Uint32.valueOf(5), Uint32.valueOf(Uint8.valueOf(5)));
94         assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
95         assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
96
97         assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
98         assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
99
100         assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
101         assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
102         assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
103     }
104
105     @Test
106     public void testToUint8() {
107         assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint8());
108     }
109
110     @Test
111     public void testToUint16() {
112         assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint16());
113     }
114
115     @Test
116     public void testSerialization() throws IOException, ClassNotFoundException {
117         final Uint32 source = Uint32.valueOf(255);
118         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
119         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
120             oos.writeObject(source);
121         }
122
123         final Object read;
124         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
125             read = ois.readObject();
126         }
127
128         assertSame(source, read);
129     }
130
131     @Test
132     public void testNegativeValues() {
133         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((byte)-1));
134         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((short)-1));
135         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1));
136         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1L));
137     }
138
139     @Test
140     public void testLargeValues() {
141         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(4294967296L));
142     }
143
144     @Test
145     public void testNullValueOfString() {
146         assertThrows(NullPointerException.class, () -> Uint32.valueOf((String) null));
147     }
148 }