(qs, WIP) Fix output determinism, fix tests

This commit is contained in:
Viktor Lofgren 2024-03-28 13:11:26 +01:00
parent 0ebadd03a5
commit 6f567fbea8
2 changed files with 10 additions and 7 deletions

View File

@ -370,7 +370,9 @@ public class QWordGraph implements Iterable<QWord> {
}
}
var branches = pathsByCommonWord.entrySet().stream().map(e -> {
var branches = pathsByCommonWord.entrySet().stream()
.sorted(Map.Entry.comparingByKey(reachability.topologicalComparator()))
.map(e -> {
String commonWord = e.getKey().word();
String branchPart = new WordPaths(e.getValue()).render(reachability);
return STR."\{commonWord} \{branchPart}";
@ -382,7 +384,7 @@ public class QWordGraph implements Iterable<QWord> {
}
// Remove any double spaces that may have been introduced
return concat.toString().replaceAll("\\s+", " ");
return concat.toString().replaceAll("\\s+", " ").trim();
}

View File

@ -99,8 +99,9 @@ class QWordGraphTest {
QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("b"), "d");
assertEquals(" ^ a(b|d)c $ ", graph.compileToQuery());
assertEquals("a c ( b | d )", graph.compileToQuery());
}
@Test
void testCompile2() {
// Construct a graph like
@ -108,7 +109,7 @@ class QWordGraphTest {
// ^ - a - b - c - $
QWordGraph graph = new QWordGraph("a", "b", "c");
assertEquals(" ^ abc $ ", graph.compileToQuery());
assertEquals("a b c", graph.compileToQuery());
}
@Test
@ -119,7 +120,7 @@ class QWordGraphTest {
// \- d -/
QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("a"), "d");
assertEquals(" ^ (a|d)bc $ ", graph.compileToQuery());
assertEquals("b c ( a | d )", graph.compileToQuery());
}
@Test
@ -130,7 +131,7 @@ class QWordGraphTest {
// \- d -/
QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("c"), "d");
assertEquals(" ^ ab(c|d) $ ", graph.compileToQuery());
assertEquals("a b ( c | d )", graph.compileToQuery());
}
@Test
@ -143,6 +144,6 @@ class QWordGraphTest {
QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("c"), "d");
graph.addVariant(graph.node("b"), "e");
assertEquals(" ^ a(b|e)(c|d) $ ", graph.compileToQuery());
assertEquals("a ( b ( c | d ) | c e )", graph.compileToQuery());
}
}