Imported Upstream version 1.64.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     {                                                                          \
17         BOOST_PP_CAT(x, _type)                                                 \
18         () : ::test::registered_test_base(BOOST_PP_STRINGIZE(x))               \
19         {                                                                      \
20             ::test::test_list::add_test(this);                                 \
21         }                                                                      \
22         void run();                                                            \
23     };                                                                         \
24     BOOST_PP_CAT(x, _type) x;                                                  \
25     void BOOST_PP_CAT(x, _type)::run()
26
27 #define RUN_TESTS()                                                            \
28     int main(int, char**)                                                      \
29     {                                                                          \
30         ::test::write_compiler_info();                                         \
31         ::test::test_list::run_tests();                                        \
32         return boost::report_errors();                                         \
33     }
34
35 namespace test {
36 struct registered_test_base
37 {
38     registered_test_base* next;
39     char const* name;
40     explicit registered_test_base(char const* n) : name(n) {}
41     virtual void run() = 0;
42     virtual ~registered_test_base() {}
43 };
44
45 namespace test_list {
46 static inline registered_test_base*& first()
47 {
48     static registered_test_base* ptr = 0;
49     return ptr;
50 }
51
52 static inline registered_test_base*& last()
53 {
54     static registered_test_base* ptr = 0;
55     return ptr;
56 }
57
58 static inline void add_test(registered_test_base* test)
59 {
60     if (last()) {
61         last()->next = test;
62     } else {
63         first() = test;
64     }
65
66     last() = test;
67 }
68
69 static inline void run_tests()
70 {
71     for (registered_test_base* i = first(); i; i = i->next) {
72         std::cout << "Running " << i->name << "\n" << std::flush;
73         i->run();
74         std::cerr << std::flush;
75         std::cout << std::flush;
76     }
77 }
78 }
79
80 inline void write_compiler_info()
81 {
82 #if defined(BOOST_GCC_CXX11)
83     char const* cpp11 = "true";
84 #else
85     char const* cpp11 = "false";
86 #endif
87
88     std::cout << "Compiler: " << BOOST_COMPILER << "\n"
89               << "Library: " << BOOST_STDLIB << "\n"
90               << "C++11: " << cpp11 << "\n"
91               << "\n"
92               << std::flush;
93 }
94 }
95
96 #include <boost/preprocessor/cat.hpp>
97 #include <boost/preprocessor/seq/fold_left.hpp>
98 #include <boost/preprocessor/seq/for_each_product.hpp>
99 #include <boost/preprocessor/seq/seq.hpp>
100 #include <boost/preprocessor/seq/to_tuple.hpp>
101
102 // Run test with every combination of the parameters (a sequence of sequences)
103 #define UNORDERED_TEST(name, parameters)                                       \
104     BOOST_PP_SEQ_FOR_EACH_PRODUCT(UNORDERED_TEST_OP, ((name))((1))parameters)
105
106 #define UNORDERED_TEST_REPEAT(name, n, parameters)                             \
107     BOOST_PP_SEQ_FOR_EACH_PRODUCT(UNORDERED_TEST_OP, ((name))((n))parameters)
108
109 #define UNORDERED_TEST_OP(r, product)                                          \
110     UNORDERED_TEST_OP2(BOOST_PP_SEQ_ELEM(0, product),                          \
111         BOOST_PP_SEQ_ELEM(1, product),                                         \
112         BOOST_PP_SEQ_TAIL(BOOST_PP_SEQ_TAIL(product)))
113
114 #define UNORDERED_TEST_OP2(name, n, params)                                    \
115     UNORDERED_AUTO_TEST(                                                       \
116         BOOST_PP_SEQ_FOLD_LEFT(UNORDERED_TEST_OP_JOIN, name, params))          \
117     {                                                                          \
118         for (int i = 0; i < n; ++i)                                            \
119             name BOOST_PP_SEQ_TO_TUPLE(params);                                \
120     }
121
122 #define UNORDERED_TEST_OP_JOIN(s, state, elem)                                 \
123     BOOST_PP_CAT(state, BOOST_PP_CAT(_, elem))
124
125 #endif