11

main()使用 boost 进行测试时如何定义自己的函数?

Boost 使用的是它自己的 main 函数,但我使用的是自定义内存管理器,需要在分配任何内存之前对其进行初始化,否则会出错。

4

4 回答 4

14

我不相信你真的需要你自己的主。我认为使用global fixture你会更好:

struct AllocatorSetup {
    AllocatorSetup()   { /* setup your allocator here */ }
    ~AllocatorSetup()  { /* shutdown your allocator/check memory leaks here */ }
};

BOOST_GLOBAL_FIXTURE( AllocatorSetup );
于 2012-09-22T07:13:02.043 回答
8

你必须定义

BOOST_TEST_NO_MAIN

在提升包括之前。

BOOST_TEST_MAIN

是默认值。 http://www.boost.org/doc/libs/1_36_0/libs/test/doc/html/utf/compilation.html

于 2012-09-21T18:13:34.683 回答
0

您可以定义一个静态对象,他的构造函数将在 main 之前执行:

class Alloc_Setup {
   Alloc_Setup() {
       // Your init code
   }
   ~Alloc_Setup() {
       // Your cleanup
   }
};
Alloc_Setup setup;
int main() {} // (generated by boost)
于 2012-09-21T17:34:41.123 回答
-1

内存可以在之前分配main

static int* x = new int(1);
int main() { return *x; }

你也可以让你的内存管理器成为一个全局变量,
但是你不能强制执行全局变量初始化的特定顺序。(至少在标准 C++ 中)

在 Windows 中,您可以将内存管理器放入 DLL 中,它将在调用应用程序入口点之前被初始化,但是,其他东西可能会在之前分配内存 - 另一个 DLL 或 DLL 的 CRT。

于 2012-09-21T11:30:06.047 回答