File: integer_sort_test.cpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (131 lines) | stat: -rw-r--r-- 4,400 bytes parent folder | download | duplicates (2)
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
//  Boost Sort library int_test.cpp file  ------------------------------------//

//  Copyright Steven Ross 2009-2014. Use, modification and
//  distribution is subject to 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/libs/sort for library home page.

#include <boost/cstdint.hpp>
#include <boost/sort/spreadsort/spreadsort.hpp>
// Include unit test framework
#include <boost/test/included/test_exec_monitor.hpp>
#include <boost/test/test_tools.hpp>
#include <vector>

#include <iostream>


using namespace std;
using namespace boost::sort::spreadsort;

struct rightshift {
  int operator()(int x, unsigned offset) { return x >> offset; }
};

struct rightshift_max {
  boost::intmax_t operator()(const boost::intmax_t &x, unsigned offset) {
    return x >> offset;
  }
};

struct negrightshift {
  int operator()(const int &x, const unsigned offset) { return -(x >> offset); }
};

struct negrightshift_max {
  boost::intmax_t operator()(const boost::intmax_t &x, const unsigned offset) {
    return -(x >> offset);
  }
};

boost::int32_t 
rand_32(bool sign = true) {
   boost::int32_t result = rand() | (rand()<< 16);
   if (rand() % 2)
     result |= 1 << 15;
   //Adding the sign bit
   if (sign && (rand() % 2))
     result *= -1;
   return result;
}

void int_test()
{
  // Prepare inputs
  vector<int> base_vec;
  unsigned count = 100000;
  srand(1);
  //Generating semirandom numbers
  for (unsigned u = 0; u < count; ++u)
    base_vec.push_back(rand_32());
  vector<int> sorted_vec = base_vec;
  vector<int> test_vec = base_vec;
  std::sort(sorted_vec.begin(), sorted_vec.end());
  //Testing basic call
  integer_sort(test_vec.begin(), test_vec.end());
  BOOST_CHECK(test_vec == sorted_vec);
  //boost::sort::spreadsort variant
  test_vec = base_vec;
  boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());
  BOOST_CHECK(test_vec == sorted_vec);
  //One functor
  test_vec = base_vec;
  integer_sort(test_vec.begin(), test_vec.end(), rightshift());
  BOOST_CHECK(test_vec == sorted_vec);
  //Both functors
  test_vec = base_vec;
  integer_sort(test_vec.begin(), test_vec.end(), rightshift(), less<int>());
  BOOST_CHECK(test_vec == sorted_vec);
  //reverse order
  std::sort(sorted_vec.begin(), sorted_vec.end(), greater<int>());
  integer_sort(test_vec.begin(), test_vec.end(), negrightshift(),
               greater<int>());
  BOOST_CHECK(test_vec == sorted_vec);

  //Making sure we're correctly sorting boost::intmax_ts; should use std::sort
  vector<boost::intmax_t> long_base_vec;
  for (unsigned u = 0; u < base_vec.size(); ++u)
    long_base_vec.push_back((((boost::intmax_t)rand_32()) <<
                             ((8 * sizeof(int)) -1)) + rand_32(false));
  vector<boost::intmax_t> long_sorted_vec = long_base_vec;
  vector<boost::intmax_t> long_test_vec = long_base_vec;
  integer_sort(long_test_vec.begin(), long_test_vec.end());
  std::sort(long_sorted_vec.begin(), long_sorted_vec.end());
  BOOST_CHECK(long_test_vec == long_sorted_vec);
  //One functor
  long_test_vec = long_base_vec;
  integer_sort(long_test_vec.begin(), long_test_vec.end(), rightshift_max());
  BOOST_CHECK(long_test_vec == long_sorted_vec);
  //Both functors
  long_test_vec = long_base_vec;
  integer_sort(long_test_vec.begin(), long_test_vec.end(), rightshift_max(),
               less<boost::intmax_t>());
  BOOST_CHECK(long_test_vec == long_sorted_vec);
  //reverse order
  std::sort(long_sorted_vec.begin(), long_sorted_vec.end(),
            greater<boost::intmax_t>());
  integer_sort(long_test_vec.begin(), long_test_vec.end(), negrightshift_max(),
               greater<boost::intmax_t>());
  BOOST_CHECK(long_test_vec == long_sorted_vec);
}

// Verify that 0 and 1 elements work correctly.
void corner_test() {
  vector<int> test_vec;
  boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());
  const int test_value = 42;
  test_vec.push_back(test_value);
  boost::sort::spreadsort::spreadsort(test_vec.begin(), test_vec.end());
  BOOST_CHECK(test_vec.size() == 1);
  BOOST_CHECK(test_vec[0] == test_value);
}

// test main 
int test_main( int, char*[] )
{
  int_test();
  corner_test();    
  return 0;
}