fix compactertest after rework
This commit is contained in:
@ -20,8 +20,8 @@ class ASTCompacterTest {
|
|||||||
|
|
||||||
private Lexer initLexer(String program) {
|
private Lexer initLexer(String program) {
|
||||||
try {
|
try {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("examplePrograms/" + program).toURI());
|
||||||
String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
final String programCode = Files.readString(path, StandardCharsets.US_ASCII);
|
||||||
return new StupsLexer(CharStreams.fromString(programCode));
|
return new StupsLexer(CharStreams.fromString(programCode));
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {
|
||||||
ignore.printStackTrace();
|
ignore.printStackTrace();
|
||||||
@ -31,56 +31,68 @@ class ASTCompacterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRemoveEpsilon() throws URISyntaxException, IOException {
|
void testDeleteChildren() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
Grammar grammar = Grammar.fromFile(path);
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||||
|
|
||||||
Lexer lex = this.initLexer("GeneralOperator.stups");
|
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
||||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
|
||||||
assertThat(ASTCompacter.removeEpsilon(tree, grammar)).isEqualTo(2);
|
final long before = tree.size();
|
||||||
|
|
||||||
|
ASTCompacter.deleteChildren(tree, grammar);
|
||||||
|
|
||||||
|
assertThat(before - tree.size()).isEqualTo(3);
|
||||||
System.out.println(tree);
|
System.out.println(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCompact() throws URISyntaxException, IOException {
|
void testPromote() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
Grammar grammar = Grammar.fromFile(path);
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||||
|
|
||||||
Lexer lex = this.initLexer("GeneralOperator.stups");
|
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
||||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
|
||||||
assertThat(ASTCompacter.compact(tree, grammar)).isEqualTo(14);
|
final long before = tree.size();
|
||||||
|
|
||||||
|
ASTCompacter.promote(tree, grammar);
|
||||||
|
|
||||||
|
assertThat(before - tree.size()).isEqualTo(14);
|
||||||
System.out.println(tree);
|
System.out.println(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRemoveNullable() throws URISyntaxException, IOException {
|
void testDeleteEmpty() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
Grammar grammar = Grammar.fromFile(path);
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||||
|
|
||||||
Lexer lex = this.initLexer("GeneralOperator.stups");
|
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
||||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
ASTCompacter.removeEpsilon(tree, grammar);
|
ASTCompacter.deleteChildren(tree, grammar);
|
||||||
|
|
||||||
assertThat(ASTCompacter.removeNullable(tree, grammar)).isEqualTo(2);
|
final long before = tree.size();
|
||||||
|
|
||||||
|
ASTCompacter.deleteIfEmpty(tree, grammar);
|
||||||
|
|
||||||
|
assertThat(before - tree.size()).isEqualTo(2);
|
||||||
System.out.println(tree);
|
System.out.println(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testClean() throws URISyntaxException, IOException {
|
void testClean() throws URISyntaxException, IOException {
|
||||||
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
final Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
|
||||||
Grammar grammar = Grammar.fromFile(path);
|
final Grammar grammar = Grammar.fromFile(path);
|
||||||
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
|
||||||
|
|
||||||
Lexer lex = this.initLexer("GeneralOperator.stups");
|
final Lexer lex = this.initLexer("GeneralOperator.stups");
|
||||||
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
final AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
|
||||||
|
|
||||||
ASTCompacter.clean(tree, grammar);
|
ASTCompacter.clean(tree, grammar);
|
||||||
|
|
||||||
assertThat(tree.size()).isEqualTo(29);
|
assertThat(tree.size()).isEqualTo(28);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user