1

運用システム=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 がエラーを引き起こす理由と、これらのエラーを取り除くにはどうすればよいですか。

4

1 に答える 1

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ストリームに出力するテストランナーができます。

それが役に立てば幸い

于 2013-01-09T16:14:52.873 に答える