(qs, WIP) Fix output determinism, fix tests

This commit is contained in:
Viktor Lofgren 2024-03-28 13:11:26 +01:00
parent f82ebd7716
commit 4cc11e183c
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 commonWord = e.getKey().word();
String branchPart = new WordPaths(e.getValue()).render(reachability); String branchPart = new WordPaths(e.getValue()).render(reachability);
return STR."\{commonWord} \{branchPart}"; return STR."\{commonWord} \{branchPart}";
@ -382,7 +384,7 @@ public class QWordGraph implements Iterable<QWord> {
} }
// Remove any double spaces that may have been introduced // 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"); QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("b"), "d"); graph.addVariant(graph.node("b"), "d");
assertEquals(" ^ a(b|d)c $ ", graph.compileToQuery()); assertEquals("a c ( b | d )", graph.compileToQuery());
} }
@Test @Test
void testCompile2() { void testCompile2() {
// Construct a graph like // Construct a graph like
@ -108,7 +109,7 @@ class QWordGraphTest {
// ^ - a - b - c - $ // ^ - a - b - c - $
QWordGraph graph = new QWordGraph("a", "b", "c"); QWordGraph graph = new QWordGraph("a", "b", "c");
assertEquals(" ^ abc $ ", graph.compileToQuery()); assertEquals("a b c", graph.compileToQuery());
} }
@Test @Test
@ -119,7 +120,7 @@ class QWordGraphTest {
// \- d -/ // \- d -/
QWordGraph graph = new QWordGraph("a", "b", "c"); QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("a"), "d"); graph.addVariant(graph.node("a"), "d");
assertEquals(" ^ (a|d)bc $ ", graph.compileToQuery()); assertEquals("b c ( a | d )", graph.compileToQuery());
} }
@Test @Test
@ -130,7 +131,7 @@ class QWordGraphTest {
// \- d -/ // \- d -/
QWordGraph graph = new QWordGraph("a", "b", "c"); QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("c"), "d"); graph.addVariant(graph.node("c"), "d");
assertEquals(" ^ ab(c|d) $ ", graph.compileToQuery()); assertEquals("a b ( c | d )", graph.compileToQuery());
} }
@Test @Test
@ -143,6 +144,6 @@ class QWordGraphTest {
QWordGraph graph = new QWordGraph("a", "b", "c"); QWordGraph graph = new QWordGraph("a", "b", "c");
graph.addVariant(graph.node("c"), "d"); graph.addVariant(graph.node("c"), "d");
graph.addVariant(graph.node("b"), "e"); graph.addVariant(graph.node("b"), "e");
assertEquals(" ^ a(b|e)(c|d) $ ", graph.compileToQuery()); assertEquals("a ( b ( c | d ) | c e )", graph.compileToQuery());
} }
} }