Skip to content

Commit a997ffb

Browse files
Add verification test for String.equals
We add tests which compare the internal String.equals with a reference implementation which loops over the characters of the string.
1 parent 55d36b5 commit a997ffb

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed
572 Bytes
Binary file not shown.

regression/jbmc-strings/StringEquals/Test.java

+48
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Uses CProverString, must be compiled with ../cprover/CProverString.java
2+
import org.cprover.*;
3+
14
public class Test {
25
public static void check(int i, String s) {
36
String t = "Hello World";
@@ -15,4 +18,49 @@ else if (i==4)
1518
else if (i==5)
1619
assert(s.equals(x));
1720
}
21+
22+
public static boolean referenceImplementation(String s, Object o) {
23+
if (! (o instanceof String))
24+
return false;
25+
26+
String s2 = (String) o;
27+
if (s.length() != s2.length())
28+
return false;
29+
30+
for (int i = 0; i < s.length(); i++) {
31+
if (CProverString.charAt(s, i) != CProverString.charAt(s2, i))
32+
return false;
33+
}
34+
35+
return true;
36+
}
37+
38+
public static boolean verifyNonNull(String s, Object o) {
39+
// Filter
40+
if (s == null || o == null)
41+
return false;
42+
43+
// Act
44+
boolean result = s.equals(o);
45+
boolean referenceResult = referenceImplementation(s, o);
46+
47+
// Assert
48+
assert result == referenceResult;
49+
50+
// Return
51+
return result;
52+
}
53+
54+
public static boolean verify(String s, Object o) {
55+
// Act
56+
boolean result = s.equals(o);
57+
boolean referenceResult = referenceImplementation(s, o);
58+
59+
// Assert
60+
assert result == referenceResult;
61+
62+
// Return
63+
return result;
64+
}
65+
1866
}

regression/jbmc-strings/StringEquals/test.desc

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Test.class
33
--refine-strings --string-max-length 40 --function Test.check
44
^EXIT=10$
55
^SIGNAL=0$
6-
assertion at file Test.java line 6 .* SUCCESS
7-
assertion at file Test.java line 8 .* FAILURE
8-
assertion at file Test.java line 10 .* SUCCESS
9-
assertion at file Test.java line 12 .* FAILURE
10-
assertion at file Test.java line 14 .* SUCCESS
11-
assertion at file Test.java line 16 .* FAILURE
6+
assertion at file Test.java line 9 .* SUCCESS
7+
assertion at file Test.java line 11 .* FAILURE
8+
assertion at file Test.java line 13 .* SUCCESS
9+
assertion at file Test.java line 15 .* FAILURE
10+
assertion at file Test.java line 17 .* SUCCESS
11+
assertion at file Test.java line 19 .* FAILURE
1212
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
KNOWNBUG
2+
Test.class
3+
--refine-strings --string-max-input-length 5 --string-max-length 100 --unwind 10 --function Test.verify --java-throw-runtime-exceptions
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 60 .* SUCCESS
7+
--
8+
--
9+
null case not handled currently
10+
TG-2179
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
Test.class
3+
--refine-strings --string-max-input-length 20 --string-max-length 100 --unwind 30 --function Test.verifyNonNull
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 48 .* SUCCESS

0 commit comments

Comments
 (0)