blob: fb5cd23af10e7a9ae8546535c52ccf10dcb335aa [file] [log] [blame]
Andreas Wundsam7c15b172014-04-24 19:02:40 -07001package org.projectfloodlight.openflow.types;
2
3import static org.hamcrest.CoreMatchers.equalTo;
4import static org.hamcrest.CoreMatchers.not;
5import static org.junit.Assert.assertEquals;
6import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertNotEquals;
8import static org.junit.Assert.assertThat;
9import static org.junit.Assert.assertTrue;
Andreas Wundsamc5e98702014-05-05 13:12:19 -070010import static org.junit.Assert.fail;
Andreas Wundsam7c15b172014-04-24 19:02:40 -070011
Andreas Wundsam972bc422014-05-05 13:29:50 -070012import org.hamcrest.Matchers;
Andreas Wundsam7c15b172014-04-24 19:02:40 -070013import org.junit.Test;
14
15import com.google.common.hash.HashCode;
16import com.google.common.hash.Hasher;
17import com.google.common.hash.Hashing;
18
19public class U128Test {
20 @Test
21 public void testPositiveRaws() {
22 assertThat(U128.of(0, 0).getMsb(), equalTo(0L));
23 assertThat(U128.of(0, 0).getLsb(), equalTo(0L));
24
25 assertThat(U128.of(1, 2).getMsb(), equalTo(1L));
26 assertThat(U128.of(1, 2).getLsb(), equalTo(2L));
27 }
28
29 @Test
30 public void testPutTo() {
31 U128 h = U128.of(0x1234_5678_90ab_cdefL,0xdeafbeefdeadbeefL);
32 U128 hSame = U128.of(0x1234_5678_90ab_cdefL,0xdeafbeefdeadbeefL);
33
34 U128 hBothDiff = U128.of(0x1234_5678_90ab_cdefL,0x1234_5678_90ab_cdefL);
35 U128 hMsbDiff = U128.of(0x0234_5678_90ab_cdefL,0xdeafbeefdeadbeefL);
36 U128 hLsbDiff = U128.of(0x1234_5678_90ab_cdefL,0xdeafbeefdeadbeeeL);
37
38 assertThat(hash(h), equalTo(hash(hSame)));
39 assertThat(hash(h), not(hash(hBothDiff)));
40 assertThat(hash(h), not(hash(hMsbDiff)));
41 assertThat(hash(h), not(hash(hLsbDiff)));
42 }
43
44 private HashCode hash(U128 f) {
45 Hasher hash = Hashing.murmur3_128().newHasher();
46 f.putTo(hash);
47 return hash.hash();
48
49 }
50
51 @Test
52 public void testEqualHashCode() {
53 U128 h1 = U128.of(0xdeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
54 U128 h2 = U128.of(0xdeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
55 U128 h3 = U128.of(0xeeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
56 U128 h3_2 = U128.of(0xdeafbeefdeadbeefL, 0xeeafbeefdeadbeefL);
57
58 assertTrue(h1.equals(h1));
59 assertTrue(h1.equals(h2));
60 assertFalse(h1.equals(h3));
61 assertFalse(h1.equals(h3_2));
62 assertTrue(h2.equals(h1));
63
64 assertEquals(h1.hashCode(), h2.hashCode());
65 assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
66 assertNotEquals(h1.hashCode(), h3_2.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
67 }
68
69 @Test
70 public void testXor() {
71 U128 hNull = U128.of(0, 0);
72 U128 hDeadBeef = U128.of(0xdeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
73 assertThat(hNull.xor(hNull), equalTo(hNull));
74 assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef));
75 assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef));
76 assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull));
77
78
79 U128 h1_0 = U128.of(1L, 0);
80 U128 h8_0 = U128.of(0x8000000000000000L, 0);
81 U128 h81_0 = U128.of(0x8000000000000001L, 0);
82 assertThat(h1_0.xor(h8_0), equalTo(h81_0));
83
84 U128 h0_1 = U128.of(0, 1L);
85 U128 h0_8 = U128.of(0, 0x8000000000000000L);
86 U128 h0_81 = U128.of(0, 0x8000000000000001L);
87 assertThat(h0_1.xor(h0_8), equalTo(h0_81));
88 }
89
90 @Test
Andreas Wundsamc5e98702014-05-05 13:12:19 -070091 public void testKeyBits() {
92 U128 zeroU = U128.of(0,0);
93 assertThat(zeroU.prefixBits(0), equalTo(0));
94 assertThat(zeroU.prefixBits(16), equalTo(0));
95 assertThat(zeroU.prefixBits(32), equalTo(0));
96
97 checkInvalidKeyBitSize(zeroU, 33);
98 checkInvalidKeyBitSize(zeroU, 64);
99 assertThat(zeroU.prefixBits(3), equalTo(0));
100
101 U128 positiveU = U128.of(0x1234_5678_1234_5678L, 0x1234_5678_1234_5678L);
102 assertThat(positiveU.prefixBits(0), equalTo(0));
103 assertThat(positiveU.prefixBits(16), equalTo(0x1234));
104 assertThat(positiveU.prefixBits(32), equalTo(0x12345678));
105 checkInvalidKeyBitSize(positiveU, 33);
106 checkInvalidKeyBitSize(positiveU, 64);
107
108 U128 signedBitU = U128.of(0x8765_4321_8765_4321L, 0x1234_5678_1234_5678L);
109 assertThat(signedBitU.prefixBits(0), equalTo(0));
110 assertThat(signedBitU.prefixBits(16), equalTo(0x8765));
111 assertThat(signedBitU.prefixBits(32), equalTo(0x8765_4321));
112 checkInvalidKeyBitSize(signedBitU, 33);
113 checkInvalidKeyBitSize(signedBitU, 64);
114 }
115
116 private void
117 checkInvalidKeyBitSize(U128 u, int prefixBit) {
118 try {
119 u.prefixBits(prefixBit);
120 fail("Expected exception not thrown for "+prefixBit + " bits");
121 } catch(IllegalArgumentException e) {
122 // expected
123 }
124 }
125
Andreas Wundsam972bc422014-05-05 13:29:50 -0700126
127 @Test
128 public void testCompare() {
129 U128 u0_0 = U128.of(0, 0);
130 U128 u0_1 = U128.of(0, 1);
131 U128 u0_8 = U128.of(0, 0x8765_4321_8765_4321L);
132 U128 u1_0 = U128.of(0x1234_5678_1234_5678L, 0);
133 U128 u8_0 = U128.of(0x8765_4321_8765_4321L, 0);
134 U128 uf_0 = U128.of(0xFFFF_FFFF_FFFF_FFFFL, 0);
135
136 U128[] us = new U128[] { u0_0, u0_1, u0_8, u1_0, u8_0, uf_0 };
137
138 for(int i = 0; i< us.length; i++) {
139 U128 u_base = us[i];
140 assertThat(
141 String.format("%s should be equal to itself (compareTo)", u_base),
142 u_base.compareTo(u_base), equalTo(0));
143 assertThat(
144 String.format("%s should be equal to itself (equals)", u_base),
145 u_base.equals(u_base), equalTo(true));
146 assertThat(
147 String.format("%s should be equal to itself (equals, by value)", u_base),
148 u_base.equals(U128.of(u_base.getMsb(), u_base.getLsb())), equalTo(true));
149
150 for(int j = i+1; j< us.length; j++) {
151 U128 u_greater = us[j];
152 assertThat(
153 String.format("%s should not be equal to %s", u_base, u_greater),
154 u_base.equals(u_base), equalTo(true));
155 assertThat(
156 String.format("%s should be smaller than %s", u_base, u_greater),
157 u_base.compareTo(u_greater), Matchers.lessThan(0));
158 assertThat(
159 String.format("%s should be greater than %s", u_greater, u_base),
160 u_greater.compareTo(u_base), Matchers.greaterThan(0));
161 }
162 }
163 }
164
Andreas Wundsamc5e98702014-05-05 13:12:19 -0700165 @Test
Andreas Wundsam7c15b172014-04-24 19:02:40 -0700166 public void testCombine() {
167 long key = 0x1234567890abcdefL;
168 long val = 0xdeafbeefdeadbeefL;
169 U128 hkey = U128.of(key, key*2);
170 U128 hVal = U128.of(val, val/2);
171
172 assertThat(hkey.combineWithValue(hVal, 0), equalTo(hkey.xor(hVal)));
173 assertThat(hkey.combineWithValue(hVal, 64), equalTo(U128.of(hkey.getMsb(), hkey.getLsb() ^ hVal.getLsb())));
174 assertThat(hkey.combineWithValue(hVal, 128), equalTo(hkey));
175
176 long mask8 = 0xFF00_0000_0000_0000L;
177
178 assertThat(hkey.combineWithValue(hVal, 8), equalTo(U128.of(hkey.getMsb() & mask8 | hkey.getMsb() ^ hVal.getMsb() & ~mask8,
179 hkey.getLsb() ^ hVal.getLsb() )));
180 }
181
182}