Skip to content

Commit 906bfcb

Browse files
author
Antti Kauppila
committed
Valgrind support added for unittests
Added an option to select Valgrind to be used for unittests from command line
1 parent 0c227a0 commit 906bfcb

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

UNITTESTS/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ if (COVERAGE)
8282

8383
endif(COVERAGE)
8484

85+
if (VALGRIND)
86+
find_program(MEMORYCHECK_COMMAND valgrind)
87+
endif(VALGRIND)
88+
8589
####################
8690
# UNIT TESTS
8791
####################
@@ -196,6 +200,7 @@ foreach(testfile ${unittest-file-list})
196200
if (unittest-test-sources)
197201
# Create the executable.
198202
add_executable(${TEST_SUITE_NAME} ${unittest-test-sources})
203+
199204
target_include_directories(${TEST_SUITE_NAME} PRIVATE
200205
${unittest-includes})
201206
target_compile_options(${TEST_SUITE_NAME} PRIVATE

UNITTESTS/mbed_unittest.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ def _mbed_unittest_test(options, cwd, pwd):
7676
tool.create_makefiles(path_to_src=src_path,
7777
generator=options.cmake_generator,
7878
coverage_output_type=options.coverage,
79-
debug=options.debug_build)
79+
debug=options.debug_build,
80+
valgrind=options.valgrind)
8081

8182
# Build tests
8283
tool.build_tests()
8384

8485
if options.run_only:
85-
tool.run_tests(filter_regex=options.test_regex)
86+
tool.run_tests(filter_regex=options.test_regex,
87+
valgrind=options.valgrind)
8688

8789
# If code coverage generation:
8890
if options.coverage:

UNITTESTS/moduletests/storage/blockdevice/SlicingBlockDevice/moduletest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ TEST_F(SlicingBlockModuleTest, slice_in_middle)
120120
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
121121
bd.read(buf, BLOCK_SIZE * 3, BLOCK_SIZE);
122122
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
123+
124+
delete[] program;
123125
}
124126

125127
TEST_F(SlicingBlockModuleTest, slice_at_the_end)
@@ -143,6 +145,8 @@ TEST_F(SlicingBlockModuleTest, slice_at_the_end)
143145
//Verify that blocks before and after the slicing blocks are not touched
144146
bd.read(buf, BLOCK_SIZE * 7, BLOCK_SIZE);
145147
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
148+
149+
delete[] program;
146150
}
147151

148152
TEST_F(SlicingBlockModuleTest, over_write)
@@ -163,6 +167,8 @@ TEST_F(SlicingBlockModuleTest, over_write)
163167

164168
//Program a test value to address that is one pass the device size
165169
EXPECT_EQ(slice.program(program, 2 * BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
170+
171+
delete[] buf;
166172
delete[] program;
167173
}
168174

UNITTESTS/unit_test/options.py

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ def get_options_parser():
103103
help="Build directory. Default: UNITTESTS/build/",
104104
dest="build")
105105

106+
parser.add_argument("--valgrind",
107+
help="Use Valgrind when running executables",
108+
action="store_true",
109+
dest="valgrind")
110+
106111
parser.add_argument("--new",
107112
action="append",
108113
help="Source file from which to generate test files. E.g. rtos/Semaphore.cpp",

UNITTESTS/unit_test/test.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def create_makefiles(self,
5959
path_to_src=None,
6060
generator=None,
6161
coverage_output_type=None,
62-
debug=False):
62+
debug=False,
63+
valgrind=False):
6364
"""
6465
Create Makefiles and prepare targets with CMake.
6566
@@ -94,6 +95,12 @@ def create_makefiles(self,
9495
if coverage_output_type:
9596
args.append("-DCOVERAGE:STRING=%s" % coverage_output_type)
9697

98+
if valgrind:
99+
args.append("-DVALGRIND=1")
100+
args.append("-DMEMORYCHECK_COMMAND_OPTIONS=\"--track-origins=yes\" \"--leak-check=full\" \"--show-reachable=yes\" \"--error-exitcode=1\"")
101+
else:
102+
args.append("-DVALGRIND=0")
103+
97104
if path_to_src is not None:
98105
args.append(path_to_src)
99106

@@ -118,7 +125,7 @@ def build_tests(self):
118125
"Building unit tests failed.",
119126
"Unit tests built successfully.")
120127

121-
def run_tests(self, filter_regex=None):
128+
def run_tests(self, filter_regex=None, valgrind=False):
122129
"""
123130
Run unit tests.
124131
@@ -127,11 +134,16 @@ def run_tests(self, filter_regex=None):
127134
"""
128135

129136
args = [self.make_program, "test"]
130-
131-
if filter_regex:
132-
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
137+
if valgrind:
138+
if filter_regex:
139+
args.append("ARGS=-R %s -V -D ExperimentalMemCheck" % filter_regex)
140+
else:
141+
args.append("ARGS=-V -D ExperimentalMemCheck")
133142
else:
134-
args.append("ARGS=-V -D ExperimentalTest")
143+
if filter_regex:
144+
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
145+
else:
146+
args.append("ARGS=-V -D ExperimentalTest")
135147

136148
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
137149
args.append("VERBOSE=1")

0 commit comments

Comments
 (0)