blob: f1573d067811be9b2d6c5bb71715c3e7253485dd [file] [log] [blame]
Madan Jampani83c27832015-12-07 10:42:13 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani83c27832015-12-07 10:42:13 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.util;
17
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -070018import java.util.ArrayList;
19import java.util.Iterator;
Madan Jampani83c27832015-12-07 10:42:13 -080020import java.util.Objects;
21
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -070022import org.junit.Ignore;
Madan Jampani83c27832015-12-07 10:42:13 -080023import org.junit.Test;
24
25import com.google.common.collect.Maps;
26
27import static org.junit.Assert.*;
28
29/**
30 * Unit tests for ExtendedSet.
31 */
32public class ExtendedSetTest {
33
34 @Test
35 public void testGet() {
36 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
37 TestValue e1 = new TestValue("foo", 1);
38 set.add(e1);
39 TestValue lookupValue = new TestValue("foo", 2);
40 TestValue setEntry = set.get(lookupValue);
41 assertEquals(e1, setEntry);
42 }
43
44 @Test
45 public void testInsertOrReplace() {
46 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
47 TestValue small = new TestValue("foo", 1);
48 TestValue medium = new TestValue("foo", 2);
49 TestValue large = new TestValue("foo", 3);
50 // input TestValue will replace existing TestValue if its value2() is greater
51 // than existing entry's value2()
52 assertTrue(set.insertOrReplace(small, existing -> existing.value2() < small.value2()));
53 assertTrue(set.insertOrReplace(large, existing -> existing.value2() < large.value2()));
54 assertFalse(set.insertOrReplace(medium, existing -> existing.value2() < medium.value2()));
55
56 assertTrue(set.contains(small));
57 assertTrue(set.contains(medium));
58 assertTrue(set.contains(large));
59 }
60
61 @Test
62 public void testConditionalRemove() {
63 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
64 TestValue small = new TestValue("foo", 1);
65 TestValue medium = new TestValue("foo", 2);
66
67 assertTrue(set.add(small));
68 set.conditionalRemove(medium, existing -> existing.value2() < medium.value2);
69 assertFalse(set.contains(small));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -070070
71 assertTrue(set.add(small));
72 set.conditionalRemove(medium, existing -> existing.value2() > medium.value2);
73 assertTrue(set.contains(small));
74
75 }
76
77 @Test
78 public void testIsEmpty() {
79 ExtendedSet<TestValue> nonemptyset = new ExtendedSet<>(Maps.newConcurrentMap());
80 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -070081 assertTrue(nonemptyset.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -070082 assertTrue(nonemptyset.contains(val));
83 assertFalse(nonemptyset.isEmpty());
84
85 ExtendedSet<TestValue> emptyset = new ExtendedSet<>(Maps.newConcurrentMap());
86 assertTrue(emptyset.isEmpty());
87
88 }
89
90 @Test
91 public void testClear() {
92 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
93 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -070094 assertTrue(set.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -070095 assertTrue(set.contains(val));
96 set.clear();
97 assertFalse(set.contains(val));
98 }
99
100 @Test
101 public void testSize() {
102 ExtendedSet<TestValue> nonemptyset = new ExtendedSet<>(Maps.newConcurrentMap());
103 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700104 assertTrue(nonemptyset.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700105 assertTrue(nonemptyset.contains(val));
106 assertEquals(1, nonemptyset.size());
107 TestValue secval = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700108 assertTrue(nonemptyset.add(secval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700109 assertTrue(nonemptyset.contains(secval));
110 assertEquals(2, nonemptyset.size());
111
112 ExtendedSet<TestValue> emptyset = new ExtendedSet<>(Maps.newConcurrentMap());
113 assertEquals(0, emptyset.size());
114 }
115
116 @Test
117 public void testIterator() {
118 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
119 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700120 assertTrue(set.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700121 TestValue nextval = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700122 assertTrue(set.add(nextval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700123 assertTrue(set.contains(nextval));
124 Iterator<TestValue> iterator = set.iterator();
125 assertEquals(val, iterator.next());
126 assertTrue(iterator.hasNext());
127 assertEquals(nextval, iterator.next());
128 }
129
130 @Test
131 public void testToArray() {
132 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
133 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700134 assertTrue(set.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700135 TestValue nextval = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700136 assertTrue(set.add(nextval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700137 Object[] array = set.toArray();
138 TestValue[] valarray = {val, nextval};
139 assertArrayEquals(valarray, array);
140 assertTrue(set.toArray(new TestValue[0])[0] instanceof TestValue);
141
142 }
143
144 @Test
145 public void testContainsAll() {
146 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
147 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700148 assertTrue(set.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700149 TestValue nextval = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700150 assertTrue(set.add(nextval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700151 ArrayList<TestValue> vals = new ArrayList<TestValue>();
152 vals.add(val);
153 vals.add(nextval);
154 assertTrue(set.containsAll(vals));
155 }
156
157 @Test
158 public void testRemove() {
159 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
160 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700161 assertTrue(set.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700162 TestValue nextval = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700163 assertTrue(set.add(nextval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700164 assertTrue(set.remove(val));
165 assertFalse(set.contains(val));
166 assertTrue(set.remove(nextval));
167 assertFalse(set.contains(nextval));
168 }
169
170 @Test
171 public void testAddAll() {
172 ExtendedSet<TestValue> nonemptyset = new ExtendedSet<>(Maps.newConcurrentMap());
173 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700174 assertTrue(nonemptyset.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700175 TestValue nextval = new TestValue("goo", 2);
176 TestValue finalval = new TestValue("shoo", 3);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700177 TestValue extremeval = new TestValue("who", 4);
178 assertTrue(nonemptyset.add(extremeval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700179 ArrayList<TestValue> vals = new ArrayList<TestValue>();
180 vals.add(nextval);
181 vals.add(finalval);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700182 vals.add(extremeval);
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700183 assertTrue(nonemptyset.addAll(vals));
184 assertTrue(nonemptyset.contains(nextval));
185 assertTrue(nonemptyset.contains(finalval));
186
187 ExtendedSet<TestValue> emptyset = new ExtendedSet<>(Maps.newConcurrentMap());
188 vals = new ArrayList<TestValue>();
189 vals.add(val);
190 vals.add(nextval);
191 vals.add(finalval);
192 assertTrue(emptyset.addAll(vals));
193 assertTrue(emptyset.contains(val));
194 assertTrue(emptyset.contains(nextval));
195 assertTrue(emptyset.contains(finalval));
196 }
197
198 @Test
199 public void testRemoveAll() {
200 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
201 TestValue val = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700202 assertTrue(set.add(val));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700203 TestValue nextval = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700204 assertTrue(set.add(nextval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700205 TestValue finalval = new TestValue("shoo", 3);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700206 assertTrue(set.add(finalval));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700207 ArrayList<TestValue> vals = new ArrayList<TestValue>();
208 vals.add(nextval);
209 vals.add(finalval);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700210 vals.add(new TestValue("who", 4));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700211 assertTrue(set.removeAll(vals));
212 assertFalse(set.contains(nextval));
213 assertFalse(set.contains(finalval));
214 }
215
216 @Test
217 @Ignore("retainAll appears to violate the documented semantics because it does" +
218 " not properly remove the items that are not in the Collection parameter.")
219 public void testRetainAll() {
220 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
221 TestValue small = new TestValue("foo", 1);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700222 assertTrue(set.add(small));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700223 TestValue medium = new TestValue("goo", 2);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700224 assertTrue(set.add(medium));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700225 TestValue large = new TestValue("shoo", 3);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700226 assertTrue(set.add(large));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700227 TestValue extreme = new TestValue("who", 4);
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700228 assertTrue(set.add(extreme));
Adnaan Sachidanandan1860a352017-06-23 10:47:19 -0700229 ArrayList<TestValue> firstvals = new ArrayList<TestValue>();
230 firstvals.add(medium);
231 firstvals.add(extreme);
232 set.retainAll(firstvals);
233 assertTrue(set.contains(medium));
234 assertTrue(set.contains(extreme));
235 assertFalse(set.contains(small));
236 assertFalse(set.contains(large));
237 ArrayList<TestValue> secondval = new ArrayList<TestValue>();
238 secondval.add(medium);
239 set.retainAll(secondval);
240 assertFalse(set.contains(extreme));
241 assertTrue(set.contains(medium));
Madan Jampani83c27832015-12-07 10:42:13 -0800242 }
243
Adnaan Sachidanandan79490f42017-06-26 09:26:09 -0700244 @Test
245 public void testAddFailure() {
246 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
247 TestValue val = new TestValue("foo", 1);
248 assertTrue(set.add(val));
249 assertFalse(set.add(val));
250 }
251
252 @Test
253 public void testRemoveFailure() {
254 ExtendedSet<TestValue> set = new ExtendedSet<TestValue>(Maps.newConcurrentMap());
255 TestValue val = new TestValue("foo", 1);
256 assertFalse(set.remove(val));
257 }
258
Madan Jampani83c27832015-12-07 10:42:13 -0800259 private class TestValue {
260 private String value1;
261 private int value2;
262
263 public TestValue(String v1, int v2) {
264 this.value1 = v1;
265 this.value2 = v2;
266 }
267
268 public String value1() {
269 return value1;
270 }
271
272 public int value2() {
273 return value2;
274 }
275
276 @Override
277 public boolean equals(Object other) {
278 if (other instanceof TestValue) {
279 TestValue that = (TestValue) other;
280 return Objects.equals(value1, that.value1);
281 }
282 return false;
283 }
284
285 @Override
286 public int hashCode() {
287 return Objects.hash(value1);
288 }
289 }
290}