Skip to content

Commit bb96458

Browse files
author
Owen Jones
committed
Add unit test for java_replace_nondet
1 parent ea871b6 commit bb96458

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
434 Bytes
Binary file not shown.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*******************************************************************\
2+
3+
Module: Tests for the replace nondet pass to make sure it works both
4+
when remove_returns has been run before and after the call.
5+
6+
Author: Diffblue Ltd.
7+
8+
\*******************************************************************/
9+
10+
#include <testing-utils/catch.hpp>
11+
#include <testing-utils/load_java_class.h>
12+
13+
#include <goto-programs/goto_convert_functions.h>
14+
#include <goto-programs/remove_virtual_functions.h>
15+
#include <util/config.h>
16+
#include <goto-instrument/cover.h>
17+
#include <util/options.h>
18+
#include <iostream>
19+
#include <goto-programs/remove_returns.h>
20+
#include <goto-programs/replace_java_nondet.h>
21+
#include <goto-programs/remove_instanceof.h>
22+
#include <goto-programs/goto_program.h>
23+
24+
void validate_method_removal(
25+
goto_programt::instructionst instructions)
26+
{
27+
bool method_removed = true;
28+
29+
// Quick loop to check that our method call has been replaced.
30+
for(const auto &inst : instructions)
31+
{
32+
if(inst.is_function_call())
33+
{
34+
const code_function_callt &function_call =
35+
to_code_function_call(inst.code);
36+
37+
// Small check to make sure the instruction is a symbol.
38+
if(function_call.function().id() != ID_symbol)
39+
continue;
40+
41+
const irep_idt function_id =
42+
to_symbol_expr(function_call.function()).get_identifier();
43+
44+
if(
45+
function_id ==
46+
"java::org.cprover.CProver.nondetWithoutNull:()"
47+
"Ljava/lang/Object;")
48+
{
49+
method_removed = false;
50+
}
51+
}
52+
}
53+
54+
REQUIRE(method_removed);
55+
}
56+
57+
TEST_CASE(
58+
"Load class with a generated java nondet method call, run remove returns "
59+
"both before and after the nondet statements have been removed, check results "
60+
"are as expected.", "[!mayfail][core][java_bytecode][replace_nondet]")
61+
{
62+
GIVEN("A class with a call to CProver.nondetWithoutNull()")
63+
{
64+
symbol_tablet raw_symbol_table = load_java_class(
65+
"Main", "./java_bytecode/java_replace_nondet", "Main.replaceNondet");
66+
67+
journalling_symbol_tablet symbol_table =
68+
journalling_symbol_tablet::wrap(raw_symbol_table);
69+
70+
null_message_handlert null_output;
71+
goto_functionst functions;
72+
goto_convert(symbol_table, functions, null_output);
73+
74+
const std::string function_name = "java::Main.replaceNondet:()V";
75+
goto_functionst::goto_functiont &goto_function =
76+
functions.function_map.at(function_name);
77+
78+
goto_model_functiont model_function(
79+
symbol_table, functions, function_name, goto_function);
80+
81+
remove_instanceof(goto_function, symbol_table);
82+
83+
remove_virtual_functions(model_function);
84+
85+
WHEN("Remove returns is called before java nondet.")
86+
{
87+
remove_returns(model_function, [](const irep_idt &) { return false; });
88+
89+
replace_java_nondet(functions);
90+
91+
THEN("The nondet method call should have been removed.")
92+
{
93+
validate_method_removal(goto_function.body.instructions);
94+
}
95+
}
96+
97+
WHEN("Remove returns is called after java nondet.")
98+
{
99+
replace_java_nondet(functions);
100+
101+
remove_returns(model_function, [](const irep_idt &) { return false; });
102+
103+
THEN("The nondet method call should have been removed.")
104+
{
105+
validate_method_removal(goto_function.body.instructions);
106+
}
107+
}
108+
}
109+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Main {
2+
public void replaceNondet() {
3+
Object test = org.cprover.CProver.nondetWithoutNull();
4+
}
5+
}

0 commit comments

Comments
 (0)