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.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;
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;
26 public class Uint16Test {
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());
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);
42 assertEquals(0, zero.compareTo(zero));
43 assertThat(zero.compareTo(five), lessThan(0));
44 assertThat(zero.compareTo(max), lessThan(0));
46 assertThat(five.compareTo(zero), greaterThan(0));
47 assertEquals(0, five.compareTo(five));
48 assertThat(five.compareTo(max), lessThan(0));
50 assertThat(max.compareTo(zero), greaterThan(0));
51 assertThat(max.compareTo(five), greaterThan(0));
52 assertEquals(0, max.compareTo(max));
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);
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));
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());
79 public void testHashCode() {
80 assertEquals(Short.hashCode((short)-63), Uint16.fromShortBits((short)-63).hashCode());
84 public void testFloatValue() {
85 assertEquals(0, Uint16.valueOf(0).floatValue(), 0);
89 public void testDoubleValue() {
90 assertEquals(0, Uint16.valueOf(0).doubleValue(), 0);
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)));
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());
105 public void testToUint8() {
106 assertThrows(IllegalArgumentException.class, () -> Uint16.MAX_VALUE.toUint8());
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);
118 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
119 read = ois.readObject();
122 assertSame(source, read);
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));
134 public void testLargeValues() {
135 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536));
136 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536L));
140 public void testNullValueOfString() {
141 assertThrows(NullPointerException.class, () -> Uint16.valueOf((String) null));