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; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | |
| 10 | import java.math.BigInteger; |
| 11 | |
| 12 | import org.junit.Test; |
| 13 | |
| 14 | public class U64Test { |
| 15 | |
| 16 | @Test |
| 17 | public void testPositiveRaws() { |
| 18 | for(long positive: new long[] { 0, 1, 100, Long.MAX_VALUE }) { |
| 19 | assertEquals(positive, U64.ofRaw(positive).getValue()); |
| 20 | assertEquals(BigInteger.valueOf(positive), U64.ofRaw(positive).getBigInteger()); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | @Test |
| 25 | public void testNegativeRaws() { |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame^] | 26 | long minus_1 = 0xFFFF_FFFF_FFFF_FFFFL; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 27 | assertEquals(minus_1, U64.ofRaw(minus_1).getValue()); |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame^] | 28 | assertEquals(new BigInteger("FFFF_FFFF_FFFF_FFFF".replace("_", ""), 16), U64.ofRaw(minus_1).getBigInteger()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 29 | assertEquals(new BigInteger("18446744073709551615"), U64.ofRaw(minus_1).getBigInteger()); |
| 30 | } |
Andreas Wundsam | bc0aead | 2014-04-24 19:01:36 -0700 | [diff] [blame^] | 31 | |
| 32 | @Test |
| 33 | public void testEqualHashCode() { |
| 34 | U64 h1 = U64.of(0xdeafbeefdeadbeefL); |
| 35 | U64 h2 = U64.of(0xdeafbeefdeadbeefL); |
| 36 | U64 h3 = U64.of(0xeeafbeefdeadbeefL); |
| 37 | |
| 38 | assertTrue(h1.equals(h1)); |
| 39 | assertTrue(h1.equals(h2)); |
| 40 | assertFalse(h1.equals(h3)); |
| 41 | assertTrue(h2.equals(h1)); |
| 42 | |
| 43 | assertEquals(h1.hashCode(), h2.hashCode()); |
| 44 | assertNotEquals(h1.hashCode(), h3.hashCode()); // not technically a requirement, but we'll hopefully be lucky. |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testXor() { |
| 49 | U64 hNull = U64.of(0); |
| 50 | U64 hDeadBeef = U64.of(0xdeafbeefdeadbeefL); |
| 51 | assertThat(hNull.xor(hNull), equalTo(hNull)); |
| 52 | assertThat(hNull.xor(hDeadBeef), equalTo(hDeadBeef)); |
| 53 | assertThat(hDeadBeef.xor(hNull), equalTo(hDeadBeef)); |
| 54 | assertThat(hDeadBeef.xor(hDeadBeef), equalTo(hNull)); |
| 55 | |
| 56 | |
| 57 | U64 h1 = U64.of(1L); |
| 58 | U64 h8 = U64.of(0x8000000000000000L); |
| 59 | U64 h81 = U64.of(0x8000000000000001L); |
| 60 | assertThat(h1.xor(h8), equalTo(h81)); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testCombine() { |
| 65 | long key = 0x1234567890abcdefL; |
| 66 | long val = 0xdeafbeefdeadbeefL; |
| 67 | U64 hkey = U64.of(key); |
| 68 | U64 hVal = U64.of(val); |
| 69 | |
| 70 | assertThat(hkey.combineWithValue(hVal, 0), equalTo(hkey.xor(hVal))); |
| 71 | assertThat(hkey.combineWithValue(hVal, 64), equalTo(hkey)); |
| 72 | long mask32 = 0x00000000FFFFFFFFL; |
| 73 | assertThat(hkey.combineWithValue(hVal, 32), |
| 74 | equalTo(U64.of(key & ~mask32| (key ^ val) & mask32))); |
| 75 | |
| 76 | long tenMask = 0x003FFFFFFFFFFFFFL; |
| 77 | assertThat(hkey.combineWithValue(hVal, 10), |
| 78 | equalTo(U64.of(key & ~tenMask | (key ^ val) & tenMask))); |
| 79 | } |
| 80 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 81 | } |