blob: 99827da2ad9fc2f260c0d85f0e32ffa6c2a1a5f6 [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 }
75 }, fakeDB, "set");
76
77 }
78
Aaron Kruglikov92511f22015-10-12 14:39:04 -070079 @Test
80 public void testSize() throws Exception {
81 //Check correct sizing throughout population
82 for (int i = 0; i < 10; i++) {
83 set.add(i);
84 assertEquals("The set should have i + 1 entries.", i + 1, set.size());
85 }
86 }
87
88 @Test
89 public void testIsEmpty() throws Exception {
90 //test empty condition
91 assertTrue("The set should be initialized empty.", set.isEmpty());
92 fillSet(5, this.set);
93 assertFalse("The set should no longer be empty.", set.isEmpty());
94 set.clear();
95 assertTrue("The set should have been cleared.", set.isEmpty());
96 }
97
98 @Test
99 public void testContains() throws Exception {
100 //Test contains
101 assertFalse("The set should not contain anything", set.contains(1));
102 fillSet(10, this.set);
103 for (int i = 0; i < 10; i++) {
104 assertTrue("The set should contain all values 0-9.", set.contains(i));
105 }
106 }
107
108 @Test
109 public void testIterator() throws Exception {
110 //Test iterator behavior (no order guarantees are made)
111 Set<Integer> validationSet = Sets.newHashSet();
112 fillSet(10, this.set);
113 fillSet(10, validationSet);
114 set.iterator().forEachRemaining(item -> assertTrue("Items were mismatched.", validationSet.remove(item)));
115 //All values should have been seen and removed
116 assertTrue("All entries in the validation set should have been removed.", validationSet.isEmpty());
117 }
118
119 @Test
120 public void testToArray() throws Exception {
121 //Test creation of a new array of the values
122 fillSet(10, set);
123 Object[] arr = set.toArray();
124 assertEquals("The array should be of length 10.", 10, arr.length);
125 for (int i = 0; i < 10; i++) {
HIGUCHI Yuta805d96c2016-03-17 13:29:09 -0700126 assertTrue("All elements of the array should be in the set.", set.contains(arr[i]));
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700127 }
128 }
129
130 @Test
131 public void testToArray1() throws Exception {
132 //Test creation of a new array with the possibility of populating passed array if space allows
133 Integer[] originalArray = new Integer[9];
134 fillSet(9, set);
135 //Test the case where the array and set match in size
136 Object[] retArray = set.toArray(originalArray);
HIGUCHI Yuta805d96c2016-03-17 13:29:09 -0700137 assertSame("If the set can fit the array should be the one passed in.", originalArray, retArray);
Aaron Kruglikov92511f22015-10-12 14:39:04 -0700138 //Test the case where the passe array is too small to fit the set
139 set.add(9);
140 assertNotEquals("A new set should be generated if the contents will not fit in the passed set",
141 set.toArray(originalArray), originalArray);
142 //Now test the case where there should be a null terminator
143 set.clear();
144 fillSet(5, set);
145 assertNull("The character one after last should be null if the array is larger than the set.",
146 set.toArray(originalArray)[5]);
147 }
148
149 @Test
150 public void testAdd() throws Exception {
151 //Test of add
152 for (int i = 0; i < 10; i++) {
153 assertEquals("The size of the set is wrong.", i, set.size());
154 assertTrue("The first add of an element should be true.", set.add(i));
155 assertFalse("The second add of an element should be false.", set.add(i));
156 }
157 }
158
159 @Test
160 public void testRemove() throws Exception {
161 //Test removal
162 fillSet(10, set);
163 for (int i = 0; i < 10; i++) {
164 assertEquals("The size of the set is wrong.", 10 - i, set.size());
165 assertTrue("The first removal should be true.", set.remove(i));
166 assertFalse("The second removal should be false (item no longer contained).", set.remove(i));
167 }
168 assertTrue("All elements should have been removed.", set.isEmpty());
169 }
170
171 @Test
172 public void testContainsAll() throws Exception {
173 //Test contains with short circuiting
174 Set<Integer> integersToCheck = Sets.newHashSet();
175 fillSet(10, integersToCheck);
176 fillSet(10, set);
177 assertTrue("The sets should be identical so mutual subsets.", set.containsAll(integersToCheck));
178 set.remove(9);
179 assertFalse("The set should contain one fewer value.", set.containsAll(integersToCheck));
180 }
181
182 @Test
183 public void testAddAll() throws Exception {
184 //Test multi-adds with change checking
185 Set<Integer> integersToCheck = Sets.newHashSet();
186 fillSet(10, integersToCheck);
187 assertFalse("Set should be empty and so integers to check should not be a subset.",
188 set.containsAll(integersToCheck));
189 assertTrue("The set should have changed as a result of add all.", set.addAll(integersToCheck));
190 assertFalse("The set should not have changed as a result of add all a second time.",
191 set.addAll(integersToCheck));
192 assertTrue("The sets should now be equivalent.", set.containsAll(integersToCheck));
193 assertTrue("The sets should now be equivalent.", integersToCheck.containsAll(set));
194 }
195
196 @Test
197 public void testRetainAll() throws Exception {
198 //Test ability to generate the intersection set
199 Set<Integer> retainSet = Sets.newHashSet();
200 fillSet(10, set);
201 assertTrue("The set should have changed.", set.retainAll(retainSet));
202 assertTrue("The set should have been emptied.", set.isEmpty());
203 fillSet(10, set);
204 fillSet(10, retainSet);
205 Set<Integer> duplicateSet = new HashSet<>(set);
206 assertFalse("The set should not have changed.", set.retainAll(retainSet));
207 assertEquals("The set should be the same as the duplicate.", duplicateSet, set);
208 retainSet.remove(9);
209 assertTrue("The set should have changed.", set.retainAll(retainSet));
210 duplicateSet.remove(9);
211 assertEquals("The set should have had the nine element removed.", duplicateSet, set);
212 }
213
214 @Test
215 public void testRemoveAll() throws Exception {
216 //Test for mass removal and change checking
217 Set<Integer> removeSet = Sets.newHashSet();
218 fillSet(10, set);
219 Set<Integer> duplicateSet = Sets.newHashSet(set);
220 assertFalse("No elements should change.", set.removeAll(removeSet));
221 assertEquals("Set should not have diverged from the duplicate.", duplicateSet, set);
222 fillSet(5, removeSet);
223 assertTrue("Elements should have been removed.", set.removeAll(removeSet));
224 assertNotEquals("Duplicate set should no longer be equivalent.", duplicateSet, set);
225 assertEquals("Five elements should have been removed from set.", 5, set.size());
226 for (Integer item : removeSet) {
227 assertFalse("No element of remove set should remain.", set.contains(item));
228 }
229 }
230
231 @Test
232 public void testClear() throws Exception {
233 //Test set emptying
234 assertTrue("The set should be initialized empty.", set.isEmpty());
235 set.clear();
236 assertTrue("Clear should have no effect on an empty set.", set.isEmpty());
237 fillSet(10, set);
238 assertFalse("The set should no longer be empty.", set.isEmpty());
239 set.clear();
240 assertTrue("The set should be empty after clear.", set.isEmpty());
241 }
242
243 /**
244 * Populated the map with integers from (0) up to (numEntries - 1).
245 *
246 * @param numEntries number of entries to add
247 */
248 private void fillSet(int numEntries, Set<Integer> set) {
249 checkNotNull(set);
250 for (int i = 0; i < numEntries; i++) {
251 set.add(i);
252 }
253 }
Jonathan Hart4bb4b052015-10-26 18:10:56 -0700254}