【问题标题】:When does a dataset(and samples) destruct in boost::test?boost::test 中的数据集(和样本)何时破坏?
【发布时间】:2017-05-24 06:54:47
【问题描述】:

我正在尝试学习如何使用boost::test 的数据驱动测试功能。我曾经遇到过我认为与数据集(和样本)的破坏有关的麻烦。以如下代码sn-p为例:

#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);
}

这段代码 sn-p 来自boost::test 的文档,我只在构造函数/析构函数中添加了fprintf(stderr,''')。我在我的arch linux(boost 1.63.0, gcc 6.3.1, compiler option -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. 似乎数据集在测试用例开始之前就被破坏了 运行,有意义吗?(虽然在这个sn-p中没有演示,但似乎数据样本在测试用例开始运行之前就被破坏了,有意义吗?)
  2. 我认为如果你声明一个构造函数,编译器不会为你隐式生成一个默认构造函数。如果你声明一个析构函数,编译器不会为你隐式生成复制/移动操作符/构造函数,那么“另一个”数据集是如何构造的(从输出中,有多个数据集被破坏)?李>

非常感谢您的帮助。

【问题讨论】:

    标签: c++ boost boost-test


    【解决方案1】:

    地址0x7ffd69e66e3e的第一个数据集是因为调用了它的构造函数而构造的

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

    这是您在默认构造函数中看到的唯一一个。所有其他数据集实际上都已移动。例如在operator^ 中就有这样一个举动,暗示了boost::unit_test::data::monomorphic::zip 类的构造。

    在初始化时(在输入main 之前),UTF 声明了调用make_test_case_genBOOST_DATA_TEST_CASE。这会为数据集的每个样本注册一个单元测试。单元测试直接持有样本。

    一旦注册了所有单元测试,就不再需要保存数据集了。所以在测试开始之前删除数据集是非常有意义的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      相关资源
      最近更新 更多