1 package org.openflow.util;
3 import java.math.BigInteger;
4 import java.nio.ByteBuffer;
6 import junit.framework.TestCase;
8 public class UnsignedTest extends TestCase {
9 public static String ULONG_MAX = "18446744073709551615";
12 * Tests that we correctly extract an unsigned long into a BigInteger
15 public void testGetUnsignedLong() throws Exception {
16 ByteBuffer bb = ByteBuffer.allocate(8);
17 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
18 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
21 BigInteger bi = Unsigned.getUnsignedLong(bb);
22 BigInteger uLongMax = new BigInteger(ULONG_MAX);
23 for (int i = 0; i < uLongMax.bitCount(); ++i) {
24 TestCase.assertTrue("Bit: " + i + " should be: " + uLongMax.testBit(i),
25 uLongMax.testBit(i) == bi.testBit(i));
27 TestCase.assertEquals(ULONG_MAX, bi.toString());
29 bb = ByteBuffer.allocate(10);
31 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
32 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
36 bi = Unsigned.getUnsignedLong(bb, 1);
37 uLongMax = new BigInteger(ULONG_MAX);
38 for (int i = 0; i < uLongMax.bitCount(); ++i) {
39 TestCase.assertTrue("Bit: " + i + " should be: " + uLongMax.testBit(i),
40 uLongMax.testBit(i) == bi.testBit(i));
42 TestCase.assertEquals(ULONG_MAX, bi.toString());
46 * Tests that we correctly put an unsigned long into a ByteBuffer
49 public void testPutUnsignedLong() throws Exception {
50 ByteBuffer bb = ByteBuffer.allocate(8);
51 BigInteger uLongMax = new BigInteger(ULONG_MAX);
52 Unsigned.putUnsignedLong(bb, uLongMax);
53 for (int i = 0; i < 8; ++i) {
54 TestCase.assertTrue("Byte: " + i + " should be 0xff, was: " + bb.get(i),
55 (bb.get(i) & (short)0xff) == 0xff);
58 bb = ByteBuffer.allocate(10);
59 Unsigned.putUnsignedLong(bb, uLongMax, 1);
61 for (int i = 0; i < 8; ++i) {
62 TestCase.assertTrue("Byte: " + i + " should be 0xff, was: " +
63 bb.get(offset+i), (bb.get(offset+i) & (short)0xff) == 0xff);