2012-06-07 173 views
1

操作系統= Ubuntu。爲什麼CPPUNIT_ASSERT_MESSAGE會導致OpenMP錯誤?

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

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流,而不是停止試驗的方式。

希望它有幫助