1 #include <lemon/time_measure.h>
2 #include <lemon/smart_graph.h>
3 #include <lemon/graph_utils.h>
4 #include <lemon/maps.h>
5 #include <lemon/radix_sort.h>
7 #include "test_tools.h"
15 using namespace lemon;
17 void checkRadixSort() {
19 vector<int> data1(n), data2(n);
20 for (int i = 0; i < n; ++i) {
21 data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
23 radixSort(data1.begin(), data1.end());
24 sort(data2.begin(), data2.end());
25 for (int i = 0; i < n; ++i) {
26 check(data1[i] == data2[i], "Test failed");
31 void checkCounterSort() {
33 vector<int> data1(n), data2(n);
34 for (int i = 0; i < n; ++i) {
35 data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
37 counterSort(data1.begin(), data1.end());
38 sort(data2.begin(), data2.end());
39 for (int i = 0; i < n; ++i) {
40 check(data1[i] == data2[i], "Test failed");
49 void checkGraphRadixSort() {
50 typedef SmartGraph Graph;
51 typedef Graph::Node Node;
52 typedef Graph::Edge Edge;
55 const int e = (int)(n * log((double)n));
60 for (int i = 0; i < n; ++i) {
61 nodes.push_back(graph.addNode());
64 for (int i = 0; i < e; ++i) {
65 int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
66 int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
67 edges.push_back(graph.addEdge(nodes[s], nodes[t]));
70 radixSort(edges.begin(), edges.end(),
71 mapFunctor(composeMap(IdMap<Graph, Node>(graph),
74 Graph::EdgeMap<bool> was(graph, false);
76 for (int i = 0; i < (int)edges.size(); ++i) {
77 check(!was[edges[i]], "Test failed");
81 for (int i = 1; i < (int)edges.size(); ++i) {
82 check(graph.id(graph.source(edges[i - 1])) <=
83 graph.id(graph.source(edges[i])), "Test failed");
88 void checkGraphCounterSort() {
89 typedef SmartGraph Graph;
90 typedef Graph::Node Node;
91 typedef Graph::Edge Edge;
94 const int e = (int)(n * log((double)n));
99 for (int i = 0; i < n; ++i) {
100 nodes.push_back(graph.addNode());
103 for (int i = 0; i < e; ++i) {
104 int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
105 int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
106 edges.push_back(graph.addEdge(nodes[s], nodes[t]));
109 counterSort(edges.begin(), edges.end(),
110 mapFunctor(composeMap(IdMap<Graph, Node>(graph),
113 counterSort(edges.begin(), edges.end(),
114 mapFunctor(composeMap(IdMap<Graph, Node>(graph),
117 Graph::EdgeMap<bool> was(graph, false);
119 for (int i = 0; i < (int)edges.size(); ++i) {
120 check(!was[edges[i]], "Test failed");
121 was[edges[i]] = true;
124 for (int i = 1; i < (int)edges.size(); ++i) {
125 check(graph.id(graph.target(edges[i - 1])) <
126 graph.id(graph.target(edges[i])) ||
127 (graph.id(graph.target(edges[i - 1])) ==
128 graph.id(graph.target(edges[i])) &&
129 graph.id(graph.source(edges[i - 1])) <=
130 graph.id(graph.source(edges[i]))), "Test failed");
135 void checkGraphSorts() {
136 checkGraphRadixSort();
137 checkGraphCounterSort();