Skip to content

Commit fde85b8

Browse files
committed
Memory analyzer unit tests: embed source files
Make the unit tests less fragile by not relying on a particular directory that they are being run from. Test files had to be loaded from a specific location; instead, place them in the source code and generate temporary files at runtime.
1 parent d83aee3 commit fde85b8

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

unit/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ file(GLOB_RECURSE testing_utils "testing-utils/*.cpp" "testing-utils/*.h")
1616
if(NOT WITH_MEMORY_ANALYZER)
1717
file(GLOB_RECURSE memory_analyzer_sources "memory-analyzer/*.cpp")
1818
list(REMOVE_ITEM sources ${memory_analyzer_sources})
19+
else()
20+
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer"
21+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer
22+
)
23+
function(make_mm_inc input output)
24+
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer/${output}"
25+
COMMAND $<TARGET_FILE:file_converter>
26+
"${CMAKE_CURRENT_SOURCE_DIR}/memory-analyzer/${input}" >
27+
"${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer/${output}"
28+
DEPENDS
29+
"${CMAKE_CURRENT_SOURCE_DIR}/memory-analyzer/${input}"
30+
"${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer"
31+
$<TARGET_FILE:file_converter>
32+
)
33+
endfunction(make_mm_inc)
34+
35+
make_mm_inc(input.txt input.inc)
36+
make_mm_inc(test.c test.inc)
37+
38+
set(generated_mm_files
39+
"${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer/input.inc"
40+
"${CMAKE_CURRENT_BINARY_DIR}/memory-analyzer/test.inc"
41+
)
42+
set_source_files_properties(
43+
"${CMAKE_CURRENT_SOURCE_DIR}/memory-analyzer/gdb_api.cpp"
44+
PROPERTIES
45+
OBJECT_DEPENDS "${generated_mm_files}"
46+
)
1947
endif()
2048

2149
list(REMOVE_ITEM sources
@@ -44,6 +72,7 @@ target_include_directories(unit
4472
${CBMC_BINARY_DIR}
4573
${CBMC_SOURCE_DIR}
4674
${CMAKE_CURRENT_SOURCE_DIR}
75+
${CMAKE_CURRENT_BINARY_DIR}
4776
${CUDD_INCLUDE}
4877
)
4978
target_link_libraries(

unit/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,16 @@ N_CATCH_TESTS = $(shell \
242242
$$(printf ' -not -name %s ' $(EXCLUDED_TESTS))) | \
243243
grep -E "(SCENARIO|TEST_CASE)" | grep -c -v '\[\.\]')
244244

245-
CLEANFILES = $(CATCH_TEST) testing-utils/testing-utils$(LIBEXT)
245+
memory-analyzer/input.inc: memory-analyzer/input.txt
246+
../src/ansi-c/file_converter$(EXEEXT) $< > $@
247+
248+
memory-analyzer/test.inc: memory-analyzer/test.c
249+
../src/ansi-c/file_converter$(EXEEXT) $< > $@
250+
251+
memory-analyzer/gdb_api$(OBJEXT): memory-analyzer/input.inc memory-analyzer/test.inc
252+
253+
CLEANFILES = $(CATCH_TEST) testing-utils/testing-utils$(LIBEXT) \
254+
memory-analyzer/input.inc memory-analyzer/test.inc
246255

247256
# only add a dependency for libraries to avoid triggering implicit rules, which
248257
# would cause unnecessary rebuilds

unit/memory-analyzer/gdb_api.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@ Author: Malte Mues <[email protected]>
1818
#include <iostream>
1919

2020
#include <memory-analyzer/gdb_api.cpp>
21+
2122
#include <util/run.h>
23+
#include <util/tempfile.h>
2224

23-
void compile_test_file()
25+
struct compile_test_filet
2426
{
25-
REQUIRE(
26-
run("gcc", {"gcc", "-g", "-o", "test", "memory-analyzer/test.c"}) == 0);
27-
}
27+
compile_test_filet() : compiled("test", "")
28+
{
29+
temporary_filet tmp("test", ".c");
30+
std::ofstream of(tmp().c_str());
31+
REQUIRE(of.is_open());
32+
33+
of <<
34+
#include <memory-analyzer/test.inc>
35+
; // NOLINT(whitespace/semicolon)
36+
of.close();
37+
38+
REQUIRE(run("gcc", {"gcc", "-g", "-o", compiled(), tmp()}) == 0);
39+
}
40+
41+
temporary_filet compiled;
42+
};
2843

2944
void check_for_gdb()
3045
{
@@ -46,10 +61,10 @@ class gdb_api_testt : public gdb_apit
4661
void gdb_api_internals_test()
4762
{
4863
check_for_gdb();
49-
compile_test_file();
64+
compile_test_filet test_file;
5065

5166
std::vector<std::string> args;
52-
args.push_back("test");
67+
args.push_back(test_file.compiled());
5368

5469
SECTION("parse gdb output record")
5570
{
@@ -67,9 +82,18 @@ void gdb_api_internals_test()
6782

6883
SECTION("read a line from an input stream")
6984
{
85+
temporary_filet tmp("input", ".txt");
86+
std::ofstream of(tmp().c_str());
87+
REQUIRE(of.is_open());
88+
89+
of <<
90+
#include <memory-analyzer/input.inc>
91+
; // NOLINT(whitespace/semicolon)
92+
of.close();
93+
7094
gdb_api_testt gdb_api(args);
7195

72-
FILE *f = fopen("memory-analyzer/input.txt", "r");
96+
FILE *f = fopen(tmp().c_str(), "r");
7397
gdb_api.response_stream = f;
7498

7599
std::string line = gdb_api.read_next_line();
@@ -79,7 +103,7 @@ void gdb_api_internals_test()
79103
REQUIRE(line == std::string(1120, 'a') + "\n");
80104

81105
line = gdb_api.read_next_line();
82-
REQUIRE(line == "xyz");
106+
REQUIRE(line == "xyz\n");
83107
}
84108

85109
SECTION("start and exit gdb")
@@ -102,10 +126,10 @@ TEST_CASE("gdb api internals test", "[core][memory-analyzer]")
102126
TEST_CASE("gdb api test", "[core][memory-analyzer]")
103127
{
104128
check_for_gdb();
105-
compile_test_file();
129+
compile_test_filet test_file;
106130

107131
std::vector<std::string> args;
108-
args.push_back("test");
132+
args.push_back(test_file.compiled());
109133

110134
{
111135
gdb_apit gdb_api(args, true);

0 commit comments

Comments
 (0)