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"
31 using namespace lemon;
33 static const int n = 10000;
36 typedef int argument_type;
37 typedef int result_type;
38 int operator()(int a) { return - a; }
41 int negate(int a) { return - a; }
44 bool isTheSame(T &a, T&b)
46 typename T::iterator ai=a.begin();
47 typename T::iterator bi=b.begin();
48 for(;ai!=a.end()||bi!=b.end();++ai,++bi)
49 if(*ai!=*bi) return false;
50 return ai==a.end()&&bi==b.end();
54 T listsort(typename T::iterator b, typename T::iterator e)
57 typename T::iterator bn=b;
63 typename T::iterator m=b;
65 for(typename T::iterator i=b;i!=e;++i,x=!x)
67 T l1(listsort<T>(b,m));
68 T l2(listsort<T>(m,e));
70 while((!l1.empty())&&(!l2.empty()))
71 if(l1.front()<=l2.front())
73 l.push_back(l1.front());
77 l.push_back(l2.front());
82 l.push_back(l1.front());
87 l.push_back(l2.front());
94 void generateIntSequence(int n, T & data) {
96 int root = 136, value = 1;
97 for (int i = 0; i < n; ++i) {
98 data.push_back(value - prime / 2);
99 value = (value * root) % prime;
104 void generateCharSequence(int n, T & data) {
106 int root = 3, value = root;
107 for (int i = 0; i < n; ++i) {
108 data.push_back(static_cast<unsigned char>(value));
109 value = (value * root) % prime;
113 void checkRadixSort() {
115 std::vector<int> data1;
116 generateIntSequence(n, data1);
118 std::vector<int> data2(data1);
119 std::sort(data1.begin(), data1.end());
121 radixSort(data2.begin(), data2.end());
122 for (int i = 0; i < n; ++i) {
123 check(data1[i] == data2[i], "Test failed");
126 // radixSort(data2.begin(), data2.end(), Negate());
127 // for (int i = 0; i < n; ++i) {
128 // check(data1[i] == data2[n - 1 - i], "Test failed");
131 // radixSort(data2.begin(), data2.end(), negate);
132 // for (int i = 0; i < n; ++i) {
133 // check(data1[i] == data2[n - 1 - i], "Test failed");
139 std::vector<unsigned char> data1(n);
140 generateCharSequence(n, data1);
142 std::vector<unsigned char> data2(data1);
143 std::sort(data1.begin(), data1.end());
145 radixSort(data2.begin(), data2.end());
146 for (int i = 0; i < n; ++i) {
147 check(data1[i] == data2[i], "Test failed");
152 std::list<int> data1;
153 generateIntSequence(n, data1);
155 std::list<int> data2(listsort<std::list<int> >(data1.begin(), data1.end()));
157 radixSort(data1.begin(), data1.end());
159 check(isTheSame(data1,data2), "Test failed");
162 // radixSort(data2.begin(), data2.end(), Negate());
163 // check(isTheSame(data1,data2), "Test failed");
164 // for (int i = 0; i < n; ++i) {
165 // check(data1[i] == data2[n - 1 - i], "Test failed");
168 // radixSort(data2.begin(), data2.end(), negate);
169 // for (int i = 0; i < n; ++i) {
170 // check(data1[i] == data2[n - 1 - i], "Test failed");
176 std::list<unsigned char> data1(n);
177 generateCharSequence(n, data1);
179 std::list<unsigned char> data2(listsort<std::list<unsigned char> >
183 radixSort(data1.begin(), data1.end());
184 check(isTheSame(data1,data2), "Test failed");
190 void checkStableRadixSort() {
192 std::vector<int> data1;
193 generateIntSequence(n, data1);
195 std::vector<int> data2(data1);
196 std::sort(data1.begin(), data1.end());
198 stableRadixSort(data2.begin(), data2.end());
199 for (int i = 0; i < n; ++i) {
200 check(data1[i] == data2[i], "Test failed");
203 stableRadixSort(data2.begin(), data2.end(), Negate());
204 for (int i = 0; i < n; ++i) {
205 check(data1[i] == data2[n - 1 - i], "Test failed");
208 stableRadixSort(data2.begin(), data2.end(), negate);
209 for (int i = 0; i < n; ++i) {
210 check(data1[i] == data2[n - 1 - i], "Test failed");
215 std::vector<unsigned char> data1(n);
216 generateCharSequence(n, data1);
218 std::vector<unsigned char> data2(data1);
219 std::sort(data1.begin(), data1.end());
221 radixSort(data2.begin(), data2.end());
222 for (int i = 0; i < n; ++i) {
223 check(data1[i] == data2[i], "Test failed");
228 std::list<int> data1;
229 generateIntSequence(n, data1);
231 std::list<int> data2(listsort<std::list<int> >(data1.begin(),
233 stableRadixSort(data1.begin(), data1.end());
234 check(isTheSame(data1,data2), "Test failed");
236 // stableRadixSort(data2.begin(), data2.end(), Negate());
237 // for (int i = 0; i < n; ++i) {
238 // check(data1[i] == data2[n - 1 - i], "Test failed");
241 // stableRadixSort(data2.begin(), data2.end(), negate);
242 // for (int i = 0; i < n; ++i) {
243 // check(data1[i] == data2[n - 1 - i], "Test failed");
248 std::list<unsigned char> data1(n);
249 generateCharSequence(n, data1);
251 std::list<unsigned char> data2(listsort<std::list<unsigned char> >
254 radixSort(data1.begin(), data1.end());
255 check(isTheSame(data1,data2), "Test failed");
263 checkStableRadixSort();