Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 3 | import static org.hamcrest.CoreMatchers.equalTo; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 4 | import static org.junit.Assert.assertEquals; |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 5 | import static org.junit.Assert.assertFalse; |
| 6 | import static org.junit.Assert.assertNotEquals; |
| 7 | import static org.junit.Assert.assertThat; |
| 8 | import static org.junit.Assert.assertTrue; |
Andreas Wundsam | c5e9870 | 2014-05-05 13:12:19 -0700 | [diff] [blame] | 9 | import static org.junit.Assert.fail; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | |
| 11 | import java.math.BigInteger; |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 12 | import java.text.MessageFormat; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 13 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 14 | import org.junit.Before; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 15 | import org.junit.Test; |
| 16 | |
| 17 | public class U64Test { |
| 18 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 19 | private Triple[] triples; |
| 20 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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() { |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 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()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 35 | } |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame] | 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 |
Andreas Wundsam | c5e9870 | 2014-05-05 13:12:19 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 112 | @Before |
| 113 | public void setup() { |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 123 | triples = new Triple[] { |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 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 | }; |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 140 | } |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 141 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 142 | @Test |
| 143 | public void testAddSubtract() { |
Andreas Wundsam | c277520 | 2014-06-30 21:31:40 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | c5e9870 | 2014-05-05 13:12:19 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 3ea5f7e | 2014-07-01 12:21:28 -0700 | [diff] [blame] | 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 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 195 | } |