Skip to content

Commit f496000

Browse files
Cleaned up ParserSymbolsEscapedTest an reduced the number of test inputs
1 parent ad21694 commit f496000

File tree

2 files changed

+23
-115
lines changed

2 files changed

+23
-115
lines changed

src/org/sosy_lab/java_smt/basicimpl/FormulaCreator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,8 @@ public Object convertValue(
783783
}
784784

785785
/** Variable names (symbols) can be wrapped with "|". This function removes those chars. */
786-
protected static String dequote(String s) {
786+
@VisibleForTesting
787+
public static String dequote(String s) {
787788
int l = s.length();
788789
if (l >= 2 && s.charAt(0) == '|' && s.charAt(l - 1) == '|') {
789790
return s.substring(1, l - 1);

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

Lines changed: 21 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
import static com.google.common.truth.Truth.assertThat;
1414
import static com.google.common.truth.TruthJUnit.assume;
1515
import static org.junit.Assert.assertThrows;
16-
import static org.sosy_lab.java_smt.basicimpl.FormulaCreator.RESERVED;
16+
import static org.sosy_lab.java_smt.basicimpl.FormulaCreator.dequote;
1717

1818
import com.google.common.collect.FluentIterable;
19-
import com.google.common.collect.ImmutableList;
2019
import com.google.common.collect.ImmutableSet;
2120
import java.util.Collection;
2221
import org.junit.Before;
2322
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.Parameterized;
2425
import org.junit.runners.Parameterized.Parameter;
2526
import org.junit.runners.Parameterized.Parameters;
2627
import org.sosy_lab.java_smt.SolverContextFactory.Solvers;
2728
import org.sosy_lab.java_smt.api.BooleanFormula;
2829
import org.sosy_lab.java_smt.basicimpl.FormulaCreator;
2930

30-
public class ParserSymbolsEscapedTest extends SolverBasedTest0.ParameterizedSolverBasedTest0 {
31+
@RunWith(Parameterized.class)
32+
public class ParserSymbolsEscapedTest extends SolverBasedTest0 {
3133

3234
public static ImmutableSet<String> TEST_SYMBOLS =
3335
ImmutableSet.of(
@@ -46,110 +48,43 @@ public class ParserSymbolsEscapedTest extends SolverBasedTest0.ParameterizedSolv
4648
".8",
4749
"+34",
4850
"-32",
51+
4952
// Quoted symbols from the standard:
5053
"this is a quoted symbol",
5154
"so is\nthis one",
5255
"\" can occur too",
5356
"af klj ^*0 asfe2 (&*)&(#^ $ > > >?\" ’]]984",
57+
5458
// Some more edge cases
5559
"",
5660
" ",
61+
",",
62+
"'",
5763
"\n",
5864
"ꯍ",
5965
"#b101011",
6066
"#b2",
6167
"1",
68+
",1",
6269
"01",
6370
"1.0",
6471
"01.0",
6572
"#xA04",
6673
"#xR04",
67-
"#o70");
68-
69-
private static final ImmutableList<String> reserved =
70-
ImmutableList.of(
71-
// Reserved
74+
"#o70",
7275
"_",
73-
"!",
74-
"as",
75-
"let",
76-
"exists",
7776
"forall",
78-
"match",
79-
"par",
80-
81-
// Commands
8277
"assert",
83-
"check-sat",
84-
"check-sat-assuming",
85-
"declare-const",
86-
"declare-datatype",
87-
"declare-datatypes",
88-
"declare-fun",
89-
"declare-sort",
90-
"define-fun",
9178
"define-fun-rec",
92-
"define-funs-rec",
93-
"define-sort",
94-
"echo",
95-
"exit",
96-
"get-assertions",
97-
"get-assignment",
98-
"get-info",
99-
"get-model",
100-
"get-option",
101-
"get-proof",
102-
"get-unsat-assumptions",
103-
"get-unsat-core",
104-
"get-value",
105-
"pop",
106-
"push",
107-
"reset",
108-
"reset-assertions",
109-
"set-info",
110-
"set-logic",
111-
"set-option",
112-
113-
// Predefined symbols
114-
// Array
115-
"select",
11679
"store",
117-
// Bitvector
11880
"concat",
119-
"extract",
120-
"bvnot",
121-
"bvneg",
122-
"bvand",
123-
"bvor",
124-
"bvadd",
125-
"bvmul",
126-
"bvudiv",
127-
"bvurem",
128-
"bvshl",
129-
"bvlshr",
130-
"true",
131-
"false",
132-
"not",
133-
"=>",
134-
"and",
135-
"or",
136-
"xor",
137-
"=",
138-
"distinct",
139-
"ite"
140-
// TODO: Add more theories
141-
);
81+
"=");
14282

14383
@Parameters(name = "{0},{1}")
14484
public static Collection<Object[]> data() {
14585
ImmutableSet.Builder<Object[]> builder = ImmutableSet.builder();
14686
for (Solvers solver : Solvers.values()) {
147-
for (String symbol :
148-
FluentIterable.concat(
149-
RESERVED,
150-
TEST_SYMBOLS,
151-
VariableNamesTest.NAMES,
152-
VariableNamesTest.UNSUPPORTED_NAMES)) {
87+
for (String symbol : FluentIterable.concat(TEST_SYMBOLS)) {
15388
for (String variant :
15489
ImmutableSet.of(
15590
symbol,
@@ -179,45 +114,21 @@ public void init() {
179114
requireParser();
180115
}
181116

182-
/** Is this <code>True</code> if the symbol is a reserved keyword in the SMTLIB standard. */
183-
private static boolean isReserved(String pSymbol) {
184-
return RESERVED.contains(pSymbol);
185-
}
186-
187-
/** <code>True</code> if the symbol is a simple symbol according to the SMTLIB standard. */
188-
private static boolean isSimple(String pSymbol) {
189-
return pSymbol.matches("^[~!@$%^&*_\\-+=<>.?\\/0-9a-zA-Z]+$")
190-
&& !Character.isDigit(pSymbol.charAt(0))
191-
&& !isReserved(pSymbol);
192-
}
193-
194117
/**
195118
* <code>True</code> if the symbol is a valid symbol according to the SMTLIB standard.
196119
*
197-
* <p>Valid symbols can be either <code>simple</code> symbols or <code>quoted</code> symbols.
120+
* <p>This function is stricter than {@link FormulaCreator#isValidName(String)} and won't allow
121+
* names such as <code>"a \nb"</code> without any SMTLIB quotes.
198122
*/
199123
private static boolean isValid(String pSymbol) {
200124
if (pSymbol.length() >= 2 && pSymbol.startsWith("|") && pSymbol.endsWith("|")) {
201-
return FormulaCreator.isValidName(removeQuotes(pSymbol));
125+
return FormulaCreator.isValidName(dequote(pSymbol));
202126
} else {
203127
return pSymbol.matches("^[~!@$%^&*_\\-+=<>.?\\/0-9a-zA-Z]+$")
204128
&& FormulaCreator.isValidName(pSymbol);
205129
}
206130
}
207131

208-
/**
209-
* Remove quotes from the symbol.
210-
*
211-
* <p>If the symbol is not quoted, return it unchanged.
212-
*/
213-
private static String removeQuotes(String pSymbol) {
214-
if (pSymbol.length() >= 2 && pSymbol.startsWith("|") && pSymbol.endsWith("|")) {
215-
return pSymbol.substring(1, pSymbol.length() - 1);
216-
} else {
217-
return pSymbol;
218-
}
219-
}
220-
221132
/**
222133
* Add quotes to the symbol
223134
*
@@ -227,22 +138,16 @@ private static String addQuotes(String pSymbol) {
227138
return "|" + pSymbol + "|";
228139
}
229140

141+
/** Returns <code>True</code> if the symbol is quoted */
230142
private static boolean hasQuotes(String pSymbol) {
231143
return pSymbol.length() >= 2 && pSymbol.startsWith("|") && pSymbol.endsWith("|");
232144
}
233145

234146
/**
235-
* Return the canonical representation of the symbol.
147+
* Parse a variable definition in the SMTLIB format and return the term.
236148
*
237-
* <p>The symbol will have quotes if and only if the same symbol without quotes would not be legal
238-
* in SMTLIB.
149+
* <p>Returns a variable or a UF depending on the value of <code>asUfSymbol</code>
239150
*/
240-
private static String canonical(String pSymbol) {
241-
String noquotes = removeQuotes(pSymbol);
242-
return isSimple(noquotes) ? noquotes : addQuotes(noquotes);
243-
}
244-
245-
/** Parse a variable definition in the SMTLIB format and return the term. */
246151
private BooleanFormula parseSymbol(String pSymbol, boolean asUfSymbol) {
247152
String sort = solver == Solvers.BITWUZLA ? "(_ BitVec 8)" : "Int";
248153
String query =
@@ -254,10 +159,12 @@ private BooleanFormula parseSymbol(String pSymbol, boolean asUfSymbol) {
254159
return mgr.parse(query);
255160
}
256161

162+
/* Only consider valid SMTLIB symbols for this test */
257163
private void forValidSymbols(String pSymbol) {
258164
assume().that(isValid(pSymbol)).isTrue();
259165
}
260166

167+
/* Only consider invalid symbols. */
261168
private void forInvalidSymbols(String pSymbol) {
262169
assume().that(isValid(pSymbol)).isFalse();
263170
}

0 commit comments

Comments
 (0)