File: test_comparison.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (137 lines) | stat: -rw-r--r-- 3,040 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#include <random>
#include <vector>
#include <cstring>

#if defined(BOOST_UUID_HAS_THREE_WAY_COMPARISON)

BOOST_PRAGMA_MESSAGE( "Three way comparisons are tested: BOOST_UUID_HAS_THREE_WAY_COMPARISON=" BOOST_STRINGIZE(BOOST_UUID_HAS_THREE_WAY_COMPARISON) )

#elif !defined(__cpp_impl_three_way_comparison)

BOOST_PRAGMA_MESSAGE( "Three way comparisons not tested because __cpp_impl_three_way_comparison is not defined" )

#elif !defined(__cpp_lib_three_way_comparison)

BOOST_PRAGMA_MESSAGE( "Three way comparisons not tested because __cpp_lib_three_way_comparison is not defined" )

#else

BOOST_PRAGMA_MESSAGE( "Three way comparisons not tested: __cpp_impl_three_way_comparison=" BOOST_STRINGIZE(__cpp_impl_three_way_comparison) ", __cpp_lib_three_way_comparison=" BOOST_STRINGIZE(__cpp_lib_three_way_comparison) )

#endif

using namespace boost::uuids;

void test_comparison( uuid const& u1, uuid const& u2 )
{
    int r = std::memcmp( u1.data, u2.data, 16 );

    if( r == 0 )
    {
        BOOST_TEST_EQ( u1, u2 );
    }

    if( r < 0 )
    {
        BOOST_TEST_LT( u1, u2 );
    }

    if( r > 0 )
    {
        BOOST_TEST_GT( u1, u2 );
    }

    if( r <= 0 )
    {
        BOOST_TEST_LE( u1, u2 );
    }

    if( r >= 0 )
    {
        BOOST_TEST_GE( u1, u2 );
    }

#if defined(BOOST_UUID_HAS_THREE_WAY_COMPARISON)

    constexpr auto eq = std::strong_ordering::equal;
    constexpr auto lt = std::strong_ordering::less;
    constexpr auto gt = std::strong_ordering::greater;

    if( r == 0 )
    {
        BOOST_TEST( ( u1 <=> u2 ) == eq );
    }

    if( r < 0 )
    {
        BOOST_TEST( ( u1 <=> u2 ) == lt );
    }

    if( r > 0 )
    {
        BOOST_TEST( ( u1 <=> u2 ) == gt );
    }

#endif
}

int main()
{
    std::vector<uuid> v;

    {
        uuid u1 = {};
        v.push_back( u1 );

        for( int i = 0; i < 16; ++i )
        {
            uuid u2( u1 );
            u2.data[ i ] = 0x01;

            v.push_back( u2 );
        }
    }

    {
        uuid u1 = {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }};
        v.push_back( u1 );

        for( int i = 0; i < 16; ++i )
        {
            uuid u2( u1 );
            u2.data[ i ] = 0xFE;

            v.push_back( u2 );
        }
    }

    {
        std::mt19937 rng;
        basic_random_generator<std::mt19937> gen( &rng );

        for( int i = 0; i < 16; ++i )
        {
            v.push_back( gen() );
        }
    }

    for( std::size_t i = 0, n = v.size(); i < n; ++i )
    {
        for( std::size_t j = 0; j < n; ++j )
        {
            test_comparison( v[ i ], v[ j ] );
        }
    }

    return boost::report_errors();
}