Release series 1.1.x has reached its end of life.
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include <lemon/time_measure.h>
20 #include <lemon/smart_graph.h>
21 #include <lemon/maps.h>
22 #include <lemon/radix_sort.h>
23 #include <lemon/math.h>
25 #include "test_tools.h"
30 using namespace lemon;
32 static const int n = 10000;
35 typedef int argument_type;
36 typedef int result_type;
37 int operator()(int a) { return - a; }
40 int negate(int a) { return - a; }
43 void generateIntSequence(int n, std::vector<int>& data) {
45 int root = 136, value = 1;
46 for (int i = 0; i < n; ++i) {
47 data.push_back(value - prime / 2);
48 value = (value * root) % prime;
52 void generateCharSequence(int n, std::vector<unsigned char>& data) {
54 int root = 3, value = root;
55 for (int i = 0; i < n; ++i) {
56 data.push_back(static_cast<unsigned char>(value));
57 value = (value * root) % prime;
61 void checkRadixSort() {
63 std::vector<int> data1;
64 generateIntSequence(n, data1);
66 std::vector<int> data2(data1);
67 std::sort(data1.begin(), data1.end());
69 radixSort(data2.begin(), data2.end());
70 for (int i = 0; i < n; ++i) {
71 check(data1[i] == data2[i], "Test failed");
74 radixSort(data2.begin(), data2.end(), Negate());
75 for (int i = 0; i < n; ++i) {
76 check(data1[i] == data2[n - 1 - i], "Test failed");
79 radixSort(data2.begin(), data2.end(), negate);
80 for (int i = 0; i < n; ++i) {
81 check(data1[i] == data2[n - 1 - i], "Test failed");
87 std::vector<unsigned char> data1(n);
88 generateCharSequence(n, data1);
90 std::vector<unsigned char> data2(data1);
91 std::sort(data1.begin(), data1.end());
93 radixSort(data2.begin(), data2.end());
94 for (int i = 0; i < n; ++i) {
95 check(data1[i] == data2[i], "Test failed");
102 void checkStableRadixSort() {
104 std::vector<int> data1;
105 generateIntSequence(n, data1);
107 std::vector<int> data2(data1);
108 std::sort(data1.begin(), data1.end());
110 stableRadixSort(data2.begin(), data2.end());
111 for (int i = 0; i < n; ++i) {
112 check(data1[i] == data2[i], "Test failed");
115 stableRadixSort(data2.begin(), data2.end(), Negate());
116 for (int i = 0; i < n; ++i) {
117 check(data1[i] == data2[n - 1 - i], "Test failed");
120 stableRadixSort(data2.begin(), data2.end(), negate);
121 for (int i = 0; i < n; ++i) {
122 check(data1[i] == data2[n - 1 - i], "Test failed");
127 std::vector<unsigned char> data1(n);
128 generateCharSequence(n, data1);
130 std::vector<unsigned char> data2(data1);
131 std::sort(data1.begin(), data1.end());
133 radixSort(data2.begin(), data2.end());
134 for (int i = 0; i < n; ++i) {
135 check(data1[i] == data2[i], "Test failed");
144 checkStableRadixSort();