Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testPatternPermutations within ByteMachingTest #182

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 +
'}';
}
}

}