@Test public void smoke() { CaseCombinator p = new CaseCombinator(); List<String> result = p.combinations("a-B="); List<String> witness = new ArrayList<String>(); witness.add("a-b="); witness.add("A-b="); witness.add("a-B="); witness.add("A-B="); assertEquals(witness, result); }
List<String> combinations(String in) { int length = in.length(); List<String> permutationsList = new ArrayList<String>(); int totalCombinations = computeTotalNumerOfCombinations(in, length); for (int j = 0; j < totalCombinations; j++) { StringBuilder newCombination = new StringBuilder(); int pos = 0; for (int i = 0; i < length; i++) { char c = in.charAt(i); if (isEnglishLetter(c)) { c = permute(c, j, pos); pos++; } newCombination.append(c); } permutationsList.add(newCombination.toString()); } return permutationsList; }
private char permute(char c, int permutation, int position) { int mask = 1 << position; boolean shouldBeInUpperCase = (permutation & mask) != 0; boolean isEffectivelyUpperCase = isUpperCase(c); if (shouldBeInUpperCase && !isEffectivelyUpperCase) return toUpperCase(c); if (!shouldBeInUpperCase && isEffectivelyUpperCase) return toLowerCase(c); return c; }
@Test public void other() { List<String> result = p.combinations("aBCd"); assertEquals(16, result.size()); Set<String> witness = new HashSet<String>(result); // check that there are no duplicates assertEquals(16, witness.size()); } }
private int computeTotalNumerOfCombinations(String in, int length) { int count = 0; for (int i = 0; i < length; i++) { char c = in.charAt(i); if (isEnglishLetter(c)) count++; } // return 2^count (2 to the power of count) return (1 << count); }
@Test public void smoke() throws Exception { srs.addRule(new ElementSelector("a/b"), new XAction()); // test for all possible case combinations of "a/b" for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testSlashSuffix() throws Exception { ElementSelector pa = new ElementSelector("a/"); srs.addRule(pa, new XAction()); for (String s : cc.combinations("a")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testTail1() throws Exception { srs.addRule(new ElementSelector("*/b"), new XAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testTail3() throws Exception { srs.addRule(new ElementSelector("*/b"), new XAction()); srs.addRule(new ElementSelector("*/a/b"), new YAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof YAction)) { fail("Wrong type"); } } }
@Test public void testSuffix() throws Exception { srs.addRule(new ElementSelector("a"), new XAction()); srs.addRule(new ElementSelector("a/*"), new YAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); assertTrue(r.get(0) instanceof YAction); } }
@Test public void smokeII() throws Exception { srs.addRule(new ElementSelector("a/b"), new XAction()); srs.addRule(new ElementSelector("a/b"), new YAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(2, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } if (!(r.get(1) instanceof YAction)) { fail("Wrong type"); } } }
@Test public void testTail2() throws Exception { SimpleRuleStore srs = new SimpleRuleStore(new ContextBase()); srs.addRule(new ElementSelector("*/c"), new XAction()); for (String s : cc.combinations("a/b/c")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof XAction)) { fail("Wrong type"); } } }
@Test public void testPrefixSuffixInteraction1() throws Exception { srs.addRule(new ElementSelector("a"), new ZAction()); srs.addRule(new ElementSelector("a/*"), new YAction()); srs.addRule(new ElementSelector("*/a/b"), new XAction(3)); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); assertTrue(r.get(0) instanceof XAction); XAction xaction = (XAction) r.get(0); assertEquals(3, xaction.id); } }
@Test public void testTail4() throws Exception { srs.addRule(new ElementSelector("*/b"), new XAction()); srs.addRule(new ElementSelector("*/a/b"), new YAction()); srs.addRule(new ElementSelector("a/b"), new ZAction()); for (String s : cc.combinations("a/b")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNotNull(r); assertEquals(1, r.size()); if (!(r.get(0) instanceof ZAction)) { fail("Wrong type"); } } }
@Test public void testDeepSuffix() throws Exception { srs.addRule(new ElementSelector("a"), new XAction(1)); srs.addRule(new ElementSelector("a/b/*"), new XAction(2)); for (String s : cc.combinations("a/other")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNull(r); } }
@Test public void testPrefixSuffixInteraction2() throws Exception { srs.addRule(new ElementSelector("tG"), new XAction()); srs.addRule(new ElementSelector("tG/tS"), new YAction()); srs.addRule(new ElementSelector("tG/tS/test"), new ZAction()); srs.addRule(new ElementSelector("tG/tS/test/*"), new XAction(9)); for (String s : cc.combinations("tG/tS/toto")) { List<Action> r = srs.matchActions(new ElementPath(s)); assertNull(r); } }