blob: b647cb98c5b55271b1feb13cf4113ccc7354d3fb [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);
81 assertTrue(nonemptyset.insertOrReplace(val, existing -> existing.value2() < val.value2()));
82 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);
94 assertTrue(set.insertOrReplace(val, existing -> existing.value2() < val.value2()));
95 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);
104 assertTrue(nonemptyset.insertOrReplace(val, existing -> existing.value2() < val.value2()));
105 assertTrue(nonemptyset.contains(val));
106 assertEquals(1, nonemptyset.size());
107 TestValue secval = new TestValue("goo", 2);
108 assertTrue(nonemptyset.insertOrReplace(secval, existing -> existing.value2() < secval.value2()));
109 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);
120 assertTrue(set.insertOrReplace(val, existing -> existing.value2() < val.value2()));
121 TestValue nextval = new TestValue("goo", 2);
122 assertTrue(set.insertOrReplace(nextval, existing -> existing.value2() < nextval.value2()));
123 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);
134 assertTrue(set.insertOrReplace(val, existing -> existing.value2() < val.value2()));
135 TestValue nextval = new TestValue("goo", 2);
136 assertTrue(set.insertOrReplace(nextval, existing -> existing.value2() < nextval.value2()));
137 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);
148 assertTrue(set.insertOrReplace(val, existing -> existing.value2() < val.value2()));
149 TestValue nextval = new TestValue("goo", 2);
150 assertTrue(set.insertOrReplace(nextval, existing -> existing.value2() < nextval.value2()));
151 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);
161 assertTrue(set.insertOrReplace(val, existing -> existing.value2() < val.value2()));
162 TestValue nextval = new TestValue("goo", 2);
163 assertTrue(set.insertOrReplace(nextval, existing -> existing.value2() < nextval.value2()));
164 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);
174 assertTrue(nonemptyset.insertOrReplace(val, existing -> existing.value2() < val.value2()));
175 TestValue nextval = new TestValue("goo", 2);
176 TestValue finalval = new TestValue("shoo", 3);
177 ArrayList<TestValue> vals = new ArrayList<TestValue>();
178 vals.add(nextval);
179 vals.add(finalval);
180 assertTrue(nonemptyset.addAll(vals));
181 assertTrue(nonemptyset.contains(nextval));
182 assertTrue(nonemptyset.contains(finalval));
183
184 ExtendedSet<TestValue> emptyset = new ExtendedSet<>(Maps.newConcurrentMap());
185 vals = new ArrayList<TestValue>();
186 vals.add(val);
187 vals.add(nextval);
188 vals.add(finalval);
189 assertTrue(emptyset.addAll(vals));
190 assertTrue(emptyset.contains(val));
191 assertTrue(emptyset.contains(nextval));
192 assertTrue(emptyset.contains(finalval));
193 }
194
195 @Test
196 public void testRemoveAll() {
197 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
198 TestValue val = new TestValue("foo", 1);
199 assertTrue(set.insertOrReplace(val, existing -> existing.value2() < val.value2()));
200 TestValue nextval = new TestValue("goo", 2);
201 assertTrue(set.insertOrReplace(nextval, existing -> existing.value2() < nextval.value2()));
202 TestValue finalval = new TestValue("shoo", 3);
203 assertTrue(set.insertOrReplace(finalval, existing -> existing.value2() < finalval.value2()));
204 ArrayList<TestValue> vals = new ArrayList<TestValue>();
205 vals.add(nextval);
206 vals.add(finalval);
207 assertTrue(set.removeAll(vals));
208 assertFalse(set.contains(nextval));
209 assertFalse(set.contains(finalval));
210 }
211
212 @Test
213 @Ignore("retainAll appears to violate the documented semantics because it does" +
214 " not properly remove the items that are not in the Collection parameter.")
215 public void testRetainAll() {
216 ExtendedSet<TestValue> set = new ExtendedSet<>(Maps.newConcurrentMap());
217 TestValue small = new TestValue("foo", 1);
218 assertTrue(set.insertOrReplace(small, existing -> existing.value2() < small.value2()));
219 TestValue medium = new TestValue("goo", 2);
220 assertTrue(set.insertOrReplace(medium, existing -> existing.value2() < medium.value2()));
221 TestValue large = new TestValue("shoo", 3);
222 assertTrue(set.insertOrReplace(large, existing -> existing.value2() < large.value2()));
223 TestValue extreme = new TestValue("who", 4);
224 assertTrue(set.insertOrReplace(extreme, existing -> existing.value2() < extreme.value2()));
225 ArrayList<TestValue> firstvals = new ArrayList<TestValue>();
226 firstvals.add(medium);
227 firstvals.add(extreme);
228 set.retainAll(firstvals);
229 assertTrue(set.contains(medium));
230 assertTrue(set.contains(extreme));
231 assertFalse(set.contains(small));
232 assertFalse(set.contains(large));
233 ArrayList<TestValue> secondval = new ArrayList<TestValue>();
234 secondval.add(medium);
235 set.retainAll(secondval);
236 assertFalse(set.contains(extreme));
237 assertTrue(set.contains(medium));
Madan Jampani83c27832015-12-07 10:42:13 -0800238 }
239
240 private class TestValue {
241 private String value1;
242 private int value2;
243
244 public TestValue(String v1, int v2) {
245 this.value1 = v1;
246 this.value2 = v2;
247 }
248
249 public String value1() {
250 return value1;
251 }
252
253 public int value2() {
254 return value2;
255 }
256
257 @Override
258 public boolean equals(Object other) {
259 if (other instanceof TestValue) {
260 TestValue that = (TestValue) other;
261 return Objects.equals(value1, that.value1);
262 }
263 return false;
264 }
265
266 @Override
267 public int hashCode() {
268 return Objects.hash(value1);
269 }
270 }
271}