blob: 7bd1c4651e41c06455efbc28eff32ac6b4c310ba [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;
Andreas Wundsamc2775202014-06-30 21:31:40 -070012import java.text.MessageFormat;
Yotam Harcholf3f11152013-09-05 16:47:16 -070013
14import org.junit.Test;
15
16public class U64Test {
17
18 @Test
19 public void testPositiveRaws() {
20 for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) {
21 assertEquals(positive, U64.ofRaw(positive).getValue());
22 assertEquals(BigInteger.valueOf(positive), U64.ofRaw(positive).getBigInteger());
23 }
24 }
25
26 @Test
27 public void testNegativeRaws() {
Andreas Wundsamc2775202014-06-30 21:31:40 -070028 long minu1 = 0xFFFF_FFFF_FFFF_FFFFL;
29 assertEquals(minu1, U64.ofRaw(minu1).getValue());
30 assertEquals(new BigInteger("FFFF_FFFF_FFFF_FFFF".replace("_", ""), 16), U64.ofRaw(minu1).getBigInteger());
31 assertEquals(new BigInteger("18446744073709551615"), U64.ofRaw(minu1).getBigInteger());
Yotam Harcholf3f11152013-09-05 16:47:16 -070032 }
Andreas Wundsambc0aead2014-04-24 19:01:36 -070033
34 @Test
35 public void testEqualHashCode() {
36 U64 h1 = U64.of(0xdeafbeefdeadbeefL);
37 U64 h2 = U64.of(0xdeafbeefdeadbeefL);
38 U64 h3 = U64.of(0xeeafbeefdeadbeefL);
39
40 assertTrue(h1.equals(h1));
41 assertTrue(h1.equals(h2));
42 assertFalse(h1.equals(h3));
43 assertTrue(h2.equals(h1));
44
45 assertEquals(h1.hashCode(), h2.hashCode());
46 assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky.
47 }
48
49 @Test
50 public void testXor() {
51 U64 hNull = U64.of(0);
52 U64 hDeadBeef = U64.of(0xdeafbeefdeadbeefL);
53 assertThat(hNull.xor(hNull), equalTo(hNull));
54 assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef));
55 assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef));
56 assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull));
57
58
59 U64 h1 = U64.of(1L);
60 U64 h8 = U64.of(0x8000000000000000L);
61 U64 h81 = U64.of(0x8000000000000001L);
62 assertThat(h1.xor(h8), equalTo(h81));
63 }
64
65 @Test
Andreas Wundsamc5e98702014-05-05 13:12:19 -070066 public void testKeyBits() {
67 U64 zeroU = U64.of(0);
68 assertThat(zeroU.prefixBits(0), equalTo(0));
69 assertThat(zeroU.prefixBits(16), equalTo(0));
70 assertThat(zeroU.prefixBits(32), equalTo(0));
71
72 checkInvalidKeyBitSize(zeroU, 33);
73 checkInvalidKeyBitSize(zeroU, 64);
74 assertThat(zeroU.prefixBits(3), equalTo(0));
75
76 U64 positiveU = U64.of(0x1234_5678_1234_5678L);
77 assertThat(positiveU.prefixBits(0), equalTo(0));
78 assertThat(positiveU.prefixBits(16), equalTo(0x1234));
79 assertThat(positiveU.prefixBits(32), equalTo(0x12345678));
80 checkInvalidKeyBitSize(positiveU, 33);
81 checkInvalidKeyBitSize(positiveU, 64);
82
83 U64 signedBitU = U64.of(0x8765_4321_8765_4321L);
84 assertThat(signedBitU.prefixBits(0), equalTo(0));
85 assertThat(signedBitU.prefixBits(16), equalTo(0x8765));
86 assertThat(signedBitU.prefixBits(32), equalTo(0x8765_4321));
87 checkInvalidKeyBitSize(signedBitU, 33);
88 checkInvalidKeyBitSize(signedBitU, 64);
89 }
90
Andreas Wundsamc2775202014-06-30 21:31:40 -070091 public static class Triple {
92 U64 a, b, c;
93
94 public Triple(U64 a, U64 b, U64 c) {
95 this.a = a;
96 this.b = b;
97 this.c = c;
98 }
99
100 public static Triple of(U64 a, U64 b, U64 c) {
101 return new Triple(a, b, c);
102 }
103
104 public String msg(String string) {
105 return MessageFormat.format(string, a,b,c);
106 }
107 }
108
109 @Test
110 public void testAddSubtract() {
111 U64 u0 = U64.of(0);
112 U64 u1 = U64.of(1);
113
114 U64 u2 = U64.of(2);
115 U64 u7f = U64.of(0x7fff_ffff_ffff_ffffL);
116 U64 u8 = U64.of(0x8000_0000_0000_0000L);
117
118 U64 uf = U64.of(-1L);
119
120 Triple[] triples = new Triple[] {
121 Triple.of(u0, u0, u0),
122 Triple.of(u0, u1, u1),
123
124 Triple.of(u1, u1, u2),
125
126 Triple.of(u1, uf, u0),
127
128 Triple.of(uf, uf, U64.of(0xffff_ffff_ffff_fffeL)),
129 Triple.of(u0, uf, uf),
130
131 Triple.of(u7f, u1, u8),
132
133 Triple.of(U64.of(0x1234_5678_9abc_def1L),
134 U64.of(0xedcb_a987_6543_210fL),
135 U64.ZERO)
136 };
137
138 for(Triple t: triples) {
139 assertThat(t.msg("{0} + {1} = {2}"), t.a.add(t.b), equalTo(t.c));
140 assertThat(t.msg("{1} + {0} = {2}"), t.b.add(t.a), equalTo(t.c));
141
142 assertThat(t.msg("{2} - {0} = {1}"), t.c.subtract(t.a), equalTo(t.b));
143 assertThat(t.msg("{2} - {1} = {0}"), t.c.subtract(t.b), equalTo(t.a));
144 }
145 }
146
Andreas Wundsamc5e98702014-05-05 13:12:19 -0700147 private void
148 checkInvalidKeyBitSize(U64 u, int prefixBit) {
149 try {
150 u.prefixBits(prefixBit);
151 fail("Expected exception not thrown for "+prefixBit + " bits");
152 } catch(IllegalArgumentException e) {
153 // expected
154 }
155 }
156
Yotam Harcholf3f11152013-09-05 16:47:16 -0700157}