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