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