• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Raffi Enficiaud 2014.
2 //  Distributed under the Boost Software License, Version 1.0.
3 //  (See accompanying file LICENSE_1_0.txt or copy at
4 //  http://www.boost.org/LICENSE_1_0.txt)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 
8 //[example_code
9 #define BOOST_TEST_MODULE dataset_example68
10 #include <boost/test/included/unit_test.hpp>
11 #include <boost/test/data/test_case.hpp>
12 #include <boost/test/data/monomorphic.hpp>
13 #include <sstream>
14 
15 namespace bdata = boost::unit_test::data;
16 
17 // Dataset generating a Fibonacci sequence
18 class fibonacci_dataset {
19 public:
20     // the type of the samples is deduced
21     enum { arity = 1 };
22 
23     struct iterator {
24 
iteratorfibonacci_dataset::iterator25         iterator() : a(1), b(1) {}
26 
operator *fibonacci_dataset::iterator27         int operator*() const   { return b; }
operator ++fibonacci_dataset::iterator28         void operator++()
29         {
30             a = a + b;
31             std::swap(a, b);
32         }
33     private:
34         int a;
35         int b; // b is the output
36     };
37 
fibonacci_dataset()38     fibonacci_dataset()             {}
39 
40     // size is infinite
size() const41     bdata::size_t   size() const    { return bdata::BOOST_TEST_DS_INFINITE_SIZE; }
42 
43     // iterator
begin() const44     iterator        begin() const   { return iterator(); }
45 };
46 
47 namespace boost { namespace unit_test { namespace data { namespace monomorphic {
48   // registering fibonacci_dataset as a proper dataset
49   template <>
50   struct is_dataset<fibonacci_dataset> : boost::mpl::true_ {};
51 }}}}
52 
53 // Creating a test-driven dataset, the zip is for checking
54 BOOST_DATA_TEST_CASE(
55     test1,
56     fibonacci_dataset() ^ bdata::make( { 1, 2, 3, 5, 8, 13, 21, 35, 56 } ),
57     fib_sample, exp)
58 {
59       BOOST_TEST(fib_sample == exp);
60 }
61 //]
62