Skip to content

Commit 3e4575b

Browse files
Simplify tests by putting related test inputs in the same function
1 parent d466679 commit 3e4575b

File tree

2 files changed

+128
-181
lines changed

2 files changed

+128
-181
lines changed

src/org/sosy_lab/java_smt/test/SanitizerTest.java

Lines changed: 66 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -25,126 +25,90 @@ public void init() {
2525
}
2626

2727
@Test
28-
public void validLogicTest() throws SolverException, InterruptedException {
28+
public void logicTest() throws SolverException, InterruptedException {
29+
// Valid input string that sets the logic to QF_ALL
2930
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
30-
BooleanFormula broken =
31+
BooleanFormula validLogic =
3132
mgr.parse("(set-logic QF_ALL)" + "(declare-const v Int)" + "(assert (= v 3))");
32-
assertThatFormula(expected).isEquivalentTo(broken);
33-
}
33+
assertThatFormula(expected).isEquivalentTo(validLogic);
3434

35-
@Test
36-
public void wrongLogicTest() {
37-
// When we change the code to not use sanitize() solver seem to just ignore set-logic
38-
// Except for OpenSMT, which always crashes
39-
assertThrows(
40-
IllegalArgumentException.class,
41-
() -> mgr.parse("(set-logic QF_UF)" + "(declare-const v Int)" + "(assert (= v 3))"));
42-
}
35+
// Invalid string that sets QF_UF, even though integers are needed
36+
// Most solvers seem to just ignore the logic that was chosen
37+
String wrongLogic = "(set-logic QF_UF)" + "(declare-const v Int)" + "(assert (= v 3))";
38+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(wrongLogic));
4339

44-
@Test
45-
public void logicAfterOptionTest() {
46-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
47-
assertThrows(
48-
IllegalArgumentException.class,
49-
() ->
50-
mgr.parse(
51-
"(set-option :produce-models true)"
52-
+ "(set-logic ALL)"
53-
+ "(declare-const v Int)"
54-
+ "(assert (= v 3))"));
55-
}
40+
// Try setting logic after another command was already used
41+
String logicAfterOption =
42+
"(set-option :produce-models true)"
43+
+ "(set-logic ALL)"
44+
+ "(declare-const v Int)"
45+
+ "(assert (= v 3))";
46+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(logicAfterOption));
5647

57-
@Test
58-
public void logicUsedTwiceTest() {
59-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
60-
assertThrows(
61-
IllegalArgumentException.class,
62-
() ->
63-
mgr.parse(
64-
"(set-logic ALL)"
65-
+ "(declare-const v Int)"
66-
+ "(set-logic QF_UF)"
67-
+ "(assert (= v 3))"));
48+
// Try setting the logic again after it has already been set
49+
String logicTwice =
50+
"(set-logic ALL)" + "(declare-const v Int)" + "(set-logic QF_UF)" + " (assert (= v 3))";
51+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(logicTwice));
52+
53+
// Call (reset) and *then* set the logic again
54+
String logicReset =
55+
"(set-logic QF_UF)"
56+
+ "(reset)"
57+
+ "(set-logic ALL)"
58+
+ "(declare-const v Int)"
59+
+ "(assert (= v 3))";
60+
assertThatFormula(mgr.parse(logicReset)).isEquivalentTo(expected);
6861
}
6962

7063
@Test
7164
public void exitAtTheEnd() throws SolverException, InterruptedException {
72-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
73-
BooleanFormula broken = mgr.parse("(declare-const v Int)" + "(assert (= v 3))" + "(exit)");
74-
assertThatFormula(expected).isEquivalentTo(broken);
75-
}
65+
BooleanFormula expected = mgr.parse("(declare-const v Int)" + "(assert (= v 3))");
7666

