2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
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;
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;
25 public class Uint16Test {
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());
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);
41 assertEquals(0, zero.compareTo(zero));
42 assertThat(zero.compareTo(five), lessThan(0));
43 assertThat(zero.compareTo(max), lessThan(0));
45 assertThat(five.compareTo(zero), greaterThan(0));
46 assertEquals(0, five.compareTo(five));
47 assertThat(five.compareTo(max), lessThan(0));
49 assertThat(max.compareTo(zero), greaterThan(0));
50 assertThat(max.compareTo(five), greaterThan(0));
51 assertEquals(0, max.compareTo(max));
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);
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));
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());
78 public void testHashCode() {
79 assertEquals(Short.hashCode((short)-63), Uint16.fromShortBits((short)-63).hashCode());
83 public void testFloatValue() {
84 assertEquals(0, Uint16.valueOf(0).floatValue(), 0);
88 public void testDoubleValue() {
89 assertEquals(0, Uint16.valueOf(0).doubleValue(), 0);
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)));
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());
103 @Test(expected = IllegalArgumentException.class)
104 public void testToUint8() {
105 Uint16.MAX_VALUE.toUint8();
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);
117 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
118 read = ois.readObject();
121 assertSame(source, read);
124 @Test(expected = IllegalArgumentException.class)
125 public void testNegativeByte() {
126 Uint16.valueOf((byte)-1);
129 @Test(expected = IllegalArgumentException.class)
130 public void testNegativeShort() {
131 Uint16.valueOf((short)-1);
134 @Test(expected = IllegalArgumentException.class)
135 public void testNegativeInt() {
139 @Test(expected = IllegalArgumentException.class)
140 public void testNegativeLong() {
144 @Test(expected = IllegalArgumentException.class)
145 public void testBigInt() {
146 Uint16.valueOf(65536);
149 @Test(expected = IllegalArgumentException.class)
150 public void testBigLong() {
151 Uint16.valueOf(65536L);
154 @Test(expected = NullPointerException.class)
155 public void testNullValueOfString() {
156 Uint16.valueOf((String) null);