summaryrefslogtreecommitdiff
path: root/matching/src/tests/test_common.cpp
blob: 30465e4d197f48fd56bf734f5bf7ca22c5f47a23 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "catch/catch.hpp"

#include <sstream>
#include <iostream>
#include <string>

#include "common_util.h"
#include "simplex.h"
#include "matching_distance.h"

using namespace md;

TEST_CASE("Rational", "[common_utils][rational]")
{
    // gcd
    REQUIRE(gcd(10, 5) == 5);
    REQUIRE(gcd(5, 10) == 5);
    REQUIRE(gcd(5, 7) == 1);
    REQUIRE(gcd(7, 5) == 1);
    REQUIRE(gcd(13, 0) == 13);
    REQUIRE(gcd(0, 13) == 13);
    REQUIRE(gcd(16, 24) == 8);
    REQUIRE(gcd(24, 16) == 8);
    REQUIRE(gcd(16, 32) == 16);
    REQUIRE(gcd(32, 16) == 16);


    // reduce
    REQUIRE(reduce({2, 1}) == std::make_pair(2, 1));
    REQUIRE(reduce({1, 2}) == std::make_pair(1, 2));
    REQUIRE(reduce({2, 2}) == std::make_pair(1, 1));
    REQUIRE(reduce({0, 2}) == std::make_pair(0, 1));
    REQUIRE(reduce({0, 20}) == std::make_pair(0, 1));
    REQUIRE(reduce({35, 49}) == std::make_pair(5, 7));
    REQUIRE(reduce({35, 25}) == std::make_pair(7, 5));

    // midpoint
    REQUIRE(midpoint(Rational {0, 1}, Rational {1, 2}) == std::make_pair(1, 4));
    REQUIRE(midpoint(Rational {1, 4}, Rational {1, 2}) == std::make_pair(3, 8));
    REQUIRE(midpoint(Rational {1, 2}, Rational {1, 2}) == std::make_pair(1, 2));
    REQUIRE(midpoint(Rational {1, 2}, Rational {1, 1}) == std::make_pair(3, 4));
    REQUIRE(midpoint(Rational {3, 7}, Rational {5, 14}) == std::make_pair(11, 28));


    // arithmetic

    REQUIRE(Rational(1, 2) + Rational(3, 5) == Rational(11, 10));
    REQUIRE(Rational(2, 5) - Rational(3, 10) == Rational(1, 10));
    REQUIRE(Rational(2, 3) * Rational(4, 7) == Rational(8, 21));
    REQUIRE(Rational(2, 3) * Rational(3, 2) == Rational(1));
    REQUIRE(Rational(2, 3) / Rational(3, 2) == Rational(4, 9));
    REQUIRE(Rational(1, 2) * Rational(3, 5) == Rational(3, 10));

    // comparison
    REQUIRE(Rational(100000, 2000000) < Rational(100001, 2000000));
    REQUIRE(!(Rational(100001, 2000000) < Rational(100000, 2000000)));
    REQUIRE(!(Rational(100000, 2000000) < Rational(100000, 2000000)));
    REQUIRE(Rational(-100000, 2000000) < Rational(100001, 2000000));
    REQUIRE(Rational(-100001, 2000000) < Rational(100000, 2000000));
};

TEST_CASE("AbstractSimplex", "[abstract_simplex]")
{
    AbstractSimplex as;
    REQUIRE(as.dim() == -1);

    as.push_back(1);
    REQUIRE(as.dim() == 0);
    REQUIRE(as.facets().size() == 0);

    as.push_back(0);
    REQUIRE(as.dim() == 1);
    REQUIRE(as.facets().size() == 2);
    REQUIRE(as.facets()[0].dim() == 0);
    REQUIRE(as.facets()[1].dim() == 0);

}

