tstOrdering.cc 1.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "gtest/gtest.h"

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

TEST(radixalgorithm, Ordering)
{

    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;
    for(size_t i = 0; i < permutation.size(); ++i)
    {
        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);
    for(size_t i = 0; i < list.size(); ++i)
    {
        std::cout << "[" << list[i] << "," << list2[i] << "]" << std::endl;
    }
}