Madan Jampani | 83c2783 | 2015-12-07 10:42:13 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | 83c2783 | 2015-12-07 10:42:13 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onlab.util; |
| 17 | |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Iterator; |
Madan Jampani | 83c2783 | 2015-12-07 10:42:13 -0800 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 22 | import org.junit.Ignore; |
Madan Jampani | 83c2783 | 2015-12-07 10:42:13 -0800 | [diff] [blame] | 23 | import org.junit.Test; |
| 24 | |
| 25 | import com.google.common.collect.Maps; |
| 26 | |
| 27 | import static org.junit.Assert.*; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for ExtendedSet. |
| 31 | */ |
| 32 | public 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 Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 70 | |
| 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 Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 81 | assertTrue(nonemptyset.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 94 | assertTrue(set.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 104 | assertTrue(nonemptyset.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 105 | assertTrue(nonemptyset.contains(val)); |
| 106 | assertEquals(1, nonemptyset.size()); |
| 107 | TestValue secval = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 108 | assertTrue(nonemptyset.add(secval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 120 | assertTrue(set.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 121 | TestValue nextval = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 122 | assertTrue(set.add(nextval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 134 | assertTrue(set.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 135 | TestValue nextval = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 136 | assertTrue(set.add(nextval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 148 | assertTrue(set.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 149 | TestValue nextval = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 150 | assertTrue(set.add(nextval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 161 | assertTrue(set.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 162 | TestValue nextval = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 163 | assertTrue(set.add(nextval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 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); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 174 | assertTrue(nonemptyset.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 175 | TestValue nextval = new TestValue("goo", 2); |
| 176 | TestValue finalval = new TestValue("shoo", 3); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 177 | TestValue extremeval = new TestValue("who", 4); |
| 178 | assertTrue(nonemptyset.add(extremeval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 179 | ArrayList<TestValue> vals = new ArrayList<TestValue>(); |
| 180 | vals.add(nextval); |
| 181 | vals.add(finalval); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 182 | vals.add(extremeval); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 183 | 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 Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 202 | assertTrue(set.add(val)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 203 | TestValue nextval = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 204 | assertTrue(set.add(nextval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 205 | TestValue finalval = new TestValue("shoo", 3); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 206 | assertTrue(set.add(finalval)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 207 | ArrayList<TestValue> vals = new ArrayList<TestValue>(); |
| 208 | vals.add(nextval); |
| 209 | vals.add(finalval); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 210 | vals.add(new TestValue("who", 4)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 211 | 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 Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 222 | assertTrue(set.add(small)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 223 | TestValue medium = new TestValue("goo", 2); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 224 | assertTrue(set.add(medium)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 225 | TestValue large = new TestValue("shoo", 3); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 226 | assertTrue(set.add(large)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 227 | TestValue extreme = new TestValue("who", 4); |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 228 | assertTrue(set.add(extreme)); |
Adnaan Sachidanandan | 1860a35 | 2017-06-23 10:47:19 -0700 | [diff] [blame] | 229 | 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 Jampani | 83c2783 | 2015-12-07 10:42:13 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Adnaan Sachidanandan | 79490f4 | 2017-06-26 09:26:09 -0700 | [diff] [blame] | 244 | @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 Jampani | 83c2783 | 2015-12-07 10:42:13 -0800 | [diff] [blame] | 259 | 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 | } |