summaryrefslogtreecommitdiff
path: root/libs/algorithm/test/ordered_test.cpp
blob: dbaf94039a845eca14b6016c8dbb5c96190604df (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
//  Copyright (c) 2010 Nuovation System Designs, LLC
//    Grant Erickson <gerickson@nuovations.com>
//
//  Reworked by Marshall Clow; August 2010
//
//  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)
//
//  See http://www.boost.org/ for latest version.

#include <algorithm>
#include <iostream>

#include <boost/algorithm/cxx11/is_sorted.hpp>

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

using namespace boost;

/* Preprocessor Defines */

#define elementsof(v)   (sizeof (v) / sizeof (v[0]))
#define a_begin(v)      (&v[0])
#define a_end(v)        (v + elementsof (v))
#define a_range(v)      v
#define b_e(v)          a_begin(v),a_end(v)

namespace ba = boost::algorithm;

static void
test_ordered(void)
{
    const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
    const int randomValues[] = { 3, 6, 1, 2, 7 };
    const int constantValues[] = { 1, 2, 2, 2, 5 };
          int nonConstantArray[] = { 1, 2, 2, 2, 5 };
    const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 };

//  Begin/end checks
    BOOST_CHECK (  ba::is_sorted (b_e(strictlyIncreasingValues)));
    BOOST_CHECK ( !ba::is_sorted (b_e(randomValues)));
    BOOST_CHECK (  ba::is_sorted (b_e(strictlyIncreasingValues), std::less<int>()));
    BOOST_CHECK ( !ba::is_sorted (b_e(strictlyIncreasingValues), std::greater<int>()));

//  Range checks
    BOOST_CHECK (  ba::is_sorted (a_range(strictlyIncreasingValues)));
    BOOST_CHECK ( !ba::is_sorted (a_range(randomValues)));
    BOOST_CHECK (  ba::is_sorted (a_range(strictlyIncreasingValues), std::less<int>()));
    BOOST_CHECK ( !ba::is_sorted (a_range(strictlyIncreasingValues), std::greater<int>()));

    BOOST_CHECK (  ba::is_sorted_until ( b_e(strictlyIncreasingValues))                       ==      a_end(strictlyIncreasingValues));
    BOOST_CHECK (  ba::is_sorted_until ( b_e(strictlyIncreasingValues),     std::less<int>()) ==      a_end(strictlyIncreasingValues));
    BOOST_CHECK (  ba::is_sorted_until ( a_range(strictlyIncreasingValues))                   == boost::end(strictlyIncreasingValues));
    BOOST_CHECK (  ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues));

//  Check for const and non-const arrays
    BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues),       std::less<int>()) ==      a_end(constantValues));
    BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues),   std::less<int>()) == boost::end(constantValues));
    BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray),     std::less<int>()) ==      a_end(nonConstantArray));
    BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) == boost::end(nonConstantArray));

    BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues),     std::less<int>()) == &randomValues[2] );
    BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues))                       == &randomValues[2] );
    BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
    BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues))                   == &randomValues[2] );

    BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less<int>()) == &inOrderUntilTheEnd[8] );
    BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd))                   == &inOrderUntilTheEnd[8] );

//  For zero and one element collections, the comparison predicate should never be called
    BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues),     std::equal_to<int>()) == a_begin(randomValues));
    BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues))                           == a_begin(randomValues));
    BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1);
    BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 )                      == a_begin(randomValues) + 1);
}


static void
test_increasing_decreasing(void)
{
    const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
    const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 };
    const int increasingValues[] = { 1, 2, 2, 2, 5 };
    const int decreasingValues[] = { 9, 7, 7, 7, 5 };
    const int randomValues[] = { 3, 6, 1, 2, 7 };
    const int constantValues[] = { 7, 7, 7, 7, 7 };

    // Test a strictly increasing sequence
    BOOST_CHECK (  ba::is_strictly_increasing (b_e(strictlyIncreasingValues)));
    BOOST_CHECK (  ba::is_increasing          (b_e(strictlyIncreasingValues)));
    BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(strictlyIncreasingValues)));
    BOOST_CHECK ( !ba::is_decreasing          (b_e(strictlyIncreasingValues)));

    BOOST_CHECK (  ba::is_strictly_increasing (a_range(strictlyIncreasingValues)));
    BOOST_CHECK (  ba::is_increasing          (a_range(strictlyIncreasingValues)));
    BOOST_CHECK ( !ba::is_strictly_decreasing (a_range(strictlyIncreasingValues)));
    BOOST_CHECK ( !ba::is_decreasing          (a_range(strictlyIncreasingValues)));

    // Test a strictly decreasing sequence
    BOOST_CHECK ( !ba::is_strictly_increasing (b_e(strictlyDecreasingValues)));
    BOOST_CHECK ( !ba::is_increasing          (b_e(strictlyDecreasingValues)));
    BOOST_CHECK (  ba::is_strictly_decreasing (b_e(strictlyDecreasingValues)));
    BOOST_CHECK (  ba::is_decreasing          (b_e(strictlyDecreasingValues)));

    // Test an increasing sequence
    BOOST_CHECK ( !ba::is_strictly_increasing (b_e(increasingValues)));
    BOOST_CHECK (  ba::is_increasing          (b_e(increasingValues)));
    BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(increasingValues)));
    BOOST_CHECK ( !ba::is_decreasing          (b_e(increasingValues)));

    // Test a decreasing sequence
    BOOST_CHECK ( !ba::is_strictly_increasing (b_e(decreasingValues)));
    BOOST_CHECK ( !ba::is_increasing          (b_e(decreasingValues)));
    BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(decreasingValues)));
    BOOST_CHECK (  ba::is_decreasing          (b_e(decreasingValues)));

    // Test a random sequence
    BOOST_CHECK ( !ba::is_strictly_increasing (b_e(randomValues)));
    BOOST_CHECK ( !ba::is_increasing          (b_e(randomValues)));
    BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(randomValues)));
    BOOST_CHECK ( !ba::is_decreasing          (b_e(randomValues)));

    // Test a constant sequence
    BOOST_CHECK ( !ba::is_strictly_increasing (b_e(constantValues)));
    BOOST_CHECK (  ba::is_increasing          (b_e(constantValues)));
    BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(constantValues)));
    BOOST_CHECK (  ba::is_decreasing          (b_e(constantValues)));
    
    // Test an empty sequence
    BOOST_CHECK (  ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues));
    BOOST_CHECK (  ba::is_increasing          (strictlyIncreasingValues, strictlyIncreasingValues));
    BOOST_CHECK (  ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues));
    BOOST_CHECK (  ba::is_decreasing          (strictlyIncreasingValues, strictlyIncreasingValues));
    
    // Test a one-element sequence
    BOOST_CHECK (  ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
    BOOST_CHECK (  ba::is_increasing          (strictlyIncreasingValues, strictlyIncreasingValues+1));
    BOOST_CHECK (  ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1));
    BOOST_CHECK (  ba::is_decreasing          (strictlyIncreasingValues, strictlyIncreasingValues+1));

    // Test a two-element sequence
    BOOST_CHECK (  ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
    BOOST_CHECK (  ba::is_increasing          (strictlyIncreasingValues, strictlyIncreasingValues+2));
    BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
    BOOST_CHECK ( !ba::is_decreasing          (strictlyIncreasingValues, strictlyIncreasingValues+2));
    
}

BOOST_AUTO_TEST_CASE( test_main )
{
    test_ordered ();
    test_increasing_decreasing ();
}