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