1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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/core.h>
21 #include <lemon/time_measure.h>
22 #include <lemon/smart_graph.h>
23 #include <lemon/maps.h>
24 #include <lemon/radix_sort.h>
25 #include <lemon/math.h>
27 #include "test_tools.h"
33 using namespace lemon;
35 static const int n = 10000;
38 typedef int argument_type;
39 typedef int result_type;
40 int operator()(int a) { return - a; }
43 int negate(int a) { return - a; }
46 bool isTheSame(T &a, T&b)
48 typename T::iterator ai=a.begin();
49 typename T::iterator bi=b.begin();
50 for(;ai!=a.end()||bi!=b.end();++ai,++bi)
51 if(*ai!=*bi) return false;
52 return ai==a.end()&&bi==b.end();
56 T listsort(typename T::iterator b, typename T::iterator e)
59 typename T::iterator bn=b;
65 typename T::iterator m=b;
67 for(typename T::iterator i=b;i!=e;++i,x=!x)
69 T l1(listsort<T>(b,m));
70 T l2(listsort<T>(m,e));
72 while((!l1.empty())&&(!l2.empty()))
73 if(l1.front()<=l2.front())
75 l.push_back(l1.front());
79 l.push_back(l2.front());
84 l.push_back(l1.front());
89 l.push_back(l2.front());
96 void generateIntSequence(int n, T & data) {
98 int root = 136, value = 1;
99 for (int i = 0; i < n; ++i) {
100 data.push_back(value - prime / 2);
101 value = (value * root) % prime;
106 void generateCharSequence(int n, T & data) {
108 int root = 3, value = root;
109 for (int i = 0; i < n; ++i) {
110 data.push_back(static_cast<unsigned char>(value));
111 value = (value * root) % prime;
115 void checkRadixSort() {
117 std::vector<int> data1;
118 generateIntSequence(n, data1);
120 std::vector<int> data2(data1);
121 std::sort(data1.begin(), data1.end());
123 radixSort(data2.begin(), data2.end());
124 for (int i = 0; i < n; ++i) {
125 check(data1[i] == data2[i], "Test failed");
128 // radixSort(data2.begin(), data2.end(), Negate());
129 // for (int i = 0; i < n; ++i) {
130 // check(data1[i] == data2[n - 1 - i], "Test failed");
133 // radixSort(data2.begin(), data2.end(), negate);
134 // for (int i = 0; i < n; ++i) {
135 // check(data1[i] == data2[n - 1 - i], "Test failed");
141 std::vector<unsigned char> data1(n);
142 generateCharSequence(n, data1);
144 std::vector<unsigned char> data2(data1);
145 std::sort(data1.begin(), data1.end());
147 radixSort(data2.begin(), data2.end());
148 for (int i = 0; i < n; ++i) {
149 check(data1[i] == data2[i], "Test failed");
154 std::list<int> data1;
155 generateIntSequence(n, data1);
157 std::list<int> data2(listsort<std::list<int> >(data1.begin(), data1.end()));
159 radixSort(data1.begin(), data1.end());
161 check(isTheSame(data1,data2), "Test failed");
164 // radixSort(data2.begin(), data2.end(), Negate());
165 // check(isTheSame(data1,data2), "Test failed");
166 // for (int i = 0; i < n; ++i) {
167 // check(data1[i] == data2[n - 1 - i], "Test failed");
170 // radixSort(data2.begin(), data2.end(), negate);
171 // for (int i = 0; i < n; ++i) {
172 // check(data1[i] == data2[n - 1 - i], "Test failed");
178 std::list<unsigned char> data1(n);
179 generateCharSequence(n, data1);
181 std::list<unsigned char> data2(listsort<std::list<unsigned char> >
185 radixSort(data1.begin(), data1.end());
186 check(isTheSame(data1,data2), "Test failed");
192 void checkStableRadixSort() {
194 std::vector<int> data1;
195 generateIntSequence(n, data1);
197 std::vector<int> data2(data1);
198 std::sort(data1.begin(), data1.end());
200 stableRadixSort(data2.begin(), data2.end());
201 for (int i = 0; i < n; ++i) {
202 check(data1[i] == data2[i], "Test failed");
205 stableRadixSort(data2.begin(), data2.end(), Negate());
206 for (int i = 0; i < n; ++i) {
207 check(data1[i] == data2[n - 1 - i], "Test failed");
210 stableRadixSort(data2.begin(), data2.end(), negate);
211 for (int i = 0; i < n; ++i) {
212 check(data1[i] == data2[n - 1 - i], "Test failed");
217 std::vector<unsigned char> data1(n);
218 generateCharSequence(n, data1);
220 std::vector<unsigned char> data2(data1);
221 std::sort(data1.begin(), data1.end());
223 radixSort(data2.begin(), data2.end());
224 for (int i = 0; i < n; ++i) {
225 check(data1[i] == data2[i], "Test failed");
230 std::list<int> data1;
231 generateIntSequence(n, data1);
233 std::list<int> data2(listsort<std::list<int> >(data1.begin(),
235 stableRadixSort(data1.begin(), data1.end());
236 check(isTheSame(data1,data2), "Test failed");
238 // stableRadixSort(data2.begin(), data2.end(), Negate());
239 // for (int i = 0; i < n; ++i) {
240 // check(data1[i] == data2[n - 1 - i], "Test failed");
243 // stableRadixSort(data2.begin(), data2.end(), negate);
244 // for (int i = 0; i < n; ++i) {
245 // check(data1[i] == data2[n - 1 - i], "Test failed");
250 std::list<unsigned char> data1(n);
251 generateCharSequence(n, data1);
253 std::list<unsigned char> data2(listsort<std::list<unsigned char> >
256 radixSort(data1.begin(), data1.end());
257 check(isTheSame(data1,data2), "Test failed");
265 checkStableRadixSort();