test/radix_sort_test.cc
author deba
Thu, 01 Dec 2005 15:08:46 +0000
changeset 1842 8abf74160dc4
child 1844 eaa5f5b855f7
permissions -rw-r--r--
NewEdgeSetAdaptor -> ListEdgeSet
and moved to edge_set.h
deba@1833
     1
#include <lemon/time_measure.h>
deba@1833
     2
#include <lemon/smart_graph.h>
deba@1833
     3
#include <lemon/graph_utils.h>
deba@1833
     4
#include <lemon/maps.h>
deba@1833
     5
#include <lemon/radix_sort.h>
deba@1833
     6
deba@1833
     7
#include "test_tools.h"
deba@1833
     8
deba@1833
     9
deba@1833
    10
#include <vector>
deba@1833
    11
#include <algorithm>
deba@1833
    12
#include <cmath>
deba@1833
    13
deba@1833
    14
using namespace std;
deba@1833
    15
using namespace lemon;
deba@1833
    16
deba@1833
    17
void checkRadixSort() {
deba@1833
    18
  int n = 10000;
deba@1833
    19
  vector<int> data1(n), data2(n);
deba@1833
    20
  for (int i = 0; i < n; ++i) {
deba@1833
    21
    data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
deba@1833
    22
  }
deba@1833
    23
  radixSort(data1.begin(), data1.end());
deba@1833
    24
  sort(data2.begin(), data2.end());
deba@1833
    25
  for (int i = 0; i < n; ++i) {
deba@1833
    26
    check(data1[i] == data2[i], "Test failed");
deba@1833
    27
  }
deba@1833
    28
}
deba@1833
    29
deba@1833
    30
deba@1833
    31
void checkCounterSort() {
deba@1833
    32
  int n = 10000;
deba@1833
    33
  vector<int> data1(n), data2(n);
deba@1833
    34
  for (int i = 0; i < n; ++i) {
deba@1833
    35
    data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
deba@1833
    36
  }
deba@1833
    37
  counterSort(data1.begin(), data1.end());
deba@1833
    38
  sort(data2.begin(), data2.end());
deba@1833
    39
  for (int i = 0; i < n; ++i) {
deba@1833
    40
    check(data1[i] == data2[i], "Test failed");
deba@1833
    41
  }
deba@1833
    42
}
deba@1833
    43
deba@1833
    44
void checkSorts() {
deba@1833
    45
  checkRadixSort();
deba@1833
    46
  checkCounterSort();
deba@1833
    47
}
deba@1833
    48
deba@1833
    49
void checkGraphRadixSort() {
deba@1833
    50
  typedef SmartGraph Graph;
deba@1833
    51
  typedef Graph::Node Node;
deba@1833
    52
  typedef Graph::Edge Edge;
deba@1833
    53
deba@1833
    54
  const int n = 100;
deba@1833
    55
  const int e = (int)(n * log((double)n));
deba@1833
    56
deba@1833
    57
  Graph graph;
deba@1833
    58
  vector<Node> nodes;
deba@1833
    59
deba@1833
    60
  for (int i = 0; i < n; ++i) {
deba@1833
    61
    nodes.push_back(graph.addNode());
deba@1833
    62
  }
deba@1833
    63
  vector<Edge> edges;
deba@1833
    64
  for (int i = 0; i < e; ++i) {
deba@1833
    65
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
    66
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
    67
    edges.push_back(graph.addEdge(nodes[s], nodes[t]));
deba@1833
    68
  }
deba@1833
    69
deba@1833
    70
  radixSort(edges.begin(), edges.end(), 
deba@1833
    71
	    mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
    72
				  sourceMap(graph))));
deba@1833
    73
deba@1833
    74
  Graph::EdgeMap<bool> was(graph, false);
deba@1833
    75
deba@1833
    76
  for (int i = 0; i < (int)edges.size(); ++i) {
deba@1833
    77
    check(!was[edges[i]], "Test failed");
deba@1833
    78
    was[edges[i]] = true;
deba@1833
    79
  }
deba@1833
    80
deba@1833
    81
  for (int i = 1; i < (int)edges.size(); ++i) {
deba@1833
    82
    check(graph.id(graph.source(edges[i - 1])) <= 
deba@1833
    83
	  graph.id(graph.source(edges[i])), "Test failed");
deba@1833
    84
  }
deba@1833
    85
deba@1833
    86
}
deba@1833
    87
deba@1833
    88
void checkGraphCounterSort() {
deba@1833
    89
  typedef SmartGraph Graph;
deba@1833
    90
  typedef Graph::Node Node;
deba@1833
    91
  typedef Graph::Edge Edge;
deba@1833
    92
deba@1833
    93
  const int n = 100;
deba@1833
    94
  const int e = (int)(n * log((double)n));
deba@1833
    95
deba@1833
    96
  Graph graph;
deba@1833
    97
  vector<Node> nodes;
deba@1833
    98
deba@1833
    99
  for (int i = 0; i < n; ++i) {
deba@1833
   100
    nodes.push_back(graph.addNode());
deba@1833
   101
  }
deba@1833
   102
  vector<Edge> edges;
deba@1833
   103
  for (int i = 0; i < e; ++i) {
deba@1833
   104
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
   105
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
   106
    edges.push_back(graph.addEdge(nodes[s], nodes[t]));
deba@1833
   107
  }
deba@1833
   108
deba@1833
   109
  counterSort(edges.begin(), edges.end(), 
deba@1833
   110
	      mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   111
				    sourceMap(graph))));
deba@1833
   112
deba@1833
   113
  counterSort(edges.begin(), edges.end(), 
deba@1833
   114
	      mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   115
				    targetMap(graph))));
deba@1833
   116
deba@1833
   117
  Graph::EdgeMap<bool> was(graph, false);
deba@1833
   118
deba@1833
   119
  for (int i = 0; i < (int)edges.size(); ++i) {
deba@1833
   120
    check(!was[edges[i]], "Test failed");
deba@1833
   121
    was[edges[i]] = true;
deba@1833
   122
  }
deba@1833
   123
deba@1833
   124
  for (int i = 1; i < (int)edges.size(); ++i) {
deba@1833
   125
    check(graph.id(graph.target(edges[i - 1])) < 
deba@1833
   126
	  graph.id(graph.target(edges[i])) || 
deba@1833
   127
	  (graph.id(graph.target(edges[i - 1])) ==
deba@1833
   128
	   graph.id(graph.target(edges[i])) &&
deba@1833
   129
	   graph.id(graph.source(edges[i - 1])) <= 
deba@1833
   130
	   graph.id(graph.source(edges[i]))), "Test failed");
deba@1833
   131
  }
deba@1833
   132
deba@1833
   133
}
deba@1833
   134
deba@1833
   135
void checkGraphSorts() {
deba@1833
   136
  checkGraphRadixSort();
deba@1833
   137
  checkGraphCounterSort();
deba@1833
   138
}
deba@1833
   139
deba@1833
   140
int main() {
deba@1833
   141
  checkSorts();
deba@1833
   142
  checkGraphSorts();
deba@1833
   143
  return 0;
deba@1833
   144
}