blob: 2c8c92f8e383ecaa11e72b5953668488be5fbab4 [file] [log] [blame]
Aaron Kruglikov92511f22015-10-12 14:39:04 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikov92511f22015-10-12 14:39:04 -07003 *
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;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070020import org.junit.Before;
21import org.junit.Test;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070022import org.onosproject.store.service.Serializer;
23
Aaron Kruglikov92511f22015-10-12 14:39:04 -070024import java.util.HashSet;
25import java.util.Set;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertNotEquals;
31import static org.junit.Assert.assertNull;
HIGUCHI Yuta805d96c2016-03-17 13:29:09 -070032import static org.junit.Assert.assertSame;
Aaron Kruglikov92511f22015-10-12 14:39:04 -070033import static org.junit.Assert.assertTrue;
34
35/**
36 * Test suite for Persistent Set.
37 */
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070038public class PersistentSetTest extends MapDBTest {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070039
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070040 private PersistentSet<Integer> set = null;
HIGUCHI Yuta49e03a32016-03-21 22:07:35 -070041
Aaron Kruglikov92511f22015-10-12 14:39:04 -070042 @Before
43 public void setUp() throws Exception {
HIGUCHI Yuta8eb6ba12016-05-21 15:01:41 -070044 //Creates a set within it and a basic integer serializer
45 set = new PersistentSet<>(new Serializer() {
Aaron Kruglikov92511f22015-10-12 14:39:04 -070046 @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 Yuta8eb6ba12016-05-21 15:01:41 -070073 return (T) Integer.valueOf(num);
Aaron Kruglikov92511f22015-10-12 14:39:04 -070074 }
Jordan Halterman2c83a102017-08-20 17:11:41 -070075
76 @Override
77 public <T> T copy(T object) {
78 return decode(encode(object));
79 }
Aaron Kruglikov92511f22015-10-12 14:39:04 -070080 }, fakeDB, "set");
81
82 }
83
Aaron Kruglikov92511f22015-10-12 14:39:04 -070084 @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 Yuta805d96c2016-03-17 13:29:09 -0700131 assertTrue("All elements of the array should be in the set.", set.contains(arr[i]));
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700132 }
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 Yuta805d96c2016-03-17 13:29:09 -0700142 assertSame("If the set can fit the array should be the one passed in.", originalArray, retArray);
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700143 //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 Hart4bb4b052015-10-26 18:10:56 -0700259}