Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.util; |
| 2 | |
| 3 | import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | import static org.hamcrest.CoreMatchers.not; |
| 5 | import static org.junit.Assert.assertThat; |
| 6 | |
| 7 | import java.util.Arrays; |
| 8 | |
| 9 | import org.junit.Before; |
| 10 | import org.junit.Test; |
| 11 | import org.projectfloodlight.openflow.types.OFPort; |
| 12 | import org.projectfloodlight.openflow.types.PrimitiveSinkable; |
| 13 | |
| 14 | import com.google.common.collect.ImmutableSortedSet; |
| 15 | import com.google.common.hash.HashCode; |
| 16 | import com.google.common.hash.HashFunction; |
| 17 | import com.google.common.hash.Hasher; |
| 18 | import com.google.common.hash.Hashing; |
| 19 | |
| 20 | public class PrimitiveSinkUtilsTest { |
| 21 | |
| 22 | private HashFunction hash; |
| 23 | |
| 24 | @Before |
| 25 | public void setup() { |
| 26 | hash = Hashing.murmur3_128(); |
| 27 | } |
| 28 | |
| 29 | @Test |
Andreas Wundsam | a224b13 | 2014-06-05 16:39:01 -0700 | [diff] [blame] | 30 | public void testPutNullableString() { |
| 31 | // test that these different invocations of putNullable |
| 32 | // differ pairwise |
| 33 | HashCode[] hs = new HashCode[] { |
| 34 | calcPutNullableString((String) null), |
| 35 | calcPutNullableString(""), |
| 36 | calcPutNullableString(null, null), |
| 37 | calcPutNullableString(null, ""), |
| 38 | calcPutNullableString("", null), |
| 39 | calcPutNullableString("a\0a", null), |
| 40 | calcPutNullableString(null, "a\0a"), |
| 41 | }; |
| 42 | |
| 43 | checkPairwiseDifferent(hs); |
| 44 | } |
| 45 | |
| 46 | @Test |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 47 | public void testPutNullable() { |
| 48 | // test that these different invocations of putNullable |
| 49 | // differ pairwise |
| 50 | HashCode[] hs = new HashCode[] { |
| 51 | calcPutNullables(), |
| 52 | calcPutNullables(OFPort.of(1)), |
| 53 | calcPutNullables(OFPort.of(1), null), |
| 54 | calcPutNullables(OFPort.of(1), null, null), |
| 55 | calcPutNullables(null, OFPort.of(1), null), |
| 56 | calcPutNullables(null, null, OFPort.of(1)) |
| 57 | }; |
| 58 | |
| 59 | checkPairwiseDifferent(hs); |
| 60 | } |
| 61 | |
| 62 | private void checkPairwiseDifferent(HashCode[] hs) { |
| 63 | for(int i=0;i<hs.length;i++) { |
| 64 | for(int j=i+1; j<hs.length;j++) { |
| 65 | assertThat(hs[i], not(hs[j])); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testPutList() { |
| 72 | HashCode[] hs = new HashCode[] { |
| 73 | calcPutList(), |
| 74 | calcPutList(OFPort.of(1)), |
| 75 | calcPutList(OFPort.of(2)), |
| 76 | calcPutList(OFPort.of(1), OFPort.of(2)), |
| 77 | calcPutList(OFPort.of(2), OFPort.of(1)), |
| 78 | calcPutList(OFPort.of(1), OFPort.of(3)), |
| 79 | calcPutList(OFPort.of(1), OFPort.of(2), OFPort.of(3)), |
| 80 | }; |
| 81 | |
| 82 | checkPairwiseDifferent(hs); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testPutSortedSet() { |
| 87 | HashCode[] hs = new HashCode[] { |
| 88 | calcPutSortedSet(), |
| 89 | calcPutSortedSet(OFPort.of(1)), |
| 90 | calcPutSortedSet(OFPort.of(2)), |
| 91 | calcPutSortedSet(OFPort.of(1), OFPort.of(2)), |
| 92 | calcPutSortedSet(OFPort.of(1), OFPort.of(3)), |
| 93 | calcPutSortedSet(OFPort.of(1), OFPort.of(2), OFPort.of(3)), |
| 94 | }; |
| 95 | |
| 96 | checkPairwiseDifferent(hs); |
| 97 | |
| 98 | assertThat(calcPutSortedSet(OFPort.of(1), OFPort.of(2)), |
| 99 | equalTo(calcPutSortedSet(OFPort.of(2), OFPort.of(1)))); |
Andreas Wundsam | a224b13 | 2014-06-05 16:39:01 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private HashCode calcPutNullableString(String... strings) { |
| 103 | Hasher h = hash.newHasher(); |
| 104 | for(String s: strings) { |
| 105 | PrimitiveSinkUtils.putNullableStringTo(h, s); |
| 106 | } |
| 107 | return h.hash(); |
| 108 | } |
Andreas Wundsam | 8e92b97 | 2014-06-05 15:44:13 -0700 | [diff] [blame] | 109 | |
| 110 | private HashCode calcPutSortedSet(OFPort... ports) { |
| 111 | Hasher h = hash.newHasher(); |
| 112 | PrimitiveSinkUtils.putSortedSetTo(h, ImmutableSortedSet.copyOf(ports)); |
| 113 | return h.hash(); |
| 114 | } |
| 115 | |
| 116 | private HashCode calcPutList(OFPort... ports) { |
| 117 | Hasher h = hash.newHasher(); |
| 118 | PrimitiveSinkUtils.putListTo(h, Arrays.asList(ports)); |
| 119 | return h.hash(); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | private HashCode calcPutNullables(PrimitiveSinkable... ps) { |
| 124 | Hasher h = hash.newHasher(); |
| 125 | for(PrimitiveSinkable p : ps) { |
| 126 | PrimitiveSinkUtils.putNullableTo(h, p); |
| 127 | } |
| 128 | return h.hash(); |
| 129 | } |
| 130 | } |