blob: 4066bf85523568c11baa65668b9284d76855a757 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import static org.hamcrest.CoreMatchers.equalTo;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNotEquals;
7import static org.junit.Assert.assertThat;
8import static org.junit.Assert.assertTrue;
9import static org.junit.Assert.fail;
10
11import java.math.BigInteger;
12import java.text.MessageFormat;
13
14import org.junit.Before;
15import org.junit.Test;
16
17public class U64Test {
18
19 private Triple[] triples;
20
21 @Test
22 public void testPositiveRaws() {
23 for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) {
24 assertEquals(positive, U64.ofRaw(positive).getValue());
25 assertEquals(BigInteger.valueOf(positive), U64.ofRaw(positive).getBigInteger());
26 }
27 }
28
29 @Test
30 public void testNegativeRaws() {
31 long minu1 = 0xFFFF_FFFF_FFFF_FFFFL;
32 assertEquals(minu1, U64.ofRaw(minu1).getValue());
33 assertEquals(new BigInteger("FFFF_FFFF_FFFF_FFFF".replace("_", ""), 16), U64.ofRaw(minu1).getBigInteger());
34 assertEquals(new BigInteger("18446744073709551615"), U64.ofRaw(minu1).getBigInteger());
35 }
36
37 @Test
38 public void testEqualHashCode() {
39 U64 h1 = U64.of(0xdeafbeefdeadbeefL);
40 U64 h2 = U64.of(0xdeafbeefdeadbeefL);
41 U64 h3 = U64.of(0xeeafbeefdeadbeefL);
42
43 assertTrue(h1.equals(h1));
44 assertTrue(h1.equals(h2));
45 assertFalse(h1.equals(h3));
46 assertTrue(h2.equals(h1));
47
48 assertEquals(h1.hashCode(), h2.hashCode());
49 assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
50 }
51
52 @Test
53 public void testXor() {
54 U64 hNull = U64.of(0);
55 U64 hDeadBeef = U64.of(0xdeafbeefdeadbeefL);
56 assertThat(hNull.xor(hNull), equalTo(hNull));
57 assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef));
58 assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef));
59 assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull));
60
61
62 U64 h1 = U64.of(1L);
63 U64 h8 = U64.of(0x8000000000000000L);
64 U64 h81 = U64.of(0x8000000000000001L);
65 assertThat(h1.xor(h8), equalTo(h81));
66 }
67
68 @Test
69 public void testKeyBits() {
70 U64 zeroU = U64.of(0);
71 assertThat(zeroU.prefixBits(0), equalTo(0));
72 assertThat(zeroU.prefixBits(16), equalTo(0));
73 assertThat(zeroU.prefixBits(32), equalTo(0));
74
75 checkInvalidKeyBitSize(zeroU, 33);
76 checkInvalidKeyBitSize(zeroU, 64);
77 assertThat(zeroU.prefixBits(3), equalTo(0));
78
79 U64 positiveU = U64.of(0x1234_5678_1234_5678L);
80 assertThat(positiveU.prefixBits(0), equalTo(0));
81 assertThat(positiveU.prefixBits(16), equalTo(0x1234));
82 assertThat(positiveU.prefixBits(32), equalTo(0x12345678));
83 checkInvalidKeyBitSize(positiveU, 33);
84 checkInvalidKeyBitSize(positiveU, 64);
85
86 U64 signedBitU = U64.of(0x8765_4321_8765_4321L);
87 assertThat(signedBitU.prefixBits(0), equalTo(0));
88 assertThat(signedBitU.prefixBits(16), equalTo(0x8765));
89 assertThat(signedBitU.prefixBits(32), equalTo(0x8765_4321));
90 checkInvalidKeyBitSize(signedBitU, 33);
91 checkInvalidKeyBitSize(signedBitU, 64);
92 }
93
94 public static class Triple {
95 U64 a, b, c;
96
97 public Triple(U64 a, U64 b, U64 c) {
98 this.a = a;
99 this.b = b;
100 this.c = c;
101 }
102
103 public static Triple of(U64 a, U64 b, U64 c) {
104 return new Triple(a, b, c);
105 }
106
107 public String msg(String string) {
108 return MessageFormat.format(string, a,b,c);
109 }
110 }
111
112 @Before
113 public void setup() {
114 U64 u0 = U64.of(0);
115 U64 u1 = U64.of(1);
116
117 U64 u2 = U64.of(2);
118 U64 u7f = U64.of(0x7fff_ffff_ffff_ffffL);
119 U64 u8 = U64.of(0x8000_0000_0000_0000L);
120
121 U64 uf = U64.of(-1L);
122
123 triples = new Triple[] {
124 Triple.of(u0, u0, u0),
125 Triple.of(u0, u1, u1),
126
127 Triple.of(u1, u1, u2),
128
129 Triple.of(u1, uf, u0),
130
131 Triple.of(uf, uf, U64.of(0xffff_ffff_ffff_fffeL)),
132 Triple.of(u0, uf, uf),
133
134 Triple.of(u7f, u1, u8),
135
136 Triple.of(U64.of(0x1234_5678_9abc_def1L),
137 U64.of(0xedcb_a987_6543_210fL),
138 U64.ZERO)
139 };
140 }
141
142 @Test
143 public void testAddSubtract() {
144 for(Triple t: triples) {
145 assertThat(t.msg("{0} + {1} = {2}"), t.a.add(t.b), equalTo(t.c));
146 assertThat(t.msg("{1} + {0} = {2}"), t.b.add(t.a), equalTo(t.c));
147
148 assertThat(t.msg("{2} - {0} = {1}"), t.c.subtract(t.a), equalTo(t.b));
149 assertThat(t.msg("{2} - {1} = {0}"), t.c.subtract(t.b), equalTo(t.a));
150 }
151 }
152
153 @Test
154 public void testAddSubtractBuilder() {
155 for(Triple t: triples) {
156 assertThat(t.msg("{0} + {1} = {2}"), t.a.builder().add(t.b).build(), equalTo(t.c));
157 assertThat(t.msg("{1} + {0} = {2}"), t.b.builder().add(t.a).build(), equalTo(t.c));
158
159 assertThat(t.msg("{2} - {0} = {1}"), t.c.builder().subtract(t.a).build(), equalTo(t.b));
160 assertThat(t.msg("{2} - {1} = {0}"), t.c.builder().subtract(t.b).build(), equalTo(t.a));
161 }
162 }
163
164 private void
165 checkInvalidKeyBitSize(U64 u, int prefixBit) {
166 try {
167 u.prefixBits(prefixBit);
168 fail("Expected exception not thrown for "+prefixBit + " bits");
169 } catch(IllegalArgumentException e) {
170 // expected
171 }
172 }
173
174 @Test
175 public void testBitwiseOperators() {
176 U64 notPi = U64.of(0x3141_5926_5358_9793L);
177 U64 notE = U64.of(0x2718_2818_8459_4523L);
178
179 assertThat(notPi.inverse(), equalTo(U64.of(0xcebe_a6d9_aca7_686cL)));
180 assertThat(notPi.and(notE), equalTo(U64.of(0x2100_0800_0058_0503L)));
181 assertThat(notPi.or(notE), equalTo(U64.of(0x3759_793e_d759_d7b3L)));
182 assertThat(notPi.xor(notE), equalTo(U64.of(0x1659_713e_d701_d2b0L)));
183 }
184
185 @Test
186 public void testBitwiseOperatorsBuilder() {
187 U64 notPi = U64.of(0x3141_5926_5358_9793L);
188 U64 notE = U64.of(0x2718_2818_8459_4523L);
189
190 assertThat(notPi.builder().invert().build(), equalTo(U64.of(0xcebe_a6d9_aca7_686cL)));
191 assertThat(notPi.builder().and(notE).build(), equalTo(U64.of(0x2100_0800_0058_0503L)));
192 assertThat(notPi.builder().or(notE).build(), equalTo(U64.of(0x3759_793e_d759_d7b3L)));
193 assertThat(notPi.builder().xor(notE).build(), equalTo(U64.of(0x1659_713e_d701_d2b0L)));
194 }
195}