public void testPut() { int size = 100, attempts = 10, maxVal = 1000; for (int attempt = 0; attempt < attempts; attempt++) { map.clear(); LongArray keys = generateRandomLongArray(size, SortedStatus.SORTED_UNIQUE, maxVal); LongArray shuffledKeys = LongCollections.collectLists(keys, keys); shuffledKeys.shuffle(myRand); for (int i = 0; i < shuffledKeys.size(); i++) { long key = shuffledKeys.get(i); long prevValue = map.get(key); assertEquals(prevValue, map.put(key, (int) (-key * key))); } } }
public void testContains2() { int arSize = 45, maxVal = Integer.MAX_VALUE, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { LongArray arr = generateRandomLongArray(arSize, SORTED_UNIQUE, maxVal); LongArray arr2 = LongCollections.collectLists(arr, map(LongFunctions.INC, arr), map(LongFunctions.DEC, arr)); arr2.sortUnique(); for (WritableLongSet curSet : createSets(arr)) { for (int i = 0; i < arr2.size(); i++) { long value = arr2.get(i); assertEquals(arr.binarySearch(value) >= 0, curSet.contains(value)); } } } }
public void checkCollect(LongList ... lists) { LongArray expected = new LongArray(); for (LongList array : lists) { expected.addAll(array); } CHECK.order(expected, collectLists(lists)); int attemptsCount = 5; for (int attempt = 0; attempt < attemptsCount; attempt++) { LongIterable[] iterables = new LongIterable[lists.length]; for (int i = 0; i < lists.length; i++) { switch (myRand.nextInt(3)) { case 0: iterables[i] = lists[i]; break; case 1: iterables[i] = lists[i].iterator(); break; case 2: int size2 = lists[i].size() / 2; iterables[i] = new LongConcatIterator(lists[i].subList(0, size2), lists[i].subList(size2, lists[i].size())); break; default: assert false; } } CHECK.order(expected, collectIterables(iterables)); for (int i = 0; i < iterables.length; i++) { if (iterables[i] instanceof LongIterator) { iterables[i] = lists[i].iterator(); } } CHECK.order(expected, collectIterables(expected.size(), iterables)); } }
public void testContains3() { int arSize = 45, maxVal = Integer.MAX_VALUE, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { LongArray arr = generateRandomLongArray(arSize, SORTED_UNIQUE, maxVal); LongArray arr2 = LongCollections.collectLists(arr, map(LongFunctions.INC, arr), map(LongFunctions.DEC, arr)); arr2.sortUnique(); for (WritableLongSet curSet : createSets(arr)) { for (int i = 0; i < arr2.size(); i++) { long value = arr2.get(i); boolean contains = arr.binarySearch(value) >= 0; assertEquals(contains, curSet.contains(value)); assertEquals(contains, curSet.exclude(value)); assertEquals(false, curSet.contains(value)); } } } }
public void testSimpleResize() { long[] values0 = {-1, 0, 10, -10, 20, 30}; LongArray values = new LongArray(values0); LongArray toAdd = LongArray.create(0, -4, -10, 20); LongList expected = LongCollections.concatLists(values, toAdd); for (WritableLongList list : createWritableLongLists(values0)) { CHECK.order(values, list); list.addAll(toAdd); CHECK.order(expected, list); } LongList valuesList = LongProgression.range(1024); values0 = valuesList.toNativeArray(); long[] expected0 = LongCollections.collectLists(valuesList, new LongList.Single(1025)).extractHostArray(); for (WritableLongList list : createWritableLongLists(values0)) { list.add(1025); CHECK.order(list, expected0); } }
public void testTailIteratorRandom() { if (!isSortedSet()) return; final int size = 200, testCount = 5; LongArray expected; LongArray testValues; for (int i = 0; i < testCount; i++) { expected = new LongArray(LongIterators.limit(randomIterator(), size)); expected.sortUnique(); testValues = LongCollections.collectLists( expected, map(LongFunctions.INC, expected), map(LongFunctions.DEC, expected)); testValues.sortUnique(); for (T set0 : createSets(expected)) { LongSortedSet sortedSet = (LongSortedSet) set0; for (int j = 0; j < testValues.size(); j++) { long key = testValues.get(j); int ind = expected. binarySearch(key); CHECK.order(sortedSet.tailIterator(key), expected.iterator(ind >= 0 ? ind : -ind - 1)); } } } }
public void testContains2() { int arSize = 45, maxVal = Integer.MAX_VALUE, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { LongArray arr = generateRandomLongArray(arSize, SORTED_UNIQUE, maxVal); LongArray arr2 = LongCollections.collectLists(arr, map(LongFunctions.INC, arr), map(LongFunctions.DEC, arr)); arr2.sortUnique(); LongSetBuilder b = new LongSetBuilder(); b.addAll(arr); for (int i = 0; i < arr2.size(); i++) { long value = arr2.get(i); assertEquals(arr.binarySearch(value) >= 0, b.contains(value)); } b.coalesce(); for (int i = 0; i < arr2.size(); i++) { long value = arr2.get(i); assertEquals(arr.binarySearch(value) >= 0, b.contains(value)); } } }
public void testContains4() { int arSize = 45, maxVal = Integer.MAX_VALUE, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { LongArray arr = generateRandomLongArray(arSize, SORTED_UNIQUE, maxVal); LongArray arr2 = collectLists(arr, map(LongFunctions.INC, arr), map(LongFunctions.DEC, arr)); arr2.sortUnique(); for (LongAmortizedSet curSet : createSets(arr)) { for (int i = 0; i < arr2.size(); i++) { long value = arr2.get(i); assertEquals(arr.binarySearch(value) >= 0, curSet.contains(value)); } for (int i = 0; i < arr.size(); i += 2) { long value = arr.get(i); curSet.remove(value); assertFalse(curSet.contains(value)); } curSet.coalesce(); for (int i = 0; i < arr.size(); i++) { assertEquals(i % 2 == 1, curSet.contains(arr.get(i))); } } } }
public void testRandom(int maxVal, int setSize, int listSize, int nAttempts) { set = new LongAmortizedSet(); LongArray toAdd = new LongArray(listSize); for (int attempt = 0; attempt < nAttempts; ++attempt) { if (attempt % 3 == 0) set = new LongAmortizedSet(); LongArray expected = generateRandomLongArray(setSize, UNORDERED, maxVal); set.clear(); set.addAll(expected); expected.sortUnique(); checkSet(set, expected); toAdd.clear(); toAdd.addAll(generateRandomLongArray(listSize, SORTED_UNIQUE, maxVal)); for (LongIterator it : set) { toAdd.removeAllSorted(it.value()); } set.addAll(toAdd); LongArray expected2 = LongArray.copy(expected); expected2.merge(toAdd); checkSet(set, expected2); set.removeAll(toAdd); set.coalesce(); LongArray notExpected = collectLists(map(LongFunctions.INC, expected), map(LongFunctions.DEC, expected)); notExpected.removeAll(expected); set.removeAll(notExpected); checkSet(set, expected); } }
public void testContains() { int arSize = 45, maxVal = Integer.MAX_VALUE, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { LongArray arr = generateRandomLongArray(arSize, SORTED_UNIQUE, maxVal); LongArray arr2 = LongCollections.collectLists(arr, map(LongFunctions.INC, arr), map(LongFunctions.DEC, arr)); arr2.sortUnique(); for (LongSet curSet : createSets(arr)) { for (int i = 0; i < arr2.size(); i++) { long value = arr2.get(i); assertEquals(arr.binarySearch(value) >= 0, curSet.contains(value)); } assertTrue(curSet.containsAll(arr)); assertTrue(curSet.containsAll(curSet)); assertTrue(curSet.containsAll(curSet.iterator())); assertTrue(curSet.containsAll(LongList.EMPTY)); assertFalse(curSet.containsAll(concatLists(arr, new LongList.Single(arr.getLast(0) + 1)))); assertEquals(arr.size(), curSet.size()); } } }