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 Uint8Test {
24 public void testValueOf() {
25 assertEquals(127, Uint8.valueOf(Byte.MAX_VALUE).byteValue());
26 assertEquals(255, Uint8.valueOf(255).intValue());
27 assertEquals(255L, Uint8.valueOf(255L).longValue());
28 assertEquals(0, Uint8.valueOf("0").intValue());
32 public void testCompareTo() {
33 final Uint8 five = Uint8.valueOf(5);
34 final Uint8 zero = Uint8.valueOf(0);
35 final Uint8 max = Uint8.valueOf(255);
37 assertEquals(0, zero.compareTo(zero));
38 assertEquals(-5, zero.compareTo(five));
39 assertEquals(-255, zero.compareTo(max));
41 assertEquals(5, five.compareTo(zero));
42 assertEquals(0, five.compareTo(five));
43 assertEquals(-250, five.compareTo(max));
45 assertEquals(255, max.compareTo(zero));
46 assertEquals(250, max.compareTo(five));
47 assertEquals(0, max.compareTo(max));
51 public void testEquals() {
52 final Uint8 five = Uint8.valueOf(5);
53 final Uint8 zero = Uint8.valueOf(0);
54 final Uint8 max = Uint8.valueOf(255);
56 final Uint8 test = new Uint8(five);
57 assertFalse(test.equals(zero));
58 assertFalse(test.equals(new Object()));
59 assertFalse(test.equals(max));
60 assertTrue(test.equals(test));
61 assertTrue(test.equals(five));
62 assertTrue(five.equals(test));
66 public void testToString() {
67 assertEquals("0", Uint8.valueOf(0).toString());
68 assertEquals("127", Uint8.valueOf(127).toString());
69 assertEquals("128", Uint8.valueOf(128).toString());
70 assertEquals("255", Uint8.valueOf(255).toString());
74 public void testHashCode() {
75 assertEquals(Byte.hashCode((byte)-63), Uint8.fromByteBits((byte)-63).hashCode());
79 public void testFloatValue() {
80 assertEquals(0, Uint8.valueOf(0).floatValue(), 0);
84 public void testDoubleValue() {
85 assertEquals(0, Uint8.valueOf(0).doubleValue(), 0);
89 public void testConversions() {
90 assertSame(Uint8.valueOf(5), Uint8.valueOf(Uint16.valueOf(5)));
91 assertSame(Uint8.valueOf(10), Uint8.valueOf(Uint32.valueOf(10)));
92 assertSame(Uint8.valueOf(20), Uint8.valueOf(Uint64.valueOf(20)));
94 assertEquals(Uint16.valueOf(255), Uint8.MAX_VALUE.toUint16());
95 assertEquals(Uint32.valueOf(255), Uint8.MAX_VALUE.toUint32());
96 assertEquals(Uint64.valueOf(255), Uint8.MAX_VALUE.toUint64());
100 public void testSerialization() throws IOException, ClassNotFoundException {
101 final Uint8 source = Uint8.valueOf(255);
102 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
103 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
104 oos.writeObject(source);
108 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
109 read = ois.readObject();
112 assertSame(source, read);
115 @Test(expected = IllegalArgumentException.class)
116 public void testNegativeByte() {
117 Uint8.valueOf((byte)-1);
120 @Test(expected = IllegalArgumentException.class)
121 public void testNegativeShort() {
122 Uint8.valueOf((short)-1);
125 @Test(expected = IllegalArgumentException.class)
126 public void testNegativeInt() {
130 @Test(expected = IllegalArgumentException.class)
131 public void testNegativeLong() {
135 @Test(expected = IllegalArgumentException.class)
136 public void testBigShort() {
137 Uint8.valueOf((short)256);
140 @Test(expected = IllegalArgumentException.class)
141 public void testBigInt() {
145 @Test(expected = IllegalArgumentException.class)
146 public void testBigLong() {
150 @Test(expected = NullPointerException.class)
151 public void testNullValueOfString() {
152 Uint8.valueOf((String) null);