1 #include <cxxtest/TestSuite.h>
3 #include <cmath>
4 #include <libnr/nr-matrix.h>
5 #include <libnr/nr-matrix-ops.h>
6 #include <libnr/nr-matrix-fns.h> /* identity, matrix_equalp */
7 #include <libnr/nr-point-fns.h>
8 #include <libnr/nr-rotate.h>
9 #include <libnr/nr-rotate-fns.h>
10 #include <libnr/nr-rotate-ops.h>
11 using NR::X;
12 using NR::Y;
15 class NrRotateTest : public CxxTest::TestSuite
16 {
17 public:
19 NrRotateTest() :
20 m_id( NR::identity() ),
21 r_id( 0.0 ),
22 rot234( .234 ),
23 b( -2.0, 3.0 ),
24 rot180( NR::Point(-1.0, 0.0) )
25 {
26 }
27 virtual ~NrRotateTest() {}
29 // createSuite and destroySuite get us per-suite setup and teardown
30 // without us having to worry about static initialization order, etc.
31 static NrRotateTest *createSuite() { return new NrRotateTest(); }
32 static void destroySuite( NrRotateTest *suite ) { delete suite; }
34 NR::Matrix const m_id;
35 NR::rotate const r_id;
36 NR::rotate const rot234;
37 NR::Point const b;
38 NR::rotate const rot180;
43 void testCtorsCompares(void)
44 {
45 TS_ASSERT_EQUALS( r_id, r_id );
46 TS_ASSERT_EQUALS( rot234, rot234 );
47 TS_ASSERT_DIFFERS( rot234, r_id );
48 TS_ASSERT_EQUALS( r_id, NR::rotate(NR::Point(1.0, 0.0)) );
49 TS_ASSERT_EQUALS( NR::Matrix(r_id), m_id );
50 TS_ASSERT( NR::Matrix(r_id).test_identity() );
52 TS_ASSERT(rotate_equalp(rot234, NR::rotate(NR::Point(cos(.234), sin(.234))), 1e-12));
53 }
55 void testAssignmentOp(void)
56 {
57 NR::rotate rot234_eq(r_id);
58 rot234_eq = rot234;
59 TS_ASSERT_EQUALS( rot234, rot234_eq );
60 TS_ASSERT_DIFFERS( rot234_eq, r_id );
61 }
63 void testInverse(void)
64 {
65 TS_ASSERT_EQUALS( r_id.inverse(), r_id );
66 TS_ASSERT_EQUALS( rot234.inverse(), NR::rotate(-.234) );
67 }
69 void testOpStarPointRotate(void)
70 {
71 TS_ASSERT_EQUALS( b * r_id, b );
72 TS_ASSERT_EQUALS( b * rot180, -b );
73 TS_ASSERT_EQUALS( b * rot234, b * NR::Matrix(rot234) );
74 TS_ASSERT(point_equalp(b * NR::rotate(M_PI / 2),
75 NR::rot90(b),
76 1e-14));
77 TS_ASSERT_EQUALS( b * rotate_degrees(90.), NR::rot90(b) );
78 }
80 void testOpStarRotateRotate(void)
81 {
82 TS_ASSERT_EQUALS( r_id * r_id, r_id );
83 TS_ASSERT_EQUALS( rot180 * rot180, r_id );
84 TS_ASSERT_EQUALS( rot234 * r_id, rot234 );
85 TS_ASSERT_EQUALS( r_id * rot234, rot234 );
86 TS_ASSERT( rotate_equalp(rot234 * rot234.inverse(), r_id, 1e-14) );
87 TS_ASSERT( rotate_equalp(rot234.inverse() * rot234, r_id, 1e-14) );
88 TS_ASSERT( rotate_equalp(( NR::rotate(0.25) * NR::rotate(.5) ),
89 NR::rotate(.75),
90 1e-10) );
91 }
93 void testOpDivRotateRotate(void)
94 {
95 TS_ASSERT_EQUALS( rot234 / r_id, rot234 );
96 TS_ASSERT_EQUALS( rot234 / rot180, rot234 * rot180 );
97 TS_ASSERT( rotate_equalp(rot234 / rot234, r_id, 1e-14) );
98 TS_ASSERT( rotate_equalp(r_id / rot234, rot234.inverse(), 1e-14) );
99 }
101 };
103 /*
104 Local Variables:
105 mode:c++
106 c-file-style:"stroustrup"
107 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
108 indent-tabs-mode:nil
109 fill-column:99
110 End:
111 */
112 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :