blob: 81d9d7f5dc811b7fdef28c448fa484934f76d3c8 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -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;
10import static org.junit.Assert.fail;
11
12import java.text.MessageFormat;
13
14import org.hamcrest.Matchers;
15import org.jboss.netty.buffer.ChannelBuffer;
16import org.jboss.netty.buffer.ChannelBuffers;
17import org.junit.Before;
18import org.junit.Test;
19
20import com.google.common.hash.HashCode;
21import com.google.common.hash.Hasher;
22import com.google.common.hash.Hashing;
23
24public class U128Test {
25 private Triple[] triples;
26
27
28 @Test
29 public void testPositiveRaws() {
30 assertThat(U128.of(0, 0).getMsb(), equalTo(0L));
31 assertThat(U128.of(0, 0).getLsb(), equalTo(0L));
32
33 assertThat(U128.of(1, 2).getMsb(), equalTo(1L));
34 assertThat(U128.of(1, 2).getLsb(), equalTo(2L));
35 }
36
37 @Test
38 public void testReadBytes() {
39 ChannelBuffer empty = ChannelBuffers.wrappedBuffer(new byte[16]);
40 U128 uEmpty = U128.read16Bytes(empty);
41 assertThat(uEmpty.getMsb(), equalTo(0L));
42 assertThat(uEmpty.getLsb(), equalTo(0L));
43
44 ChannelBuffer value = ChannelBuffers.wrappedBuffer(
45 new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88,
46 (byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd,
47 (byte) 0xee, (byte) 0xff, 0x11 });
48 U128 uValue = U128.read16Bytes(value);
49 assertThat(uValue.getMsb(), equalTo(0x1122334455667788L));
50 assertThat(uValue.getLsb(), equalTo(0x99aabbccddeeff11L));
51 }
52
53 @Test
54 public void testPutTo() {
55 U128 h = U128.of(0x1234_5678_90ab_cdefL,0xdeafbeefdeadbeefL);
56 U128 hSame = U128.of(0x1234_5678_90ab_cdefL,0xdeafbeefdeadbeefL);
57
58 U128 hBothDiff = U128.of(0x1234_5678_90ab_cdefL,0x1234_5678_90ab_cdefL);
59 U128 hMsbDiff = U128.of(0x0234_5678_90ab_cdefL,0xdeafbeefdeadbeefL);
60 U128 hLsbDiff = U128.of(0x1234_5678_90ab_cdefL,0xdeafbeefdeadbeeeL);
61
62 assertThat(hash(h), equalTo(hash(hSame)));
63 assertThat(hash(h), not(hash(hBothDiff)));
64 assertThat(hash(h), not(hash(hMsbDiff)));
65 assertThat(hash(h), not(hash(hLsbDiff)));
66 }
67
68 private HashCode hash(U128 f) {
69 Hasher hash = Hashing.murmur3_128().newHasher();
70 f.putTo(hash);
71 return hash.hash();
72
73 }
74
75 @Test
76 public void testEqualHashCode() {
77 U128 h1 = U128.of(0xdeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
78 U128 h2 = U128.of(0xdeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
79 U128 h3 = U128.of(0xeeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
80 U128 h3_2 = U128.of(0xdeafbeefdeadbeefL, 0xeeafbeefdeadbeefL);
81
82 assertTrue(h1.equals(h1));
83 assertTrue(h1.equals(h2));
84 assertFalse(h1.equals(h3));
85 assertFalse(h1.equals(h3_2));
86 assertTrue(h2.equals(h1));
87
88 assertEquals(h1.hashCode(), h2.hashCode());
89 assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
90 assertNotEquals(h1.hashCode(), h3_2.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
91 }
92
93 @Test
94 public void testXor() {
95 U128 hNull = U128.of(0, 0);
96 U128 hDeadBeef = U128.of(0xdeafbeefdeadbeefL, 0xdeafbeefdeadbeefL);
97 assertThat(hNull.xor(hNull), equalTo(hNull));
98 assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef));
99 assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef));
100 assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull));
101
102
103 U128 h1_0 = U128.of(1L, 0);
104 U128 h8_0 = U128.of(0x8000000000000000L, 0);
105 U128 h81_0 = U128.of(0x8000000000000001L, 0);
106 assertThat(h1_0.xor(h8_0), equalTo(h81_0));
107
108 U128 h0_1 = U128.of(0, 1L);
109 U128 h0_8 = U128.of(0, 0x8000000000000000L);
110 U128 h0_81 = U128.of(0, 0x8000000000000001L);
111 assertThat(h0_1.xor(h0_8), equalTo(h0_81));
112 }
113
114 @Test
115 public void testKeyBits() {
116 U128 zeroU = U128.of(0,0);
117 assertThat(zeroU.prefixBits(0), equalTo(0));
118 assertThat(zeroU.prefixBits(16), equalTo(0));
119 assertThat(zeroU.prefixBits(32), equalTo(0));
120
121 checkInvalidKeyBitSize(zeroU, 33);
122 checkInvalidKeyBitSize(zeroU, 64);
123 assertThat(zeroU.prefixBits(3), equalTo(0));
124
125 U128 positiveU = U128.of(0x1234_5678_1234_5678L, 0x1234_5678_1234_5678L);
126 assertThat(positiveU.prefixBits(0), equalTo(0));
127 assertThat(positiveU.prefixBits(16), equalTo(0x1234));
128 assertThat(positiveU.prefixBits(32), equalTo(0x12345678));
129 checkInvalidKeyBitSize(positiveU, 33);
130 checkInvalidKeyBitSize(positiveU, 64);
131
132 U128 signedBitU = U128.of(0x8765_4321_8765_4321L, 0x1234_5678_1234_5678L);
133 assertThat(signedBitU.prefixBits(0), equalTo(0));
134 assertThat(signedBitU.prefixBits(16), equalTo(0x8765));
135 assertThat(signedBitU.prefixBits(32), equalTo(0x8765_4321));
136 checkInvalidKeyBitSize(signedBitU, 33);
137 checkInvalidKeyBitSize(signedBitU, 64);
138 }
139
140 private void
141 checkInvalidKeyBitSize(U128 u, int prefixBit) {
142 try {
143 u.prefixBits(prefixBit);
144 fail("Expected exception not thrown for "+prefixBit + " bits");
145 } catch(IllegalArgumentException e) {
146 // expected
147 }
148 }
149
150 public static class Triple {
151 U128 a, b, c;
152
153 public Triple(U128 a, U128 b, U128 c) {
154 this.a = a;
155 this.b = b;
156 this.c = c;
157 }
158
159 public static Triple of(U128 a, U128 b, U128 c) {
160 return new Triple(a, b, c);
161 }
162
163 public String msg(String string) {
164 return MessageFormat.format(string, a,b,c);
165 }
166 }
167
168 @Before
169 public void setup() {
170 U128 u0_0 = U128.of(0, 0);
171 U128 u0_1 = U128.of(0, 1);
172 U128 u1_0 = U128.of(1, 0);
173 U128 u1_1 = U128.of(1, 1);
174
175 U128 u0_2 = U128.of(0, 2);
176 U128 u2_0 = U128.of(2, 0);
177
178 U128 u0_f = U128.of(0, -1L);
179 U128 uf_0 = U128.of(-1L, 0);
180
181 triples = new Triple[] {
182 Triple.of(u0_0, u0_0, u0_0),
183 Triple.of(u0_0, u0_1, u0_1),
184 Triple.of(u0_0, u1_0, u1_0),
185 Triple.of(u0_1, u1_0, u1_1),
186
187 Triple.of(u0_1, u0_1, u0_2),
188 Triple.of(u1_0, u1_0, u2_0),
189
190 Triple.of(u0_1, u0_f, u1_0),
191
192 Triple.of(u0_1, u0_f, u1_0),
193 Triple.of(u0_f, u0_f, U128.of(1, 0xffff_ffff_ffff_fffeL)),
194 Triple.of(uf_0, u0_f, U128.of(-1, -1)),
195 Triple.of(uf_0, u1_0, U128.ZERO),
196
197 Triple.of(U128.of(0x1234_5678_9abc_def1L, 0x1234_5678_9abc_def1L),
198 U128.of(0xedcb_a987_6543_210eL, 0xedcb_a987_6543_210fL),
199 U128.ZERO)
200 };
201 }
202
203 @Test
204 public void testAddSubtract() {
205 for(Triple t: triples) {
206 assertThat(t.msg("{0} + {1} = {2}"), t.a.add(t.b), equalTo(t.c));
207 assertThat(t.msg("{1} + {0} = {2}"), t.b.add(t.a), equalTo(t.c));
208
209 assertThat(t.msg("{2} - {0} = {1}"), t.c.subtract(t.a), equalTo(t.b));
210 assertThat(t.msg("{2} - {1} = {0}"), t.c.subtract(t.b), equalTo(t.a));
211 }
212 }
213
214 @Test
215 public void testAddSubtractBuilder() {
216 for(Triple t: triples) {
217 assertThat(t.msg("{0} + {1} = {2}"), t.a.builder().add(t.b).build(), equalTo(t.c));
218 assertThat(t.msg("{1} + {0} = {2}"), t.b.builder().add(t.a).build(), equalTo(t.c));
219
220 assertThat(t.msg("{2} - {0} = {1}"), t.c.builder().subtract(t.a).build(), equalTo(t.b));
221 assertThat(t.msg("{2} - {1} = {0}"), t.c.builder().subtract(t.b).build(), equalTo(t.a));
222 }
223 }
224
225 @Test
226 public void testCompare() {
227 U128 u0_0 = U128.of(0, 0);
228 U128 u0_1 = U128.of(0, 1);
229 U128 u0_8 = U128.of(0, 0x8765_4321_8765_4321L);
230 U128 u1_0 = U128.of(0x1234_5678_1234_5678L, 0);
231 U128 u8_0 = U128.of(0x8765_4321_8765_4321L, 0);
232 U128 uf_0 = U128.of(0xFFFF_FFFF_FFFF_FFFFL, 0);
233
234 U128[] us = new U128[] { u0_0, u0_1, u0_8, u1_0, u8_0, uf_0 };
235
236 for(int i = 0; i< us.length; i++) {
237 U128 u_base = us[i];
238 assertThat(
239 String.format("%s should be equal to itself (compareTo)", u_base),
240 u_base.compareTo(u_base), equalTo(0));
241 assertThat(
242 String.format("%s should be equal to itself (equals)", u_base),
243 u_base.equals(u_base), equalTo(true));
244 assertThat(
245 String.format("%s should be equal to itself (equals, by value)", u_base),
246 u_base.equals(U128.of(u_base.getMsb(), u_base.getLsb())), equalTo(true));
247
248 for(int j = i+1; j< us.length; j++) {
249 U128 u_greater = us[j];
250 assertThat(
251 String.format("%s should not be equal to %s", u_base, u_greater),
252 u_base.equals(u_base), equalTo(true));
253 assertThat(
254 String.format("%s should be smaller than %s", u_base, u_greater),
255 u_base.compareTo(u_greater), Matchers.lessThan(0));
256 assertThat(
257 String.format("%s should be greater than %s", u_greater, u_base),
258 u_greater.compareTo(u_base), Matchers.greaterThan(0));
259 }
260 }
261 }
262
263 @Test
264 public void testBitwiseOperators() {
265 U128 one = U128.of(0x5, 0x8);
266 U128 two = U128.of(0x7, 0x3);
267
268 assertThat(one.inverse(), equalTo(U128.of(0xfffffffffffffffaL, 0xfffffffffffffff7L)));
269 assertThat(one.and(two), equalTo(U128.of(0x5L, 0x0L)));
270 assertThat(one.or(two), equalTo(U128.of(0x7L, 0xbL)));
271 assertThat(one.xor(two), equalTo(U128.of(0x2L, 0xbL)));
272 }
273
274 @Test
275 public void testBitwiseOperatorsBuilder() {
276 U128 one = U128.of(0x5, 0x8);
277 U128 two = U128.of(0x7, 0x3);
278
279 assertThat(one.builder().invert().build(), equalTo(U128.of(0xfffffffffffffffaL, 0xfffffffffffffff7L)));
280 assertThat(one.builder().and(two).build(), equalTo(U128.of(0x5L, 0x0L)));
281 assertThat(one.builder().or(two).build(), equalTo(U128.of(0x7L, 0xbL)));
282 assertThat(one.builder().xor(two).build(), equalTo(U128.of(0x2L, 0xbL)));
283 }
284
285}