1 | //----------------------------------------------------------------------------- |
---|
2 | // boost-libs variant/test/variant_comparison_test.cpp source file |
---|
3 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
4 | //----------------------------------------------------------------------------- |
---|
5 | // |
---|
6 | // Copyright (c) 2003 |
---|
7 | // Eric Friedman, Itay Maman |
---|
8 | // |
---|
9 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
10 | // accompanying file LICENSE_1_0.txt or copy at |
---|
11 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
12 | |
---|
13 | #include "boost/variant/variant.hpp" |
---|
14 | #include "boost/test/minimal.hpp" |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <sstream> |
---|
18 | #include <string> |
---|
19 | |
---|
20 | #include <algorithm> |
---|
21 | #include <vector> |
---|
22 | |
---|
23 | #include "boost/detail/workaround.hpp" |
---|
24 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551)) |
---|
25 | # pragma warn -lvc // temporary used for parameter warning |
---|
26 | #endif |
---|
27 | |
---|
28 | template <typename T> |
---|
29 | void assert_equality_comparable( |
---|
30 | const T& x, const T& y, const T& z |
---|
31 | ) |
---|
32 | { |
---|
33 | // identity check |
---|
34 | BOOST_CHECK( !(&x == &y) || (x == y) ); |
---|
35 | BOOST_CHECK( !(&x == &z) || (x == z) ); |
---|
36 | BOOST_CHECK( !(&y == &z) || (y == z) ); |
---|
37 | |
---|
38 | // reflexivity check |
---|
39 | BOOST_CHECK( (x == x) ); |
---|
40 | BOOST_CHECK( (y == y) ); |
---|
41 | BOOST_CHECK( (z == z) ); |
---|
42 | |
---|
43 | // symmetry check |
---|
44 | BOOST_CHECK( !(x == y) || (y == x) ); |
---|
45 | BOOST_CHECK( !(y == x) || (x == y) ); |
---|
46 | |
---|
47 | BOOST_CHECK( !(x == z) || (z == x) ); |
---|
48 | BOOST_CHECK( !(z == x) || (x == z) ); |
---|
49 | |
---|
50 | BOOST_CHECK( !(y == z) || (z == y) ); |
---|
51 | BOOST_CHECK( !(z == y) || (y == z) ); |
---|
52 | |
---|
53 | // transitivity check |
---|
54 | BOOST_CHECK( !(x == y && y == z) || (x == z) ); |
---|
55 | BOOST_CHECK( !(x == z && z == y) || (x == y) ); |
---|
56 | BOOST_CHECK( !(y == z && z == x) || (y == x) ); |
---|
57 | } |
---|
58 | |
---|
59 | template <typename T> |
---|
60 | void assert_less_than_comparable( |
---|
61 | const T& x, const T& y, const T& z |
---|
62 | ) |
---|
63 | { |
---|
64 | // irreflexivity check |
---|
65 | BOOST_CHECK( !(x < x) ); |
---|
66 | BOOST_CHECK( !(y < y) ); |
---|
67 | BOOST_CHECK( !(z < z) ); |
---|
68 | |
---|
69 | // transitivity check |
---|
70 | BOOST_CHECK( (x < y) ); |
---|
71 | BOOST_CHECK( (y < z) ); |
---|
72 | BOOST_CHECK( (x < z) ); |
---|
73 | |
---|
74 | // antisymmetry check |
---|
75 | BOOST_CHECK( !(y < x) ); |
---|
76 | BOOST_CHECK( !(z < y) ); |
---|
77 | BOOST_CHECK( !(z < x) ); |
---|
78 | } |
---|
79 | |
---|
80 | template <typename It> |
---|
81 | std::string print_range(It first, It last) |
---|
82 | { |
---|
83 | std::ostringstream result; |
---|
84 | |
---|
85 | while (first != last) |
---|
86 | { |
---|
87 | result << *first << ' '; |
---|
88 | ++first; |
---|
89 | } |
---|
90 | |
---|
91 | return result.str(); |
---|
92 | } |
---|
93 | |
---|
94 | int test_main(int , char* []) |
---|
95 | { |
---|
96 | typedef boost::variant<int, std::string> var_t; |
---|
97 | |
---|
98 | var_t var1(3); |
---|
99 | var_t var2(5); |
---|
100 | var_t var3("goodbye"); |
---|
101 | var_t var4("hello"); |
---|
102 | |
---|
103 | assert_equality_comparable(var1, var1, var1); |
---|
104 | assert_equality_comparable(var_t(var1), var_t(var1), var_t(var1)); |
---|
105 | assert_equality_comparable(var1, var2, var3); |
---|
106 | |
---|
107 | assert_less_than_comparable(var1, var2, var3); |
---|
108 | assert_less_than_comparable(var2, var3, var4); |
---|
109 | |
---|
110 | std::vector<var_t> vec; |
---|
111 | vec.push_back( var3 ); |
---|
112 | vec.push_back( var2 ); |
---|
113 | vec.push_back( var4 ); |
---|
114 | vec.push_back( var1 ); |
---|
115 | std::sort(vec.begin(), vec.end()); |
---|
116 | |
---|
117 | std::string sort_result( print_range(vec.begin(), vec.end()) ); |
---|
118 | BOOST_CHECK( sort_result == "3 5 goodbye hello " ); |
---|
119 | |
---|
120 | return boost::exit_success; |
---|
121 | } |
---|