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