Merge branch 'master' of ../controller
[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.assertTrue;
14
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;
22
23 public class Uint32Test {
24     @Test
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());
31     }
32
33     @Test
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);
38
39         assertEquals(0, zero.compareTo(zero));
40         assertEquals(-1, zero.compareTo(five));
41         assertEquals(-1, zero.compareTo(max));
42
43         assertEquals(1, five.compareTo(zero));
44         assertEquals(0, five.compareTo(five));
45         assertEquals(-1, five.compareTo(max));
46
47         assertEquals(1, max.compareTo(zero));
48         assertEquals(1, max.compareTo(five));
49         assertEquals(0, max.compareTo(max));
50     }
51
52     @Test
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);
57
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));
65     }
66
67     @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());
73     }
74
75     @Test
76     public void testHashCode() {
77         assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode());
78     }
79
80     @Test
81     public void testFloatValue() {
82         assertEquals(0, Uint32.valueOf(0).floatValue(), 0);
83     }
84
85     @Test
86     public void testDoubleValue() {
87         assertEquals(0, Uint32.valueOf(0).doubleValue(), 0);
88     }
89
90     @Test
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)));
95
96         assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
97         assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
98
99         assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
100         assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
101         assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
102     }
103
104     @Test(expected = IllegalArgumentException.class)
105     public void testToUint8() {
106         Uint32.MAX_VALUE.toUint8();
107     }
108
109     @Test(expected = IllegalArgumentException.class)
110     public void testToUint16() {
111         Uint32.MAX_VALUE.toUint16();
112     }
113
114     @Test
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);
120         }
121
122         final Object read;
123         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
124             read = ois.readObject();
125         }
126
127         assertSame(source, read);
128     }
129
130     @Test(expected = IllegalArgumentException.class)
131     public void testNegativeByte() {
132         Uint32.valueOf((byte)-1);
133     }
134
135     @Test(expected = IllegalArgumentException.class)
136     public void testNegativeShort() {
137         Uint32.valueOf((short)-1);
138     }
139
140     @Test(expected = IllegalArgumentException.class)
141     public void testNegativeInt() {
142         Uint32.valueOf(-1);
143     }
144
145     @Test(expected = IllegalArgumentException.class)
146     public void testNegativeLong() {
147         Uint32.valueOf(-1L);
148     }
149
150     @Test(expected = IllegalArgumentException.class)
151     public void testBigLong() {
152         Uint32.valueOf(4294967296L);
153     }
154
155     @Test(expected = NullPointerException.class)
156     public void testNullValueOfString() {
157         Uint32.valueOf((String) null);
158     }
159 }