Skip to content

Commit 2ef935e

Browse files
Add unit tests for java block instrumentation
This checks that coverage instrumentation assertions are added at the first instruction with given bytecode index. And that there is no such assertion in the middle of instructions with same bytecode index.
1 parent 82079d8 commit 2ef935e

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

src/goto-instrument/Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ INCLUDES= -I ..
9494

9595
LIBS =
9696

97-
CLEANFILES = goto-instrument$(EXEEXT)
97+
CLEANFILES = goto-instrument$(EXEEXT) goto-instrument$(LIBEXT)
9898

9999
include ../config.inc
100100
include ../common
101101

102-
all: goto-instrument$(EXEEXT)
102+
all: goto-instrument$(EXEEXT) goto-instrument$(LIBEXT)
103103

104104
ifneq ($(LIB_GLPK),)
105105
LIBS += $(LIB_GLPK)
@@ -111,6 +111,9 @@ endif
111111
goto-instrument$(EXEEXT): $(OBJ)
112112
$(LINKBIN)
113113

114+
goto-instrument$(LIBEXT): $(OBJ)
115+
$(LINKLIB)
116+
114117
.PHONY: goto-instrument-mac-signed
115118

116119
goto-instrument-mac-signed: goto-instrument$(EXEEXT)

unit/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ target_include_directories(unit
3636
${CBMC_SOURCE_DIR}
3737
${CMAKE_CURRENT_SOURCE_DIR}
3838
)
39-
target_link_libraries(unit testing-utils ansi-c solvers java_bytecode)
39+
target_link_libraries(
40+
unit
41+
testing-utils
42+
ansi-c
43+
solvers
44+
java_bytecode
45+
goto-programs
46+
goto-instrument-lib
47+
)
48+
4049
add_test(
4150
NAME unit
4251
COMMAND $<TARGET_FILE:unit>

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ CPROVER_LIBS =../src/java_bytecode/java_bytecode$(LIBEXT) \
6767
../src/util/util$(LIBEXT) \
6868
../src/big-int/big-int$(LIBEXT) \
6969
../src/goto-programs/goto-programs$(LIBEXT) \
70+
../src/goto-instrument/goto-instrument$(LIBEXT) \
7071
../src/pointer-analysis/pointer-analysis$(LIBEXT) \
7172
../src/langapi/langapi$(LIBEXT) \
7273
../src/assembler/assembler$(LIBEXT) \

unit/java_bytecode/java_virtual_functions/virtual_functions.cpp

+52-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include <testing-utils/catch.hpp>
1010
#include <testing-utils/load_java_class.h>
11-
#include <testing-utils/require_goto_statements.h>
1211

1312
#include <goto-programs/goto_convert_functions.h>
1413
#include <goto-programs/remove_virtual_functions.h>
1514
#include <util/config.h>
16-
17-
#include <util/std_expr.h>
15+
#include <goto-instrument/cover.h>
16+
#include <util/options.h>
17+
#include <iostream>
1818

1919
void check_function_call(
2020
const equal_exprt &eq_expr,
@@ -115,6 +115,55 @@ SCENARIO(
115115
}
116116

117117
REQUIRE(found_function);
118+
119+
WHEN("basic blocks are instrumented")
120+
{
121+
optionst options;
122+
options.set_option("cover", "location");
123+
REQUIRE_FALSE(
124+
instrument_cover_goals(
125+
options, new_table, new_goto_functions, null_output));
126+
127+
auto function = new_goto_functions.function_map.find(function_name);
128+
REQUIRE(function != new_goto_functions.function_map.end());
129+
130+
const goto_programt &goto_program = function->second.body;
131+
auto it = goto_program.instructions.begin();
132+
REQUIRE(it != goto_program.instructions.end());
133+
134+
// Skip the first instruction which is a declaration with no location
135+
++it;
136+
137+
THEN("Each instruction with a new bytecode index begins with ASSERT"
138+
" and coverage assertions are at the beginning of the block")
139+
{
140+
irep_idt current_index;
141+
142+
while(it->type != goto_program_instruction_typet::END_FUNCTION)
143+
{
144+
const source_locationt &loc = it->source_location;
145+
REQUIRE(loc != source_locationt::nil());
146+
REQUIRE(!loc.get_java_bytecode_index().empty());
147+
const auto new_index = loc.get_java_bytecode_index();
148+
149+
if(new_index != current_index)
150+
{
151+
current_index = new_index;
152+
// instruction with a new bytecode index begins with ASSERT
153+
REQUIRE(it->type == goto_program_instruction_typet::ASSERT);
154+
// the assertion corresponds to a line coverage goal
155+
REQUIRE(!loc.get_basic_block_covered_lines().empty());
156+
}
157+
else
158+
{
159+
// there is no line coverage goal in the middle of a block
160+
REQUIRE(loc.get_basic_block_covered_lines().empty());
161+
}
162+
163+
++it;
164+
}
165+
}
166+
}
118167
}
119168
}
120169
}

0 commit comments

Comments
 (0)