test/radix_sort_test.cc
author deba
Wed, 01 Mar 2006 10:17:25 +0000
changeset 1990 15fb7a4ea6be
parent 1844 eaa5f5b855f7
child 2242 16523135943d
permissions -rw-r--r--
Some classes assumed that the GraphMaps should be inherited
from an ObserverBase. These classes parents replaced with
DefaultMap which cause that the graph maps should not be
inherited from the ObserverBase.
alpar@1956
     1
/* -*- C++ -*-
alpar@1956
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@1956
     8
 *
alpar@1956
     9
 * Permission to use, modify and distribute this software is granted
alpar@1956
    10
 * provided that this copyright notice appears in all copies. For
alpar@1956
    11
 * precise terms see the accompanying LICENSE file.
alpar@1956
    12
 *
alpar@1956
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@1956
    14
 * express or implied, and with no claim as to its suitability for any
alpar@1956
    15
 * purpose.
alpar@1956
    16
 *
alpar@1956
    17
 */
alpar@1956
    18
deba@1833
    19
#include <lemon/time_measure.h>
deba@1833
    20
#include <lemon/smart_graph.h>
deba@1833
    21
#include <lemon/graph_utils.h>
deba@1833
    22
#include <lemon/maps.h>
deba@1833
    23
#include <lemon/radix_sort.h>
deba@1833
    24
deba@1833
    25
#include "test_tools.h"
deba@1833
    26
deba@1833
    27
deba@1833
    28
#include <vector>
deba@1833
    29
#include <algorithm>
deba@1833
    30
#include <cmath>
deba@1833
    31
deba@1833
    32
using namespace std;
deba@1833
    33
using namespace lemon;
deba@1833
    34
deba@1833
    35
void checkRadixSort() {
deba@1844
    36
  {
deba@1844
    37
    int n = 10000;
deba@1844
    38
    vector<int> data1(n), data2(n);
deba@1844
    39
    for (int i = 0; i < n; ++i) {
deba@1844
    40
      data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
deba@1844
    41
    }
deba@1844
    42
    radixSort(data1.begin(), data1.end());
deba@1844
    43
    sort(data2.begin(), data2.end());
deba@1844
    44
    for (int i = 0; i < n; ++i) {
deba@1844
    45
      check(data1[i] == data2[i], "Test failed");
deba@1844
    46
    }
deba@1844
    47
  } {
deba@1844
    48
    int n = 10000;
deba@1844
    49
    vector<unsigned char> data1(n), data2(n);
deba@1844
    50
    for (int i = 0; i < n; ++i) {
deba@1844
    51
      data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0)));
deba@1844
    52
    }
deba@1844
    53
    radixSort(data1.begin(), data1.end());
deba@1844
    54
    sort(data2.begin(), data2.end());
deba@1844
    55
    for (int i = 0; i < n; ++i) {
deba@1844
    56
      check(data1[i] == data2[i], "Test failed");
deba@1844
    57
    }
deba@1833
    58
  }
deba@1833
    59
}
deba@1833
    60
deba@1833
    61
deba@1833
    62
void checkCounterSort() {
deba@1844
    63
  {
deba@1844
    64
    int n = 10000;
deba@1844
    65
    vector<int> data1(n), data2(n);
deba@1844
    66
    for (int i = 0; i < n; ++i) {
deba@1844
    67
      data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
deba@1844
    68
    }
deba@1844
    69
    counterSort(data1.begin(), data1.end());
deba@1844
    70
    sort(data2.begin(), data2.end());
deba@1844
    71
    for (int i = 0; i < n; ++i) {
deba@1844
    72
      check(data1[i] == data2[i], "Test failed");
deba@1844
    73
    } 
deba@1844
    74
  } {
deba@1844
    75
    int n = 10000;
deba@1844
    76
    vector<unsigned char> data1(n), data2(n);
deba@1844
    77
    for (int i = 0; i < n; ++i) {
deba@1844
    78
      data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0)));
deba@1844
    79
    }
deba@1844
    80
    counterSort(data1.begin(), data1.end());
deba@1844
    81
    sort(data2.begin(), data2.end());
deba@1844
    82
    for (int i = 0; i < n; ++i) {
deba@1844
    83
      check(data1[i] == data2[i], "Test failed");
deba@1844
    84
    } 
deba@1833
    85
  }
deba@1833
    86
}
deba@1833
    87
deba@1833
    88
void checkSorts() {
deba@1833
    89
  checkRadixSort();
deba@1833
    90
  checkCounterSort();
deba@1833
    91
}
deba@1833
    92
deba@1833
    93
