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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertTrue;
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;
22 public class Uint16Test {
24 public void testValueOf() {
25 assertEquals(127, Uint16.valueOf(Byte.MAX_VALUE).byteValue());
26 assertEquals(32767, Uint16.valueOf(Short.MAX_VALUE).shortValue());
27 assertEquals(65535, Uint16.valueOf(65535).intValue());
28 assertEquals(65535L, Uint16.valueOf(65535L).longValue());
29 assertEquals(0, Uint16.valueOf("0").intValue());
33 public void testCompareTo() {
34 final Uint16 five = Uint16.valueOf(5);
35 final Uint16 zero = Uint16.valueOf(0);
36 final Uint16 max = Uint16.valueOf(65535);
38 assertEquals(0, zero.compareTo(zero));
39 assertEquals(-1, zero.compareTo(five));
40 assertEquals(-1, zero.compareTo(max));
42 assertEquals(1, five.compareTo(zero));
43 assertEquals(0, five.compareTo(five));
44 assertEquals(-1, five.compareTo(max));
46 assertEquals(1, max.compareTo(zero));
47 assertEquals(1, max.compareTo(five));
48 assertEquals(0, max.compareTo(max));
52 public void testEquals() {
53 final Uint16 five = Uint16.valueOf(5);
54 final Uint16 zero = Uint16.valueOf(0);
55 final Uint16 max = Uint16.valueOf(65535);
57 final Uint16 test = new Uint16((short) 5);
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));
67 public void testToString() {
68 assertEquals("0", Uint16.valueOf(0).toString());
69 assertEquals("32767", Uint16.valueOf(32767).toString());
70 assertEquals("32768", Uint16.valueOf(32768).toString());
71 assertEquals("65535", Uint16.valueOf(65535).toString());
75 public void testHashCode() {
76 assertEquals(Short.hashCode((short)-63), Uint16.fromShortBits((short)-63).hashCode());
80 public void testFloatValue() {
81 assertEquals(0, Uint16.valueOf(0).floatValue(), 0);
85 public void testDoubleValue() {
86 assertEquals(0, Uint16.valueOf(0).doubleValue(), 0);
90 public void testConversions() {
91 assertSame(Uint16.valueOf(5), Uint16.valueOf(Uint8.valueOf(5)));
92 assertSame(Uint16.valueOf(10), Uint16.valueOf(Uint32.valueOf(10)));
93 assertSame(Uint16.valueOf(20), Uint16.valueOf(Uint64.valueOf(20)));
97 public void testSerialization() throws IOException, ClassNotFoundException {
98 final Uint16 source = Uint16.valueOf(255);
99 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
100 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
101 oos.writeObject(source);
105 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
106 read = ois.readObject();
109 assertSame(source, read);
112 @Test(expected = IllegalArgumentException.class)
113 public void testNegativeByte() {
114 Uint16.valueOf((byte)-1);
117 @Test(expected = IllegalArgumentException.class)
118 public void testNegativeShort() {
119 Uint16.valueOf((short)-1);
122 @Test(expected = IllegalArgumentException.class)
123 public void testNegativeInt() {
127 @Test(expected = IllegalArgumentException.class)
128 public void testNegativeLong() {
132 @Test(expected = IllegalArgumentException.class)
133 public void testBigInt() {
134 Uint16.valueOf(65536);
137 @Test(expected = IllegalArgumentException.class)
138 public void testBigLong() {
139 Uint16.valueOf(65536L);