为什么CPPUNIT_ASSERT_MESSAGE会导致OpenMP出错?

时间:2012-06-07 09:41:57

标签: c++ openmp cppunit bjam boost.build

操作系统= Ubuntu。

bjam usage = 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导致错误,我该怎么做才能摆脱这些错误。

1 个答案:

答案 0 :(得分:1)

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流而不是停止测试。

希望有所帮助