~lib2geom-hackers/lib2geom/trunk

« back to all changes in this revision

Viewing changes to types-test.cpp

  • Committer: njh
  • Date: 2006-05-22 11:50:24 UTC
  • Revision ID: svn-v4:4601daaa-0314-0410-9a8b-c964a3c23b6b:trunk/lib2geom:1
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "../utest/utest.h"
 
2
#include <libnr/types.h>
 
3
#include <libnr/point-fns.h>
 
4
#include <cmath>
 
5
using Geom::Point;
 
6
using Geom::X;
 
7
using Geom::Y;
 
8
 
 
9
 
 
10
int main(int argc, char *argv[]) {
 
11
        utest_start("Basic Geom::Point operations");
 
12
 
 
13
        UTEST_TEST("X,Y values") {
 
14
                UTEST_ASSERT(X == 0);
 
15
                UTEST_ASSERT(Y == 1);
 
16
        }
 
17
 
 
18
        Geom::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
        }
 
23
 
 
24
        Geom::Point const b(-2.0, 3.0);
 
25
 
 
26
        UTEST_TEST("copy constructor") {
 
27
                Geom::Point a_copy(a);
 
28
                UTEST_ASSERT(a == a_copy);
 
29
                UTEST_ASSERT(!(a != a_copy));
 
30
        }
 
31
 
 
32
        UTEST_TEST("non-const operator[]") {
 
33
                Geom::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
        }
 
40
 
 
41
        Geom::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
        }
 
49
 
 
50
        UTEST_TEST("unary-") {
 
51
                UTEST_ASSERT(-a == Point(-a[X], -a[Y]));
 
52
        }
 
53
 
 
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
        }
 
59
 
 
60
        UTEST_TEST("dot") {
 
61
                UTEST_ASSERT( dot(a, b) == ( a[X] * b[X]  +
 
62
                                             a[Y] * b[Y] ) );
 
63
                UTEST_ASSERT( dot(a, Geom::rot90(a)) == 0.0 );
 
64
                UTEST_ASSERT( dot(-a, Geom::rot90(a)) == 0.0 );
 
65
        }
 
66
 
 
67
        double const small = pow(2.0, -1070);
 
68
 
 
69
        Point const small_left(-small, 0.0);
 
70
        Point const smallish_3_neg4(3.0 * small, -4.0 * small);
 
71
 
 
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);
 
76
 
 
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
        }
 
81
 
 
82
        UTEST_TEST("operator+=") {
 
83
                Point x(a);
 
84
                x += b;
 
85
                UTEST_ASSERT(x == ab);
 
86
        }
 
87
 
 
88
        UTEST_TEST("operator/=") {
 
89
                Point x(a);
 
90
                x /= .5;
 
91
                UTEST_ASSERT(x == a + a);
 
92
        }
 
93
 
 
94
        UTEST_TEST("normalize") {
 
95
                Point x(small_left);
 
96
                x.normalize();
 
97
                UTEST_ASSERT(x == Point(-1.0, 0.0));
 
98
 
 
99
                x = smallish_3_neg4;
 
100
                x.normalize();
 
101
                UTEST_ASSERT(x == Point(0.6, -0.8));
 
102
        }
 
103
 
 
104
        return utest_end() ? 0 : 1;
 
105
}