blob: 7cdf7391bccd390ca941338cf99563dbb9da229c [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3* University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package org.openflow.util;
19
20import java.math.BigInteger;
21import java.nio.ByteBuffer;
22
23import junit.framework.TestCase;
24
25public class UnsignedTest extends TestCase {
26 public static String ULONG_MAX = "18446744073709551615";
27
28 /**
29 * Tests that we correctly extract an unsigned long into a BigInteger
30 * @throws Exception
31 */
32 public void testGetUnsignedLong() throws Exception {
33 ByteBuffer bb = ByteBuffer.allocate(8);
34 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
35 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
36 bb.position(0);
37 bb.limit(8);
38 BigInteger bi = Unsigned.getUnsignedLong(bb);
39 BigInteger uLongMax = new BigInteger(ULONG_MAX);
40 for (int i = 0; i < uLongMax.bitCount(); ++i) {
41 TestCase.assertTrue("Bit: " + i + " should be: " + uLongMax.testBit(i),
42 uLongMax.testBit(i) == bi.testBit(i));
43 }
44 TestCase.assertEquals(ULONG_MAX, bi.toString());
45
46 bb = ByteBuffer.allocate(10);
47 bb.put((byte)0x00);
48 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
49 bb.put((byte)0xff).put((byte)0xff).put((byte)0xff).put((byte)0xff);
50 bb.put((byte)0x00);
51 bb.position(0);
52 bb.limit(10);
53 bi = Unsigned.getUnsignedLong(bb, 1);
54 uLongMax = new BigInteger(ULONG_MAX);
55 for (int i = 0; i < uLongMax.bitCount(); ++i) {
56 TestCase.assertTrue("Bit: " + i + " should be: " + uLongMax.testBit(i),
57 uLongMax.testBit(i) == bi.testBit(i));
58 }
59 TestCase.assertEquals(ULONG_MAX, bi.toString());
60 }
61
62 /**
63 * Tests that we correctly put an unsigned long into a ByteBuffer
64 * @throws Exception
65 */
66 public void testPutUnsignedLong() throws Exception {
67 ByteBuffer bb = ByteBuffer.allocate(8);
68 BigInteger uLongMax = new BigInteger(ULONG_MAX);
69 Unsigned.putUnsignedLong(bb, uLongMax);
70 for (int i = 0; i < 8; ++i) {
71 TestCase.assertTrue("Byte: " + i + " should be 0xff, was: " + bb.get(i),
72 (bb.get(i) & (short)0xff) == 0xff);
73 }
74
75 bb = ByteBuffer.allocate(10);
76 Unsigned.putUnsignedLong(bb, uLongMax, 1);
77 int offset = 1;
78 for (int i = 0; i < 8; ++i) {
79 TestCase.assertTrue("Byte: " + i + " should be 0xff, was: " +
80 bb.get(offset+i), (bb.get(offset+i) & (short)0xff) == 0xff);
81 }
82 }
83}