Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [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 | |
HIGUCHI Yuta | 805d96c | 2016-03-17 13:29:09 -0700 | [diff] [blame] | 17 | package org.onosproject.persistence.impl; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Sets; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.Serializer; |
| 23 | |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 24 | import java.util.HashSet; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.junit.Assert.assertFalse; |
| 30 | import static org.junit.Assert.assertNotEquals; |
| 31 | import static org.junit.Assert.assertNull; |
HIGUCHI Yuta | 805d96c | 2016-03-17 13:29:09 -0700 | [diff] [blame] | 32 | import static org.junit.Assert.assertSame; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 33 | import static org.junit.Assert.assertTrue; |
| 34 | |
| 35 | /** |
| 36 | * Test suite for Persistent Set. |
| 37 | */ |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 38 | public class PersistentSetTest extends MapDBTest { |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 39 | |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 40 | private PersistentSet<Integer> set = null; |
HIGUCHI Yuta | 49e03a3 | 2016-03-21 22:07:35 -0700 | [diff] [blame] | 41 | |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 42 | @Before |
| 43 | public void setUp() throws Exception { |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 44 | //Creates a set within it and a basic integer serializer |
| 45 | set = new PersistentSet<>(new Serializer() { |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 46 | @Override |
| 47 | public <T> byte[] encode(T object) { |
| 48 | if (object == null) { |
| 49 | return null; |
| 50 | } |
| 51 | int num = (Integer) object; |
| 52 | byte[] result = new byte[4]; |
| 53 | |
| 54 | result[0] = (byte) (num >> 24); |
| 55 | result[1] = (byte) (num >> 16); |
| 56 | result[2] = (byte) (num >> 8); |
| 57 | result[3] = (byte) num; |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public <T> T decode(byte[] bytes) { |
| 63 | if (bytes == null) { |
| 64 | return null; |
| 65 | } |
| 66 | int num = 0x00000000; |
| 67 | |
| 68 | num = num | bytes[0] << 24; |
| 69 | num = num | bytes[1] << 16; |
| 70 | num = num | bytes[2] << 8; |
| 71 | num = num | bytes[3]; |
| 72 | |
HIGUCHI Yuta | 8eb6ba1 | 2016-05-21 15:01:41 -0700 | [diff] [blame] | 73 | return (T) Integer.valueOf(num); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 74 | } |
Jordan Halterman | 2c83a10 | 2017-08-20 17:11:41 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public <T> T copy(T object) { |
| 78 | return decode(encode(object)); |
| 79 | } |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 80 | }, fakeDB, "set"); |
| 81 | |
| 82 | } |
| 83 | |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 84 | @Test |
| 85 | public void testSize() throws Exception { |
| 86 | //Check correct sizing throughout population |
| 87 | for (int i = 0; i < 10; i++) { |
| 88 | set.add(i); |
| 89 | assertEquals("The set should have i + 1 entries.", i + 1, set.size()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testIsEmpty() throws Exception { |
| 95 | //test empty condition |
| 96 | assertTrue("The set should be initialized empty.", set.isEmpty()); |
| 97 | fillSet(5, this.set); |
| 98 | assertFalse("The set should no longer be empty.", set.isEmpty()); |
| 99 | set.clear(); |
| 100 | assertTrue("The set should have been cleared.", set.isEmpty()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testContains() throws Exception { |
| 105 | //Test contains |
| 106 | assertFalse("The set should not contain anything", set.contains(1)); |
| 107 | fillSet(10, this.set); |
| 108 | for (int i = 0; i < 10; i++) { |
| 109 | assertTrue("The set should contain all values 0-9.", set.contains(i)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testIterator() throws Exception { |
| 115 | //Test iterator behavior (no order guarantees are made) |
| 116 | Set<Integer> validationSet = Sets.newHashSet(); |
| 117 | fillSet(10, this.set); |
| 118 | fillSet(10, validationSet); |
| 119 | set.iterator().forEachRemaining(item -> assertTrue("Items were mismatched.", validationSet.remove(item))); |
| 120 | //All values should have been seen and removed |
| 121 | assertTrue("All entries in the validation set should have been removed.", validationSet.isEmpty()); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testToArray() throws Exception { |
| 126 | //Test creation of a new array of the values |
| 127 | fillSet(10, set); |
| 128 | Object[] arr = set.toArray(); |
| 129 | assertEquals("The array should be of length 10.", 10, arr.length); |
| 130 | for (int i = 0; i < 10; i++) { |
HIGUCHI Yuta | 805d96c | 2016-03-17 13:29:09 -0700 | [diff] [blame] | 131 | assertTrue("All elements of the array should be in the set.", set.contains(arr[i])); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testToArray1() throws Exception { |
| 137 | //Test creation of a new array with the possibility of populating passed array if space allows |
| 138 | Integer[] originalArray = new Integer[9]; |
| 139 | fillSet(9, set); |
| 140 | //Test the case where the array and set match in size |
| 141 | Object[] retArray = set.toArray(originalArray); |
HIGUCHI Yuta | 805d96c | 2016-03-17 13:29:09 -0700 | [diff] [blame] | 142 | assertSame("If the set can fit the array should be the one passed in.", originalArray, retArray); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 143 | //Test the case where the passe array is too small to fit the set |
| 144 | set.add(9); |
| 145 | assertNotEquals("A new set should be generated if the contents will not fit in the passed set", |
| 146 | set.toArray(originalArray), originalArray); |
| 147 | //Now test the case where there should be a null terminator |
| 148 | set.clear(); |
| 149 | fillSet(5, set); |
| 150 | assertNull("The character one after last should be null if the array is larger than the set.", |
| 151 | set.toArray(originalArray)[5]); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testAdd() throws Exception { |
| 156 | //Test of add |
| 157 | for (int i = 0; i < 10; i++) { |
| 158 | assertEquals("The size of the set is wrong.", i, set.size()); |
| 159 | assertTrue("The first add of an element should be true.", set.add(i)); |
| 160 | assertFalse("The second add of an element should be false.", set.add(i)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testRemove() throws Exception { |
| 166 | //Test removal |
| 167 | fillSet(10, set); |
| 168 | for (int i = 0; i < 10; i++) { |
| 169 | assertEquals("The size of the set is wrong.", 10 - i, set.size()); |
| 170 | assertTrue("The first removal should be true.", set.remove(i)); |
| 171 | assertFalse("The second removal should be false (item no longer contained).", set.remove(i)); |
| 172 | } |
| 173 | assertTrue("All elements should have been removed.", set.isEmpty()); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testContainsAll() throws Exception { |
| 178 | //Test contains with short circuiting |
| 179 | Set<Integer> integersToCheck = Sets.newHashSet(); |
| 180 | fillSet(10, integersToCheck); |
| 181 | fillSet(10, set); |
| 182 | assertTrue("The sets should be identical so mutual subsets.", set.containsAll(integersToCheck)); |
| 183 | set.remove(9); |
| 184 | assertFalse("The set should contain one fewer value.", set.containsAll(integersToCheck)); |
| 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void testAddAll() throws Exception { |
| 189 | //Test multi-adds with change checking |
| 190 | Set<Integer> integersToCheck = Sets.newHashSet(); |
| 191 | fillSet(10, integersToCheck); |
| 192 | assertFalse("Set should be empty and so integers to check should not be a subset.", |
| 193 | set.containsAll(integersToCheck)); |
| 194 | assertTrue("The set should have changed as a result of add all.", set.addAll(integersToCheck)); |
| 195 | assertFalse("The set should not have changed as a result of add all a second time.", |
| 196 | set.addAll(integersToCheck)); |
| 197 | assertTrue("The sets should now be equivalent.", set.containsAll(integersToCheck)); |
| 198 | assertTrue("The sets should now be equivalent.", integersToCheck.containsAll(set)); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testRetainAll() throws Exception { |
| 203 | //Test ability to generate the intersection set |
| 204 | Set<Integer> retainSet = Sets.newHashSet(); |
| 205 | fillSet(10, set); |
| 206 | assertTrue("The set should have changed.", set.retainAll(retainSet)); |
| 207 | assertTrue("The set should have been emptied.", set.isEmpty()); |
| 208 | fillSet(10, set); |
| 209 | fillSet(10, retainSet); |
| 210 | Set<Integer> duplicateSet = new HashSet<>(set); |
| 211 | assertFalse("The set should not have changed.", set.retainAll(retainSet)); |
| 212 | assertEquals("The set should be the same as the duplicate.", duplicateSet, set); |
| 213 | retainSet.remove(9); |
| 214 | assertTrue("The set should have changed.", set.retainAll(retainSet)); |
| 215 | duplicateSet.remove(9); |
| 216 | assertEquals("The set should have had the nine element removed.", duplicateSet, set); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testRemoveAll() throws Exception { |
| 221 | //Test for mass removal and change checking |
| 222 | Set<Integer> removeSet = Sets.newHashSet(); |
| 223 | fillSet(10, set); |
| 224 | Set<Integer> duplicateSet = Sets.newHashSet(set); |
| 225 | assertFalse("No elements should change.", set.removeAll(removeSet)); |
| 226 | assertEquals("Set should not have diverged from the duplicate.", duplicateSet, set); |
| 227 | fillSet(5, removeSet); |
| 228 | assertTrue("Elements should have been removed.", set.removeAll(removeSet)); |
| 229 | assertNotEquals("Duplicate set should no longer be equivalent.", duplicateSet, set); |
| 230 | assertEquals("Five elements should have been removed from set.", 5, set.size()); |
| 231 | for (Integer item : removeSet) { |
| 232 | assertFalse("No element of remove set should remain.", set.contains(item)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | @Test |
| 237 | public void testClear() throws Exception { |
| 238 | //Test set emptying |
| 239 | assertTrue("The set should be initialized empty.", set.isEmpty()); |
| 240 | set.clear(); |
| 241 | assertTrue("Clear should have no effect on an empty set.", set.isEmpty()); |
| 242 | fillSet(10, set); |
| 243 | assertFalse("The set should no longer be empty.", set.isEmpty()); |
| 244 | set.clear(); |
| 245 | assertTrue("The set should be empty after clear.", set.isEmpty()); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Populated the map with integers from (0) up to (numEntries - 1). |
| 250 | * |
| 251 | * @param numEntries number of entries to add |
| 252 | */ |
| 253 | private void fillSet(int numEntries, Set<Integer> set) { |
| 254 | checkNotNull(set); |
| 255 | for (int i = 0; i < numEntries; i++) { |
| 256 | set.add(i); |
| 257 | } |
| 258 | } |
Jonathan Hart | 4bb4b05 | 2015-10-26 18:10:56 -0700 | [diff] [blame] | 259 | } |