Skip to content

Commit baaa8d3

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 e51c34d commit baaa8d3

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
598 Bytes
Binary file not shown.

regression/jbmc-strings/StringEquals/Test.java

Lines changed: 50 additions & 0 deletions
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,51 @@ 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 (s == null)
24+
return o == null;
25+
if (! (o instanceof String))
26+
return false;
27+
28+
String s2 = (String) o;
29+
if (s.length() != s2.length())
30+
return false;
31+
32+
for (int i = 0; i < s.length(); i++) {
33+
if (CProverString.charAt(s, i) != CProverString.charAt(s2, i))
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
40+
public static boolean verifyNonNull(String s, Object o) {
41+
// Filter
42+
if (s == null || o == null)
43+
return false;
44+
45+
// Act
46+
boolean result = s.equals(o);
47+
boolean referenceResult = referenceImplementation(s, o);
48+
49+
// Assert
50+
assert result == referenceResult;
51+
52+
// Return
53+
return result;
54+
}
55+
56+
public static boolean verify(String s, Object o) {
57+
// Act
58+
boolean result = s.equals(o);
59+
boolean referenceResult = referenceImplementation(s, o);
60+
61+
// Assert
62+
assert result == referenceResult;
63+
64+
// Return
65+
return result;
66+
}
67+
1868
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
KNOWNBUG
2+
Test.class
3+
--string-max-input-length 20 --string-max-length 100 --unwind 30 --function Test.verify
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 62 .* SUCCESS
7+
--
8+
--
9+
Our current implementation does not handle the null case
10+
TG-2179
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
Test.class
3+
--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 50 .* SUCCESS
7+
--

0 commit comments

Comments
 (0)