Implement Uint{8,16}.valueOf(String, int) 59/66759/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Dec 2017 12:53:37 +0000 (13:53 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Dec 2017 16:30:41 +0000 (17:30 +0100)
This adds a simplistic implementation deferring parsing to
Short/Integer.valueOf(String, int) and performing range checking.

Also expands the test suite to cover Uint{8,16,32,64}, which flushes
out a bug in Uint64's handling of cache slots -- this is fixed, too.

Change-Id: I8fa977028a6f00ee8f25a19fdd2fd5d2b08b4bfa
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint16.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint32.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint64.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Uint8.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java [new file with mode: 0644]
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java [new file with mode: 0644]
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java [new file with mode: 0644]
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java [new file with mode: 0644]

index 882ae2f71ab65c51501ebe6ef57a217a3ad8c0b7..0bf19dbef339527e22b3f09a47ae2d3ccf9efc63 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -64,7 +65,8 @@ public final class Uint16 extends Number implements Comparable<Uint16>, Immutabl
 
     private final short value;
 
-    private Uint16(final short value) {
+    @VisibleForTesting
+    Uint16(final short value) {
         this.value = value;
     }
 
@@ -126,13 +128,16 @@ public final class Uint16 extends Number implements Comparable<Uint16>, Immutabl
         return valueOf(uint.longValue());
     }
 
+    public static Uint16 valueOf(final Uint64 uint) {
+        return valueOf(uint.longValue());
+    }
+
     public static Uint16 valueOf(final String string) {
         return valueOf(string, 10);
     }
 
     public static Uint16 valueOf(final String string, final int radix) {
-        // FIXME: implement this
-        throw new UnsupportedOperationException();
+        return valueOf(Integer.parseInt(string, radix));
     }
 
     @Override
index 613d586c79942b959732e87dc559087d2eaa5680..b9a03a5ce96f192fdc96cec9cbd72f5b9c7ee482 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -66,7 +67,8 @@ public final class Uint32 extends Number implements Comparable<Uint32>, Immutabl
 
     private final int value;
 
-    private Uint32(final int value) {
+    @VisibleForTesting
+    Uint32(final int value) {
         this.value = value;
     }
 
@@ -133,6 +135,10 @@ public final class Uint32 extends Number implements Comparable<Uint32>, Immutabl
         return instanceFor(uint.intValue());
     }
 
+    public static Uint32 valueOf(final Uint64 uint) {
+        return valueOf(uint.longValue());
+    }
+
     public static Uint32 valueOf(final String string) {
         return valueOf(string, 10);
     }
index 743a8e37b356354eb57a457950bfd3dc25ba551c..86d439328ebee2d846bff6b3d2b05aad628a53e6 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -68,13 +69,14 @@ public final class Uint64 extends Number implements Comparable<Uint64>, Immutabl
 
     private final long value;
 
-    private Uint64(final long value) {
+    @VisibleForTesting
+    Uint64(final long value) {
         this.value = value;
     }
 
     private static Uint64 instanceFor(final long value) {
         final int slot = (int)value;
-        if (value < 0 || slot >= CACHE.length) {
+        if (slot < 0 || slot >= CACHE.length) {
             for (Uint64 c : COMMON) {
                 if (c.value == value) {
                     return c;
index 997cfdf41b715abc6e519b063099fa2c730715b2..90ed5fac9006c1d1abf72ac8582b765e1e926c7f 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.common;
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
 import org.opendaylight.yangtools.concepts.Immutable;
 
 /**
@@ -27,7 +28,8 @@ public final class Uint8 extends Number implements Comparable<Uint8>, Immutable
 
     private final byte value;
 
-    private Uint8(final byte value) {
+    @VisibleForTesting
+    Uint8(final byte value) {
         this.value = value;
     }
 
@@ -76,13 +78,20 @@ public final class Uint8 extends Number implements Comparable<Uint8>, Immutable
         return valueOf(uint.intValue());
     }
 
+    public static Uint8 valueOf(final Uint32 uint) {
+        return valueOf(uint.longValue());
+    }
+
+    public static Uint8 valueOf(final Uint64 uint) {
+        return valueOf(uint.longValue());
+    }
+
     public static Uint8 valueOf(final String string) {
         return valueOf(string, 10);
     }
 
     public static Uint8 valueOf(final String string, final int radix) {
-        // FIXME: implement this
-        throw new UnsupportedOperationException();
+        return valueOf(Short.parseShort(string, radix));
     }
 
     @Override
diff --git a/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java b/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint16Test.java
new file mode 100644 (file)
index 0000000..2cbfc86
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.junit.Test;
+
+public class Uint16Test {
+    @Test
+    public void testValueOf() {
+        assertEquals(127, Uint16.valueOf(Byte.MAX_VALUE).byteValue());
+        assertEquals(32767, Uint16.valueOf(Short.MAX_VALUE).shortValue());
+        assertEquals(65535, Uint16.valueOf(65535).intValue());
+        assertEquals(65535L, Uint16.valueOf(65535L).longValue());
+        assertEquals(0, Uint16.valueOf("0").intValue());
+    }
+
+    @Test
+    public void testCompareTo() {
+        final Uint16 five = Uint16.valueOf(5);
+        final Uint16 zero = Uint16.valueOf(0);
+        final Uint16 max = Uint16.valueOf(65535);
+
+        assertEquals(0, zero.compareTo(zero));
+        assertEquals(-1, zero.compareTo(five));
+        assertEquals(-1, zero.compareTo(max));
+
+        assertEquals(1, five.compareTo(zero));
+        assertEquals(0, five.compareTo(five));
+        assertEquals(-1, five.compareTo(max));
+
+        assertEquals(1, max.compareTo(zero));
+        assertEquals(1, max.compareTo(five));
+        assertEquals(0, max.compareTo(max));
+    }
+
+    @Test
+    public void testEquals() {
+        final Uint16 five = Uint16.valueOf(5);
+        final Uint16 zero = Uint16.valueOf(0);
+        final Uint16 max = Uint16.valueOf(65535);
+
+        final Uint16 test = new Uint16((short) 5);
+        assertFalse(test.equals(zero));
+        assertFalse(test.equals(new Object()));
+        assertFalse(test.equals(max));
+        assertTrue(test.equals(test));
+        assertTrue(test.equals(five));
+        assertTrue(five.equals(test));
+    }
+
+    @Test
+    public void testToString() {
+        assertEquals("0", Uint16.valueOf(0).toString());
+        assertEquals("32767", Uint16.valueOf(32767).toString());
+        assertEquals("32768", Uint16.valueOf(32768).toString());
+        assertEquals("65535", Uint16.valueOf(65535).toString());
+    }
+
+    @Test
+    public void testHashCode() {
+        assertEquals(Short.hashCode((short)-63), Uint16.fromShortBits((short)-63).hashCode());
+    }
+
+    @Test
+    public void testFloatValue() {
+        assertEquals(0, Uint16.valueOf(0).floatValue(), 0);
+    }
+
+    @Test
+    public void testDoubleValue() {
+        assertEquals(0, Uint16.valueOf(0).doubleValue(), 0);
+    }
+
+    @Test
+    public void testConversions() {
+        assertSame(Uint16.valueOf(5), Uint16.valueOf(Uint8.valueOf(5)));
+        assertSame(Uint16.valueOf(10), Uint16.valueOf(Uint32.valueOf(10)));
+        assertSame(Uint16.valueOf(20), Uint16.valueOf(Uint64.valueOf(20)));
+    }
+
+    @Test
+    public void testSerialization() throws IOException, ClassNotFoundException {
+        final Uint16 source = Uint16.valueOf(255);
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+            oos.writeObject(source);
+        }
+
+        final Object read;
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+            read = ois.readObject();
+        }
+
+        assertSame(source, read);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeByte() {
+        Uint16.valueOf((byte)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeShort() {
+        Uint16.valueOf((short)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeInt() {
+        Uint16.valueOf(-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeLong() {
+        Uint16.valueOf(-1L);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigInt() {
+        Uint16.valueOf(65536);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigLong() {
+        Uint16.valueOf(65536L);
+    }
+}
diff --git a/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java b/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint32Test.java
new file mode 100644 (file)
index 0000000..72a4c23
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.primitives.UnsignedInteger;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.junit.Test;
+
+public class Uint32Test {
+    @Test
+    public void testValueOf() {
+        assertEquals(127, Uint32.valueOf(Byte.MAX_VALUE).byteValue());
+        assertEquals(32767, Uint32.valueOf(Short.MAX_VALUE).shortValue());
+        assertEquals(2147483647, Uint32.valueOf(Integer.MAX_VALUE).intValue());
+        assertEquals(4294967295L, Uint32.valueOf(4294967295L).longValue());
+        assertEquals(0, Uint32.valueOf("0").intValue());
+    }
+
+    @Test
+    public void testCompareTo() {
+        final Uint32 five = Uint32.valueOf(5);
+        final Uint32 zero = Uint32.valueOf(0);
+        final Uint32 max = Uint32.valueOf(4294967295L);
+
+        assertEquals(0, zero.compareTo(zero));
+        assertEquals(-1, zero.compareTo(five));
+        assertEquals(-1, zero.compareTo(max));
+
+        assertEquals(1, five.compareTo(zero));
+        assertEquals(0, five.compareTo(five));
+        assertEquals(-1, five.compareTo(max));
+
+        assertEquals(1, max.compareTo(zero));
+        assertEquals(1, max.compareTo(five));
+        assertEquals(0, max.compareTo(max));
+    }
+
+    @Test
+    public void testEquals() {
+        final Uint32 five = Uint32.valueOf(5);
+        final Uint32 zero = Uint32.valueOf(0);
+        final Uint32 max = Uint32.valueOf(4294967295L);
+
+        final Uint32 test = new Uint32(5);
+        assertFalse(test.equals(zero));
+        assertFalse(test.equals(new Object()));
+        assertFalse(test.equals(max));
+        assertTrue(test.equals(test));
+        assertTrue(test.equals(five));
+        assertTrue(five.equals(test));
+    }
+
+    @Test
+    public void testToString() {
+        assertEquals("0", Uint32.valueOf(0).toString());
+        assertEquals("2147483647", Uint32.valueOf(2147483647L).toString());
+        assertEquals("2147483648", Uint32.valueOf(2147483648L).toString());
+        assertEquals("4294967295", Uint32.valueOf(4294967295L).toString());
+    }
+
+    @Test
+    public void testHashCode() {
+        assertEquals(Integer.hashCode(-63), Uint32.fromIntBits(-63).hashCode());
+    }
+
+    @Test
+    public void testFloatValue() {
+        assertEquals(0, Uint32.valueOf(0).floatValue(), 0);
+    }
+
+    @Test
+    public void testDoubleValue() {
+        assertEquals(0, Uint32.valueOf(0).doubleValue(), 0);
+    }
+
+    @Test
+    public void testConversions() {
+        assertSame(Uint32.valueOf(5), Uint32.valueOf(Uint8.valueOf(5)));
+        assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
+        assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
+
+        assertSame(Uint32.valueOf(5), Uint32.fromUnsignedInteger(UnsignedInteger.fromIntBits(5)));
+        assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toUnsignedInteger());
+    }
+
+    @Test
+    public void testSerialization() throws IOException, ClassNotFoundException {
+        final Uint32 source = Uint32.valueOf(255);
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+            oos.writeObject(source);
+        }
+
+        final Object read;
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+            read = ois.readObject();
+        }
+
+        assertSame(source, read);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeByte() {
+        Uint32.valueOf((byte)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeShort() {
+        Uint32.valueOf((short)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeInt() {
+        Uint32.valueOf(-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeLong() {
+        Uint32.valueOf(-1L);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigLong() {
+        Uint32.valueOf(4294967296L);
+    }
+}
diff --git a/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java b/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint64Test.java
new file mode 100644 (file)
index 0000000..33025d9
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.primitives.UnsignedLong;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.math.BigInteger;
+import org.junit.Test;
+
+public class Uint64Test {
+    @Test
+    public void testValueOf() {
+        assertEquals(127, Uint64.valueOf(Byte.MAX_VALUE).byteValue());
+        assertEquals(32767, Uint64.valueOf(Short.MAX_VALUE).shortValue());
+        assertEquals(2147483647, Uint64.valueOf(Integer.MAX_VALUE).intValue());
+        assertEquals(9223372036854775807L, Uint64.valueOf(Long.MAX_VALUE).longValue());
+        assertEquals(0, Uint64.valueOf("0").intValue());
+    }
+
+    @Test
+    public void testCompareTo() {
+        final Uint64 five = Uint64.valueOf(5);
+        final Uint64 zero = Uint64.valueOf(0);
+        final Uint64 max = Uint64.valueOf(4294967295L);
+
+        assertEquals(0, zero.compareTo(zero));
+        assertEquals(-1, zero.compareTo(five));
+        assertEquals(-1, zero.compareTo(max));
+
+        assertEquals(1, five.compareTo(zero));
+        assertEquals(0, five.compareTo(five));
+        assertEquals(-1, five.compareTo(max));
+
+        assertEquals(1, max.compareTo(zero));
+        assertEquals(1, max.compareTo(five));
+        assertEquals(0, max.compareTo(max));
+    }
+
+    @Test
+    public void testEquals() {
+        final Uint64 five = Uint64.valueOf(5);
+        final Uint64 zero = Uint64.valueOf(0);
+        final Uint64 max = Uint64.valueOf(4294967295L);
+
+        final Uint64 test = new Uint64(5);
+        assertFalse(test.equals(zero));
+        assertFalse(test.equals(new Object()));
+        assertFalse(test.equals(max));
+        assertTrue(test.equals(test));
+        assertTrue(test.equals(five));
+        assertTrue(five.equals(test));
+    }
+
+    @Test
+    public void testToString() {
+        assertEquals("0", Uint64.valueOf(0).toString());
+        assertEquals("2147483647", Uint64.valueOf(2147483647L).toString());
+        assertEquals("2147483648", Uint64.valueOf(2147483648L).toString());
+        assertEquals("4294967295", Uint64.valueOf(4294967295L).toString());
+    }
+
+    @Test
+    public void testHashCode() {
+        assertEquals(Long.hashCode(-63), Uint64.fromLongBits(-63L).hashCode());
+    }
+
+    @Test
+    public void testFloatValue() {
+        assertEquals(0, Uint64.valueOf(0).floatValue(), 0);
+    }
+
+    @Test
+    public void testDoubleValue() {
+        assertEquals(0, Uint64.valueOf(0).doubleValue(), 0);
+    }
+
+    @Test
+    public void testConversions() {
+        assertSame(Uint64.valueOf(5), Uint64.valueOf(Uint8.valueOf(5)));
+        assertSame(Uint64.valueOf(10), Uint64.valueOf(Uint16.valueOf(10)));
+        assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
+        assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
+
+        assertSame(Uint64.valueOf(5), Uint64.fromUnsignedLong(UnsignedLong.fromLongBits(5)));
+        assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toUnsignedLong());
+    }
+
+    @Test
+    public void testSerialization() throws IOException, ClassNotFoundException {
+        final Uint64 source = Uint64.valueOf(255);
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+            oos.writeObject(source);
+        }
+
+        final Object read;
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+            read = ois.readObject();
+        }
+
+        assertSame(source, read);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeByte() {
+        Uint64.valueOf((byte)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeShort() {
+        Uint64.valueOf((short)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeInt() {
+        Uint64.valueOf(-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeLong() {
+        Uint64.valueOf(-1L);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeBigInteger() {
+        Uint64.valueOf(new BigInteger("-1"));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigBigInteger() {
+        Uint64.valueOf(new BigInteger("0x10000000000000000"));
+    }
+}
diff --git a/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java b/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Uint8Test.java
new file mode 100644 (file)
index 0000000..137af53
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.junit.Test;
+
+public class Uint8Test {
+    @Test
+    public void testValueOf() {
+        assertEquals(127, Uint8.valueOf(Byte.MAX_VALUE).byteValue());
+        assertEquals(255, Uint8.valueOf(255).intValue());
+        assertEquals(255L, Uint8.valueOf(255L).longValue());
+        assertEquals(0, Uint8.valueOf("0").intValue());
+    }
+
+    @Test
+    public void testCompareTo() {
+        final Uint8 five = Uint8.valueOf(5);
+        final Uint8 zero = Uint8.valueOf(0);
+        final Uint8 max = Uint8.valueOf(255);
+
+        assertEquals(0, zero.compareTo(zero));
+        assertEquals(-5, zero.compareTo(five));
+        assertEquals(-255, zero.compareTo(max));
+
+        assertEquals(5, five.compareTo(zero));
+        assertEquals(0, five.compareTo(five));
+        assertEquals(-250, five.compareTo(max));
+
+        assertEquals(255, max.compareTo(zero));
+        assertEquals(250, max.compareTo(five));
+        assertEquals(0, max.compareTo(max));
+    }
+
+    @Test
+    public void testEquals() {
+        final Uint8 five = Uint8.valueOf(5);
+        final Uint8 zero = Uint8.valueOf(0);
+        final Uint8 max = Uint8.valueOf(255);
+
+        final Uint8 test = new Uint8((byte) 5);
+        assertFalse(test.equals(zero));
+        assertFalse(test.equals(new Object()));
+        assertFalse(test.equals(max));
+        assertTrue(test.equals(test));
+        assertTrue(test.equals(five));
+        assertTrue(five.equals(test));
+    }
+
+    @Test
+    public void testToString() {
+        assertEquals("0", Uint8.valueOf(0).toString());
+        assertEquals("127", Uint8.valueOf(127).toString());
+        assertEquals("128", Uint8.valueOf(128).toString());
+        assertEquals("255", Uint8.valueOf(255).toString());
+    }
+
+    @Test
+    public void testHashCode() {
+        assertEquals(Byte.hashCode((byte)-63), Uint8.fromByteBits((byte)-63).hashCode());
+    }
+
+    @Test
+    public void testFloatValue() {
+        assertEquals(0, Uint8.valueOf(0).floatValue(), 0);
+    }
+
+    @Test
+    public void testDoubleValue() {
+        assertEquals(0, Uint8.valueOf(0).doubleValue(), 0);
+    }
+
+    @Test
+    public void testConversions() {
+        assertSame(Uint8.valueOf(5), Uint8.valueOf(Uint16.valueOf(5)));
+        assertSame(Uint8.valueOf(10), Uint8.valueOf(Uint32.valueOf(10)));
+        assertSame(Uint8.valueOf(20), Uint8.valueOf(Uint64.valueOf(20)));
+    }
+
+    @Test
+    public void testSerialization() throws IOException, ClassNotFoundException {
+        final Uint8 source = Uint8.valueOf(255);
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+            oos.writeObject(source);
+        }
+
+        final Object read;
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+            read = ois.readObject();
+        }
+
+        assertSame(source, read);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeByte() {
+        Uint8.valueOf((byte)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeShort() {
+        Uint8.valueOf((short)-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeInt() {
+        Uint8.valueOf(-1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNegativeLong() {
+        Uint8.valueOf(-1L);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigShort() {
+        Uint8.valueOf((short)256);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigInt() {
+        Uint8.valueOf(256);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testBigLong() {
+        Uint8.valueOf(256L);
+    }
+}