77-
@Test
78-
public void exitNotTheEnd() {
79-
assertThrows(
80-
IllegalArgumentException.class,
81-
() ->
82-
mgr.parse(
83-
"(declare-const v Int)" + "(assert (= v 3))" + "(exit)" + "(assert (= v 0))"));
84-
}
67+
// A valid input string with (exit) at the end
68+
BooleanFormula exitAtTheEnd =
69+
mgr.parse("(declare-const v Int)" + "(assert (= v 3))" + "(exit)");
70+
assertThatFormula(expected).isEquivalentTo(exitAtTheEnd);
8571

86-
@Test
87-
public void logicResetTest() throws SolverException, InterruptedException {
88-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
89-
BooleanFormula broken =
90-
mgr.parse(
91-
"(set-logic QF_UF)"
92-
+ "(reset)"
93-
+ "(set-logic ALL)"
94-
+ "(declare-const v Int)"
95-
+ "(assert (= v 3))");
96-
assertThatFormula(expected).isEquivalentTo(broken);
72+
// An invalid input string where (exit) is used before the end of the file
73+
String exitNotAtTheEnd =
74+
"(declare-const v Int)" + "(assert (= v 3))" + "(exit)" + "(assert (= v 0))";
75+
assertThrows(IllegalArgumentException.class, () -> mgr.parse(exitNotAtTheEnd));
9776
}
9877

9978
@Test
10079
public void stackPushTest() throws SolverException, InterruptedException {
10180
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
102-
BooleanFormula broken =
103-
mgr.parse(
104-
"(declare-const v Int)"
105-
+ "(push 1)"
106-
+ "(assert (= v 0))"
107-
+ "(pop 1)"
108-
+ "(assert (= v 3))");
109-
assertThatFormula(expected).isEquivalentTo(broken);
110-
}
11181

112-
@Test
113-
public void stackResetAssertionsTest() throws SolverException, InterruptedException {
114-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
115-
BooleanFormula broken =
116-
mgr.parse(
117-
"(declare-const v Int)"
118-
+ "(assert (= v 3))"
119-
+ "(reset-assertions)"
120-
+ "(declare-const v Int)"
121-
+ "(assert (= v 0))");
122-
assertThatFormula(expected).isEquivalentTo(broken);
123-
}
82+
// Push assertions and then pop the stack to remove them
83+
String stackPush =
84+
"(declare-const v Int)" + "(push 1)" + "(assert (= v 0))" + "(pop 1)" + "(assert (= v 3))";
85+
assertThatFormula(mgr.parse(stackPush)).isEquivalentTo(expected);
12486

125-
@Test
126-
public void globalStackResetAssertionsTest() throws SolverException, InterruptedException {
127-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
128-
BooleanFormula broken =
129-
mgr.parse(
130-
"(set-option :global-declarations true)"
131-
+ "(declare-const v Int)"
132-
+ "(assert (= v 3))"
133-
+ "(reset-assertions)"
134-
+ "(assert (= v 0))");
135-
assertThatFormula(expected).isEquivalentTo(broken);
136-
}
87+
// Use (reset-assertions) to remove all assertions from the stack
88+
String stackResetAssertions =
89+
"(declare-const v Int)"
90+
+ "(assert (= v 3))"
91+
+ "(reset-assertions)"
92+
+ "(declare-const v Int)"
93+
+ "(assert (= v 0))";
94+
assertThatFormula((mgr.parse(stackResetAssertions))).isEquivalentTo(expected);
13795

138-
@Test
139-
public void stackResetTest() throws SolverException, InterruptedException {
140-
BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))");
141-
BooleanFormula broken =
142-
mgr.parse(
143-
"(declare-const v Int)"
144-
+ "(assert (= v 0))"
145-
+ "(reset)"
146-
+ "(declare-const v Int)"
147-
+ "(assert (= v 3))");
148-
assertThatFormula(expected).isEquivalentTo(broken);
96+
// With :global-declarations the reset will also remove all declarations
97+
String globalStackResetAssertions =
98+
"(set-option :global-declarations true)"
99+
+ "(declare-const v Int)"
100+
+ "(assert (= v 3))"
101+
+ "(reset-assertions)"
102+
+ "(assert (= v 0))";
103+
assertThatFormula(mgr.parse(globalStackResetAssertions)).isEquivalentTo(expected);
104+
105+
// Just calling (reset) will also clear the stack
106+
String stackReset =
107+
"(declare-const v Int)"
108+
+ "(assert (= v 0))"
109+
+ "(reset)"
110+
+ "(declare-const v Int)"
111+
+ "(assert (= v 3))";
112+
assertThatFormula(mgr.parse(stackReset)).isEquivalentTo(expected);
149113
}
150114
}

