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