@Test public void testInducedPermutation() { List<String> origData = Arrays.asList(new String[] {"a", "b", "c", "d", "d"}); List<String> permutedData = Arrays.asList(new String[] {"d", "b", "c", "a", "d"}); DummyRandomKey drk = new DummyRandomKey(RandomKey.inducedPermutation(origData, permutedData)); List<String> decoded = drk.decode(origData); Assert.assertEquals("d", decoded.get(0)); Assert.assertEquals("b", decoded.get(1)); Assert.assertEquals("c", decoded.get(2)); Assert.assertEquals("a", decoded.get(3)); Assert.assertEquals("d", decoded.get(4)); try { RandomKey.inducedPermutation( Arrays.asList(new String[] {"a", "b", "c", "d", "d"}), Arrays.asList(new String[] {"a", "b", "c", "d"})); Assert.fail("Uncaught exception"); } catch (MathIllegalArgumentException e) { // no-op } try { RandomKey.inducedPermutation( Arrays.asList(new String[] {"a", "b", "c", "d", "d"}), Arrays.asList(new String[] {"a", "b", "c", "d", "f"})); Assert.fail("Uncaught exception"); } catch (MathIllegalArgumentException e) { // no-op } }
@Test public void testRandomPermutation() { // never generate an invalid one for (int i = 0; i < 10; i++) { DummyRandomKey drk = new DummyRandomKey(RandomKey.randomPermutation(20)); Assert.assertNotNull(drk); } }
@Test public void testComparatorPermutation() { List<String> data = Arrays.asList(new String[] {"x", "b", "c", "z", "b"}); List<Double> permutation = RandomKey.comparatorPermutation( data, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); Double[] permArr = new Double[data.size()]; permArr = permutation.toArray(permArr); Assert.assertArrayEquals(new Double[] {0.6, 0.0, 0.4, 0.8, 0.2}, permArr); List<String> decodedData = new DummyRandomKey(permutation).decode(data); Assert.assertEquals("b", decodedData.get(0)); Assert.assertEquals("b", decodedData.get(1)); Assert.assertEquals("c", decodedData.get(2)); Assert.assertEquals("x", decodedData.get(3)); Assert.assertEquals("z", decodedData.get(4)); permutation = RandomKey.comparatorPermutation( data, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); permArr = new Double[data.size()]; permArr = permutation.toArray(permArr); Assert.assertArrayEquals(new Double[] {0.2, 0.6, 0.4, 0.0, 0.8}, permArr); decodedData = new DummyRandomKey(permutation).decode(data); Assert.assertEquals("z", decodedData.get(0)); Assert.assertEquals("x", decodedData.get(1)); Assert.assertEquals("c", decodedData.get(2)); Assert.assertEquals("b", decodedData.get(3)); Assert.assertEquals("b", decodedData.get(4)); }
@Test public void testIdentityPermutation() { DummyRandomKey drk = new DummyRandomKey(RandomKey.identityPermutation(5)); List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"})); Assert.assertEquals("a", decoded.get(0)); Assert.assertEquals("b", decoded.get(1)); Assert.assertEquals("c", decoded.get(2)); Assert.assertEquals("d", decoded.get(3)); Assert.assertEquals("e", decoded.get(4)); }