-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathArrayFormulaManagerTest.java
More file actions
277 lines (241 loc) · 11.4 KB
/
ArrayFormulaManagerTest.java
File metadata and controls
277 lines (241 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2021 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.test;
import static com.google.common.truth.TruthJUnit.assume;
import static org.sosy_lab.java_smt.api.FormulaType.IntegerType;
import static org.sosy_lab.java_smt.api.FormulaType.RationalType;
import static org.sosy_lab.java_smt.api.FormulaType.StringType;
import static org.sosy_lab.java_smt.api.FormulaType.getBitvectorTypeWithSize;
import static org.sosy_lab.java_smt.api.FormulaType.getSinglePrecisionFloatingPointType;
import org.junit.Before;
import org.junit.Test;
import org.sosy_lab.java_smt.SolverContextFactory.Solvers;
import org.sosy_lab.java_smt.api.ArrayFormula;
import org.sosy_lab.java_smt.api.BitvectorFormula;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.FloatingPointFormula;
import org.sosy_lab.java_smt.api.FormulaType;
import org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType;
import org.sosy_lab.java_smt.api.FormulaType.BitvectorType;
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
import org.sosy_lab.java_smt.api.SolverException;
import org.sosy_lab.java_smt.api.StringFormula;
/** Tests Arrays for all solvers that support it. */
public class ArrayFormulaManagerTest extends SolverBasedTest0.ParameterizedSolverBasedTest0 {
@Before
public void init() {
requireArrays();
}
@Test
public void testIntIndexIntValue() throws SolverException, InterruptedException {
requireIntegers();
// (arr2 = store(arr1, 4, 2)) & !(select(arr2, 4) = 2)
ArrayFormulaType<IntegerFormula, IntegerFormula> type =
FormulaType.getArrayType(IntegerType, IntegerType);
IntegerFormula num2 = imgr.makeNumber(2);
IntegerFormula num4 = imgr.makeNumber(4);
ArrayFormula<IntegerFormula, IntegerFormula> arr1 = amgr.makeArray("arr1", type);
ArrayFormula<IntegerFormula, IntegerFormula> arr2 = amgr.makeArray("arr2", type);
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, num4, num2)),
bmgr.not(imgr.equal(num2, amgr.select(arr2, num4))));
assertThatFormula(query).isUnsatisfiable();
}
/*
* Test whether String Arrays are possible with String indexes
*/
@Test
public void testStringIndexStringValue() throws SolverException, InterruptedException {
requireStrings();
// (arr2 = store(arr1, "four", "two")) & !(select(arr2, "four") = "two")
StringFormula num2 = smgr.makeString("two");
StringFormula num4 = smgr.makeString("four");
ArrayFormula<StringFormula, StringFormula> arr1 =
amgr.makeArray("arr1", StringType, StringType);
ArrayFormula<StringFormula, StringFormula> arr2 =
amgr.makeArray("arr2", StringType, StringType);
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, num4, num2)),
bmgr.not(smgr.equal(num2, amgr.select(arr2, num4))));
assertThatFormula(query).isUnsatisfiable();
}
/*
* Test whether String Arrays with Int indexes are possible
*/
@Test
public void testIntIndexStringValue() throws SolverException, InterruptedException {
requireStrings();
// (arr2 = store(arr1, 4, "two")) & !(select(arr2, 4) = "two")
StringFormula stringTwo = smgr.makeString("two");
IntegerFormula intFour = imgr.makeNumber(4);
ArrayFormula<IntegerFormula, StringFormula> arr1 =
amgr.makeArray("arr1", IntegerType, StringType);
ArrayFormula<IntegerFormula, StringFormula> arr2 =
amgr.makeArray("arr2", IntegerType, StringType);
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, intFour, stringTwo)),
bmgr.not(smgr.equal(stringTwo, amgr.select(arr2, intFour))));
assertThatFormula(query).isUnsatisfiable();
}
/*
* Test whether String Arrays with bitvector indexes are possible
*/
@Test
public void testBvIndexStringValue() throws SolverException, InterruptedException {
requireStrings();
// (arr2 = store(arr1, 0100, "two")) & !(select(arr2, 0100) = "two")
StringFormula stringTwo = smgr.makeString("two");
BitvectorFormula bv4 = bvmgr.makeBitvector(4, 4);
ArrayFormula<BitvectorFormula, StringFormula> arr1 =
amgr.makeArray("arr1", getBitvectorTypeWithSize(4), StringType);
ArrayFormula<BitvectorFormula, StringFormula> arr2 =
amgr.makeArray("arr2", getBitvectorTypeWithSize(4), StringType);
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, bv4, stringTwo)),
bmgr.not(smgr.equal(stringTwo, amgr.select(arr2, bv4))));
assertThatFormula(query).isUnsatisfiable();
}
/*
* Test whether Bitvector Arrays are possible with bv index
*/
@Test
public void testBvIndexBvValue() throws SolverException, InterruptedException {
requireBitvectors();
// (arr2 = store(arr1, 0100, 0010)) & !(select(arr2, 0100) = 0010)
BitvectorFormula num2 = bvmgr.makeBitvector(4, 2);
BitvectorFormula num4 = bvmgr.makeBitvector(4, 4);
ArrayFormula<BitvectorFormula, BitvectorFormula> arr1 =
amgr.makeArray("arr1", getBitvectorTypeWithSize(4), getBitvectorTypeWithSize(4));
ArrayFormula<BitvectorFormula, BitvectorFormula> arr2 =
amgr.makeArray("arr2", getBitvectorTypeWithSize(4), getBitvectorTypeWithSize(4));
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, num4, num2)),
bmgr.not(bvmgr.equal(num2, amgr.select(arr2, num4))));
assertThatFormula(query).isUnsatisfiable();
}
/*
* Test whether Rational Arrays are possible with Rational index
*/
@Test
public void testRationalIndexRationalValue() throws SolverException, InterruptedException {
requireRationals();
// (arr2 = store(arr1, 4, 2)) & !(select(arr2, 4) = 2)
RationalFormula num2 = rmgr.makeNumber(2);
RationalFormula num4 = rmgr.makeNumber(4);
ArrayFormula<RationalFormula, RationalFormula> arr1 =
amgr.makeArray("arr1", RationalType, RationalType);
ArrayFormula<RationalFormula, RationalFormula> arr2 =
amgr.makeArray("arr2", RationalType, RationalType);
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, num4, num2)),
bmgr.not(rmgr.equal(num2, amgr.select(arr2, num4))));
assertThatFormula(query).isUnsatisfiable();
}
/*
* Test whether Float Arrays are possible with Float index
*/
@Test
public void testFloatIndexFloatValue() throws SolverException, InterruptedException {
requireFloats();
// (arr2 = store(arr1, 4.0, 2.0)) & !(select(arr2, 4.0) = 2.0)
FloatingPointFormula num2 = fpmgr.makeNumber(2, getSinglePrecisionFloatingPointType());
FloatingPointFormula num4 = fpmgr.makeNumber(4, getSinglePrecisionFloatingPointType());
ArrayFormula<FloatingPointFormula, FloatingPointFormula> arr1 =
amgr.makeArray(
"arr1", getSinglePrecisionFloatingPointType(), getSinglePrecisionFloatingPointType());
ArrayFormula<FloatingPointFormula, FloatingPointFormula> arr2 =
amgr.makeArray(
"arr2", getSinglePrecisionFloatingPointType(), getSinglePrecisionFloatingPointType());
BooleanFormula query =
bmgr.and(
amgr.equivalence(arr2, amgr.store(arr1, num4, num2)),
bmgr.not(fpmgr.equalWithFPSemantics(num2, amgr.select(arr2, num4))));
assertThatFormula(query).isUnsatisfiable();
}
@Test
public void testArrayConstWithDefault() throws SolverException, InterruptedException {
requireIntegers();
assume()
.withMessage("Solver %s does not yet support array initialization", solverToUse())
.that(solverToUse())
.isNotEqualTo(Solvers.OPENSMT);
for (int elem : new int[] {0, 1, 5, 100, -100}) {
IntegerFormula defaultElement = imgr.makeNumber(elem);
IntegerFormula otherElem = imgr.makeNumber(elem + 1);
ArrayFormula<IntegerFormula, IntegerFormula> arr =
amgr.makeArray(FormulaType.getArrayType(IntegerType, IntegerType), defaultElement);
for (int i : new int[] {1, 3, 9, 13}) {
IntegerFormula index = imgr.makeNumber(i);
// select(arr, i) == defaultElement, and not otherElem
assertThatFormula(imgr.equal(defaultElement, amgr.select(arr, index))).isTautological();
assertThatFormula(imgr.equal(otherElem, amgr.select(arr, index))).isUnsatisfiable();
// select(store(arr, i, j)) == j, and not defaultElement
IntegerFormula selectFromStore = amgr.select(amgr.store(arr, index, otherElem), index);
assertThatFormula(imgr.equal(otherElem, selectFromStore)).isTautological();
assertThatFormula(imgr.equal(defaultElement, selectFromStore)).isUnsatisfiable();
}
}
}
@Test
public void testArrayConstBvWithDefault() throws SolverException, InterruptedException {
requireBitvectors();
assume()
.withMessage("Solver %s does not yet support array initialization", solverToUse())
.that(solverToUse())
.isNotEqualTo(Solvers.BOOLECTOR);
final int size = 8;
for (int elem : new int[] {0, 1, 5, 100, -100}) {
BitvectorFormula defaultElement = bvmgr.makeBitvector(size, elem);
BitvectorFormula otherElem = bvmgr.makeBitvector(size, elem + 1);
BitvectorType bvType = getBitvectorTypeWithSize(size);
ArrayFormula<BitvectorFormula, BitvectorFormula> arr =
amgr.makeArray(FormulaType.getArrayType(bvType, bvType), defaultElement);
for (int i : new int[] {1, 3, 9, 13}) {
BitvectorFormula index = bvmgr.makeBitvector(size, i);
// select(arr, i) == defaultElement, and not otherElem
assertThatFormula(bvmgr.equal(defaultElement, amgr.select(arr, index))).isTautological();
assertThatFormula(bvmgr.equal(otherElem, amgr.select(arr, index))).isUnsatisfiable();
// select(store(arr, i, j)) == j, and not defaultElement
BitvectorFormula selectFromStore = amgr.select(amgr.store(arr, index, otherElem), index);
assertThatFormula(bvmgr.equal(otherElem, selectFromStore)).isTautological();
assertThatFormula(bvmgr.equal(defaultElement, selectFromStore)).isUnsatisfiable();
}
}
}
@Test
public void testArrayWithManyValues() throws SolverException, InterruptedException {
requireIntegers();
requireArrays();
// The example array formula is: for x in [1...N]: arr = store(arr, x, x)
// as SMTLIB: arr = store(store(store(... store(array, 0, 0), 1, 1), ... , N-1, N-1)
ArrayFormula<IntegerFormula, IntegerFormula> array =
amgr.makeArray("array", IntegerType, IntegerType);
ArrayFormula<IntegerFormula, IntegerFormula> storedArray = array;
final int numValues = 100;
for (int i = 0; i < numValues; i++) {
storedArray = amgr.store(storedArray, imgr.makeNumber(i), imgr.makeNumber(i));
}
// (x >= 0 && x < numValues) => (x == arr[x]) for the defined array
IntegerFormula x = imgr.makeVariable("x");
BooleanFormula indexAssignment = imgr.equal(x, amgr.select(storedArray, x)); // x == arr[x]
BooleanFormula query =
bmgr.implication(
bmgr.and(
imgr.greaterOrEquals(x, imgr.makeNumber(0)),
imgr.lessThan(x, imgr.makeNumber(numValues))),
indexAssignment);
assertThatFormula(query).isTautological();
}
}