Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 02ec284

Browse files
committedJun 11, 2018
Add test for StartsWith
One of this test is an exhaustive comparison with the result of a reference implementation.
1 parent 5e1b377 commit 02ec284

File tree

6 files changed

+104
-3
lines changed

6 files changed

+104
-3
lines changed
 
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Must be compiled with CProverString:
2+
// javac Test.java ../cprover/CProverString.java
3+
public class Test {
4+
5+
// Reference implementation
6+
public static boolean referenceStartsWith(String s, String prefix, int offset) {
7+
if (offset < 0 || offset > s.length() - prefix.length()) {
8+
return false;
9+
}
10+
11+
for (int i = 0; i < prefix.length(); i++) {
12+
if (org.cprover.CProverString.charAt(s, offset + i)
13+
!= org.cprover.CProverString.charAt(prefix, i)) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
20+
public static boolean check(String s, String t, int offset) {
21+
// Filter out null strings
22+
if(s == null || t == null) {
23+
return false;
24+
}
25+
26+
// Act
27+
final boolean result = s.startsWith(t, offset);
28+
29+
// Assert
30+
final boolean referenceResult = referenceStartsWith(s, t, offset);
31+
assert(result == referenceResult);
32+
33+
// Check reachability
34+
assert(result == false);
35+
return result;
36+
}
37+
38+
public static boolean checkDet() {
39+
boolean result = false;
40+
result = "foo".startsWith("foo", 0);
41+
assert(result);
42+
result = "foo".startsWith("f", -1);
43+
assert(!result);
44+
result = "foo".startsWith("oo", 1);
45+
assert(result);
46+
result = "foo".startsWith("f", 1);
47+
assert(!result);
48+
result = "foo".startsWith("bar", 0);
49+
assert(!result);
50+
result = "foo".startsWith("oo", 2);
51+
assert(!result);
52+
assert(false);
53+
return result;
54+
}
55+
56+
public static boolean checkNonDet(String s) {
57+
// Filter
58+
if (s == null) {
59+
return false;
60+
}
61+
62+
// Act
63+
final boolean result = s.startsWith(s, 1);
64+
65+
// Assert
66+
assert(!result);
67+
68+
// Check reachability
69+
assert(false);
70+
return result;
71+
}
72+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
Test.class
3+
--refine-strings --string-max-input-length 10 --unwind 10 --function Test.check
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 31 .*: SUCCESS
7+
assertion at file Test.java line 34 .*: FAILURE
8+
--
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CORE
2+
Test.class
3+
--refine-strings --string-max-input-length 100 --unwind 10 --function Test.checkDet
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 41 .*: SUCCESS
7+
assertion at file Test.java line 43 .*: SUCCESS
8+
assertion at file Test.java line 45 .*: SUCCESS
9+
assertion at file Test.java line 47 .*: SUCCESS
10+
assertion at file Test.java line 49 .*: SUCCESS
11+
assertion at file Test.java line 51 .*: SUCCESS
12+
assertion at file Test.java line 52 .*: FAILURE
13+
--
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
Test.class
3+
--refine-strings --string-max-input-length 100 --unwind 10 --function Test.checkNonDet
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 66 .*: SUCCESS
7+
assertion at file Test.java line 69 .*: FAILURE
8+
--

‎src/solvers/refinement/string_constraint_generator_testing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ Author: Romain Brenguier, romain.brenguier@diffblue.com
2121
/// These axioms are:
2222
/// 1. \f$ {\tt isprefix} \Rightarrow {\tt offset_within_bounds}\f$
2323
/// 2. \f$ \forall 0 \le qvar<|{\tt prefix}|.\ {\tt isprefix}
24-
/// \Rightarrow s0[witness+{\tt offset}]=s2[witness] \f$
24+
/// \Rightarrow s0[qvar+{\tt offset}]=s2[qvar] \f$
2525
/// 3. \f$ (\lnot {\tt isprefix} \Rightarrow
2626
/// \lnot {\tt offset_within_bounds}
2727
/// \lor (0 \le witness<|{\tt prefix}|
2828
/// \land {\tt str}[witness+{\tt offset}] \ne {\tt prefix}[witness])\f$
2929
/// where \f$ {\tt offset_within_bounds} \f$ is
30-
/// \f$ offset \ge 0 \land offset < |str| \land
31-
/// |str| - |{\tt prefix}| \ge offset \f$
30+
/// \f$ offset \ge 0 \land offset \le |str| \land
31+
/// |str| - offset \ge |{\tt prefix}| \f$
3232
/// \param prefix: an array of characters
3333
/// \param str: an array of characters
3434
/// \param offset: an integer

0 commit comments

Comments
 (0)
Please sign in to comment.