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