summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/variant/test/variant_comparison_test.cpp
blob: a918a6750a7ae1b4230f0eb4f5def2bb738fa651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//-----------------------------------------------------------------------------
// boost-libs variant/test/variant_comparison_test.cpp source file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003 Eric Friedman, Itay Maman
// Copyright (c) 2014-2019 Antony Polukhin
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "boost/variant/variant.hpp"
#include "boost/core/lightweight_test.hpp"

#include <iostream>
#include <sstream>
#include <string>

#include <algorithm>
#include <vector>

#include "boost/detail/workaround.hpp"
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
#    pragma warn -lvc // temporary used for parameter warning
#endif

template <typename T>
void assert_equality_comparable(
      const T& x, const T& y, const T& z
    )
{
    // identity check
    BOOST_TEST( !(&x == &y) || (x == y) );
    BOOST_TEST( !(&x == &z) || (x == z) );
    BOOST_TEST( !(&y == &z) || (y == z) );
    BOOST_TEST( !(&x == &y) || !(x != y) );
    BOOST_TEST( !(&x == &z) || !(x != z) );
    BOOST_TEST( !(&y == &z) || !(y != z) );

    // reflexivity check
    BOOST_TEST( (x == x) );
    BOOST_TEST( (y == y) );
    BOOST_TEST( (z == z) );

    // symmetry check
    BOOST_TEST( !(x == y) || (y == x) );
    BOOST_TEST( !(y == x) || (x == y) );
    BOOST_TEST( (x != y) || (y == x) );
    BOOST_TEST( (y != x) || (x == y) );

    BOOST_TEST( !(x == z) || (z == x) );
    BOOST_TEST( !(z == x) || (x == z) );
    BOOST_TEST( (x != z) || (z == x) );
    BOOST_TEST( (z != x) || (x == z) );

    BOOST_TEST( !(y == z) || (z == y) );
    BOOST_TEST( !(z == y) || (y == z) );
    BOOST_TEST( (y != z) || (z == y) );
    BOOST_TEST( (z != y) || (y == z) );

    // transitivity check
    BOOST_TEST( !(x == y && y == z) || (x == z) );
    BOOST_TEST( !(x == z && z == y) || (x == y) );
    BOOST_TEST( !(y == z && z == x) || (y == x) );
}

template <typename T>
void assert_less_than_comparable(
      const T& x, const T& y, const T& z
    )
{
    // irreflexivity check
    BOOST_TEST( !(x < x) );
    BOOST_TEST( !(y < y) );
    BOOST_TEST( !(z < z) );
    BOOST_TEST( !(x > x) );
    BOOST_TEST( !(y > y) );
    BOOST_TEST( !(z > z) );

    BOOST_TEST( (x <= x) );
    BOOST_TEST( (y <= y) );
    BOOST_TEST( (z <= z) );
    BOOST_TEST( (x >= x) );
    BOOST_TEST( (y >= y) );
    BOOST_TEST( (z >= z) );

    // transitivity check
    BOOST_TEST( (x < y) );
    BOOST_TEST( (y < z) );
    BOOST_TEST( (x < z) );

    BOOST_TEST( (x <= y) );
    BOOST_TEST( (y <= z) );
    BOOST_TEST( (x <= z) );

    BOOST_TEST( (z > y) );
    BOOST_TEST( (y > x) );
    BOOST_TEST( (z > x) );

    BOOST_TEST( (z >= y) );
    BOOST_TEST( (y >= x) );
    BOOST_TEST( (z >= x) );

    // antisymmetry check
    BOOST_TEST( !(y < x) );
    BOOST_TEST( !(z < y) );
    BOOST_TEST( !(z < x) );
}

template <typename It>
std::string print_range(It first, It last)
{
    std::ostringstream result;

    while (first != last)
    {
        result << *first << ' ';
        ++first;
    }

    return result.str();
}

int main()
{
    typedef boost::variant<int, std::string> var_t;
    
    var_t var1(3);
    var_t var2(5);
    var_t var3("goodbye");
    var_t var4("hello");

    assert_equality_comparable(var1, var1, var1);
    assert_equality_comparable(var_t(var1), var_t(var1), var_t(var1));
    assert_equality_comparable(var1, var2, var3);

    assert_less_than_comparable(var1, var2, var3);
    assert_less_than_comparable(var2, var3, var4);

    std::vector<var_t> vec;
    vec.push_back( var3 );
    vec.push_back( var2 );
    vec.push_back( var4 );
    vec.push_back( var1 );
    std::sort(vec.begin(), vec.end());

    std::string sort_result( print_range(vec.begin(), vec.end()) );
    BOOST_TEST( sort_result == "3 5 goodbye hello " );

    // https://svn.boost.org/trac/boost/ticket/11751
    int a = 0, b = 0;

    boost::variant< int& > v (a), u (b);
    BOOST_TEST(v == u);

    return boost::report_errors();
}