Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / unordered / test / helpers / test.hpp
1
2 // Copyright 2006-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #if !defined(BOOST_UNORDERED_TEST_TEST_HEADER)
7 #define BOOST_UNORDERED_TEST_TEST_HEADER
8
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/preprocessor/cat.hpp>
11 #include <boost/preprocessor/stringize.hpp>
12 #include <iostream>
13
14 #define UNORDERED_AUTO_TEST(x)                                              \
15     struct BOOST_PP_CAT(x, _type) : public ::test::registered_test_base {   \
16         BOOST_PP_CAT(x, _type)()                                            \
17             : ::test::registered_test_base(BOOST_PP_STRINGIZE(x))           \
18         {                                                                   \
19             ::test::test_list::add_test(this);                              \
20         }                                                                   \
21         void run();                                                         \
22     };                                                                      \
23     BOOST_PP_CAT(x, _type) x;                                               \
24     void BOOST_PP_CAT(x, _type)::run()                                      \
25
26 #define RUN_TESTS() int main(int, char**)                                   \
27     { ::test::test_list::run_tests(); return boost::report_errors(); }      \
28
29 namespace test {
30     struct registered_test_base {
31         registered_test_base* next;
32         char const* name;
33         explicit registered_test_base(char const* n) : name(n) {}
34         virtual void run() = 0;
35         virtual ~registered_test_base() {}
36     };
37
38     namespace test_list {
39         static inline registered_test_base*& first() {
40             static registered_test_base* ptr = 0;
41             return ptr;
42         }
43
44         static inline registered_test_base*& last() {
45             static registered_test_base* ptr = 0;
46             return ptr;
47         }
48
49         static inline void add_test(registered_test_base* test) {
50             if(last()) {
51                 last()->next = test;
52             }
53             else {
54                 first() = test;
55             }
56
57             last() = test;
58         }
59
60         static inline void run_tests() {
61             for(registered_test_base* i = first(); i; i = i->next) {
62                 std::cout<<"Running "<<i->name<<"\n"<<std::flush;
63                 i->run();
64                 std::cerr<<std::flush;
65                 std::cout<<std::flush;
66             }
67         }
68     }
69 }
70
71 #include <boost/preprocessor/seq/for_each_product.hpp>
72 #include <boost/preprocessor/seq/fold_left.hpp>
73 #include <boost/preprocessor/seq/to_tuple.hpp>
74 #include <boost/preprocessor/seq/seq.hpp>
75 #include <boost/preprocessor/cat.hpp>
76
77 // Run test with every combination of the parameters (a sequence of sequences)
78 #define UNORDERED_TEST(name, parameters)                                    \
79     BOOST_PP_SEQ_FOR_EACH_PRODUCT(UNORDERED_TEST_OP,                        \
80         ((name))((1)) parameters)                                           \
81
82 #define UNORDERED_TEST_REPEAT(name, n, parameters)                          \
83     BOOST_PP_SEQ_FOR_EACH_PRODUCT(UNORDERED_TEST_OP,                        \
84         ((name))((n)) parameters)                                           \
85
86 #define UNORDERED_TEST_OP(r, product)                                       \
87     UNORDERED_TEST_OP2(                                                     \
88         BOOST_PP_SEQ_ELEM(0, product),                                      \
89         BOOST_PP_SEQ_ELEM(1, product),                                      \
90         BOOST_PP_SEQ_TAIL(BOOST_PP_SEQ_TAIL(product)))                      \
91
92 #define UNORDERED_TEST_OP2(name, n, params)                                 \
93     UNORDERED_AUTO_TEST(                                                    \
94         BOOST_PP_SEQ_FOLD_LEFT(UNORDERED_TEST_OP_JOIN, name, params))       \
95     {                                                                       \
96         for (int i = 0; i < n; ++i)                                         \
97             name BOOST_PP_SEQ_TO_TUPLE(params);                             \
98     }                                                                       \
99
100 #define UNORDERED_TEST_OP_JOIN(s, state, elem)                              \
101     BOOST_PP_CAT(state, BOOST_PP_CAT(_, elem))                              \
102
103
104 #endif