test/radix_sort_test.cc
author kpeter
Thu, 28 Feb 2008 02:58:26 +0000
changeset 2584 84ef3c5b3698
parent 2553 bfced05fa852
permissions -rw-r--r--
Test file for the min cost flow algorithms.
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@2553
     5
 * Copyright (C) 2003-2008
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>
alpar@2569
    30
#include <lemon/math.h>
deba@1833
    31
deba@1833
    32
using namespace std;
deba@1833
    33
using namespace lemon;
deba@1833
    34
deba@2386
    35
typedef unsigned char uchar;
deba@2386
    36
deba@1833
    37
void checkRadixSort() {
deba@1844
    38
  {
deba@1844
    39
    int n = 10000;
deba@1844
    40
    vector<int> data1(n), data2(n);
deba@1844
    41
    for (int i = 0; i < n; ++i) {
deba@2242
    42
      data1[i] = data2[i] = rnd[1000] - 500;
deba@1844
    43
    }
deba@1844
    44
    radixSort(data1.begin(), data1.end());
deba@1844
    45
    sort(data2.begin(), data2.end());
deba@1844
    46
    for (int i = 0; i < n; ++i) {
deba@1844
    47
      check(data1[i] == data2[i], "Test failed");
deba@1844
    48
    }
deba@1844
    49
  } {
deba@1844
    50
    int n = 10000;
deba@1844
    51
    vector<unsigned char> data1(n), data2(n);
deba@1844
    52
    for (int i = 0; i < n; ++i) {
deba@2386
    53
      data1[i] = data2[i] = rnd[uchar(200)];
deba@1844
    54
    }
deba@1844
    55
    radixSort(data1.begin(), data1.end());
deba@1844
    56
    sort(data2.begin(), data2.end());
deba@1844
    57
    for (int i = 0; i < n; ++i) {
deba@1844
    58
      check(data1[i] == data2[i], "Test failed");
deba@1844
    59
    }
deba@1833
    60
  }
deba@1833
    61
}
deba@1833
    62
deba@1833
    63
deba@1833
    64
void checkCounterSort() {
deba@1844
    65
  {
deba@1844
    66
    int n = 10000;
deba@1844
    67
    vector<int> data1(n), data2(n);
deba@1844
    68
    for (int i = 0; i < n; ++i) {
deba@2242
    69
      data1[i] = data2[i] = rnd[1000] - 500;
deba@1844
    70
    }
deba@1844
    71
    counterSort(data1.begin(), data1.end());
deba@1844
    72
    sort(data2.begin(), data2.end());
deba@1844
    73
    for (int i = 0; i < n; ++i) {
deba@1844
    74
      check(data1[i] == data2[i], "Test failed");
deba@1844
    75
    } 
deba@1844
    76
  } {
deba@1844
    77
    int n = 10000;
deba@1844
    78
    vector<unsigned char> data1(n), data2(n);
deba@1844
    79
    for (int i = 0; i < n; ++i) {
deba@2386
    80
      data1[i] = data2[i] = rnd[uchar(200)];
deba@1844
    81
    }
deba@1844
    82
    counterSort(data1.begin(), data1.end());
deba@1844
    83
    sort(data2.begin(), data2.end());
deba@1844
    84
    for (int i = 0; i < n; ++i) {
deba@1844
    85
      check(data1[i] == data2[i], "Test failed");
deba@1844
    86
    } 
deba@1833
    87
  }
deba@1833
    88
}
deba@1833
    89
deba@1833
    90
void checkSorts() {
deba@1833
    91
  checkRadixSort();
deba@1833
    92
  checkCounterSort();
deba@1833
    93
}
deba@1833
    94
deba@1833
    95
void checkGraphRadixSort() {
deba@1833
    96
  typedef SmartGraph Graph;
deba@1833
    97
  typedef Graph::Node Node;
deba@1833
    98
  typedef Graph::Edge Edge;
deba@1833
    99
deba@1833
   100
  const int n = 100;
deba@2386
   101
  const int e = int(n * log(double(n)));
deba@1833
   102
deba@1833
   103
  Graph graph;
deba@1833
   104
  vector<Node> nodes;
deba@1833
   105
deba@1833
   106
  for (int i = 0; i < n; ++i) {
deba@1833
   107
    nodes.push_back(graph.addNode());
deba@1833
   108
  }
deba@1833
   109
  vector<Edge> edges;
deba@1833
   110
  for (int i = 0; i < e; ++i) {
deba@2242
   111
    int s = rnd[n];
deba@2242
   112
    int t = rnd[n];
deba@1833
   113
    edges.push_back(graph.addEdge(nodes[s], nodes[t]));
deba@1833
   114
  }
deba@1833
   115
deba@1833
   116
  radixSort(edges.begin(), edges.end(), 
deba@1833
   117
	    mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   118
				  sourceMap(graph))));
