Merge "Updated registration of Binding Aware notification listeners."
[controller.git] / third-party / openflowj / src / test / java / org / openflow / util / UnsignedTest.java
1 package org.openflow.util;
2
3 import java.math.BigInteger;
4 import java.nio.ByteBuffer;
5
6 import junit.framework.TestCase;
7
8 public class UnsignedTest extends TestCase {
9     public static String ULONG_MAX = "18446744073709551615";
10
11   /**
12    * Tests that we correctly extract an unsigned long into a BigInteger
13    * @throws Exception
14    */
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);
19     bb.position(0);
20     bb.limit(8);
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));
26     }
27     TestCase.assertEquals(ULONG_MAX, bi.toString());
28
29     bb = ByteBuffer.allocate(10);
30     bb.put((byte)0x00);
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);
33     bb.put((byte)0x00);
34     bb.position(0);
35     bb.limit(10);
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));
41     }
42     TestCase.assertEquals(ULONG_MAX, bi.toString());
43   }
44
45   /**
46    * Tests that we correctly put an unsigned long into a ByteBuffer
47    * @throws Exception
48    */
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);
56     }
57
58     bb = ByteBuffer.allocate(10);
59     Unsigned.putUnsignedLong(bb, uLongMax, 1);
60     int offset = 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);
64     }
65   }
66 }