标签: cpputest

gcc/linux:CppuTest使用静态向量显示内存泄漏,误报?

在xxxx.h文件中:

struct dn_instance_pair
{
    std::string theDn;
    int theInstance;
};
typedef struct dn_instance_pair t_dn_inst_pair;

struct table_rowid_type
{
    char theTable[101];
    sqlite3_int64 theRowid;
    int operation;
};

// static class members
static vector<t_dn_inst_pair> dninstList;
static vector<t_table_rowid_type> tablerowidList;
Run Code Online (Sandbox Code Playgroud)

在xxxx.cpp中

// declaration of vectors.
// Included to this post only for completeness.
vector<t_dn_inst_pair> xxxx::dninstList;
vector<t_table_rowid_type> xxxx::tablerowidList;
Run Code Online (Sandbox Code Playgroud)

这些向量在静态回调函数中处理,因此它们也必须是静态的.

在cpputest中,当尝试在这些向量中的任何一个中添加某些内容时,会发生故障:

Leak size: 8 Allocated at: <unknown> and line: 0. Type: "new" Content: "<\ufffdP@"
Run Code Online (Sandbox Code Playgroud)

添加到向量中的东西是自动变量,它发生在正常函数中:

t_dn_inst_pair thePair;
thePair.theDn = updated_dn;
thePair.theInstance = updated_instance;
Run Code Online (Sandbox Code Playgroud)

在测试用例结束时清除向量:

xxxx::yyyy()->dninstList.clear();
Run Code Online (Sandbox Code Playgroud)

(yyyy()返回指向单例xxxx对象的指针)

页面http://blog.objectmentor.com/articles/2010/02/04/cpputest-recent-experiences 讨论了相同类型的内存泄漏: …

c++ linux gcc memory-leaks cpputest

6
推荐指数
1
解决办法
1740
查看次数

g ++ 4.7.2上的-std = c ++ 11的CppUTest错误

我一直在使用CppUTest与g ++ 4.7.2一段时间没有问题.但是,我刚刚打开-std=c++11选项,所以我可以开始使用std::unique_ptr它立即失败.

即使只是编译主模块:

#include <CppUTest/CommandLineTestRunner.h>

int main(int argc, char ** argv) {
    return CommandLineTestRunner::RunAllTests(argc, argv);
}
Run Code Online (Sandbox Code Playgroud)

失败的变化:

In file included from /usr/include/CppUTest/TestHarness.h:77:0,
                 from /usr/include/CppUTest/CommandLineTestRunner.h:31,
                 from tests/testmain.cpp:15:
/usr/include/CppUTest/MemoryLeakWarningPlugin.h:56:53: error: declaration of ‘void* operator new(size_t) throw (std::bad_alloc)’ has a different exception specifier
In file included from /usr/include/c++/4.7/ext/new_allocator.h:34:0,
                 from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34,
                 from /usr/include/c++/4.7/bits/allocator.h:48,
                 from /usr/include/c++/4.7/string:43,
                 from /usr/include/CppUTest/SimpleString.h:136,
                 from /usr/include/CppUTest/Utest.h:34,
                 from /usr/include/CppUTest/TestHarness.h:71,
                 from /usr/include/CppUTest/CommandLineTestRunner.h:31,
                 from tests/testmain.cpp:15:
/usr/include/c++/4.7/new:93:7: error: from previous declaration ‘void* operator new(std::size_t)’
Run Code Online (Sandbox Code Playgroud)

删除该-std=c++11选项使一切工作再次正常.

CppUTest文档对与重载的新运算符冲突的宏做了一些评论,并建议首先包括#including标准头,但我得到这个问题而根本不包括任何头,尽管它看起来像CppUTest/CommandLineTestRunner.h包括 …

cpputest c++11 g++-4.7

6
推荐指数
0
解决办法
738
查看次数

CppUTest单元测试框架多重定义异常

我将尝试将此作为一个纯粹的最小示例,以尽可能适用于尽可能多的人,并保护可能违反NDA的任何类型的代码共享.希望这没关系!

我使用CppUTest和CppUMock与Gitlab持续集成软件结合使用(与CMake的创建的gcc/g ++以及makefile文件编译)为未来的承诺和软件版本创建单元测试环境.但是,我遇到了一些问题.假设我有以下文件夹设置(除了/ tests文件夹的内容之外,我具有最小的更改能力):

+-- src
    +-- driver1.c
    +-- driver2.c
+-- inc
    +-- driver1.h
    +-- driver2.h
+-- tests
    +-- test_driver1.cpp
    +-- test_driver2.cpp
    +-- main.cpp
    +-- cmakelists.txt
Run Code Online (Sandbox Code Playgroud)

CMakeLists文件将包含inc文件夹的包含,src文件夹的编译以及tests文件夹的编译.但是,假设driver2.c依赖于driver1.c定义的方法.如果没有模拟设置,这很好,因为你可以正常测试对driver2方法的调用结果.但是,假设我想模拟driver1的method1函数,以便我可以检查driver2是否正确调用了method1(使用CppUMock).如果没有编译driver1,那么通常会没问题,但是在test_driver2.cpp文件中添加类似的内容:

void method1(int n) {
    mock().actualCall("method1").withParameter("n", n);
}
Run Code Online (Sandbox Code Playgroud)

将导致与driver1.c中的实际method1发生冲突,并发生链接器错误,如下所示:

