blob: f5bf3e4f34360fb40bca061d4d2a8d75f7563bcd [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.util;
2
3import static org.hamcrest.CoreMatchers.equalTo;
4import static org.hamcrest.CoreMatchers.not;
5import static org.junit.Assert.assertThat;
6
7import java.util.Arrays;
8
9import org.junit.Before;
10import org.junit.Test;
11import org.projectfloodlight.openflow.types.OFPort;
12import org.projectfloodlight.openflow.types.PrimitiveSinkable;
13
14import com.google.common.collect.ImmutableSortedSet;
15import com.google.common.hash.HashCode;
16import com.google.common.hash.HashFunction;
17import com.google.common.hash.Hasher;
18import com.google.common.hash.Hashing;
19
20public 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 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
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))));
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 }
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}