blob: b0cca234bce8bc326bbaa1d05d951752285c9281 [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;
Andreas Wundsamc5e98702014-05-05 13:12:19 -07009import static org.junit.Assert.fail;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010
11import java.math.BigInteger;
12
13import org.junit.Test;
14
15public class U64Test {
16
17 @Test
18 public void testPositiveRaws() {
19 for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) {
20 assertEquals(positive, U64.ofRaw(positive).getValue());
21 assertEquals(BigInteger.valueOf(positive), U64.ofRaw(positive).getBigInteger());
22 }
23 }
24
25 @Test
26 public void testNegativeRaws() {
Andreas Wundsambc0aead2014-04-24 19:01:36 -070027 long minus_1 = 0xFFFF_FFFF_FFFF_FFFFL;
Yotam Harcholf3f11152013-09-05 16:47:16 -070028 assertEquals(minus_1, U64.ofRaw(minus_1).getValue());
Andreas Wundsambc0aead2014-04-24 19:01:36 -070029 assertEquals(new BigInteger("FFFF_FFFF_FFFF_FFFF".replace("_", ""), 16), U64.ofRaw(minus_1).getBigInteger());
Yotam Harcholf3f11152013-09-05 16:47:16 -070030 assertEquals(new BigInteger("18446744073709551615"), U64.ofRaw(minus_1).getBigInteger());
31 }
Andreas Wundsambc0aead2014-04-24 19:01:36 -070032
33 @Test
34 public void testEqualHashCode() {
35 U64 h1 = U64.of(0xdeafbeefdeadbeefL);
36 U64 h2 = U64.of(0xdeafbeefdeadbeefL);
37 U64 h3 = U64.of(0xeeafbeefdeadbeefL);
38
39 assertTrue(h1.equals(h1));
40 assertTrue(h1.equals(h2));
41 assertFalse(h1.equals(h3));
42 assertTrue(h2.equals(h1));
43
44 assertEquals(h1.hashCode(), h2.hashCode());
45 assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
46 }
47
48 @Test
49 public void testXor() {
50 U64 hNull = U64.of(0);
51 U64 hDeadBeef = U64.of(0xdeafbeefdeadbeefL);
52 assertThat(hNull.xor(hNull), equalTo(hNull));
53 assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef));
54 assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef));
55 assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull));
56
57
58 U64 h1 = U64.of(1L);
59 U64 h8 = U64.of(0x8000000000000000L);
60 U64 h81 = U64.of(0x8000000000000001L);
61 assertThat(h1.xor(h8), equalTo(h81));
62 }
63
64 @Test
65 public void testCombine() {
66 long key = 0x1234567890abcdefL;
67 long val = 0xdeafbeefdeadbeefL;
68 U64 hkey = U64.of(key);
69 U64 hVal = U64.of(val);
70
71 assertThat(hkey.combineWithValue(hVal, 0), equalTo(hkey.xor(hVal)));
72 assertThat(hkey.combineWithValue(hVal, 64), equalTo(hkey));
73 long mask32 = 0x00000000FFFFFFFFL;
74 assertThat(hkey.combineWithValue(hVal, 32),
75 equalTo(U64.of(key & ~mask32| (key ^ val) & mask32)));
76
77 long tenMask = 0x003FFFFFFFFFFFFFL;
78 assertThat(hkey.combineWithValue(hVal, 10),
79 equalTo(U64.of(key & ~tenMask | (key ^ val) & tenMask)));
80 }
81
Andreas Wundsamc5e98702014-05-05 13:12:19 -070082 @Test
83 public void testKeyBits() {
84 U64 zeroU = U64.of(0);
85 assertThat(zeroU.prefixBits(0), equalTo(0));
86 assertThat(zeroU.prefixBits(16), equalTo(0));
87 assertThat(zeroU.prefixBits(32), equalTo(0));
88
89 checkInvalidKeyBitSize(zeroU, 33);
90 checkInvalidKeyBitSize(zeroU, 64);
91 assertThat(zeroU.prefixBits(3), equalTo(0));
92
93 U64 positiveU = U64.of(0x1234_5678_1234_5678L);
94 assertThat(positiveU.prefixBits(0), equalTo(0));
95 assertThat(positiveU.prefixBits(16), equalTo(0x1234));
96 assertThat(positiveU.prefixBits(32), equalTo(0x12345678));
97 checkInvalidKeyBitSize(positiveU, 33);
98 checkInvalidKeyBitSize(positiveU, 64);
99
100 U64 signedBitU = U64.of(0x8765_4321_8765_4321L);
101 assertThat(signedBitU.prefixBits(0), equalTo(0));
102 assertThat(signedBitU.prefixBits(16), equalTo(0x8765));
103 assertThat(signedBitU.prefixBits(32), equalTo(0x8765_4321));
104 checkInvalidKeyBitSize(signedBitU, 33);
105 checkInvalidKeyBitSize(signedBitU, 64);
106 }
107
108 private void
109 checkInvalidKeyBitSize(U64 u, int prefixBit) {
110 try {
111 u.prefixBits(prefixBit);
112 fail("Expected exception not thrown for "+prefixBit + " bits");
113 } catch(IllegalArgumentException e) {
114 // expected
115 }
116 }
117
Yotam Harcholf3f11152013-09-05 16:47:16 -0700118}