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>
7 using NR::Point;
8 using NR::X;
9 using NR::Y;
11 class NrTypesTest : public CxxTest::TestSuite
12 {
13 public:
14 NrTypesTest() :
15 a( 1.5, 2.0 ),
16 b(-2.0, 3.0),
17 ab(-0.5, 5.0),
18 small(pow(2.0, -1070)),
19 small_left(-small, 0.0),
20 smallish_3_neg4(3.0 * small, -4.0 * small)
21 {}
22 virtual ~NrTypesTest() {}
24 // createSuite and destroySuite get us per-suite setup and teardown
25 // without us having to worry about static initialization order, etc.
26 static NrTypesTest *createSuite() { return new NrTypesTest(); }
27 static void destroySuite( NrTypesTest *suite ) { delete suite; }
29 NR::Point const a;
30 NR::Point const b;
31 NR::Point const ab;
32 double const small;
33 Point const small_left;
34 Point const smallish_3_neg4;
37 void testXYValues( void )
38 {
39 TS_ASSERT_EQUALS( X, 0 );
40 TS_ASSERT_EQUALS( Y, 1 );
41 }
43 void testXYCtorAndArrayConst(void)
44 {
45 TS_ASSERT_EQUALS( a[X], 1.5 );
46 TS_ASSERT_EQUALS( a[Y], 2.0 );
47 }
49 void testCopyCtor(void)
50 {
51 NR::Point a_copy(a);
53 TS_ASSERT_EQUALS( a, a_copy );
54 TS_ASSERT( !(a != a_copy) );
55 }
57 void testNonConstArrayOperator(void)
58 {
59 NR::Point a_copy(a);
60 a_copy[X] = -2.0;
61 TS_ASSERT_DIFFERS( a_copy, a );
62 TS_ASSERT_DIFFERS( a_copy, b );
63 a_copy[Y] = 3.0;
64 TS_ASSERT_EQUALS( a_copy, b );
65 }
67 void testBinaryPlusMinus(void)
68 {
69 TS_ASSERT_DIFFERS( a, b );
70 TS_ASSERT_EQUALS( a + b, ab );
71 TS_ASSERT_EQUALS( ab - a, b );
72 TS_ASSERT_EQUALS( ab - b, a );
73 TS_ASSERT_DIFFERS( ab + a, b );
74 }
76 void testUnaryMinus(void)
77 {
78 TS_ASSERT_EQUALS( -a, Point(-a[X], -a[Y]) );
79 }
81 void tetScaleDivide(void)
82 {
83 TS_ASSERT_EQUALS( -a, -1.0 * a );
84 TS_ASSERT_EQUALS( a + a + a, 3.0 * a );
85 TS_ASSERT_EQUALS( a / .5, 2.0 * a );
86 }
88 void testDot(void)
89 {
90 TS_ASSERT_EQUALS( dot(a, b), ( a[X] * b[X] +
91 a[Y] * b[Y] ) );
92 TS_ASSERT_EQUALS( dot(a, NR::rot90(a)), 0.0 );
93 TS_ASSERT_EQUALS( dot(-a, NR::rot90(a)), 0.0 );
94 }
96 void testL1L2LInftyNorms(void)
97 {
98 // TODO look at TS_ASSERT_DELTA
100 TS_ASSERT_EQUALS( L1(small_left), small );
101 TS_ASSERT_EQUALS( L2(small_left), small );
102 TS_ASSERT_EQUALS( LInfty(small_left), small );
104 TS_ASSERT_EQUALS( L1(smallish_3_neg4), 7.0 * small );
105 TS_ASSERT_EQUALS( L2(smallish_3_neg4), 5.0 * small );
106 TS_ASSERT_EQUALS( LInfty(smallish_3_neg4), 4.0 * small );
107 }
109 void testOperatorPlusEquals(void)
110 {
111 Point x(a);
112 x += b;
113 TS_ASSERT_EQUALS( x, ab );
114 }
116 void tetOperatorDivEquals(void)
117 {
118 Point x(a);
119 x /= .5;
120 TS_ASSERT_EQUALS( x, a + a );
121 }
123 void testNormalize(void)
124 {
125 Point x(small_left);
126 x.normalize();
127 TS_ASSERT_EQUALS( x, Point(-1.0, 0.0) );
129 x = smallish_3_neg4;
130 x.normalize();
131 TS_ASSERT_EQUALS( x, Point(0.6, -0.8) );
132 }
134 };
136 /*
137 Local Variables:
138 mode:c++
139 c-file-style:"stroustrup"
140 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
141 indent-tabs-mode:nil
142 fill-column:99
143 End:
144 */
145 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :