Skip to content

Commit 9209b98

Browse files
authored
Merge pull request #12064 from AnttiKauppila/valgrind_for_unittest
Valgrind support added for unittests
2 parents 0770798 + 142581c commit 9209b98

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
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/get_tools.py

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def get_cmake_tool():
5353

5454
return _get_program(["cmake"])
5555

56+
def get_valgrind_tool():
57+
"""
58+
Get Valgrind program
59+
"""
60+
61+
return _get_program(["valgrind"])
62+
5663
def get_cxx_tool():
5764
"""
5865
Get C++ compiler

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

+25-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
from .get_tools import get_make_tool, \
2929
get_cmake_tool, \
3030
get_cxx_tool, \
31-
get_c_tool
31+
get_c_tool, \
32+
get_valgrind_tool
3233
from .settings import DEFAULT_CMAKE_GENERATORS
3334

3435
class UnitTestTool(object):
@@ -59,7 +60,8 @@ def create_makefiles(self,
5960
path_to_src=None,
6061
generator=None,
6162
coverage_output_type=None,
62-
debug=False):
63+
debug=False,
64+
valgrind=False):
6365
"""
6466
Create Makefiles and prepare targets with CMake.
6567
@@ -94,6 +96,17 @@ def create_makefiles(self,
9496
if coverage_output_type:
9597
args.append("-DCOVERAGE:STRING=%s" % coverage_output_type)
9698

99+
if valgrind:
100+
valgrind = get_valgrind_tool()
101+
if valgrind is None:
102+
logging.error(
103+
"No Valgrind found in Path. Install all the required tools.\n")
104+
sys.exit(1)
105+
args.append("-DVALGRIND=1")
106+
args.append("-DMEMORYCHECK_COMMAND_OPTIONS=\"--track-origins=yes\" \"--leak-check=full\" \"--show-reachable=yes\" \"--error-exitcode=1\"")
107+
else:
108+
args.append("-DVALGRIND=0")
109+
97110
if path_to_src is not None:
98111
args.append(path_to_src)
99112

@@ -118,7 +131,7 @@ def build_tests(self):
118131
"Building unit tests failed.",
119132
"Unit tests built successfully.")
120133

121-
def run_tests(self, filter_regex=None):
134+
def run_tests(self, filter_regex=None, valgrind=False):
122135
"""
123136
Run unit tests.
124137
@@ -127,11 +140,16 @@ def run_tests(self, filter_regex=None):
127140
"""
128141

129142
args = [self.make_program, "test"]
130-
131-
if filter_regex:
132-
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
143+
if valgrind:
144+
if filter_regex:
145+
args.append("ARGS=-R %s -V -D ExperimentalMemCheck" % filter_regex)
146+
else:
147+
args.append("ARGS=-V -D ExperimentalMemCheck")
133148
else:
134-
args.append("ARGS=-V -D ExperimentalTest")
149+
if filter_regex:
150+
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
151+
else:
152+
args.append("ARGS=-V -D ExperimentalTest")
135153

136154
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
137155
args.append("VERBOSE=1")

0 commit comments

Comments
 (0)