summaryrefslogtreecommitdiffstats
path: root/src/libnr/nr-point-fns-test.h
blob: 509d9d2fab931d06eb4d37c00e02633b8e96e64d (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
// nr-point-fns-test.h
#include <cxxtest/TestSuite.h>

#include <cassert>
#include <cmath>
#include <glib/gmacros.h>
#include <stdlib.h>

#include "libnr/nr-point-fns.h"
#include "isnan.h"

using NR::Point;

class NrPointFnsTest : public CxxTest::TestSuite
{
public:
    NrPointFnsTest() :
        setupValid(true),
        p3n4( 3.0, -4.0 ),
        p0( 0.0, 0.0 ),
        small( pow( 2.0, -1070 ) ),
        inf( 1e400 ),
        nan( inf - inf ),
        small_left( -small, 0.0 ),
        small_n3_4( -3.0 * small, 4.0 * small ),
        part_nan( 3., nan ),
        inf_left( -inf, 5.0 )
    {
        TS_ASSERT( isNaN(nan) );
        TS_ASSERT( !isNaN(small) );

        setupValid &= isNaN(nan);
        setupValid &= !isNaN(small);
    }
    virtual ~NrPointFnsTest() {}

// createSuite and destroySuite get us per-suite setup and teardown
// without us having to worry about static initialization order, etc.
    static NrPointFnsTest *createSuite() { return new NrPointFnsTest(); }
    static void destroySuite( NrPointFnsTest *suite ) { delete suite; }

// Called before each test in this suite
    void setUp()
    {
        TS_ASSERT( setupValid );
    }

    bool setupValid;
    Point const p3n4;
    Point const p0;
    double const small;
    double const inf;
    double const nan;

    Point const small_left;
    Point const small_n3_4;
    Point const part_nan;
    Point const inf_left;


    void testL1(void)
    {
        TS_ASSERT_EQUALS( NR::L1(p0), 0.0  );
        TS_ASSERT_EQUALS( NR::L1(p3n4), 7.0  );
        TS_ASSERT_EQUALS( NR::L1(small_left), small  );
        TS_ASSERT_EQUALS( NR::L1(inf_left), inf  );
        TS_ASSERT_EQUALS( NR::L1(small_n3_4), 7.0 * small  );
        TS_ASSERT(isNaN(NR::L1(part_nan)));
    }

    void testL2(void)
    {
        TS_ASSERT_EQUALS( NR::L2(p0), 0.0 );
        TS_ASSERT_EQUALS( NR::L2(p3n4), 5.0 );
        TS_ASSERT_EQUALS( NR::L2(small_left), small );
        TS_ASSERT_EQUALS( NR::L2(inf_left), inf );
        TS_ASSERT_EQUALS( NR::L2(small_n3_4), 5.0 * small );
        TS_ASSERT( isNaN(NR::L2(part_nan)) );
    }

    void testLInfty(void)
    {
        TS_ASSERT_EQUALS( NR::LInfty(p0), 0.0 );
        TS_ASSERT_EQUALS( NR::LInfty(p3n4), 4.0 );
        TS_ASSERT_EQUALS( NR::LInfty(small_left), small );
        TS_ASSERT_EQUALS( NR::LInfty(inf_left), inf );
        TS_ASSERT_EQUALS( NR::LInfty(small_n3_4), 4.0 * small );
        TS_ASSERT( isNaN(NR::LInfty(part_nan)) );
    }

    void testIsZero(void)
    {
        TS_ASSERT( NR::is_zero(p0) );
        TS_ASSERT( !NR::is_zero(p3n4) );
        TS_ASSERT( !NR::is_zero(small_left) );
        TS_ASSERT( !NR::is_zero(inf_left) );
        TS_ASSERT( !NR::is_zero(small_n3_4) );
        TS_ASSERT( !NR::is_zero(part_nan) );
    }

    void testAtan2(void)
    {
        TS_ASSERT_EQUALS( NR::atan2(p3n4), atan2(-4.0, 3.0) );
        TS_ASSERT_EQUALS( NR::atan2(small_left), atan2(0.0, -1.0) );
        TS_ASSERT_EQUALS( NR::atan2(small_n3_4), atan2(4.0, -3.0) );
    }

    void testUnitVector(void)
    {
        TS_ASSERT_EQUALS( NR::unit_vector(p3n4), Point(.6, -0.8) );
        TS_ASSERT_EQUALS( NR::unit_vector(small_left), Point(-1.0, 0.0) );
        TS_ASSERT_EQUALS( NR::unit_vector(small_n3_4), Point(-.6, 0.8) );
    }

    void testIsUnitVector(void)
    {
        TS_ASSERT( !NR::is_unit_vector(p3n4) );
        TS_ASSERT( !NR::is_unit_vector(small_left) );
        TS_ASSERT( !NR::is_unit_vector(small_n3_4) );
        TS_ASSERT( !NR::is_unit_vector(part_nan) );
        TS_ASSERT( !NR::is_unit_vector(inf_left) );
        TS_ASSERT( !NR::is_unit_vector(Point(.5, 0.5)) );
        TS_ASSERT( NR::is_unit_vector(Point(.6, -0.8)) );
        TS_ASSERT( NR::is_unit_vector(Point(-.6, 0.8)) );
        TS_ASSERT( NR::is_unit_vector(Point(-1.0, 0.0)) );
        TS_ASSERT( NR::is_unit_vector(Point(1.0, 0.0)) );
        TS_ASSERT( NR::is_unit_vector(Point(0.0, -1.0)) );
        TS_ASSERT( NR::is_unit_vector(Point(0.0, 1.0)) );
    }
};

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :