public void testValuesIterator() { IntArray expected = IntArray.create(1, 1, 2, 2, 3, 3); map.putAll(LongArray.create(0, 2, 4, 6, 8, 10), expected); IntArray actual = IntCollections.collectIterable(expected.size(), map.valuesIterator()); actual.sort(); assertEquals(expected, actual); int size = 20, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { map.clear(); LongArray keys = generateRandomLongArray(size, SortedStatus.SORTED_UNIQUE, 100, 200); keys.shuffle(myRand); expected = new IntArray(size); for (int i = 0; i < keys.size(); i++) { long key = keys.get(i); expected.add((int) (key * key)); } map.putAll(keys, expected); actual = new IntArray(map.valuesIterator()); actual.sort(); expected.sort(); CHECK.order(actual.toNativeArray(), expected.toNativeArray()); } }
private static void testRevPerm(IntArray perms) { IntArray rev = new IntArray(perms); IntegersUtils.reversePerm(rev); int n = perms.size(); IntArray e = new IntArray(n); for (int i = 0; i < n; ++i) { e.add(perms.get(rev.get(i))); } CHECK.order(IntProgression.arithmetic(0, n).iterator(), e.iterator()); }
public void testIndexOf() { int arrayLength = 1000; int maxValue = Integer.MAX_VALUE; IntArray res = new IntArray(arrayLength); int[] intArr = new int[arrayLength]; for (int i = 0; i < arrayLength; i++) { int val = myRand.nextInt(maxValue); res.add(val); intArr[i] = val; } for (int i = 0; i < arrayLength; i++) { int val = res.get(i); assertEquals(res.indexOf(val), IntCollections.indexOf(val, intArr)); } }
public void testToTableString() { long[][] keysArray = {{}, {-1}, {0, 1, 2, 3}, {10, 20, 30}, {1, 3, 5, 7, 11, 13}}; for (long[] keys : keysArray) { IntArray values = new IntArray(keys.length); for (long key : keys) { values.add((int) (key * key)); } for (T map : createMapsFromLists(new LongArray(keys), values)) { String output; if (map instanceof AbstractWritableLongIntMap) { output = ((AbstractWritableLongIntMap) map).toTableString(); } else if (map instanceof WritableLongIntMapFromLongObjMap) { output = ((WritableLongIntMapFromLongObjMap) map).toTableString(); } else { continue; } int idx = 0, commasCount = 0; for (; idx < output.length() && output.charAt(idx) != '\n'; idx++) { if (output.charAt(idx) == ',') commasCount++; } assertEquals(Math.max(0, keys.length - 1), commasCount); assertFalse(idx == output.length()); idx++; for (; idx < output.length() && output.charAt(idx) != '\n'; idx++) { if (output.charAt(idx) == ',') commasCount--; } assertEquals(0, commasCount); } } }
public void testRemoveAllAtSorted() { int arSize = 100; int indicesSize = 10; int attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { array = generateRandomLongArray(arSize, UNORDERED); IntArray indices = generateRandomIntArray(indicesSize, SORTED_UNIQUE, array.size()); if (attempt % 2 == 0) { indices.add(indices.get(indices.size() / 2) + 1); indices.sortUnique(); } LongArray expected = LongArray.copy(array); for (int i = indices.size() - 1; i >= 0; i--) { expected.removeAt(indices.get(i)); } array.removeAllAtSorted(indices.iterator()); CHECK.order(array, expected); } }
public void testCreate() { LongArray values = LongArray.create(0,2,4,6,8); IntArray counts = IntArray.create(0, 1, 0, 3, 0); LongArray expected = LongArray.create(2, 6, 6, 6); LongSameValuesList actual = LongSameValuesList.create(values, counts); CHECK.order(expected, actual); actual = LongSameValuesList.create(LongArray.create(9, 9, 2, 4, 6), IntArray.create(0, 1, 0, 3, 1)); expected = LongArray.create(9, 4, 4, 4, 6); CHECK.order(expected, actual); if (i == attemptsCount - 1) { values.add(myRand.nextLong()); counts.add(0); expected.addAll(LongCollections.repeat(values.get(j), counts.get(j))); counts = new IntArray(IntCollections.repeat(1, 100)); actual = LongSameValuesList.create(values, counts); CHECK.order(values, actual);
public void testRandomCase() { int arrayLength = 1000; int indicesLength = 100; int maxValue = Integer.MAX_VALUE; LongArray list = LongArray.create(); for ( int i = 0; i < arrayLength; i++) { list.add((long) myRand.nextInt(maxValue)); } IntArray arrayIndexes = IntArray.create(); for ( int i = 0; i < indicesLength; i++) { arrayIndexes.add(myRand.nextInt(arrayLength)); } LongArray expected = LongArray.create(); for ( int i = 0; i < indicesLength; i++) { expected.add(list.get(arrayIndexes.get(i))); } LongIndexedIterator res = new LongIndexedIterator(list, arrayIndexes.iterator()); CHECK.order(res, expected.iterator()); } }
@Override protected LongIntListMap createMapWithCapacity(int capacity) { return new LongIntListMap(new LongArray(capacity), new IntArray(capacity)); }
@Override public void testHashCode() { int attemptsCount = 10, shuffleCount = 10; int sizeMax = 600, step = 50; for (int attempt = 0; attempt < attemptsCount; attempt++) { for (int size = step; size < sizeMax; size += step) { LongArray keys = generateRandomLongArray(size, SORTED_UNIQUE); IntArray values = generateRandomIntArray(keys.size(), UNORDERED); int expectedHash = 0; for (int i = 0; i < size; i++) { expectedHash += IntegersUtils.hash(keys.get(i)) + (new Integer(values.get(i))).hashCode(); } for (WritableLongObjMap<Integer> map0 : createObjMapsFromLists(keys, values.toList())) { assertEquals(expectedHash, map0.hashCode()); } IntArray indices = new IntArray(IntProgression.range(size)); for (int i = 0; i < shuffleCount; i++) { map = createMap(); map.putAll(keys.get(indices), values.get(indices)); assertEquals(expectedHash, map.hashCode()); indices.shuffle(myRand); } } } } }
for (int i = 0; i < count; i++) { source = LongArray.copy(values); IntArray indices = IntArray.create(); int maxDiff = 4; int curIdx = myRand.nextInt(maxDiff); while (curIdx < source.size()) { indices.add(curIdx); source.insert(curIdx, myRand.nextInt()); curIdx += 1 + myRand.nextInt(maxDiff); for (int j = 0; j < indices.size(); j++) { resArray.removeAt(indices.get(j));
public void testNoSuchElementExceptionCase() { LongArray list = LongArray.create(2, 3, 9); IntListIterator indices = IntArray.create(0, 2).iterator(); LongListIndexedIterator res = new LongListIndexedIterator(list, indices); // check for NSEE try { res.value(); fail("not caught NSEE"); } catch(NoSuchElementException ex) { } } }
@Override public void testHashCode() { int attemptsCount = 10, shuffleCount = 10; int sizeMax = 600, step = 50; for (int attempt = 0; attempt < attemptsCount; attempt++) { for (int mSize = step; mSize < sizeMax; mSize += step) { LongArray keys = generateRandomLongArray(mSize, SORTED_UNIQUE); int size = keys.size(); IntArray values = generateRandomIntArray(size, UNORDERED); int expectedHash = 0; for (int i = 0; i < size; i++) { expectedHash += IntegersUtils.hash(keys.get(i)) + IntegersUtils.hash(values.get(i)); } for (T map0 : createMapsFromLists(keys, values)) { assertEquals(expectedHash, map0.hashCode()); } IntArray indices = new IntArray(IntProgression.range(size)); for (int i = 0; i < shuffleCount; i++) { map = createMap(); map.putAll(keys.get(indices), values.get(indices)); assertEquals(expectedHash, map.hashCode()); indices.shuffle(myRand); } } } }
public void testPrepareIndices() { IntArray indices = IntArray.create(0, 1, 2, 5, 6, 7); LongListRemovingDecorator.prepareSortedIndices(indices); System.out.println(indices); } }
public void testValueComplexCase() { IntArray keys = new IntArray(IntProgression.arithmetic(0, 1000, 1)); LongArray values0 = new LongArray(LongProgression.arithmetic(0, 1000, 1)); LongArray values = new LongArray(values0.toNativeArray()); values.apply(0, 1000, new LongToLong() { @Override public long invoke(long a) { return a * a - a; } }); res = IntLongIterators.pair(keys.iterator(), values.iterator()); for (int i = 0; i < 1000; i++) { res.next(); int v1 = res.left(); long v2 = res.right(); assertEquals(i, v1); assertEquals(i * i - i, v2); } } }
@Override protected List<LongIntListMap> createMapsFromLists(LongList keys, IntList values) { IntArray indices = new IntArray(IntProgression.range(keys.size())); indices.shuffle(myRand); LongIntListMap createdMap = new LongIntListMap(); for (LongIntIterator it : LongIntIterators.pair(keys.get(indices), values.get(indices))) { createdMap.add(it.left(), it.right()); } final LongArray keys0 = new LongArray(keys); final IntArray values0 = new IntArray(values); IntegersUtils.quicksort(keys.size(), LongFunctions.comparator(keys0), new IntIntProcedure() { @Override public void invoke(int a, int b) { keys0.swap(a, b); values0.swap(a, b); } }); return Arrays.asList(new LongIntListMap(keys0, values0), createdMap); }
IntArray indices = IntArray.create(values.length); prepareSortedIndices(indices); resArray = createFromPrepared(source, indices); indices = IntArray.create(0, values.length + 1); prepareSortedIndices(indices); resArray = createFromPrepared(source, indices); indices = IntArray.create(0); prepareSortedIndices(indices); resArray = createFromPrepared(source, indices); source = LongArray.copy(values); source.insert(pos, myRand.nextInt()); indices = IntArray.create(pos); prepareSortedIndices(indices); resArray = createFromPrepared(source, indices); for (int i = 0; i < count; i++) { source = LongArray.copy(values); indices = IntArray.create(); int maxDiff = 4; int curIdx = myRand.nextInt(maxDiff); while (curIdx < source.size()) { indices.add(curIdx); source.insert(curIdx, myRand.nextInt()); curIdx += 1 + myRand.nextInt(maxDiff);
public void testIterators() { // assumed that method createMapsFromLists returns variants with all constructors int attemptsCount = 10; int maxSize = 50; // int maxSize = 1000; for (int attempt = 0; attempt < attemptsCount; attempt++) { LongArray keys = generateRandomLongArray(maxSize, SORTED_UNIQUE); IntArray values = generateRandomIntArray(keys.size(), UNORDERED); for (T map : createMapsFromLists(keys, values)) { LongArray actualKeys = LongCollections.collectIterables(map.size(), map.keysIterator()); IntArray actualValues = IntCollections.collectIterable(map.size(), map.valuesIterator()); CHECK.unordered(actualKeys, keys); CHECK.unordered(actualValues, values); actualKeys.clear(); actualValues.clear(); for (LongIntIterator iter : map) { actualKeys.add(iter.left()); actualValues.add(iter.right()); } CHECK.unordered(actualKeys, keys); CHECK.unordered(actualValues, values); } } }
public void add(int value) { res.add(value); } }, maxLength, minMaxValues);
public void testRandomCreateFromSorted() { int arrLength = 100; int indicesLength = 50; int maxValue = 1000; LongList base = generateRandomLongArray(arrLength, UNORDERED, maxValue); for (int test = 0; test < 20; test++) { IntArray indices = generateRandomIntArray(indicesLength, SORTED_UNIQUE, arrLength); LongArray expected = LongArray.copy(base); for (int i = indices.size() - 1; i >= 0; i--) { int val = indices.get(i); expected.removeRange(val, val + 1); } WritableLongListRemovingDecorator arr = WritableLongListRemovingDecorator.createFromSorted(base, indices); CHECK.order(arr.iterator(), expected.iterator()); } }