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