| 1 | // Boost.TypeErasure library |
| 2 | // |
| 3 | // Copyright 2011 Steven Watanabe |
| 4 | // |
| 5 | // Distributed under the Boost Software License Version 1.0. (See |
| 6 | // accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // $Id$ |
| 10 | |
| 11 | #include <boost/type_erasure/any.hpp> |
| 12 | #include <boost/type_erasure/builtin.hpp> |
| 13 | #include <boost/type_erasure/operators.hpp> |
| 14 | #include <boost/type_erasure/any_cast.hpp> |
| 15 | #include <boost/type_erasure/tuple.hpp> |
| 16 | #include <boost/mpl/vector.hpp> |
| 17 | |
| 18 | #define BOOST_TEST_MAIN |
| 19 | #include <boost/test/unit_test.hpp> |
| 20 | |
| 21 | using namespace boost::type_erasure; |
| 22 | |
| 23 | BOOST_AUTO_TEST_CASE(test_basic) |
| 24 | { |
| 25 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<> > test_concept; |
| 26 | any<test_concept> x(1); |
| 27 | any<test_concept> y(2); |
| 28 | |
| 29 | BOOST_CHECK((x < y)); |
| 30 | BOOST_CHECK(!(y < x)); |
| 31 | BOOST_CHECK(!(x < x)); |
| 32 | |
| 33 | BOOST_CHECK(!(x > y)); |
| 34 | BOOST_CHECK((y > x)); |
| 35 | BOOST_CHECK(!(x > x)); |
| 36 | |
| 37 | BOOST_CHECK((x <= y)); |
| 38 | BOOST_CHECK(!(y <= x)); |
| 39 | BOOST_CHECK((x <= x)); |
| 40 | |
| 41 | BOOST_CHECK(!(x >= y)); |
| 42 | BOOST_CHECK((y >= x)); |
| 43 | BOOST_CHECK((x >= x)); |
| 44 | } |
| 45 | |
| 46 | BOOST_AUTO_TEST_CASE(test_mixed_less) |
| 47 | { |
| 48 | typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, less_than_comparable<_a, _b> > test_concept; |
| 49 | tuple<test_concept, _a, _b> t(1, 2.0); |
| 50 | any<test_concept, _a> x(get<0>(t)); |
| 51 | any<test_concept, _b> y(get<1>(t)); |
| 52 | |
| 53 | BOOST_CHECK((x < y)); |
| 54 | BOOST_CHECK((y > x)); |
| 55 | BOOST_CHECK(!(y <= x)); |
| 56 | BOOST_CHECK(!(x >= y)); |
| 57 | } |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(test_mixed_equal) |
| 60 | { |
| 61 | typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, less_than_comparable<_a, _b> > test_concept; |
| 62 | tuple<test_concept, _a, _b> t(1, 1); |
| 63 | any<test_concept, _a> x(get<0>(t)); |
| 64 | any<test_concept, _b> y(get<1>(t)); |
| 65 | |
| 66 | BOOST_CHECK(!(x < y)); |
| 67 | BOOST_CHECK(!(y > x)); |
| 68 | BOOST_CHECK((y <= x)); |
| 69 | BOOST_CHECK((x >= y)); |
| 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(test_mixed_greater) |
| 73 | { |
| 74 | typedef boost::mpl::vector<copy_constructible<_a>, copy_constructible<_b>, less_than_comparable<_a, _b> > test_concept; |
| 75 | tuple<test_concept, _a, _b> t(2.0, 1); |
| 76 | any<test_concept, _a> x(get<0>(t)); |
| 77 | any<test_concept, _b> y(get<1>(t)); |
| 78 | |
| 79 | BOOST_CHECK(!(x < y)); |
| 80 | BOOST_CHECK(!(y > x)); |
| 81 | BOOST_CHECK((y <= x)); |
| 82 | BOOST_CHECK((x >= y)); |
| 83 | } |
| 84 | |
| 85 | BOOST_AUTO_TEST_CASE(test_fixed_lhs_less) |
| 86 | { |
| 87 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<int, _self> > test_concept; |
| 88 | int x(1); |
| 89 | any<test_concept> y(2.0); |
| 90 | |
| 91 | BOOST_CHECK((x < y)); |
| 92 | BOOST_CHECK((y > x)); |
| 93 | BOOST_CHECK(!(y <= x)); |
| 94 | BOOST_CHECK(!(x >= y)); |
| 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(test_fixed_lhs_equal) |
| 98 | { |
| 99 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<int, _self> > test_concept; |
| 100 | int x(1); |
| 101 | any<test_concept> y(1); |
| 102 | |
| 103 | BOOST_CHECK(!(x < y)); |
| 104 | BOOST_CHECK(!(y > x)); |
| 105 | BOOST_CHECK((y <= x)); |
| 106 | BOOST_CHECK((x >= y)); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(test_fixed_lhs_greater) |
| 111 | { |
| 112 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<int, _self> > test_concept; |
| 113 | int x(1); |
| 114 | any<test_concept> y(0.5); |
| 115 | |
| 116 | BOOST_CHECK(!(x < y)); |
| 117 | BOOST_CHECK(!(y > x)); |
| 118 | BOOST_CHECK((y <= x)); |
| 119 | BOOST_CHECK((x >= y)); |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(test_fixed_rhs_less) |
| 123 | { |
| 124 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<_self, int> > test_concept; |
| 125 | any<test_concept> x(1.0); |
| 126 | int y(2); |
| 127 | |
| 128 | BOOST_CHECK((x < y)); |
| 129 | BOOST_CHECK((y > x)); |
| 130 | BOOST_CHECK(!(y <= x)); |
| 131 | BOOST_CHECK(!(x >= y)); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(test_fixed_rhs_equal) |
| 135 | { |
| 136 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<_self, int> > test_concept; |
| 137 | any<test_concept> x(1); |
| 138 | int y(1); |
| 139 | |
| 140 | BOOST_CHECK(!(x < y)); |
| 141 | BOOST_CHECK(!(y > x)); |
| 142 | BOOST_CHECK((y <= x)); |
| 143 | BOOST_CHECK((x >= y)); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(test_fixed_rhs_greater) |
| 148 | { |
| 149 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<_self, int> > test_concept; |
| 150 | any<test_concept> x(2.0); |
| 151 | int y(1); |
| 152 | |
| 153 | BOOST_CHECK(!(x < y)); |
| 154 | BOOST_CHECK(!(y > x)); |
| 155 | BOOST_CHECK((y <= x)); |
| 156 | BOOST_CHECK((x >= y)); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(test_relaxed) |
| 160 | { |
| 161 | typedef boost::mpl::vector<copy_constructible<>, less_than_comparable<>, relaxed> test_concept; |
| 162 | any<test_concept> x(1); |
| 163 | any<test_concept> y(2); |
| 164 | any<test_concept> z(std::string("test" )); |
| 165 | |
| 166 | BOOST_CHECK((x < y)); |
| 167 | BOOST_CHECK(!(y < x)); |
| 168 | BOOST_CHECK(!(x < x)); |
| 169 | |
| 170 | BOOST_CHECK(!(x > y)); |
| 171 | BOOST_CHECK((y > x)); |
| 172 | BOOST_CHECK(!(x > x)); |
| 173 | |
| 174 | BOOST_CHECK((x <= y)); |
| 175 | BOOST_CHECK(!(y <= x)); |
| 176 | BOOST_CHECK((x <= x)); |
| 177 | |
| 178 | BOOST_CHECK(!(x >= y)); |
| 179 | BOOST_CHECK((y >= x)); |
| 180 | BOOST_CHECK((x >= x)); |
| 181 | |
| 182 | bool expected = x < z; |
| 183 | |
| 184 | BOOST_CHECK_EQUAL((x < z), expected); |
| 185 | BOOST_CHECK_EQUAL(!(z < x), expected); |
| 186 | |
| 187 | BOOST_CHECK_EQUAL(!(x > z), expected); |
| 188 | BOOST_CHECK_EQUAL((z > x), expected); |
| 189 | |
| 190 | BOOST_CHECK_EQUAL((x <= z), expected); |
| 191 | BOOST_CHECK_EQUAL(!(z <= x), expected); |
| 192 | |
| 193 | BOOST_CHECK_EQUAL(!(x >= z), expected); |
| 194 | BOOST_CHECK_EQUAL((z >= x), expected); |
| 195 | |
| 196 | BOOST_CHECK_EQUAL((y < z), expected); |
| 197 | BOOST_CHECK_EQUAL(!(z < y), expected); |
| 198 | |
| 199 | BOOST_CHECK_EQUAL(!(y > z), expected); |
| 200 | BOOST_CHECK_EQUAL((z > y), expected); |
| 201 | |
| 202 | BOOST_CHECK_EQUAL((y <= z), expected); |
| 203 | BOOST_CHECK_EQUAL(!(z <= y), expected); |
| 204 | |
| 205 | BOOST_CHECK_EQUAL(!(y >= z), expected); |
| 206 | BOOST_CHECK_EQUAL((z >= y), expected); |
| 207 | } |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(test_overload) |
| 210 | { |
| 211 | typedef boost::mpl::vector< |
| 212 | copy_constructible<_a>, |
| 213 | copy_constructible<_b>, |
| 214 | less_than_comparable<_a>, |
| 215 | less_than_comparable<_a, int>, |
| 216 | less_than_comparable<int, _a>, |
| 217 | less_than_comparable<_b>, |
| 218 | less_than_comparable<_b, int>, |
| 219 | less_than_comparable<int, _b>, |
| 220 | less_than_comparable<_a, _b> |
| 221 | > test_concept; |
| 222 | tuple<test_concept, _a, _b> t(1, 2); |
| 223 | any<test_concept, _a> x(get<0>(t)); |
| 224 | any<test_concept, _b> y(get<1>(t)); |
| 225 | |
| 226 | BOOST_CHECK(!(x < x)); |
| 227 | BOOST_CHECK(x <= x); |
| 228 | BOOST_CHECK(!(x > x)); |
| 229 | BOOST_CHECK(x >= x); |
| 230 | |
| 231 | BOOST_CHECK(!(x < 1)); |
| 232 | BOOST_CHECK(x <= 1); |
| 233 | BOOST_CHECK(!(x > 1)); |
| 234 | BOOST_CHECK(x >= 1); |
| 235 | |
| 236 | BOOST_CHECK(!(1 < x)); |
| 237 | BOOST_CHECK(1 <= x); |
| 238 | BOOST_CHECK(!(1 > x)); |
| 239 | BOOST_CHECK(1 >= x); |
| 240 | |
| 241 | BOOST_CHECK(!(y < y)); |
| 242 | BOOST_CHECK(y <= y); |
| 243 | BOOST_CHECK(!(y > y)); |
| 244 | BOOST_CHECK(y >= y); |
| 245 | |
| 246 | BOOST_CHECK(!(y < 2)); |
| 247 | BOOST_CHECK(y <= 2); |
| 248 | BOOST_CHECK(!(y > 2)); |
| 249 | BOOST_CHECK(y >= 2); |
| 250 | |
| 251 | BOOST_CHECK(!(2 < y)); |
| 252 | BOOST_CHECK(2 <= y); |
| 253 | BOOST_CHECK(!(2 > y)); |
| 254 | BOOST_CHECK(2 >= y); |
| 255 | |
| 256 | BOOST_CHECK(x < y); |
| 257 | BOOST_CHECK(y > x); |
| 258 | BOOST_CHECK(!(y <= x)); |
| 259 | BOOST_CHECK(!(x >= y)); |
| 260 | } |
| 261 | |