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