1 // nr-types-test.h
2 #include <cxxtest/TestSuite.h>
4 #include "libnr/nr-types.h"
5 #include "libnr/nr-point-fns.h"
6 #include <cmath>
8 class NrTypesTest : public CxxTest::TestSuite
9 {
10 public:
11 NrTypesTest() :
12 a( 1.5, 2.0 ),
13 b(-2.0, 3.0),
14 ab(-0.5, 5.0),
15 small(pow(2.0, -1070)),
16 small_left(-small, 0.0),
17 smallish_3_neg4(3.0 * small, -4.0 * small)
18 {}
19 virtual ~NrTypesTest() {}
21 // createSuite and destroySuite get us per-suite setup and teardown
22 // without us having to worry about static initialization order, etc.
23 static NrTypesTest *createSuite() { return new NrTypesTest(); }
24 static void destroySuite( NrTypesTest *suite ) { delete suite; }
26 NR::Point const a;
27 NR::Point const b;
28 NR::Point const ab;
29 double const small;
30 NR::Point const small_left;
31 NR::Point const smallish_3_neg4;
34 void testXYValues( void )
35 {
36 TS_ASSERT_EQUALS( NR::X, 0 );
37 TS_ASSERT_EQUALS( NR::Y, 1 );
38 }
40 void testXYCtorAndArrayConst(void)
41 {
42 TS_ASSERT_EQUALS( a[NR::X], 1.5 );
43 TS_ASSERT_EQUALS( a[NR::Y], 2.0 );
44 }
46 void testCopyCtor(void)
47 {
48 NR::Point a_copy(a);
50 TS_ASSERT_EQUALS( a, a_copy );
51 TS_ASSERT( !(a != a_copy) );
52 }
54 void testNonConstArrayOperator(void)
55 {
56 NR::Point a_copy(a);
57 a_copy[NR::X] = -2.0;
58 TS_ASSERT_DIFFERS( a_copy, a );
59 TS_ASSERT_DIFFERS( a_copy, b );
60 a_copy[NR::Y] = 3.0;
61 TS_ASSERT_EQUALS( a_copy, b );
62 }
64 void testBinaryPlusMinus(void)
65 {
66 TS_ASSERT_DIFFERS( a, b );
67 TS_ASSERT_EQUALS( a + b, ab );
68 TS_ASSERT_EQUALS( ab - a, b );
69 TS_ASSERT_EQUALS( ab - b, a );
70 TS_ASSERT_DIFFERS( ab + a, b );
71 }
73 void testUnaryMinus(void)
74 {
75 TS_ASSERT_EQUALS( -a, NR::Point(-a[NR::X], -a[NR::Y]) );
76 }
78 void tetScaleDivide(void)
79 {
80 TS_ASSERT_EQUALS( -a, -1.0 * a );
81 TS_ASSERT_EQUALS( a + a + a, 3.0 * a );
82 TS_ASSERT_EQUALS( a / .5, 2.0 * a );
83 }
85 void testDot(void)
86 {
87 TS_ASSERT_EQUALS( dot(a, b), ( a[NR::X] * b[NR::X] +
88 a[NR::Y] * b[NR::Y] ) );
89 TS_ASSERT_EQUALS( dot(a, NR::rot90(a)), 0.0 );
90 TS_ASSERT_EQUALS( dot(-a, NR::rot90(a)), 0.0 );
91 }
93 void testL1L2LInftyNorms(void)
94 {
95 // TODO look at TS_ASSERT_DELTA
97 TS_ASSERT_EQUALS( L1(small_left), small );
98 TS_ASSERT_EQUALS( L2(small_left), small );
99 TS_ASSERT_EQUALS( LInfty(small_left), small );
101 TS_ASSERT_EQUALS( L1(smallish_3_neg4), 7.0 * small );
102 TS_ASSERT_EQUALS( L2(smallish_3_neg4), 5.0 * small );
103 TS_ASSERT_EQUALS( LInfty(smallish_3_neg4), 4.0 * small );
104 }
106 void testOperatorPlusEquals(void)
107 {
108 NR::Point x(a);
109 x += b;
110 TS_ASSERT_EQUALS( x, ab );
111 }
113 void tetOperatorDivEquals(void)
114 {
115 NR::Point x(a);
116 x /= .5;
117 TS_ASSERT_EQUALS( x, a + a );
118 }
120 void testNormalize(void)
121 {
122 NR::Point x(small_left);
123 x.normalize();
124 TS_ASSERT_EQUALS( x, NR::Point(-1.0, 0.0) );
126 x = smallish_3_neg4;
127 x.normalize();
128 TS_ASSERT_EQUALS( x, NR::Point(0.6, -0.8) );
129 }
131 };
133 /*
134 Local Variables:
135 mode:c++
136 c-file-style:"stroustrup"
137 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
138 indent-tabs-mode:nil
139 fill-column:99
140 End:
141 */
142 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :