83c516ceeabb72220201a57d043b05fd7b55ea01
[yangtools.git] / yang / 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.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.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.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 testCompareTo() {
34         final Uint8 five = Uint8.valueOf(5);
35         final Uint8 zero = Uint8.valueOf(0);
36         final Uint8 max = Uint8.valueOf(255);
37
38         assertEquals(0, zero.compareTo(zero));
39         assertEquals(-5, zero.compareTo(five));
40         assertEquals(-255, zero.compareTo(max));
41
42         assertEquals(5, five.compareTo(zero));
43         assertEquals(0, five.compareTo(five));
44         assertEquals(-250, five.compareTo(max));
45
46         assertEquals(255, max.compareTo(zero));
47         assertEquals(250, max.compareTo(five));
48         assertEquals(0, max.compareTo(max));
49     }
50
51     @Test
52     public void testEquals() {
53         final Uint8 five = Uint8.valueOf(5);
54         final Uint8 zero = Uint8.valueOf(0);
55         final Uint8 max = Uint8.valueOf(255);
56
57         final Uint8 test = new Uint8(five);
58         assertFalse(test.equals(zero));
59         assertFalse(test.equals(new Object()));
60         assertFalse(test.equals(max));
61         assertTrue(test.equals(test));
62         assertTrue(test.equals(five));
63         assertTrue(five.equals(test));
64     }
65
66     @Test
67     public void testToString() {
68         assertEquals("0", Uint8.valueOf(0).toString());
69         assertEquals("127", Uint8.valueOf(127).toString());
70         assertEquals("128", Uint8.valueOf(128).toString());
71         assertEquals("255", Uint8.valueOf(255).toString());
72     }
73
74     @Test
75     public void testHashCode() {
76         assertEquals(Byte.hashCode((byte)-63), Uint8.fromByteBits((byte)-63).hashCode());
77     }
78
79     @Test
80     public void testFloatValue() {
81         assertEquals(0, Uint8.valueOf(0).floatValue(), 0);
82     }
83
84     @Test
85     public void testDoubleValue() {
86         assertEquals(0, Uint8.valueOf(0).doubleValue(), 0);
87     }
88
89     @Test
90     public void testConversions() {
91         assertSame(Uint8.valueOf(5), Uint8.valueOf(Uint16.valueOf(5)));
92         assertSame(Uint8.valueOf(10), Uint8.valueOf(Uint32.valueOf(10)));
93         assertSame(Uint8.valueOf(20), Uint8.valueOf(Uint64.valueOf(20)));
94
95         assertEquals(Uint16.valueOf(255), Uint8.MAX_VALUE.toUint16());
96         assertEquals(Uint32.valueOf(255), Uint8.MAX_VALUE.toUint32());
97         assertEquals(Uint64.valueOf(255), Uint8.MAX_VALUE.toUint64());
98     }
99
100     @Test
101     public void testSerialization() throws IOException, ClassNotFoundException {
102         final Uint8 source = Uint8.valueOf(255);
103         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
104         try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
105             oos.writeObject(source);
106         }
107
108         final Object read;
109         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
110             read = ois.readObject();
111         }
112
113         assertSame(source, read);
114     }
115
116     @Test
117     public void testNegativeValues() {
118         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((byte)-1));
119         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)-1));
120         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1));
121         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(-1L));
122     }
123
124     @Test
125     public void testLargeValues() {
126         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf((short)256));
127         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256));
128         assertThrows(IllegalArgumentException.class, () -> Uint8.valueOf(256L));
129     }
130
131     @Test
132     public void testNullValueOfString() {
133         assertThrows(NullPointerException.class, () -> Uint8.valueOf((String) null));
134     }
135 }