CMakeFiles/Tests.dir/.../src/driver1.c:(.text+0x11d): multiple definition of 'method1'
CMakeFiles/Tests.dir/.../src/test_driver2.cpp:(.text+0x0): first defined here
Run Code Online (Sandbox Code Playgroud)

根据评论者的要求,包含结构如下:

driver1.c includes driver1.h (obviously)
driver2.c includes driver2.h (obviously)
driver2.h includes driver1.h (for calling method1)
test cpp files include their respective .h files
(test_driver1.cpp -> driver1.h and test_driver2.cpp -> driver2.h)
Run Code Online (Sandbox Code Playgroud)

method1在driver1.h中声明并在driver1.c中定义.我无法编辑这些文件.

我很乐意按要求添加详细信息.

解决这个嘲弄问题的最佳方法是什么?

c c++ unit-testing cpputest

6
推荐指数
1
解决办法
692
查看次数

如何将编译选项添加到 CMake FetchContent 依赖项?

我有一个 C/C++ 项目,我想在其中使用 CppUTest。因此,我将 CppUTest 的依赖项包含在以下内容中:

include(FetchContent)
FetchContent_Declare(
        CppUTest
        GIT_REPOSITORY      https://github.com/cpputest/cpputest.git
        GIT_TAG             latest-passing-build
        CONFIGURE_COMMAND   ""
        BUILD_COMMAND       ""
)
set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)
Run Code Online (Sandbox Code Playgroud)

这很好用。

在我的(汽车)项目中,我使用 C++20 标准,并且需要启用所有/大多数警告。但当我这样做时,CppUTest 给我带来了很多警告。因此,我想使用与项目代码不同的编译器选项来编译 CppUTest。

我如何在 CMake 中配置它?我用谷歌搜索了很多,但没有找到任何有用的东西。

谢谢,斯特凡

PS:我的简化CMake文件是:

cmake_minimum_required(VERSION 3.5)

project(projectName LANGUAGES CXX C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    MESSAGE("Setting Clang flags:")
    SET(COMPILE_FLAGS_WARNINGS "-Weverything")
    SET(COMPILE_FLAGS_DEBUG    "-Og")
    SET(COMPILE_FLAGS_RELEASE  "-O2")
    SET(COMPILE_FLAGS_COVERAGE "--coverage")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    #more options here
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL …
Run Code Online (Sandbox Code Playgroud)

c++ cmake cpputest

6
推荐指数
1
解决办法
5744
查看次数

CppUTest示例构建失败

我是mac的新手!我下载了xcode4.2并尝试构建CppUTest-v3

1)当我做的时候,我有以下日志

compiling AllTests.cpp
compiling AllocationInCppFile.cpp
compiling CheatSheetTest.cpp
compiling CommandLineArgumentsTest.cpp
compiling CommandLineTestRunnerTest.cpp
compiling JUnitOutputTest.cpp
compiling MemoryLeakDetectorTest.cpp
compiling MemoryLeakOperatorOverloadsTest.cpp
compiling MemoryLeakWarningTest.cpp
compiling NullTestTest.cpp
compiling PluginTest.cpp
compiling PreprocessorTest.cpp
compiling SetPluginTest.cpp
compiling SimpleStringTest.cpp
compiling TestFailureTest.cpp
compiling TestFilterTest.cpp
compiling TestHarness_cTest.cpp
compiling TestInstallerTest.cpp
compiling TestMemoryAllocatorTest.cpp
compiling TestOutputTest.cpp
compiling TestRegistryTest.cpp
compiling TestResultTest.cpp
compiling UtestTest.cpp
compiling AllocationInCFile.c
compiling TestHarness_cTestCFile.c
compiling CommandLineArguments.cpp
compiling CommandLineTestRunner.cpp
compiling JUnitTestOutput.cpp
compiling MemoryLeakDetector.cpp
compiling MemoryLeakWarningPlugin.cpp
compiling SimpleString.cpp
compiling TestFailure.cpp
compiling TestFilter.cpp
compiling TestHarness_c.cpp
compiling TestMemoryAllocator.cpp
compiling TestOutput.cpp
compiling TestPlugin.cpp
compiling TestRegistry.cpp
compiling TestResult.cpp …
Run Code Online (Sandbox Code Playgroud)

xcode4 cpputest

2
推荐指数
1
解决办法
1807
查看次数

在同一个编译器 (vc12) 上编译时,是什么导致了重命名的差异?

我目前正在尝试编译 CppUTest 库并将其与我的项目链接。我使用 CMake 为 CppUTest 库创建了一个 Visual Studio 2013 解决方案并构建了它。

但是,当我将创建的 CppUTest.lib 链接到我的应用程序时,我收到一个链接器错误,告诉我它找不到多个符号,例如

??0Utest@@QAE@XZ)

或者

?RunAllTests@CommandLineTestRunner@@SAHHPAPAD@Z

现在,当我在 lib 和选项 /LINKERMEMBER 上使用 dumpbin.exe 时,我会得到库中包含名称的符号列表

??0Utest@@QEAA@XZ

?RunAllTests@CommandLineTestRunner@@SAHHPEAPEAD@Z

所以实际存在的名称与我的项目期望的名称略有不同,我不知道是什么导致了这个问题。是否有任何编译选项会导致这些更改,或者我是否使用不同的编译器,尽管我认为它是相同的?

c++ linker-errors name-mangling cpputest visual-studio-2013

2
推荐指数
1
解决办法
514
查看次数