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 |
| 30 | public void testPutNullable() { |
| 31 | // test that these different invocations of putNullable |
| 32 | // differ pairwise |
| 33 | HashCode[] hs = new HashCode[] { |
| 34 | calcPutNullables(), |
| 35 | calcPutNullables(OFPort.of(1)), |
| 36 | calcPutNullables(OFPort.of(1), null), |
| 37 | calcPutNullables(OFPort.of(1), null, null), |
| 38 | calcPutNullables(null, OFPort.of(1), null), |
| 39 | calcPutNullables(null, null, OFPort.of(1)) |
| 40 | }; |
| 41 | |
| 42 | checkPairwiseDifferent(hs); |
| 43 | } |
| 44 | |
| 45 | private void checkPairwiseDifferent(HashCode[] hs) { |
| 46 | for(int i=0;i<hs.length;i++) { |
| 47 | for(int j=i+1; j<hs.length;j++) { |
| 48 | assertThat(hs[i], not(hs[j])); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testPutList() { |
| 55 | HashCode[] hs = new HashCode[] { |
| 56 | calcPutList(), |
| 57 | calcPutList(OFPort.of(1)), |
| 58 | calcPutList(OFPort.of(2)), |
| 59 | calcPutList(OFPort.of(1), OFPort.of(2)), |
| 60 | calcPutList(OFPort.of(2), OFPort.of(1)), |
| 61 | calcPutList(OFPort.of(1), OFPort.of(3)), |
| 62 | calcPutList(OFPort.of(1), OFPort.of(2), OFPort.of(3)), |
| 63 | }; |
| 64 | |
| 65 | checkPairwiseDifferent(hs); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testPutSortedSet() { |
| 70 | HashCode[] hs = new HashCode[] { |
| 71 | calcPutSortedSet(), |
| 72 | calcPutSortedSet(OFPort.of(1)), |
| 73 | calcPutSortedSet(OFPort.of(2)), |
| 74 | calcPutSortedSet(OFPort.of(1), OFPort.of(2)), |
| 75 | calcPutSortedSet(OFPort.of(1), OFPort.of(3)), |
| 76 | calcPutSortedSet(OFPort.of(1), OFPort.of(2), OFPort.of(3)), |
| 77 | }; |
| 78 | |
| 79 | checkPairwiseDifferent(hs); |
| 80 | |
| 81 | assertThat(calcPutSortedSet(OFPort.of(1), OFPort.of(2)), |
| 82 | equalTo(calcPutSortedSet(OFPort.of(2), OFPort.of(1)))); |
| 83 | } |
| 84 | |
| 85 | private HashCode calcPutSortedSet(OFPort... ports) { |
| 86 | Hasher h = hash.newHasher(); |
| 87 | PrimitiveSinkUtils.putSortedSetTo(h, ImmutableSortedSet.copyOf(ports)); |
| 88 | return h.hash(); |
| 89 | } |
| 90 | |
| 91 | private HashCode calcPutList(OFPort... ports) { |
| 92 | Hasher h = hash.newHasher(); |
| 93 | PrimitiveSinkUtils.putListTo(h, Arrays.asList(ports)); |
| 94 | return h.hash(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | private HashCode calcPutNullables(PrimitiveSinkable... ps) { |
| 99 | Hasher h = hash.newHasher(); |
| 100 | for(PrimitiveSinkable p : ps) { |
| 101 | PrimitiveSinkUtils.putNullableTo(h, p); |
| 102 | } |
| 103 | return h.hash(); |
| 104 | } |
| 105 | } |