void checkGraphRadixSort() {
deba@1833
    94
  typedef SmartGraph Graph;
deba@1833
    95
  typedef Graph::Node Node;
deba@1833
    96
  typedef Graph::Edge Edge;
deba@1833
    97
deba@1833
    98
  const int n = 100;
deba@1833
    99
  const int e = (int)(n * log((double)n));
deba@1833
   100
deba@1833
   101
  Graph graph;
deba@1833
   102
  vector<Node> nodes;
deba@1833
   103
deba@1833
   104
  for (int i = 0; i < n; ++i) {
deba@1833
   105
    nodes.push_back(graph.addNode());
deba@1833
   106
  }
deba@1833
   107
  vector<Edge> edges;
deba@1833
   108
  for (int i = 0; i < e; ++i) {
deba@1833
   109
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
   110
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
   111
    edges.push_back(graph.addEdge(nodes[s], nodes[t]));
deba@1833
   112
  }
deba@1833
   113
deba@1833
   114
  radixSort(edges.begin(), edges.end(), 
deba@1833
   115
	    mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   116
				  sourceMap(graph))));
deba@1833
   117
deba@1833
   118
  Graph::EdgeMap<bool> was(graph, false);
deba@1833
   119
deba@1833
   120
  for (int i = 0; i < (int)edges.size(); ++i) {
deba@1833
   121
    check(!was[edges[i]], "Test failed");
deba@1833
   122
    was[edges[i]] = true;
deba@1833
   123
  }
deba@1833
   124
deba@1833
   125
  for (int i = 1; i < (int)edges.size(); ++i) {
deba@1833
   126
    check(graph.id(graph.source(edges[i - 1])) <= 
deba@1833
   127
	  graph.id(graph.source(edges[i])), "Test failed");
deba@1833
   128
  }
deba@1833
   129
deba@1833
   130
}
deba@1833
   131
deba@1833
   132
void checkGraphCounterSort() {
deba@1833
   133
  typedef SmartGraph Graph;
deba@1833
   134
  typedef Graph::Node Node;
deba@1833
   135
  typedef Graph::Edge Edge;
deba@1833
   136
deba@1833
   137
  const int n = 100;
deba@1833
   138
  const int e = (int)(n * log((double)n));
deba@1833
   139
deba@1833
   140
  Graph graph;
deba@1833
   141
  vector<Node> nodes;
deba@1833
   142
deba@1833
   143
  for (int i = 0; i < n; ++i) {
deba@1833
   144
    nodes.push_back(graph.addNode());
deba@1833
   145
  }
deba@1833
   146
  vector<Edge> edges;
deba@1833
   147
  for (int i = 0; i < e; ++i) {
deba@1833
   148
    int s = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
   149
    int t = (int)(n * (double)rand() / (RAND_MAX + 1.0));
deba@1833
   150
    edges.push_back(graph.addEdge(nodes[s], nodes[t]));
deba@1833
   151
  }
deba@1833
   152
deba@1833
   153
  counterSort(edges.begin(), edges.end(), 
deba@1833
   154
	      mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   155
				    sourceMap(graph))));
deba@1833
   156
deba@1833
   157
  counterSort(edges.begin(), edges.end(), 
deba@1833
   158
	      mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   159
				    targetMap(graph))));
deba@1833
   160
deba@1833
   161
  Graph::EdgeMap<bool> was(graph, false);
deba@1833
   162
deba@1833
   163
  for (int i = 0; i < (int)edges.size(); ++i) {
deba@1833
   164
    check(!was[edges[i]], "Test failed");
deba@1833
   165
    was[edges[i]] = true;
deba@1833
   166
  }
deba@1833
   167
deba@1833
   168
  for (int i = 1; i < (int)edges.size(); ++i) {
deba@1833
   169
    check(graph.id(graph.target(edges[i - 1])) < 
deba@1833
   170
	  graph.id(graph.target(edges[i])) || 
deba@1833
   171
	  (graph.id(graph.target(edges[i - 1])) ==
deba@1833
   172
	   graph.id(graph.target(edges[i])) &&
deba@1833
   173
	   graph.id(graph.source(edges[i - 1])) <= 
deba@1833
   174
	   graph.id(graph.source(edges[i]))), "Test failed");
deba@1833
   175
  }
deba@1833
   176
deba@1833
   177
}
deba@1833
   178
deba@1833
   179
void checkGraphSorts() {
deba@1833
   180
  checkGraphRadixSort();
deba@1833
   181
  checkGraphCounterSort();
deba@1833
   182
}
deba@1833
   183
deba@1833
   184
int main() {
deba@1833
   185
  checkSorts();
deba@1833
   186
  checkGraphSorts();
deba@1833
   187
  return 0;
deba@1833
   188
}