TEST_CASE("Vertical line", "[vertical_line]")
{
    // line x = 1
    DualPoint l_vertical(AxisType::x_type, AngleType::steep, 0, 1);

    REQUIRE(l_vertical.is_vertical());
    REQUIRE(l_vertical.is_steep());

    Point p_1(0.5, 0.5);
    Point p_2(1.5, 0.5);
    Point p_3(1.5, 1.5);
    Point p_4(0.5, 1.5);
    Point p_5(1, 10);

    REQUIRE(l_vertical.x_from_y(10) == 1);
    REQUIRE(l_vertical.x_from_y(-10) == 1);
    REQUIRE(l_vertical.x_from_y(0) == 1);

    REQUIRE(not l_vertical.contains(p_1));
    REQUIRE(not l_vertical.contains(p_2));
    REQUIRE(not l_vertical.contains(p_3));
    REQUIRE(not l_vertical.contains(p_4));
    REQUIRE(l_vertical.contains(p_5));

    REQUIRE(l_vertical.goes_below(p_1));
    REQUIRE(not l_vertical.goes_below(p_2));
    REQUIRE(not l_vertical.goes_below(p_3));
    REQUIRE(l_vertical.goes_below(p_4));

    REQUIRE(not l_vertical.goes_above(p_1));
    REQUIRE(l_vertical.goes_above(p_2));
    REQUIRE(l_vertical.goes_above(p_3));
    REQUIRE(not l_vertical.goes_above(p_4));

}

TEST_CASE("Horizontal line", "[horizontal_line]")
{
    // line y = 1
    DualPoint l_horizontal(AxisType::y_type, AngleType::flat, 0, 1);

    REQUIRE(l_horizontal.is_horizontal());
    REQUIRE(l_horizontal.is_flat());
    REQUIRE(l_horizontal.y_slope() == 0);
    REQUIRE(l_horizontal.y_intercept() == 1);

    Point p_1(0.5, 0.5);
    Point p_2(1.5, 0.5);
    Point p_3(1.5, 1.5);
    Point p_4(0.5, 1.5);
    Point p_5(2, 1);

    REQUIRE((not l_horizontal.contains(p_1) and
            not l_horizontal.contains(p_2) and
            not l_horizontal.contains(p_3) and
            not l_horizontal.contains(p_4) and
            l_horizontal.contains(p_5)));

    REQUIRE(not l_horizontal.goes_below(p_1));
    REQUIRE(not l_horizontal.goes_below(p_2));
    REQUIRE(l_horizontal.goes_below(p_3));
    REQUIRE(l_horizontal.goes_below(p_4));
    REQUIRE(l_horizontal.goes_below(p_5));

    REQUIRE(l_horizontal.goes_above(p_1));
    REQUIRE(l_horizontal.goes_above(p_2));
    REQUIRE(not l_horizontal.goes_above(p_3));
    REQUIRE(not l_horizontal.goes_above(p_4));
    REQUIRE(l_horizontal.goes_above(p_5));
}

TEST_CASE("Flat Line with positive slope", "[flat_line]")
{
    // line y = x / 2 + 1
    DualPoint l_flat(AxisType::y_type, AngleType::flat, 0.5, 1);

    REQUIRE(not l_flat.is_horizontal());
    REQUIRE(l_flat.is_flat());
    REQUIRE(l_flat.y_slope() == 0.5);
    REQUIRE(l_flat.y_intercept() == 1);

    REQUIRE(l_flat.y_from_x(0) == 1);
    REQUIRE(l_flat.y_from_x(1) == 1.5);
    REQUIRE(l_flat.y_from_x(2) == 2);

    Point p_1(3, 2);
    Point p_2(-2, 0.01);
    Point p_3(0, 1.25);
    Point p_4(6, 4.5);
    Point p_5(2, 2);

    std::cout << "AHOY " << l_flat.y_from_x(p_2.x) << std::endl;

    REQUIRE((not l_flat.contains(p_1) and
            not l_flat.contains(p_2) and
            not l_flat.contains(p_3) and
            not l_flat.contains(p_4) and
            l_flat.contains(p_5)));

    REQUIRE(not l_flat.goes_below(p_1));
    REQUIRE(l_flat.goes_below(p_2));
    REQUIRE(l_flat.goes_below(p_3));
    REQUIRE(l_flat.goes_below(p_4));
    REQUIRE(l_flat.goes_below(p_5));

    REQUIRE(l_flat.goes_above(p_1));
    REQUIRE(not l_flat.goes_above(p_2));
    REQUIRE(not l_flat.goes_above(p_3));
    REQUIRE(not l_flat.goes_above(p_4));
    REQUIRE(l_flat.goes_above(p_5));

}