Code

Cmake: added livarot as a lib, fixes problem with linking
[inkscape.git] / src / libnr / nr-types-test.cpp
1 #include "../utest/utest.h"
2 #include <libnr/nr-types.h>
3 #include <libnr/nr-point-fns.h>
4 #include <cmath>
5 using NR::Point;
6 using NR::X;
7 using NR::Y;
10 int main(int argc, char *argv[]) {
11         utest_start("Basic NR::Point operations");
13         UTEST_TEST("X,Y values") {
14                 UTEST_ASSERT(X == 0);
15                 UTEST_ASSERT(Y == 1);
16         }
18         NR::Point const a(1.5, 2.0);
19         UTEST_TEST("x,y constructor and operator[] const") {
20                 UTEST_ASSERT(a[X] == 1.5);
21                 UTEST_ASSERT(a[Y] == 2.0);
22         }
24         NR::Point const b(-2.0, 3.0);
26         UTEST_TEST("copy constructor") {
27                 NR::Point a_copy(a);
28                 UTEST_ASSERT(a == a_copy);
29                 UTEST_ASSERT(!(a != a_copy));
30         }
32         UTEST_TEST("non-const operator[]") {
33                 NR::Point a_copy(a);
34                 a_copy[X] = -2.0;
35                 UTEST_ASSERT(a_copy != a);
36                 UTEST_ASSERT(a_copy != b);
37                 a_copy[Y] = 3.0;
38                 UTEST_ASSERT(a_copy == b);
39         }
41         NR::Point const ab(-0.5, 5.0);
42         UTEST_TEST("binary +, -") {
43                 UTEST_ASSERT(a != b);
44                 UTEST_ASSERT(a + b == ab);
45                 UTEST_ASSERT(ab - a == b);
46                 UTEST_ASSERT(ab - b == a);
47                 UTEST_ASSERT(ab + a != b);
48         }
50         UTEST_TEST("unary-") {
51                 UTEST_ASSERT(-a == Point(-a[X], -a[Y]));
52         }
54         UTEST_TEST("scale, divide") {
55                 UTEST_ASSERT(-a == -1.0 * a);
56                 UTEST_ASSERT(a + a + a == 3.0 * a);
57                 UTEST_ASSERT(a / .5 == 2.0 * a);
58         }
60         UTEST_TEST("dot") {
61                 UTEST_ASSERT( dot(a, b) == ( a[X] * b[X]  +
62                                              a[Y] * b[Y] ) );
63                 UTEST_ASSERT( dot(a, NR::rot90(a)) == 0.0 );
64                 UTEST_ASSERT( dot(-a, NR::rot90(a)) == 0.0 );
65         }
67         double const small = pow(2.0, -1070);
69         Point const small_left(-small, 0.0);
70         Point const smallish_3_neg4(3.0 * small, -4.0 * small);
72         UTEST_TEST("L1, L2, LInfty norms") {
73                 UTEST_ASSERT(L1(small_left) == small);
74                 UTEST_ASSERT(L2(small_left) == small);
75                 UTEST_ASSERT(LInfty(small_left) == small);
77                 UTEST_ASSERT(L1(smallish_3_neg4) == 7.0 * small);
78                 UTEST_ASSERT(L2(smallish_3_neg4) == 5.0 * small);
79                 UTEST_ASSERT(LInfty(smallish_3_neg4) == 4.0 * small);
80         }
82         UTEST_TEST("operator+=") {
83                 Point x(a);
84                 x += b;
85                 UTEST_ASSERT(x == ab);
86         }
88         UTEST_TEST("operator/=") {
89                 Point x(a);
90                 x /= .5;
91                 UTEST_ASSERT(x == a + a);
92         }
94         UTEST_TEST("normalize") {
95                 Point x(small_left);
96                 x.normalize();
97                 UTEST_ASSERT(x == Point(-1.0, 0.0));
99                 x = smallish_3_neg4;
100                 x.normalize();
101                 UTEST_ASSERT(x == Point(0.6, -0.8));
102         }
104         return utest_end() ? 0 : 1;