src/org/sosy_lab/java_smt/test/TokenizerTest.java

Lines changed: 62 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,93 +19,76 @@
1919

2020
public class TokenizerTest {
2121
@Test
22-
public void validBrackets() {
23-
String smtlib = "(assert (= 3 (+ 2 1)))";
24-
Truth.assertThat(Tokenizer.tokenize(smtlib)).containsExactly(smtlib);
22+
public void parenthesesTest() {
23+
// Valid input string
24+
String validBrackets = "(assert (= 3 (+ 2 1)))";
25+
Truth.assertThat(Tokenizer.tokenize(validBrackets)).containsExactly(validBrackets);
26+
27+
// Same string, but with one too many closing brackets
28+
String invalidClose = "(assert (= 3 (+ 2 1))))";
29+
assertThrows(IllegalArgumentException.class, () -> Tokenizer.tokenize(invalidClose));
30+
31+
// Same string, but with a missing closing bracket
32+
String invalidOpen = "(assert (= 3 (+ 2 1))";
33+
assertThrows(IllegalArgumentException.class, () -> Tokenizer.tokenize(invalidOpen));
34+
35+
// Valid input string with an escaped ')' as part of a comment
36+
// This should not confuse the tokenizer!
37+
String inComment = "(assert (= 3;)\n(- 4 1)))";
38+
assertThat(Tokenizer.tokenize(inComment)).containsExactly("(assert (= 3\n(- 4 1)))");
39+
40+
// Valid input string with an escaped ')' as part of a string literal
41+
String inString = "(assert (= v \")\"))";
42+
assertThat(Tokenizer.tokenize(inString)).containsExactly(inString);
43+
44+
// Valid input string with an escaped ')' as part of a quoted symbol
45+
String inQuotedSymbol = "(assert (= |)v| 0))";
46+
assertThat(Tokenizer.tokenize(inQuotedSymbol)).containsExactly(inQuotedSymbol);
2547
}
2648

2749
@Test
28-
public void invalidClose() {
29-
String smtlib = "(assert (= 3 (+ 2 1))))";
30-
assertThrows(IllegalArgumentException.class, () -> Tokenizer.tokenize(smtlib));
31-
}
32-
33-
@Test
34-
public void invalidOpen() {
35-
String smtlib = "(assert (= 3 (+ 2 1))";
36-
assertThrows(IllegalArgumentException.class, () -> Tokenizer.tokenize(smtlib));
37-
}
38-
39-
@Test
40-
public void parenthesesInComment() {
41-
String smtlib = "(assert (= 3;)\n(- 4 1)))";
42-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly("(assert (= 3\n(- 4 1)))");
43-
}
44-
45-
@Test
46-
public void parenthesesInString() {
47-
String smtlib = "(assert (= v \")\"))";
48-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly(smtlib);
49-
}
50-
51-
@Test
52-
public void parenthesesInQuotedSymbol() {
53-
String smtlib = "(assert (= |)v| 0))";
54-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly(smtlib);
55-
}
56-
57-
@Test
58-
public void splitCommands() {
59-
String part1 = "(define-const v Int)";
60-
String part2 = "(assert (= v (+ 2 1)))";
61-
assertThat(Tokenizer.tokenize(part1 + part2)).containsExactly(part1, part2);
62-
}
63-
64-
@Test
65-
public void skipWhitespace() {
66-
String part1 = "(define-const \n v Int)";
67-
String part2 = "(assert \n(= v (+ 2 1)))";
68-
String smtlib = " " + part1 + " \n" + part2 + "\n";
69-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly(part1, part2);
70-
}
71-
72-
@Test
73-
public void windowsNewlines() {
74-
String smtlib = "(define-const\r\nv Int)";
75-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly(smtlib);
76-
}
77-
78-
@Test
79-
public void avoidLinewraps() {
80-
String smtlib = "(define-const;comment\nv\nInt)";
81-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly("(define-const\nv\nInt)");
82-
}
83-
84-
@Test
85-
public void newlineInString() {
86-
String smtlib = "(assert (= v \"\n\"))";
87-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly(smtlib);
88-
}
89-
90-
@Test
91-
public void newlineInQuotedSymbol() {
92-
String smtlib = "(assert (= |\n| 0))";
93-
assertThat(Tokenizer.tokenize(smtlib)).containsExactly(smtlib);
50+
public void newlineTest() {
51+
// Split string between commands
52+
String splitCmd1 = "(define-const v Int)";
53+
String splitCmd2 = "(assert (= v (+ 2 1)))";
54+
assertThat(Tokenizer.tokenize(splitCmd1 + " \n " + splitCmd2))
55+
.containsExactly(splitCmd1, splitCmd2);
56+
57+
// Ignore whitespace between commands
58+
String skipWhitespace1 = "(define-const \n v Int)";
59+
String skipWhitespace2 = "(assert \n(= v (+ 2 1)))";
60+
assertThat(Tokenizer.tokenize(" " + skipWhitespace1 + " \n" + skipWhitespace2 + "\n"))
61+
.containsExactly(skipWhitespace1, skipWhitespace2);
62+
63+
// Copy windows newlines in the commands
64+
String windowsNewlines = "(define-const\r\nv Int)";
65+
assertThat(Tokenizer.tokenize(windowsNewlines)).containsExactly(windowsNewlines);
66+
67+
// Avoid connecting tokens across line-wraps when comments are removed
68+
String avoidWrap = "(define-const;comment\nv\nInt)";
69+
assertThat(Tokenizer.tokenize(avoidWrap)).containsExactly("(define-const\nv\nInt)");
70+
71+
// Copy newline characters in strings
72+
String inString = "(assert (= v \"\n\"))";
73+
assertThat(Tokenizer.tokenize(inString)).containsExactly(inString);
74+
75+
// Copy newline characters in quoted symbols
76+
String inQuotedSymbol = "(assert (= |\n| 0))";
77+
assertThat(Tokenizer.tokenize(inQuotedSymbol)).containsExactly(inQuotedSymbol);
9478
}
9579

9680
@Test
9781
public void tokenTest() {
98-
String smtlib = "(assert\n(= v (+ 2 1)))";
99-
String token = Tokenizer.tokenize(smtlib).get(0);
100-
assertThat(token).isEqualTo(smtlib);
82+
// Tests if (assert... ) command is recognized
83+
String tokenSMTLIB = "(assert\n(= v (+ 2 1)))";
84+
String token = Tokenizer.tokenize(tokenSMTLIB).get(0);
85+
assertThat(token).isEqualTo(tokenSMTLIB);
10186
assertThat(Tokenizer.isAssertToken(token)).isTrue();
102-
}
10387

104-
@Test
105-
public void tokenTestWithString() {
106-
String smtlib = "(assert (= v \"\n\"))";
107-
String token = Tokenizer.tokenize(smtlib).get(0);
108-
assertThat(token).isEqualTo(smtlib);
109-
assertThat(Tokenizer.isAssertToken(token)).isTrue();
88+
// Test if (assert... ) command is recognized when it goes across several lines
89+
String stringTokenSMTLIB = "(assert (= v \"\n\"))";
90+
String stringToken = Tokenizer.tokenize(stringTokenSMTLIB).get(0);
91+
assertThat(stringToken).isEqualTo(stringTokenSMTLIB);
92+
assertThat(Tokenizer.isAssertToken(stringToken)).isTrue();
11093
}
11194
}

0 commit comments

Comments
 (0)