tstOrdering.cc 1.23 KB
Newer Older
1
2
3
4
#include <iostream>
#include "gtest/gtest.h"

#include "radixalgorithm/ordering.hh"
5
#include "radixbug/bug.hh"
6

7
8
TEST(radixalgorithm, Ordering)
{
9
10
11
12
13
14
15
16
17
18
19
20
21
  std::vector<double> list{2.0, 1.0, 3.0, 4.0, 5.0, 0.0, 10.0};
  // permutation              5,   1,   0,   2,   3,   4,    6
  // correct ordering
  std::vector<int> list2{0, 1, 2, 3, 4, 5, 6};
  auto comparator = [](double a, double b) {
    radix_line("\t[" << a << "," << b << "]");
    return a < b;
  };
  std::vector<size_t> permutation = radix::sort_permutation(list, comparator);
  // test edges
  EXPECT_EQ(permutation[0], 5);
  EXPECT_EQ(permutation[6], 6);
  std::cout << "Ordering:" << std::endl;
22
23
  for (size_t i = 0; i < permutation.size(); ++i)
  {
24
25
26
27
28
29
30
31
32
33
34
35
    std::cout << "index " << i << ". (" << list[i] << ") recieves value from "
              << permutation[i] << " (" << list[permutation[i]] << ")"
              << std::endl;
  }
  //
  // sort list given the
  radix::apply_permutation(list, permutation);
  EXPECT_DOUBLE_EQ(list[0], 0.0);
  EXPECT_DOUBLE_EQ(list[6], 10.0);
  radix::apply_permutation(list2, permutation);
  EXPECT_EQ(list2[0], 5);
  EXPECT_EQ(list2[6], 6);
36
37
  for (size_t i = 0; i < list.size(); ++i)
  {
38
39
    std::cout << "[" << list[i] << "," << list2[i] << "]" << std::endl;
  }
40
}