Migrate yang-common to JUnit5
[yangtools.git] / common / 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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.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.jupiter.api.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 testSaturatedOf() {
36         assertEquals(127, Uint32.saturatedOf((byte) 127).byteValue());
37         assertEquals(127, Uint32.saturatedOf((short) 127).byteValue());
38         assertEquals(127, Uint32.saturatedOf(127).byteValue());
39         assertEquals(127, Uint32.saturatedOf(127L).byteValue());
40
41         assertEquals(255, Uint32.saturatedOf((short) 255).intValue());
42         assertEquals(255, Uint32.saturatedOf(255).intValue());
43         assertEquals(255L, Uint32.saturatedOf(255L).longValue());
44     }
45
46     @Test
47     public void testCompareTo() {
48         final Uint32 five = Uint32.valueOf(5);
49         final Uint32 zero = Uint32.valueOf(0);
50         final Uint32 max = Uint32.valueOf(4294967295L);
51
52         assertEquals(0, zero.compareTo(zero));
53         assertEquals(-1, zero.compareTo(five));
54         assertEquals(-1, zero.compareTo(max));
55
56         assertEquals(1, five.compareTo(zero));
57         assertEquals(0, five.compareTo(five));
58         assertEquals(-1, five.compareTo(max));
59
60         assertEquals(1, max.compareTo(zero));
61         assertEquals(1, max.compareTo(five));
62         assertEquals(0, max.compareTo(max));
63     }
64
65     @Test
66     public void testEquals() {
67         final Uint32 five = Uint32.valueOf(5);
68         final Uint32 zero = Uint32.valueOf(0);
69         final Uint32 max = Uint32.valueOf(4294967295L);
70
71         final Uint32 test = new Uint32(five);
72         assertFalse(test.equals(zero));
73         assertFalse(test.equals(new Object()));
74         assertFalse(test.equals(max));
75         assertTrue(test.equals(test));
76         assertTrue(test.equals(five));
77         assertTrue(five.equals(test));
78     }
79
80     @Test
81     public void testToString() {
82         assertEquals("0", Uint32.valueOf(0).toString());
83         assertEquals("2147483647", Uint32.valueOf(2147483647L).toString());
84         assertEquals("2147483648", Uint32.valueOf(2147483648L).toString());
85         assertEquals("4294967295", Uint32.valueOf(4294967295L).toString());
86     }
87
88     @Test
89     public void testHashCode() {
90         assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode());
91     }
92
93     @Test
94     public void testFloatValue() {
95         assertEquals(0, Uint32.valueOf(0).floatValue(), 0);
96     }
97
98     @Test
99     public void testDoubleValue() {
100         assertEquals(0, Uint32.valueOf(0).doubleValue(), 0);
101     }
102
103     @Test
104     public void testConversions() {
105         assertSame(Uint32.valueOf(5), Uint32.valueOf(Uint8.valueOf(5)));
106         assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
107         assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
108
109         assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
110         assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
111
112         assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
113         assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
114         assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
115     }
116
117     @Test
118     public void testToUint8() {
119         assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint8());
120     }
121
122     @Test
123     public void testToUint16() {
124         assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint16());
125     }
126
127     @Test
128     public void testSerialization() throws IOException, ClassNotFoundException {
129         final Uint32 source = Uint32.valueOf(255);
130         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
131         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
132             oos.writeObject(source);
133         }
134
135         final Object read;
136         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
137             read = ois.readObject();
138         }
139
140         assertSame(source, read);
141     }
142
143     @Test
144     public void testNegativeValues() {
145         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((byte)-1));
146         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((short)-1));
147         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1));
148         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1L));
149
150         assertEquals(Uint32.ZERO, Uint32.saturatedOf((byte)-1));
151         assertEquals(Uint32.ZERO, Uint32.saturatedOf((short)-1));
152         assertEquals(Uint32.ZERO, Uint32.saturatedOf(-1));
153         assertEquals(Uint32.ZERO, Uint32.saturatedOf(-1L));
154     }
155
156     @Test
157     public void testLargeValues() {
158         assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(4294967296L));
159
160         assertEquals(Uint32.MAX_VALUE, Uint32.saturatedOf(4294967296L));
161     }
162
163     @Test
164     public void testNullValueOfString() {
165         assertThrows(NullPointerException.class, () -> Uint32.valueOf((String) null));
166     }
167 }