17
17
#include < util/namespace.h>
18
18
#include < langapi/mode.h>
19
19
20
- exprt convert_exprt_to_string_exprt_unit_test (
20
+ refined_string_exprt convert_exprt_to_string_exprt_unit_test (
21
21
java_string_library_preprocesst &preprocess,
22
22
const exprt &deref,
23
23
const source_locationt &loc,
@@ -30,30 +30,57 @@ exprt convert_exprt_to_string_exprt_unit_test(
30
30
31
31
TEST_CASE (" Convert exprt to string exprt" )
32
32
{
33
- source_locationt loc;
34
- symbol_tablet symbol_table;
35
- namespacet ns (symbol_table);
36
- code_blockt code;
37
- java_string_library_preprocesst preprocess;
38
- preprocess.add_string_type (" java.lang.String" , symbol_table);
39
- symbol_typet java_string_type (" java::java.lang.String" );
40
- symbol_exprt expr (" a" , pointer_type (java_string_type));
41
- convert_exprt_to_string_exprt_unit_test (
42
- preprocess, expr, loc, symbol_table, code);
43
- register_language (new_java_bytecode_language);
44
-
45
- std::vector<std::string> code_string;
46
- for (auto op : code.operands ())
47
- code_string.push_back (from_expr (ns, " " , op));
48
-
49
- REQUIRE (code_string.size ()==7 );
50
- REQUIRE (code_string[0 ]==" int cprover_string_length$1;" );
51
- REQUIRE (code_string[1 ]==" char cprover_string_array$2[INFINITY()];" );
52
- REQUIRE (code_string[2 ]==" cprover_string_length$1 = a->length;" );
53
- REQUIRE (code_string[3 ]==" __CPROVER_assume(!(a->data == null));" );
54
- REQUIRE (code_string[4 ]==" cprover_string_array$2 = *a->data;" );
55
- REQUIRE (code_string[5 ]==" struct __CPROVER_refined_string_type { int length; "
56
- " char content[INFINITY()]; } cprover_string$3;" );
57
- REQUIRE (code_string[6 ]==" cprover_string$3 = { .=cprover_string_length$1, "
58
- " .=cprover_string_array$2 };" );
33
+ GIVEN (" A location, a string expression, and a symbol table" )
34
+ {
35
+ source_locationt loc;
36
+ symbol_tablet symbol_table;
37
+ namespacet ns (symbol_table);
38
+ code_blockt code;
39
+ java_string_library_preprocesst preprocess;
40
+ preprocess.add_string_type (" java.lang.String" , symbol_table);
41
+ symbol_typet java_string_type (" java::java.lang.String" );
42
+ symbol_exprt expr (" a" , pointer_type (java_string_type));
43
+
44
+ WHEN (" String expression is converted to refined string expression" )
45
+ {
46
+ refined_string_exprt string_expr =
47
+ convert_exprt_to_string_exprt_unit_test (
48
+ preprocess, expr, loc, symbol_table, code);
49
+
50
+ THEN (" The type of the returd expression is that of refined strings" )
51
+ {
52
+ REQUIRE (string_expr.id () == ID_struct);
53
+ REQUIRE (is_refined_string_type (string_expr.type ()));
54
+ }
55
+
56
+ THEN (" Code is produced" )
57
+ {
58
+ register_language (new_java_bytecode_language);
59
+
60
+ std::vector<std::string> code_string;
61
+ const std::regex spaces (" \\ s+" );
62
+ const std::regex numbers (" \\ $[0-9]*" );
63
+ for (auto op : code.operands ())
64
+ {
65
+ const std::string line = from_expr (ns, " " , op);
66
+ code_string.push_back (
67
+ std::regex_replace (
68
+ std::regex_replace (line, spaces, " " ), numbers, " " ));
69
+ }
70
+
71
+ const std::vector<std::string> reference_code = {
72
+ " char *cprover_string_content;" ,
73
+ " int cprover_string_length;" ,
74
+ " cprover_string_length = a->length;" ,
75
+ " cprover_string_content = a->data;" };
76
+
77
+ for (std::size_t i = 0 ;
78
+ i < code_string.size () && i < reference_code.size ();
79
+ ++i)
80
+ REQUIRE (code_string[i] == reference_code[i]);
81
+
82
+ REQUIRE (code_string.size () == reference_code.size ());
83
+ }
84
+ }
85
+ }
59
86
}
0 commit comments