Yuta HIGUCHI | 8f18219 | 2014-08-02 18:47:42 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util.distributed.sharedlog; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | import static org.hamcrest.Matchers.*; |
| 5 | |
| 6 | import org.junit.Test; |
| 7 | |
| 8 | import com.google.common.primitives.UnsignedLongs; |
| 9 | |
| 10 | /** |
| 11 | * Basic {@link SeqNum} class tests. |
| 12 | */ |
| 13 | public class SeqNumTest { |
| 14 | |
| 15 | /** |
| 16 | * Tests {@link SeqNum#next()} points to next sequence number, |
| 17 | * excluding reserved INITIAL value, |
| 18 | * and correctly wraps around ULONG_MAX. |
| 19 | */ |
| 20 | @Test |
| 21 | public void testNext() { |
| 22 | final SeqNum one = SeqNum.INITIAL.next(); |
| 23 | assertEquals(1L, one.longValue()); |
| 24 | |
| 25 | // succ |
| 26 | final SeqNum two = one.next(); |
| 27 | assertEquals(2L, two.longValue()); |
| 28 | |
| 29 | // succ wraps around skipping INITIAL |
| 30 | final SeqNum max = SeqNum.valueOf(UnsignedLongs.MAX_VALUE); |
| 31 | assertEquals(one, max.next()); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Tests {@link SeqNum#prev()} points to previous sequence number, |
| 36 | * excluding reserved INITIAL value, |
| 37 | * and correctly wraps around ULONG_MAX. |
| 38 | */ |
| 39 | @Test |
| 40 | public void testPrev() { |
| 41 | |
| 42 | // prev |
| 43 | final SeqNum two = SeqNum.valueOf(2); |
| 44 | final SeqNum one = two.prev(); |
| 45 | assertEquals(1L, one.longValue()); |
| 46 | |
| 47 | final SeqNum max = SeqNum.INITIAL.prev(); |
| 48 | assertEquals(UnsignedLongs.MAX_VALUE, max.longValue()); |
| 49 | |
| 50 | // prev wraps around skipping INITIAL |
| 51 | assertEquals(max, one.prev()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests that SeqNum equals and hashCode. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testEqualsObject() { |
| 59 | final SeqNum s = SeqNum.valueOf(42L); |
| 60 | |
| 61 | assertTrue(s.equals(s)); |
| 62 | assertEquals(s.hashCode(), s.hashCode()); |
| 63 | |
| 64 | assertTrue(s.equals(s.next().prev())); |
| 65 | assertEquals(s.hashCode(), s.next().prev().hashCode()); |
| 66 | |
| 67 | assertFalse(s.equals(s.next())); |
| 68 | assertFalse(s.equals(s.prev())); |
| 69 | assertFalse(s.equals(null)); |
| 70 | assertFalse(s.equals(Long.valueOf(42L))); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests that SeqNum is converted to String as unsigned decimal. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testToString() { |
| 78 | assertEquals("0", SeqNum.INITIAL.toString()); |
| 79 | assertEquals("1", SeqNum.valueOf(1).toString()); |
| 80 | // toString format is unsigned decimal string |
| 81 | assertEquals("9223372036854775808", |
| 82 | SeqNum.valueOf(Long.MAX_VALUE + 1).toString()); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests that comparison works treating long value as point in a ring. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testCompareTo() { |
| 90 | final SeqNum zero = SeqNum.INITIAL; |
| 91 | final SeqNum one = SeqNum.valueOf(1); |
| 92 | final SeqNum two = SeqNum.valueOf(2); |
| 93 | final SeqNum oneAlt = zero.next(); |
| 94 | final SeqNum negOne = one.prev(); |
| 95 | |
| 96 | // 0 < 1 |
| 97 | assertThat(zero.compareTo(one), lessThan(0)); |
| 98 | // 1 > 0 |
| 99 | assertThat(one.compareTo(zero), greaterThan(0)); |
| 100 | |
| 101 | // 0 == 0 |
| 102 | assertThat(zero.compareTo(zero), equalTo(0)); |
| 103 | // 1 == 1 |
| 104 | assertThat(oneAlt.compareTo(one), equalTo(0)); |
| 105 | assertThat(one.compareTo(oneAlt), equalTo(0)); |
| 106 | |
| 107 | // 2 > 1 |
| 108 | assertThat(two.compareTo(one), greaterThan(0)); |
| 109 | // 1 < 2 |
| 110 | assertThat(one.compareTo(two), lessThan(0)); |
| 111 | |
| 112 | // (-1) < 1 |
| 113 | assertThat(negOne.compareTo(one), lessThan(0)); |
| 114 | // 1 > (-1) |
| 115 | assertThat(one.compareTo(negOne), greaterThan(0)); |
| 116 | |
| 117 | // (-1) > (-3) |
| 118 | assertThat(negOne.compareTo(negOne.prev().prev()), greaterThan(0)); |
| 119 | // (-3) < (-1) |
| 120 | assertThat(negOne.prev().prev().compareTo(negOne), lessThan(0)); |
| 121 | |
| 122 | // (-1) > 0 [0 is always the smallest element] |
| 123 | assertThat(negOne.compareTo(zero), greaterThan(0)); |
| 124 | // 0 < (-1) [0 is always the smallest element] |
| 125 | assertThat(zero.compareTo(negOne), lessThan(0)); |
| 126 | |
| 127 | /// comparison using shorter arc |
| 128 | |
| 129 | // 0 < SLONG_MAX+1(=HALF) [clockwise arc used] |
| 130 | assertThat(zero.compareTo(zero.step(Long.MAX_VALUE).next()), lessThan(0)); |
| 131 | assertThat(zero.step(Long.MAX_VALUE).next().compareTo(zero), greaterThan(0)); |
| 132 | /// 0 is always compared clock wise (never wraps) |
| 133 | // 0 < SLONG_MAX+1(=HALF)+1 [clockwise arc used] |
| 134 | assertThat(zero.compareTo(zero.step(Long.MAX_VALUE).next().next()), lessThan(0)); |
| 135 | assertThat(zero.step(Long.MAX_VALUE).next().next().compareTo(zero), greaterThan(0)); |
| 136 | |
| 137 | // 1 < 1+SLONG_MAX(=HALF-1) [clockwise arc used] |
| 138 | assertThat(one.compareTo(one.step(Long.MAX_VALUE)), lessThan(0)); |
| 139 | assertThat(one.step(Long.MAX_VALUE).compareTo(one), greaterThan(0)); |
| 140 | // 1 < 1+SLONG_MAX+1(=HALF) [SAME, counter-clockwise arc used] |
| 141 | assertThat(one.compareTo(one.step(Long.MAX_VALUE).next()), greaterThan(0)); |
| 142 | assertThat(one.step(Long.MAX_VALUE).next().compareTo(one), lessThan(0)); |
| 143 | // 1 < 1+SLONG_MAX+2(=HALF+1) [counter-clockwise arc used] |
| 144 | assertThat(one.compareTo(one.step(Long.MAX_VALUE).next().next()), greaterThan(0)); |
| 145 | assertThat(one.step(Long.MAX_VALUE).next().next().compareTo(one), lessThan(0)); |
| 146 | |
| 147 | // (-1) < (-1)+SLONG_MAX(=HALF-1) [clockwise arc used] |
| 148 | assertThat(negOne.compareTo(negOne.step(Long.MAX_VALUE)), lessThan(0)); |
| 149 | assertThat(negOne.step(Long.MAX_VALUE).compareTo(negOne), greaterThan(0)); |
| 150 | // (-1) < (-1)+SLONG_MAX+1(=HALF) [SAME, clockwise arc used] |
| 151 | assertThat(negOne.compareTo(negOne.step(Long.MAX_VALUE).next()), lessThan(0)); |
| 152 | assertThat(negOne.step(Long.MAX_VALUE).next().compareTo(negOne), greaterThan(0)); |
| 153 | // (-1) > (-1)+SLONG_MAX+2(=HALF+1) [counter-clockwise arc used] |
| 154 | assertThat(negOne.compareTo(negOne.step(Long.MAX_VALUE).next().next()), greaterThan(0)); |
| 155 | assertThat(negOne.step(Long.MAX_VALUE).next().next().compareTo(negOne), lessThan(0)); |
| 156 | } |
Yuta HIGUCHI | 8cc4169 | 2014-08-20 21:47:03 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Tests parsing decimal unsigned long. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testDecStr() { |
| 163 | final SeqNum ref = SeqNum.valueOf(Long.MAX_VALUE + 1); |
| 164 | assertEquals(ref, SeqNum.valueOf("9223372036854775808")); |
| 165 | assertEquals(ref, SeqNum.anyValueOf("9223372036854775808")); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests parsing hexadecimal unsigned long. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testHexStr() { |
| 173 | final SeqNum ref = SeqNum.valueOf(Long.MAX_VALUE + 1); |
| 174 | assertEquals(ref, SeqNum.valueOf("0x8000000000000000")); |
| 175 | assertEquals(ref, SeqNum.anyValueOf("0x8000000000000000")); |
| 176 | } |
Yuta HIGUCHI | 8f18219 | 2014-08-02 18:47:42 -0700 | [diff] [blame] | 177 | } |