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.assertThrows;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.primitives.UnsignedInteger;
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import org.junit.Test;
24 public class Uint32Test {
26 public void testValueOf() {
27 assertEquals(127, Uint32.valueOf(Byte.MAX_VALUE).byteValue());
28 assertEquals(32767, Uint32.valueOf(Short.MAX_VALUE).shortValue());
29 assertEquals(2147483647, Uint32.valueOf(Integer.MAX_VALUE).intValue());
30 assertEquals(4294967295L, Uint32.valueOf(4294967295L).longValue());
31 assertEquals(0, Uint32.valueOf("0").intValue());
35 public void testSaturatedOf() {
36 assertEquals(127, Uint32.saturatedOf((byte) 127).byteValue());
37 assertEquals(127, Uint32.saturatedOf((short) 127).byteValue());
38 assertEquals(127, Uint32.saturatedOf(127).byteValue());
39 assertEquals(127, Uint32.saturatedOf(127L).byteValue());
41 assertEquals(255, Uint32.saturatedOf((short) 255).intValue());
42 assertEquals(255, Uint32.saturatedOf(255).intValue());
43 assertEquals(255L, Uint32.saturatedOf(255L).longValue());
47 public void testCompareTo() {
48 final Uint32 five = Uint32.valueOf(5);
49 final Uint32 zero = Uint32.valueOf(0);
50 final Uint32 max = Uint32.valueOf(4294967295L);
52 assertEquals(0, zero.compareTo(zero));
53 assertEquals(-1, zero.compareTo(five));
54 assertEquals(-1, zero.compareTo(max));
56 assertEquals(1, five.compareTo(zero));
57 assertEquals(0, five.compareTo(five));
58 assertEquals(-1, five.compareTo(max));
60 assertEquals(1, max.compareTo(zero));
61 assertEquals(1, max.compareTo(five));
62 assertEquals(0, max.compareTo(max));
66 public void testEquals() {
67 final Uint32 five = Uint32.valueOf(5);
68 final Uint32 zero = Uint32.valueOf(0);
69 final Uint32 max = Uint32.valueOf(4294967295L);
71 final Uint32 test = new Uint32(five);
72 assertFalse(test.equals(zero));
73 assertFalse(test.equals(new Object()));
74 assertFalse(test.equals(max));
75 assertTrue(test.equals(test));
76 assertTrue(test.equals(five));
77 assertTrue(five.equals(test));
81 public void testToString() {
82 assertEquals("0", Uint32.valueOf(0).toString());
83 assertEquals("2147483647", Uint32.valueOf(2147483647L).toString());
84 assertEquals("2147483648", Uint32.valueOf(2147483648L).toString());
85 assertEquals("4294967295", Uint32.valueOf(4294967295L).toString());
89 public void testHashCode() {
90 assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode());
94 public void testFloatValue() {
95 assertEquals(0, Uint32.valueOf(0).floatValue(), 0);
99 public void testDoubleValue() {
100 assertEquals(0, Uint32.valueOf(0).doubleValue(), 0);
104 public void testConversions() {
105 assertSame(Uint32.valueOf(5), Uint32.valueOf(Uint8.valueOf(5)));
106 assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
107 assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
109 assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
110 assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
112 assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
113 assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
114 assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
118 public void testToUint8() {
119 assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint8());
123 public void testToUint16() {
124 assertThrows(IllegalArgumentException.class, () -> Uint32.MAX_VALUE.toUint16());
128 public void testSerialization() throws IOException, ClassNotFoundException {
129 final Uint32 source = Uint32.valueOf(255);
130 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
131 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
132 oos.writeObject(source);
136 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
137 read = ois.readObject();
140 assertSame(source, read);
144 public void testNegativeValues() {
145 assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((byte)-1));
146 assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf((short)-1));
147 assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1));
148 assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(-1L));
150 assertEquals(Uint32.ZERO, Uint32.saturatedOf((byte)-1));
151 assertEquals(Uint32.ZERO, Uint32.saturatedOf((short)-1));
152 assertEquals(Uint32.ZERO, Uint32.saturatedOf(-1));
153 assertEquals(Uint32.ZERO, Uint32.saturatedOf(-1L));
157 public void testLargeValues() {
158 assertThrows(IllegalArgumentException.class, () -> Uint32.valueOf(4294967296L));
160 assertEquals(Uint32.MAX_VALUE, Uint32.saturatedOf(4294967296L));
164 public void testNullValueOfString() {
165 assertThrows(NullPointerException.class, () -> Uint32.valueOf((String) null));