Skip to content

Commit

Permalink
Fix testPatternPermutations within ByteMachingTest (#182)
Browse files Browse the repository at this point in the history
* Fix testPatternPermutations within ByteMachingTest

For each test within the class we add a list of "noMatches" objects
that should never be pattern matches intentinoally or not. However
we never check if we ever accidentally register these pattern when
adding other patterns. We will fix this by tracking when these
patterns get added (probably by error) and then later validate the
checks

Note, no impact to the correctness of the ByteMachine itself, just
that the test scaffolding was broken and needed to be fixed.

See #181 for more context.

* Remover unused import
  • Loading branch information
baldawar authored Sep 4, 2024
1 parent 96bb056 commit 3b57d21
Showing 1 changed file with 51 additions and 14 deletions.
65 changes: 51 additions & 14 deletions src/test/software/amazon/event/ruler/ByteMachineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ public void testEqualsIgnoreCasePattern() {

@Test
public void testEqualsIgnoreCasePatternWithExactMatchAsPrefix() {
String[] noMatches = new String[] { "", "ja", "JA", "JAV", "javax" };
String[] noMatches = new String[] { "", "j", "JA", "JAV", "javax" };
testPatternPermutations(noMatches,
new PatternMatch(Patterns.equalsIgnoreCaseMatch("jAVa"),
"java", "jAVa", "JavA", "JAVA"),
Expand Down Expand Up @@ -1523,7 +1523,7 @@ public void testSuffixEqualsIgnoreCaseWithPrefixMatchLeadingCharacterSameLowerAn

@Test
public void testSuffixEqualsIgnoreCaseWithWildcardMatchBeingAddedLater() {
String[] noMatches = new String[] { "", "!", "!A", "a", "A", "b", "B" };
String[] noMatches = new String[] { "", "!", "!A", "a", "A", "c", "B" };
testPatternPermutations(noMatches,
new PatternMatch(Patterns.suffixEqualsIgnoreCaseMatch("b!"),
"b!", "B!", "cdb!", "CdEB!"),
Expand All @@ -1534,7 +1534,7 @@ public void testSuffixEqualsIgnoreCaseWithWildcardMatchBeingAddedLater() {

@Test
public void testSuffixEqualsIgnoreCaseWithExistingWildcardMatch() {
String[] noMatches = new String[] { "", "!", "!A", "a", "A", "b", "B" };
String[] noMatches = new String[] { "", "!", "!A", "a", "A", "c", "B" };
testPatternPermutations(noMatches,
new PatternMatch(Patterns.wildcardMatch("*b"),
"!b", "b"),
Expand Down Expand Up @@ -1724,7 +1724,7 @@ public void testWildcardSingleWildcardCharacterWithOtherPatterns() {

@Test
public void testWildcardLeadingWildcardCharacterNotUsedByExactMatch() {
String[] noMatches = new String[] { "", "hello", "hellox", "blahabc" };
String[] noMatches = new String[] { "", "xello", "hellox", "blahabc" };
testPatternPermutations(noMatches,
new PatternMatch(Patterns.wildcardMatch("*hello"),
"hello", "xhello", "hehello"),
Expand Down Expand Up @@ -1988,7 +1988,7 @@ public void testWildcardSecondLastCharWildcardOccursBeforeSameFinalCharacterOfEx

@Test
public void testWildcardSecondLastCharWildcardOccursBeforeDivergentFinalCharacterOfExactMatch() {
String[] noMatches = new String[] { "", "hel", "hexl", "helo", "helxx" };
String[] noMatches = new String[] { "", "hel", "hexl", "xelo", "helxx" };
testPatternPermutations(noMatches,
new PatternMatch(Patterns.wildcardMatch("hel*o"),
"helo", "hello", "helxo", "helxxxo"),
Expand Down Expand Up @@ -2686,18 +2686,23 @@ private void testPatternPermutations(String[] noMatches, PatternMatch ... patter
System.out.println("USE ME TO REPRODUCE - ByteMachineTest.testPatternPermutations seeding with " + seed);
Random r = new Random(seed);
ByteMachine cut = new ByteMachine();
Set<String> matchValues = Stream.of(patternMatches)
// Tracks addition and removes across these patterns.
Match[] matchValues = Stream.of(patternMatches)
.map(patternMatch -> patternMatch.matches)
.flatMap(set -> set.stream())
.collect(Collectors.toSet());
matchValues.addAll(Arrays.asList(noMatches));
Matches matches = new Matches(matchValues.stream()
.map(string -> new Match(string))
.collect(Collectors.toList())
.toArray(new Match[0]));
.distinct()
.map(string -> new Match(string))
.collect(Collectors.toList()).toArray(new Match[0]);
// Tracks any unintentional matches for above patterns.
final Match[] noMatchValues = Stream.of(noMatches)
.map(string -> new Match(string))
.collect(Collectors.toList())
.toArray(new Match[0]);
Matches matches = new Matches(matchValues, noMatchValues);

List<PatternMatch[]> permutations = generateAllPermutations(patternMatches);


for (PatternMatch[] additionPermutation : permutations) {
// Magic number alert: For 5 or less patterns, it is reasonable to test all deletion permutations for each
// addition permutation. But for 6 or more patterns, the runtime becomes ridiculous, so we will settle for
Expand Down Expand Up @@ -2735,9 +2740,11 @@ private void testPatternPermutation(ByteMachine cut, PatternMatch[] additionPerm

private static class Matches {
private final Match[] matches;
private final Match[] noMatches;

public Matches(Match ... matches) {
public Matches(Match[] matches, Match[] noMatches) {
this.matches = matches;
this.noMatches = noMatches;
}

public Match[] get() {
Expand All @@ -2748,21 +2755,35 @@ public Patterns registerPattern(PatternMatch patternMatch) {
for (Match match : matches) {
match.registerPattern(patternMatch);
}
for (Match match : noMatches) { // record these to trigger failure below
match.registerPattern(patternMatch);
}
return patternMatch.pattern;
}

public Patterns deregisterPattern(PatternMatch patternMatch) {
for (Match match : matches) {
match.deregisterPattern(patternMatch);
}
// ignoring noMatches here intentionally
return patternMatch.pattern;
}

public void assertNoPatternsRegistered() {
for (Match match : matches) {
assertEquals(0, match.getNumPatternsRegistered());
assertEquals("Should be zero " + match, 0, match.getNumPatternsRegistered());
}
for (Match match : noMatches) {
assertEquals("Should be zero " + match, 0, match.getNumPatternsRegistered());
}
}

@Override
public String toString() {
return "Matches{" +
"matches=" + Arrays.asList(matches) +
'}';
}
}

private static class Match {
Expand All @@ -2788,6 +2809,14 @@ public void deregisterPattern(PatternMatch patternMatch) {
public int getNumPatternsRegistered() {
return num;
}

@Override
public String toString() {
return "Match{" +
"value='" + value + '\'' +
", num=" + num +
'}';
}
}

private static class PatternMatch {
Expand All @@ -2798,6 +2827,14 @@ public PatternMatch(Patterns pattern, String ... matches) {
this.pattern = pattern;
this.matches = new HashSet<>(Arrays.asList(matches));
}

@Override
public String toString() {
return "PatternMatch{" +
"pattern=" + pattern +
", matches=" + matches +
'}';
}
}

}

0 comments on commit 3b57d21

Please sign in to comment.