Skip to content

Commit d3005d2

Browse files
Add unit tests for get_user_specified_clinit_body
Check that the function does what is expected for some simple cases.
1 parent 78fc2b7 commit d3005d2

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

jbmc/unit/java_bytecode/java_static_initializers/java_static_initializers.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ Author: Diffblue Ltd.
77
\*******************************************************************/
88

99
#include <java_bytecode/java_static_initializers.h>
10+
11+
#include <java_bytecode/java_bytecode_language.h>
12+
#include <java_bytecode/java_utils.h>
13+
#include <testing-utils/expr_query.h>
1014
#include <testing-utils/use_catch.h>
15+
#include <util/json.h>
1116

1217
SCENARIO("is_clinit_function", "[core][java_static_initializers]")
1318
{
@@ -52,3 +57,105 @@ SCENARIO(
5257
}
5358
}
5459
}
60+
61+
SCENARIO("get_user_specified_clinit_body", "[core][java_static_initializers]")
62+
{
63+
json_objectt static_values_json{};
64+
symbol_tablet symbol_table;
65+
const std::size_t max_user_array_length = 100;
66+
std::unordered_map<std::string, object_creation_referencet> references;
67+
std::unordered_multimap<irep_idt, symbolt> class_to_declared_symbols;
68+
69+
GIVEN(
70+
"A class which has no entry in the JSON object but a clinit defined "
71+
"in the symbol table")
72+
{
73+
symbolt clinit_symbol;
74+
clinit_symbol.name = "java::TestClass.<clinit>:()V";
75+
symbol_table.insert(clinit_symbol);
76+
77+
const code_blockt clinit_body = get_user_specified_clinit_body(
78+
"java::TestClass",
79+
static_values_json,
80+
symbol_table,
81+
{},
82+
max_user_array_length,
83+
references,
84+
class_to_declared_symbols);
85+
86+
THEN("User provided clinit body is composed of one call to clinit")
87+
{
88+
const exprt function_called =
89+
make_query(clinit_body)[0].as<code_function_callt>().get().function();
90+
REQUIRE(
91+
make_query(function_called).as<symbol_exprt>().get().get_identifier() ==
92+
clinit_symbol.name);
93+
}
94+
}
95+
GIVEN("A class which has no entry in the JSON object and no clinit defined")
96+
{
97+
const auto clinit_body = get_user_specified_clinit_body(
98+
"java::TestClass",
99+
static_values_json,
100+
symbol_table,
101+
{},
102+
max_user_array_length,
103+
references,
104+
class_to_declared_symbols);
105+
106+
THEN("User provided clinit body is empty")
107+
{
108+
REQUIRE(clinit_body == code_blockt{});
109+
}
110+
}
111+
GIVEN("A class which has a static number in the provided JSON")
112+
{
113+
static_values_json["TestClass"] = [] {
114+
json_objectt entry{};
115+
entry["field_name"] = json_numbert{"42"};
116+
return entry;
117+
}();
118+
class_to_declared_symbols.emplace("java::TestClass", [] {
119+
symbolt field_symbol;
120+
field_symbol.base_name = "field_name";
121+
field_symbol.name = "field_name_for_codet";
122+
field_symbol.type = java_int_type();
123+
field_symbol.is_static_lifetime = true;
124+
return field_symbol;
125+
}());
126+
symbol_table.insert([] {
127+
symbolt clinit_symbol;
128+
clinit_symbol.name = "java::TestClass.<clinit>:()V";
129+
set_declaring_class(clinit_symbol, "java::TestClass");
130+
return clinit_symbol;
131+
}());
132+
symbol_table.insert([] {
133+
symbolt test_class_symbol;
134+
test_class_symbol.name = "java::TestClass";
135+
test_class_symbol.type = [] {
136+
java_class_typet type;
137+
type.components().emplace_back("field_name", java_int_type());
138+
return type;
139+
}();
140+
return test_class_symbol;
141+
}());
142+
const auto clinit_body = get_user_specified_clinit_body(
143+
"java::TestClass",
144+
static_values_json,
145+
symbol_table,
146+
{},
147+
max_user_array_length,
148+
references,
149+
class_to_declared_symbols);
150+
THEN("User provided clinit body set the field to the given value")
151+
{
152+
auto assignment = make_query(clinit_body)[0].as<code_assignt>().get();
153+
REQUIRE(
154+
make_query(assignment.lhs())
155+
.as<symbol_exprt>()
156+
.get()
157+
.get_identifier() == "field_name_for_codet");
158+
REQUIRE(assignment.rhs() == from_integer(42, java_int_type()));
159+
}
160+
}
161+
}

unit/testing-utils/expr_query.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class expr_queryt
5454
T value;
5555
};
5656

57-
expr_queryt<exprt> make_query(exprt e)
57+
inline expr_queryt<exprt> make_query(exprt e)
5858
{
5959
return expr_queryt<exprt>(std::move(e));
6060
}

0 commit comments

Comments
 (0)