Merge pull request #12064 from AnttiKauppila/valgrind_for_unittest

Valgrind support added for unittests
pull/12132/head
Anna Bridge 2019-12-17 16:22:45 +00:00 committed by GitHub
commit 9209b98478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 9 deletions

View File

@ -82,6 +82,10 @@ if (COVERAGE)
endif(COVERAGE) endif(COVERAGE)
if (VALGRIND)
find_program(MEMORYCHECK_COMMAND valgrind)
endif(VALGRIND)
#################### ####################
# UNIT TESTS # UNIT TESTS
#################### ####################
@ -196,6 +200,7 @@ foreach(testfile ${unittest-file-list})
if (unittest-test-sources) if (unittest-test-sources)
# Create the executable. # Create the executable.
add_executable(${TEST_SUITE_NAME} ${unittest-test-sources}) add_executable(${TEST_SUITE_NAME} ${unittest-test-sources})
target_include_directories(${TEST_SUITE_NAME} PRIVATE target_include_directories(${TEST_SUITE_NAME} PRIVATE
${unittest-includes}) ${unittest-includes})
target_compile_options(${TEST_SUITE_NAME} PRIVATE target_compile_options(${TEST_SUITE_NAME} PRIVATE

View File

@ -76,13 +76,15 @@ def _mbed_unittest_test(options, cwd, pwd):
tool.create_makefiles(path_to_src=src_path, tool.create_makefiles(path_to_src=src_path,
generator=options.cmake_generator, generator=options.cmake_generator,
coverage_output_type=options.coverage, coverage_output_type=options.coverage,
debug=options.debug_build) debug=options.debug_build,
valgrind=options.valgrind)
# Build tests # Build tests
tool.build_tests() tool.build_tests()
if options.run_only: if options.run_only:
tool.run_tests(filter_regex=options.test_regex) tool.run_tests(filter_regex=options.test_regex,
valgrind=options.valgrind)
# If code coverage generation: # If code coverage generation:
if options.coverage: if options.coverage:

View File

@ -120,6 +120,8 @@ TEST_F(SlicingBlockModuleTest, slice_in_middle)
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE)); EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
bd.read(buf, BLOCK_SIZE * 3, BLOCK_SIZE); bd.read(buf, BLOCK_SIZE * 3, BLOCK_SIZE);
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE)); EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
delete[] program;
} }
TEST_F(SlicingBlockModuleTest, slice_at_the_end) TEST_F(SlicingBlockModuleTest, slice_at_the_end)
@ -143,6 +145,8 @@ TEST_F(SlicingBlockModuleTest, slice_at_the_end)
//Verify that blocks before and after the slicing blocks are not touched //Verify that blocks before and after the slicing blocks are not touched
bd.read(buf, BLOCK_SIZE * 7, BLOCK_SIZE); bd.read(buf, BLOCK_SIZE * 7, BLOCK_SIZE);
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE)); EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
delete[] program;
} }
TEST_F(SlicingBlockModuleTest, over_write) TEST_F(SlicingBlockModuleTest, over_write)
@ -163,6 +167,8 @@ TEST_F(SlicingBlockModuleTest, over_write)
//Program a test value to address that is one pass the device size //Program a test value to address that is one pass the device size
EXPECT_EQ(slice.program(program, 2 * BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR); EXPECT_EQ(slice.program(program, 2 * BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
delete[] buf;
delete[] program; delete[] program;
} }

View File

@ -53,6 +53,13 @@ def get_cmake_tool():
return _get_program(["cmake"]) return _get_program(["cmake"])
def get_valgrind_tool():
"""
Get Valgrind program
"""
return _get_program(["valgrind"])
def get_cxx_tool(): def get_cxx_tool():
""" """
Get C++ compiler Get C++ compiler

View File

@ -103,6 +103,11 @@ def get_options_parser():
help="Build directory. Default: UNITTESTS/build/", help="Build directory. Default: UNITTESTS/build/",
dest="build") dest="build")
parser.add_argument("--valgrind",
help="Use Valgrind when running executables",
action="store_true",
dest="valgrind")
parser.add_argument("--new", parser.add_argument("--new",
action="append", action="append",
help="Source file from which to generate test files. E.g. rtos/Semaphore.cpp", help="Source file from which to generate test files. E.g. rtos/Semaphore.cpp",

View File

@ -28,7 +28,8 @@ from .utils import execute_program
from .get_tools import get_make_tool, \ from .get_tools import get_make_tool, \
get_cmake_tool, \ get_cmake_tool, \
get_cxx_tool, \ get_cxx_tool, \
get_c_tool get_c_tool, \
get_valgrind_tool
from .settings import DEFAULT_CMAKE_GENERATORS from .settings import DEFAULT_CMAKE_GENERATORS
class UnitTestTool(object): class UnitTestTool(object):
@ -59,7 +60,8 @@ class UnitTestTool(object):
path_to_src=None, path_to_src=None,
generator=None, generator=None,
coverage_output_type=None, coverage_output_type=None,
debug=False): debug=False,
valgrind=False):
""" """
Create Makefiles and prepare targets with CMake. Create Makefiles and prepare targets with CMake.
@ -94,6 +96,17 @@ class UnitTestTool(object):
if coverage_output_type: if coverage_output_type:
args.append("-DCOVERAGE:STRING=%s" % coverage_output_type) args.append("-DCOVERAGE:STRING=%s" % coverage_output_type)
if valgrind:
valgrind = get_valgrind_tool()
if valgrind is None:
logging.error(
"No Valgrind found in Path. Install all the required tools.\n")
sys.exit(1)
args.append("-DVALGRIND=1")
args.append("-DMEMORYCHECK_COMMAND_OPTIONS=\"--track-origins=yes\" \"--leak-check=full\" \"--show-reachable=yes\" \"--error-exitcode=1\"")
else:
args.append("-DVALGRIND=0")
if path_to_src is not None: if path_to_src is not None:
args.append(path_to_src) args.append(path_to_src)
@ -118,7 +131,7 @@ class UnitTestTool(object):
"Building unit tests failed.", "Building unit tests failed.",
"Unit tests built successfully.") "Unit tests built successfully.")
def run_tests(self, filter_regex=None): def run_tests(self, filter_regex=None, valgrind=False):
""" """
Run unit tests. Run unit tests.
@ -127,11 +140,16 @@ class UnitTestTool(object):
""" """
args = [self.make_program, "test"] args = [self.make_program, "test"]
if valgrind:
if filter_regex: if filter_regex:
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex) args.append("ARGS=-R %s -V -D ExperimentalMemCheck" % filter_regex)
else:
args.append("ARGS=-V -D ExperimentalMemCheck")
else: else:
args.append("ARGS=-V -D ExperimentalTest") if filter_regex:
args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex)
else:
args.append("ARGS=-V -D ExperimentalTest")
if logging.getLogger().getEffectiveLevel() == logging.DEBUG: if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
args.append("VERBOSE=1") args.append("VERBOSE=1")