2017-05-24 3 views
2

boost::testのデータ駆動型テスト機能を使用する方法を学習しようとしています。私がデータセット(およびサンプル)の破壊に関連して私が信じているトラブルにどのように遭遇したか。例として、次のコードスニペットを取る:データセット(およびサンプル)はいつboost :: testで破棄されますか?

#define BOOST_TEST_MODULE dataset_example68 
#include <boost/test/included/unit_test.hpp> 
#include <boost/test/data/test_case.hpp> 
#include <boost/test/data/monomorphic.hpp> 
#include <sstream> 
#include <cstdio> 

namespace bdata = boost::unit_test::data; 

// Dataset generating a Fibonacci sequence 
class fibonacci_dataset { 
public: 
    // Samples type is int 
    using sample=int; 
    enum { arity = 1 }; 

    struct iterator { 

     iterator() : a(1), b(1) {} 

     int operator*() const { return b; } 
     void operator++() 
     { 
      a = a + b; 
      std::swap(a, b); 
     } 
    private: 
     int a; 
     int b; // b is the output 
    }; 

    fibonacci_dataset() {fprintf(stderr, "constructed %p\n", (void*)this);} 
    ~fibonacci_dataset() {fprintf(stderr, "destructed %p\n", (void*)this);} 
    // size is infinite 
    bdata::size_t size() const { return bdata::BOOST_TEST_DS_INFINITE_SIZE; } 

    // iterator 
    iterator  begin() const { return iterator(); } 
}; 

namespace boost { namespace unit_test { namespace data { namespace monomorphic { 
    // registering fibonacci_dataset as a proper dataset 
    template <> 
    struct is_dataset<fibonacci_dataset> : boost::mpl::true_ {}; 
}}}} 

// Creating a test-driven dataset 
BOOST_DATA_TEST_CASE(
    test1, 
    fibonacci_dataset()^bdata::make({ 1, 2, 3, 5, 8, 13, 21, 35, 56 }), 
    fib_sample, exp) 
{ 
     BOOST_TEST(fib_sample == exp); 
} 

このコードスニペットは、boost::testのドキュメントからのものであり、私は、コンストラクタ/デストラクタでfprintf(stderr,''')を追加しました。私がコンパイルされ、私のアーチのLinux(ブースト1.63.0、GCC 6.3.1、コンパイラオプション-std = C++ 14)にそれを実行し、出力は以下の通りです:

constructed 0x7ffd69e66e3e 
destructed 0x7ffd69e66de0 
destructed 0x7ffd69e66e3d 
destructed 0x7ffd69e66e3e 
Running 9 test cases... 
4.cpp(53): error: in "test1/_7": check fib_sample == exp has failed [34 != 35] 
Failure occurred in a following context: 
    fib_sample = 34; exp = 35; 
4.cpp(53): error: in "test1/_8": check fib_sample == exp has failed [55 != 56] 
Failure occurred in a following context: 
    fib_sample = 55; exp = 56; 

*** 2 failures are detected in the test module "dataset_example68" 

私の質問は次のとおりです。

  1. テストケースが開始される前にデータセットが実行されているようですが、それは意味がありますか(このスニペットでは説明されていませんが、テストケースが実行される前にデータサンプルが破壊されるようです。 ?)
  2. コンストラクタを宣言すると、コンパイラは暗黙的にデフォルトのコンストラクタを生成しないと思います。デストラクタを宣言すると、コンパイラは暗黙的にコピー/移動演算子/コンストラクタを生成しないので、 "他の"データセットはどのように構築されますか(出力から、複数のデータセットが破棄されます)

ありがとうございました。

答えて

0

アドレス0x7ffd69e66e3eで最初のデータセットが

fibonacci_dataset()^bdata::make({ 1, 2, 3, 5, 8, 13, 21, 35, 56 }) 

に、そのコンストラクタへの呼び出しで構成され、これはあなたのデフォルトコンストラクタを見るだけです。他のすべてのデータセットは実際に移動されます。たとえばoperator^には、boost::unit_test::data::monomorphic::zipクラスの構築を意味するような動きがあります。

初期化時(mainに入る前)、UTFはBOOST_DATA_TEST_CASEmake_test_case_gen)を宣言します。これにより、データセットの各サンプルに対して単体テストが登録されます。単体テストはサンプルを直接保持します。

ユニットテストがすべて登録されたら、データセットを保持する必要はもうありません。したがって、テストが始まる前にデータセットを削除することは理にかなっています。

関連する問題