public void testPermutationsSimple() { IntegersUtils.allPermutations(1, new IntProcedure() { @Override public void invoke(int a) { fail(); } }); IntegersUtils.allPermutations(2, new IntProcedure() { boolean beenThere = false; @Override public void invoke(int a) { assertFalse(beenThere); assertEquals(0, a); beenThere = true; } }); final IntArray is = IntArray.create(1, 2, 3, 4); IntegersDebug.println("0 " + is); IntegersUtils.allPermutations(4, new IntProcedure() { int count = 1; @Override public void invoke(int i) { assertTrue("" + i, i >= 0); assertTrue("" + i, i < 3); is.swap(i, i + 1); IntegersDebug.println(count++ + " " + is); } }); }
private List<LongTwoWayMap.Entry> pairs(int... ps) { assertTrue(ps.length % 2 == 0); List<LongTwoWayMap.Entry> list = IntegersUtils.arrayList(); for (int i = 0; i < ps.length; i += 2) { list.add(new LongTwoWayMap.Entry(ps[i], ps[i+1])); } return list; }
@Override public int hashCode() { int h = 0; for (LongIterator it : this) { h += IntegersUtils.hash(it.value()); } return h; }
@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); }
public void testSubstringAfterLast() { assertEquals("b", IntegersUtils.substringAfterLast("a b", " ")); assertEquals("", IntegersUtils.substringAfterLast("a ", " ")); } }
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()); }
@Override public void checkMap(LongIntListMap actualMap, LongList expectedKeys, IntList expectedValues) { super.checkMap(actualMap, expectedKeys, expectedValues); final LongArray keysArray = new LongArray(expectedKeys); final IntArray valuesArray = new IntArray(expectedValues); IntegersUtils.quicksort(keysArray.size(), LongFunctions.comparator(keysArray), new IntIntProcedure() { @Override public void invoke(int a, int b) { keysArray.swap(a, b); valuesArray.swap(a, b); } }); CHECK.order(keysArray, actualMap.keysAsList()); CHECK.order(LongCollections.asLongList(valuesArray), LongCollections.asLongList(actualMap.valuesAsList())); CHECK.order(actualMap.keysIterator(0, actualMap.size()), keysArray.iterator()); CHECK.order(actualMap.valuesIterator(0, actualMap.size()), valuesArray.iterator()); }
@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 testAllPermutationsOf8() { final char c[] = "12345678".toCharArray(); final HashSet<String> perms = new HashSet<String>(); perms.add(new String(c)); final int[] permCount = new int[] {1}; IntegersUtils.allPermutations(c.length, new IntProcedure() { @Override public void invoke(int swapIdx) { permCount[0] += 1; assertTrue(swapIdx >= 0 && swapIdx < c.length - 1); char t = c[swapIdx]; c[swapIdx] = c[swapIdx + 1]; c[swapIdx + 1] = t; String perm = new String(c); assertTrue(permCount[0] + "", perms.add(perm)); } }); // Check that permCount is 8! for (int i = 2; i <= c.length; ++i) { assertEquals(permCount[0] + " " + i, 0, permCount[0] % i); } }
@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); } } } } }
public void testReversePerm() { final IntArray perms = IntArray.copy(new IntProgression.ArithmeticIterator(0, 9, 1)); IntegersUtils.allPermutations(perms.size(), new IntProcedure() { { testRevPerm(perms); } @Override public void invoke(int i) { perms.swap(i, i + 1); testRevPerm(perms); } }); }
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 array = generateRandomLongArray(size, SORTED_UNIQUE); int expectedHash = 0; for (LongIterator it : array) { expectedHash += IntegersUtils.hash(it.value()); } for (T set0 : createSets(array)) { assertEquals(expectedHash, set0.hashCode()); } if (isSortedSet()) return; IntArray indices = new IntArray(IntProgression.range(size)); for (int i = 0; i < shuffleCount; i++) { for (T set : createSets(array.get(indices))) { assertEquals(expectedHash, set.hashCode()); indices.shuffle(myRand); } } } } }
/** Prefixed with _ because it runs fairly long. */ public void _testAllPermutations() { final LongArray toAdd = LongArray.create(1, 2, 5, 7, 8, 11, 14, 15); IntegersUtils.allPermutations(toAdd.size(), new IntProcedure() { private int count; private final LongArray toAddWith4 = LongArray.copy(toAdd); { toAddWith4.add(4); runTest(); } @Override public void invoke(int swapIdx) { toAdd.swap(swapIdx, swapIdx + 1); runTest(); } private void runTest() { System.err.println(count++ + " "); set.clear(); set.addAll(toAdd); check(toAdd); set.add(4); check(toAddWith4); } private void check(LongList expectedValues) { assertEquals(expectedValues.size(), set.size()); checkSet(set, expectedValues); } }); }