gpt4 book ai didi

c++ - 为什么 CPPUNIT_ASSERT_MESSAGE 会导致 OpenMP 出错?

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:27 26 4
gpt4 key购买 nike

操作系统 = Ubuntu。

bjam 用法 = TRUE。

我想借助 OpenMP 优化我的单元测试系统。

bjam脚本文件:

lib my_lib
:
[ glob sources/*.cpp ]
:
<link>static
;


...

explicit my_project ;
unit-test my_project
:
[ glob UnitTests/*.cpp ]
my_lib
:
<linkflags>-fopenmp
<cflags>-fopenmp
;

我的部分代码:

   for(j = 0; j < AMOUNT; j++)
{
#pragma omp parallel for
for(i = 0; i < 0x10000; ++i)
{
...
try
{
variable1 = func1();
variable2 = func2();
//variable1 and variable 2 must be equal
CPPUNIT_ASSERT_MESSAGE("OLOLO", variable1 == variable2);

}
catch (const std::logic_error& exception)
{
std::cerr << exception.what() << std::endl;
CPPUNIT_ASSERT_MESSAGE("OLOLO", 0);
}
catch (const std::runtime_error & exception)
{
std::cerr << exception.what() << std::endl;
CPPUNIT_ASSERT_MESSAGE("OLOLO", 0);
}

}

}

当我启动我的测试系统时,它退出并出现错误:

terminate called without an active exception
Aborted

我评论 CPPUNIT_ASSERT_MESSAGE 行:

   for(j = 0; j < AMOUNT; j++)
{
#pragma omp parallel for
for(i = 0; i < 0x10000; ++i)
{
...
try
{
variable1 = func1();
variable2 = func2();
//CPPUNIT_ASSERT_MESSAGE("OLOLO", variable1 == variable2);

}
catch (const std::logic_error& exception)
{
std::cerr << exception.what() << std::endl;
//CPPUNIT_ASSERT_MESSAGE("OLOLO", 0);
}
catch (const std::runtime_error & exception)
{
std::cerr << exception.what() << std::endl;
//CPPUNIT_ASSERT_MESSAGE("OLOLO", 0);
}

}

}

它以我需要的方式工作。但是我需要 CPPUNIT_ASSERT_MESSAGE 来输出信息,以防出现错误的结果。为什么 CPPUNIT_ASSERT_MESSAGE 会导致错误,我应该怎么做才能消除这些错误。

最佳答案

CPPUNIT 通过在遇到错误时停止程序来工作。要在出现错误结果时输出信息而不是停止程序,那么您必须配置 XmlOutputter 并创建一个使用它的 TestRunner。

例如:

// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;

// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );

// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );

// Add the top suite to the test runner
CPPUNIT_NS::TextUi::TestRunner runner;
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
runner.addTest( suite );

runner.setOutputter( new CppUnit::XmlOutputter( &runner.result(),
std::cerr ) );

runner.run( testPath, false, true, true );

std::cout << "Test done" << std::endl;
exit( result.wasSuccessful() ? 0 : 1 );

这样你就有了一个输出到 xml 流而不是停止测试的测试运行器。

希望对你有帮助

关于c++ - 为什么 CPPUNIT_ASSERT_MESSAGE 会导致 OpenMP 出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10929347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com