diff --git a/UNITTESTS/CMakeLists.txt b/UNITTESTS/CMakeLists.txt index e9dfb7b..18dc8af 100644 --- a/UNITTESTS/CMakeLists.txt +++ b/UNITTESTS/CMakeLists.txt @@ -82,6 +82,10 @@ endif(COVERAGE) +if (VALGRIND) + find_program(MEMORYCHECK_COMMAND valgrind) +endif(VALGRIND) + #################### # UNIT TESTS #################### @@ -196,6 +200,7 @@ if (unittest-test-sources) # Create the executable. add_executable(${TEST_SUITE_NAME} ${unittest-test-sources}) + target_include_directories(${TEST_SUITE_NAME} PRIVATE ${unittest-includes}) target_compile_options(${TEST_SUITE_NAME} PRIVATE diff --git a/UNITTESTS/mbed_unittest.py b/UNITTESTS/mbed_unittest.py index 627c58e..7a3279f 100755 --- a/UNITTESTS/mbed_unittest.py +++ b/UNITTESTS/mbed_unittest.py @@ -76,13 +76,15 @@ tool.create_makefiles(path_to_src=src_path, generator=options.cmake_generator, coverage_output_type=options.coverage, - debug=options.debug_build) + debug=options.debug_build, + valgrind=options.valgrind) # Build tests tool.build_tests() 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 options.coverage: diff --git a/UNITTESTS/moduletests/storage/blockdevice/SlicingBlockDevice/moduletest.cpp b/UNITTESTS/moduletests/storage/blockdevice/SlicingBlockDevice/moduletest.cpp index 7a96d93..5a1f4f9 100644 --- a/UNITTESTS/moduletests/storage/blockdevice/SlicingBlockDevice/moduletest.cpp +++ b/UNITTESTS/moduletests/storage/blockdevice/SlicingBlockDevice/moduletest.cpp @@ -120,6 +120,8 @@ EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE)); bd.read(buf, BLOCK_SIZE * 3, BLOCK_SIZE); EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE)); + + delete[] program; } TEST_F(SlicingBlockModuleTest, slice_at_the_end) @@ -143,6 +145,8 @@ //Verify that blocks before and after the slicing blocks are not touched bd.read(buf, BLOCK_SIZE * 7, BLOCK_SIZE); EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE)); + + delete[] program; } TEST_F(SlicingBlockModuleTest, over_write) @@ -163,6 +167,8 @@ //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); + + delete[] buf; delete[] program; } diff --git a/UNITTESTS/unit_test/options.py b/UNITTESTS/unit_test/options.py index 211c5d1..0273d99 100644 --- a/UNITTESTS/unit_test/options.py +++ b/UNITTESTS/unit_test/options.py @@ -103,6 +103,11 @@ help="Build directory. Default: UNITTESTS/build/", dest="build") + parser.add_argument("--valgrind", + help="Use Valgrind when running executables", + action="store_true", + dest="valgrind") + parser.add_argument("--new", action="append", help="Source file from which to generate test files. E.g. rtos/Semaphore.cpp", diff --git a/UNITTESTS/unit_test/test.py b/UNITTESTS/unit_test/test.py index ac7d709..9dfb4ec 100644 --- a/UNITTESTS/unit_test/test.py +++ b/UNITTESTS/unit_test/test.py @@ -59,7 +59,8 @@ path_to_src=None, generator=None, coverage_output_type=None, - debug=False): + debug=False, + valgrind=False): """ Create Makefiles and prepare targets with CMake. @@ -94,6 +95,12 @@ if coverage_output_type: args.append("-DCOVERAGE:STRING=%s" % coverage_output_type) + if valgrind: + 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: args.append(path_to_src) @@ -118,7 +125,7 @@ "Building unit tests failed.", "Unit tests built successfully.") - def run_tests(self, filter_regex=None): + def run_tests(self, filter_regex=None, valgrind=False): """ Run unit tests. @@ -127,11 +134,16 @@ """ args = [self.make_program, "test"] - - if filter_regex: - args.append("ARGS=-R %s -V -D ExperimentalTest" % filter_regex) + if valgrind: + if filter_regex: + args.append("ARGS=-R %s -V -D ExperimentalMemCheck" % filter_regex) + else: + args.append("ARGS=-V -D ExperimentalMemCheck") 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: args.append("VERBOSE=1")