Skip to content

Commit 76cd14d

Browse files
Unit test for gen_nondet_string_init
1 parent c71c64b commit 76cd14d

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SRC += unit_tests.cpp \
1818
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
1919
java_bytecode/java_bytecode_convert_class/convert_abstract_class.cpp \
2020
java_bytecode/java_bytecode_parse_generics/parse_generic_class.cpp \
21+
java_bytecode/java_object_factory/gen_nondet_string_init.cpp \
2122
miniBDD_new.cpp \
2223
java_bytecode/java_string_library_preprocess/convert_exprt_to_string_exprt.cpp \
2324
java_bytecode/java_utils_test.cpp \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*******************************************************************\
2+
3+
Module: Java string library preprocess.
4+
Test for converting an expression to a string expression.
5+
6+
Author: DiffBlue Limited. All rights reserved.
7+
8+
\*******************************************************************/
9+
10+
#include <testing-utils/catch.hpp>
11+
#include <util/expr.h>
12+
#include <util/std_code.h>
13+
#include <util/namespace.h>
14+
#include <java_bytecode/java_object_factory.h>
15+
#include <java_bytecode/java_bytecode_language.h>
16+
#include <java_bytecode/java_root_class.h>
17+
#include <langapi/language_util.h>
18+
#include <langapi/mode.h>
19+
#include <iostream>
20+
21+
SCENARIO(
22+
"Generate string object",
23+
"[core][java_bytecode][java_object_factor][gen_nondet_string_init]")
24+
{
25+
GIVEN("an expression, a location, and a symbol table")
26+
{
27+
source_locationt loc;
28+
symbol_tablet symbol_table;
29+
register_language(new_java_bytecode_language);
30+
31+
// Add java.lang.Object to symbol table
32+
symbolt jlo_sym;
33+
jlo_sym.name = "java::java.lang.Object";
34+
jlo_sym.type = struct_typet();
35+
jlo_sym.is_type = true;
36+
java_root_class(jlo_sym);
37+
bool failed = symbol_table.add(jlo_sym);
38+
CHECK_RETURN(!failed);
39+
40+
// Add java.lang.String to symbol table
41+
java_string_library_preprocesst preprocess;
42+
preprocess.add_string_type("java.lang.String", symbol_table);
43+
namespacet ns(symbol_table);
44+
45+
// Declare a String named arg
46+
symbol_typet java_string_type("java::java.lang.String");
47+
symbol_exprt expr("arg", java_string_type);
48+
49+
WHEN("Initialisation code for a string is generated")
50+
{
51+
const codet code =
52+
initialize_nondet_string_struct(expr, 20, loc, symbol_table);
53+
54+
THEN("Code is produced")
55+
{
56+
std::vector<std::string> code_string;
57+
58+
const std::regex spaces("\\s+");
59+
const std::regex numbers("\\$[0-9]*");
60+
for(auto op : code.operands())
61+
{
62+
const std::string line = from_expr(ns, "", op);
63+
code_string.push_back(
64+
std::regex_replace(
65+
std::regex_replace(line, spaces, " "), numbers, ""));
66+
}
67+
68+
const std::vector<std::string> reference_code = {
69+
"int tmp_object_factory;",
70+
"tmp_object_factory = NONDET(int);",
71+
"__CPROVER_assume(tmp_object_factory >= 0);",
72+
"__CPROVER_assume(tmp_object_factory <= 20);",
73+
"char nondet_infinite_array[INFINITY()];",
74+
"nondet_infinite_array = NONDET(char [INFINITY()]);",
75+
"int return_array;",
76+
"return_array = cprover_associate_array_to_pointer_func"
77+
"(nondet_infinite_array, nondet_infinite_array);",
78+
"int return_array;",
79+
"return_array = cprover_associate_length_to_array_func"
80+
"(nondet_infinite_array, tmp_object_factory);",
81+
"arg = { [email protected]={ .@class_identifier"
82+
"=\"java.lang.String\", .@lock=false },"
83+
" .length=tmp_object_factory, "
84+
".data=nondet_infinite_array };"};
85+
86+
for(std::size_t i = 0;
87+
i < code_string.size() && i < reference_code.size();
88+
++i)
89+
REQUIRE(code_string[i] == reference_code[i]);
90+
91+
REQUIRE(code_string.size() == reference_code.size());
92+
}
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)