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 testSaturatedOf() {
38 assertEquals(127, Uint16.saturatedOf((byte) 127).byteValue());
39 assertEquals(127, Uint16.saturatedOf((short) 127).byteValue());
40 assertEquals(127, Uint16.saturatedOf(127).byteValue());
41 assertEquals(127, Uint16.saturatedOf(127L).byteValue());
43 assertEquals(255, Uint16.saturatedOf((short) 255).intValue());
44 assertEquals(255, Uint16.saturatedOf(255).intValue());
45 assertEquals(255L, Uint16.saturatedOf(255L).longValue());
49 public void testCompareTo() {
50 final Uint16 five = Uint16.valueOf(5);
51 final Uint16 zero = Uint16.valueOf(0);
52 final Uint16 max = Uint16.valueOf(65535);
54 assertEquals(0, zero.compareTo(zero));
55 assertThat(zero.compareTo(five), lessThan(0));
56 assertThat(zero.compareTo(max), lessThan(0));
58 assertThat(five.compareTo(zero), greaterThan(0));
59 assertEquals(0, five.compareTo(five));
60 assertThat(five.compareTo(max), lessThan(0));
62 assertThat(max.compareTo(zero), greaterThan(0));
63 assertThat(max.compareTo(five), greaterThan(0));
64 assertEquals(0, max.compareTo(max));
68 public void testEquals() {
69 final Uint16 five = Uint16.valueOf(5);
70 final Uint16 zero = Uint16.valueOf(0);
71 final Uint16 max = Uint16.valueOf(65535);
73 final Uint16 test = new Uint16(five);
74 assertFalse(test.equals(zero));
75 assertFalse(test.equals(new Object()));
76 assertFalse(test.equals(max));
77 assertTrue(test.equals(test));
78 assertTrue(test.equals(five));
79 assertTrue(five.equals(test));
83 public void testToString() {
84 assertEquals("0", Uint16.valueOf(0).toString());
85 assertEquals("32767", Uint16.valueOf(32767).toString());
86 assertEquals("32768", Uint16.valueOf(32768).toString());
87 assertEquals("65535", Uint16.valueOf(65535).toString());
91 public void testHashCode() {
92 assertEquals(Short.hashCode((short)-63), Uint16.fromShortBits((short)-63).hashCode());
96 public void testFloatValue() {
97 assertEquals(0, Uint16.valueOf(0).floatValue(), 0);
101 public void testDoubleValue() {
102 assertEquals(0, Uint16.valueOf(0).doubleValue(), 0);
106 public void testConversions() {
107 assertSame(Uint16.valueOf(5), Uint16.valueOf(Uint8.valueOf(5)));
108 assertSame(Uint16.valueOf(10), Uint16.valueOf(Uint32.valueOf(10)));
109 assertSame(Uint16.valueOf(20), Uint16.valueOf(Uint64.valueOf(20)));
111 assertEquals(Uint8.TEN, Uint16.TEN.toUint8());
112 assertEquals(Uint32.valueOf(65535), Uint16.MAX_VALUE.toUint32());
113 assertEquals(Uint64.valueOf(65535), Uint16.MAX_VALUE.toUint64());
117 public void testToUint8() {
118 assertThrows(IllegalArgumentException.class, () -> Uint16.MAX_VALUE.toUint8());
122 public void testSerialization() throws IOException, ClassNotFoundException {
123 final Uint16 source = Uint16.valueOf(255);
124 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
125 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
126 oos.writeObject(source);
130 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
131 read = ois.readObject();
134 assertSame(source, read);
138 public void testNegativeValues() {
139 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((byte)-1));
140 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf((short)-1));
141 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1));
142 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(-1L));
144 assertEquals(Uint16.ZERO, Uint16.saturatedOf((byte)-1));
145 assertEquals(Uint16.ZERO, Uint16.saturatedOf((short)-1));
146 assertEquals(Uint16.ZERO, Uint16.saturatedOf(-1));
147 assertEquals(Uint16.ZERO, Uint16.saturatedOf(-1L));
151 public void testLargeValues() {
152 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536));
153 assertThrows(IllegalArgumentException.class, () -> Uint16.valueOf(65536L));
155 assertEquals(Uint16.MAX_VALUE, Uint16.saturatedOf(65536));
156 assertEquals(Uint16.MAX_VALUE, Uint16.saturatedOf(65536L));
160 public void testNullValueOfString() {
161 assertThrows(NullPointerException.class, () -> Uint16.valueOf((String) null));