deba@1833
   119
deba@1833
   120
  Graph::EdgeMap<bool> was(graph, false);
deba@1833
   121
deba@2386
   122
  for (int i = 0; i < int(edges.size()); ++i) {
deba@1833
   123
    check(!was[edges[i]], "Test failed");
deba@1833
   124
    was[edges[i]] = true;
deba@1833
   125
  }
deba@1833
   126
deba@2386
   127
  for (int i = 1; i < int(edges.size()); ++i) {
deba@1833
   128
    check(graph.id(graph.source(edges[i - 1])) <= 
deba@1833
   129
	  graph.id(graph.source(edges[i])), "Test failed");
deba@1833
   130
  }
deba@1833
   131
deba@1833
   132
}
deba@1833
   133
deba@1833
   134
void checkGraphCounterSort() {
deba@1833
   135
  typedef SmartGraph Graph;
deba@1833
   136
  typedef Graph::Node Node;
deba@1833
   137
  typedef Graph::Edge Edge;
deba@1833
   138
deba@1833
   139
  const int n = 100;
deba@2386
   140
  const int e = int(n * log(double(n)));
deba@1833
   141
deba@1833
   142
  Graph graph;
deba@1833
   143
  vector<Node> nodes;
deba@1833
   144
deba@1833
   145
  for (int i = 0; i < n; ++i) {
deba@1833
   146
    nodes.push_back(graph.addNode());
deba@1833
   147
  }
deba@1833
   148
  vector<Edge> edges;
deba@1833
   149
  for (int i = 0; i < e; ++i) {
deba@2242
   150
    int s = rnd[n];
deba@2242
   151
    int t = rnd[n];
deba@1833
   152
    edges.push_back(graph.addEdge(nodes[s], nodes[t]));
deba@1833
   153
  }
deba@1833
   154
deba@1833
   155
  counterSort(edges.begin(), edges.end(), 
deba@1833
   156
	      mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   157
				    sourceMap(graph))));
deba@1833
   158
deba@1833
   159
  counterSort(edges.begin(), edges.end(), 
deba@1833
   160
	      mapFunctor(composeMap(IdMap<Graph, Node>(graph), 
deba@1833
   161
				    targetMap(graph))));
deba@1833
   162
deba@1833
   163
  Graph::EdgeMap<bool> was(graph, false);
deba@1833
   164
deba@2386
   165
  for (int i = 0; i < int(edges.size()); ++i) {
deba@1833
   166
    check(!was[edges[i]], "Test failed");
deba@1833
   167
    was[edges[i]] = true;
deba@1833
   168
  }
deba@1833
   169
deba@2386
   170
  for (int i = 1; i < int(edges.size()); ++i) {
deba@1833
   171
    check(graph.id(graph.target(edges[i - 1])) < 
deba@1833
   172
	  graph.id(graph.target(edges[i])) || 
deba@1833
   173
	  (graph.id(graph.target(edges[i - 1])) ==
deba@1833
   174
	   graph.id(graph.target(edges[i])) &&
deba@1833
   175
	   graph.id(graph.source(edges[i - 1])) <= 
deba@1833
   176
	   graph.id(graph.source(edges[i]))), "Test failed");
deba@1833
   177
  }
deba@1833
   178
deba@1833
   179
}
deba@1833
   180
deba@1833
   181
void checkGraphSorts() {
deba@1833
   182
  checkGraphRadixSort();
deba@1833
   183
  checkGraphCounterSort();
deba@1833
   184
}
deba@1833
   185
deba@1833
   186
int main() {
deba@1833
   187
  checkSorts();
deba@1833
   188
  checkGraphSorts();
deba@1833
   189
  return 0;
deba@1833
   190
}