1 | #define BOOST_TEST_MODULE dataset_example68
|
---|
2 | #include <boost/test/included/unit_test.hpp>
|
---|
3 | #include <boost/test/data/test_case.hpp>
|
---|
4 | #include <boost/test/data/monomorphic.hpp>
|
---|
5 | #include <sstream>
|
---|
6 |
|
---|
7 | namespace bdata = boost::unit_test::data;
|
---|
8 |
|
---|
9 | // Dataset generating a Fibonacci sequence
|
---|
10 | class fibonacci_dataset {
|
---|
11 | public:
|
---|
12 | // Samples type is int
|
---|
13 | using sample=int;
|
---|
14 | enum { arity = 1 };
|
---|
15 |
|
---|
16 | struct iterator {
|
---|
17 |
|
---|
18 | iterator() : a(1), b(1) {}
|
---|
19 |
|
---|
20 | int operator*() const { return b; }
|
---|
21 | void operator++()
|
---|
22 | {
|
---|
23 | a = a + b;
|
---|
24 | std::swap(a, b);
|
---|
25 | }
|
---|
26 | private:
|
---|
27 | int a;
|
---|
28 | int b; // b is the output
|
---|
29 | };
|
---|
30 |
|
---|
31 | fibonacci_dataset() {}
|
---|
32 |
|
---|
33 | // size is infinite
|
---|
34 | bdata::size_t size() const { return bdata::BOOST_TEST_DS_INFINITE_SIZE; }
|
---|
35 |
|
---|
36 | // iterator
|
---|
37 | iterator begin() const { return iterator(); }
|
---|
38 | };
|
---|
39 |
|
---|
40 | namespace boost { namespace unit_test { namespace data { namespace monomorphic {
|
---|
41 | // registering fibonacci_dataset as a proper dataset
|
---|
42 | template <>
|
---|
43 | struct is_dataset<fibonacci_dataset> : boost::mpl::true_ {};
|
---|
44 | }}}}
|
---|
45 |
|
---|
46 | // Creating a test-driven dataset
|
---|
47 | // This does compile because the operator^ due to the second operand being in the correct namespace
|
---|
48 | BOOST_DATA_TEST_CASE(
|
---|
49 | test1,
|
---|
50 | fibonacci_dataset() ^ bdata::make( { 1, 2, 3, 5, 8, 13, 21, 35, 56 } ),
|
---|
51 | fib_sample, exp)
|
---|
52 | {
|
---|
53 | BOOST_TEST(fib_sample == exp);
|
---|
54 | }
|
---|
55 |
|
---|
56 | // This does not compile because none of the operands is in the same namespace as the operator^
|
---|
57 | // It can be made working by declaring the fibonacci dataset within
|
---|
58 | // namespace boost { namespace unit_test { namespace data { namespace monomorphic {
|
---|
59 | // However, that does not sound like a good solution.
|
---|
60 | BOOST_DATA_TEST_CASE(
|
---|
61 | test2,
|
---|
62 | fibonacci_dataset() ^ fibonacci_dataset(),
|
---|
63 | fib_sample, exp)
|
---|
64 | {
|
---|
65 | BOOST_TEST(fib_sample == exp);
|
---|
66 | }
|
---|