blob: a979fab55d0ec20bda108b64b56c624b4051dd00 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Andreas Wundsambc0aead2014-04-24 19:01:36 -07003import static org.hamcrest.CoreMatchers.equalTo;
Yotam Harcholf3f11152013-09-05 16:47:16 -07004import static org.junit.Assert.assertEquals;
Andreas Wundsambc0aead2014-04-24 19:01:36 -07005import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotEquals;
7import static org.junit.Assert.assertThat;
8import static org.junit.Assert.assertTrue;
Yotam Harcholf3f11152013-09-05 16:47:16 -07009
10import java.math.BigInteger;
11
12import org.junit.Test;
13
14public class U64Test {
15
16 @Test
17 public void testPositiveRaws() {
18 for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) {
19 assertEquals(positive, U64.ofRaw(positive).getValue());
20 assertEquals(BigInteger.valueOf(positive), U64.ofRaw(positive).getBigInteger());
21 }
22 }
23
24 @Test
25 public void testNegativeRaws() {
Andreas Wundsambc0aead2014-04-24 19:01:36 -070026 long minus_1 = 0xFFFF_FFFF_FFFF_FFFFL;
Yotam Harcholf3f11152013-09-05 16:47:16 -070027 assertEquals(minus_1, U64.ofRaw(minus_1).getValue());
Andreas Wundsambc0aead2014-04-24 19:01:36 -070028 assertEquals(new BigInteger("FFFF_FFFF_FFFF_FFFF".replace("_", ""), 16), U64.ofRaw(minus_1).getBigInteger());
Yotam Harcholf3f11152013-09-05 16:47:16 -070029 assertEquals(new BigInteger("18446744073709551615"), U64.ofRaw(minus_1).getBigInteger());
30 }
Andreas Wundsambc0aead2014-04-24 19:01:36 -070031
32 @Test
33 public void testEqualHashCode() {
34 U64 h1 = U64.of(0xdeafbeefdeadbeefL);
35 U64 h2 = U64.of(0xdeafbeefdeadbeefL);
36 U64 h3 = U64.of(0xeeafbeefdeadbeefL);
37
38 assertTrue(h1.equals(h1));
39 assertTrue(h1.equals(h2));
40 assertFalse(h1.equals(h3));
41 assertTrue(h2.equals(h1));
42
43 assertEquals(h1.hashCode(), h2.hashCode());
44 assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
45 }
46
47 @Test
48 public void testXor() {
49 U64 hNull = U64.of(0);
50 U64 hDeadBeef = U64.of(0xdeafbeefdeadbeefL);
51 assertThat(hNull.xor(hNull), equalTo(hNull));
52 assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef));
53 assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef));
54 assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull));
55
56
57 U64 h1 = U64.of(1L);
58 U64 h8 = U64.of(0x8000000000000000L);
59 U64 h81 = U64.of(0x8000000000000001L);
60 assertThat(h1.xor(h8), equalTo(h81));
61 }
62
63 @Test
64 public void testCombine() {
65 long key = 0x1234567890abcdefL;
66 long val = 0xdeafbeefdeadbeefL;
67 U64 hkey = U64.of(key);
68 U64 hVal = U64.of(val);
69
70 assertThat(hkey.combineWithValue(hVal, 0), equalTo(hkey.xor(hVal)));
71 assertThat(hkey.combineWithValue(hVal, 64), equalTo(hkey));
72 long mask32 = 0x00000000FFFFFFFFL;
73 assertThat(hkey.combineWithValue(hVal, 32),
74 equalTo(U64.of(key & ~mask32| (key ^ val) & mask32)));
75
76 long tenMask = 0x003FFFFFFFFFFFFFL;
77 assertThat(hkey.combineWithValue(hVal, 10),
78 equalTo(U64.of(key & ~tenMask | (key ^ val) & tenMask)));
79 }
80
Yotam Harcholf3f11152013-09-05 16:47:16 -070081}