~lib2geom-hackers/lib2geom/trunk

« back to all changes in this revision

Viewing changes to types-test.h

  • 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
// types-test.h
 
2
#include <cxxtest/TestSuite.h>
 
3
 
 
4
#include "libnr/types.h"
 
5
#include "libnr/point-fns.h"
 
6
#include <cmath>
 
7
using Geom::Point;
 
8
using Geom::X;
 
9
using Geom::Y;
 
10
 
 
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() {}
 
23
 
 
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; }
 
28
 
 
29
    Geom::Point const a;
 
30
    Geom::Point const b;
 
31
    Geom::Point const ab;
 
32
    double const small;
 
33
    Point const small_left;
 
34
    Point const smallish_3_neg4;
 
35
 
 
36
 
 
37
    void testXYValues( void )
 
38
    {
 
39
        TS_ASSERT_EQUALS( X, 0 );
 
40
        TS_ASSERT_EQUALS( Y, 1 );
 
41
    }
 
42
 
 
43
    void testXYCtorAndArrayConst(void)
 
44
    {
 
45
        TS_ASSERT_EQUALS( a[X], 1.5 );
 
46
        TS_ASSERT_EQUALS( a[Y], 2.0 );
 
47
    }
 
48
 
 
49
    void testCopyCtor(void)
 
50
    {
 
51
        Geom::Point a_copy(a);
 
52
 
 
53
        TS_ASSERT_EQUALS( a, a_copy );
 
54
        TS_ASSERT( !(a != a_copy) );
 
55
    }
 
56
 
 
57
    void testNonConstArrayOperator(void)
 
58
    {
 
59
        Geom::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
    }
 
66
 
 
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
    }
 
75
 
 
76
    void testUnaryMinus(void)
 
77
    {
 
78
        TS_ASSERT_EQUALS( -a, Point(-a[X], -a[Y]) );
 
79
    }
 
80
 
 
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
    }
 
87
 
 
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, Geom::rot90(a)), 0.0 );
 
93
        TS_ASSERT_EQUALS( dot(-a, Geom::rot90(a)), 0.0 );
 
94
    }
 
95
 
 
96
    void testL1L2LInftyNorms(void)
 
97
    {
 
98
        // TODO look at TS_ASSERT_DELTA
 
99
 
 
100
        TS_ASSERT_EQUALS( L1(small_left), small );
 
101
        TS_ASSERT_EQUALS( L2(small_left), small );
 
102
        TS_ASSERT_EQUALS( LInfty(small_left), small );
 
103
 
 
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
    }
 
108
 
 
109
    void testOperatorPlusEquals(void)
 
110
    {
 
111
        Point x(a);
 
112
        x += b;
 
113
        TS_ASSERT_EQUALS( x, ab );
 
114
    }
 
115
 
 
116
    void tetOperatorDivEquals(void)
 
117
    {
 
118
        Point x(a);
 
119
        x /= .5;
 
120
        TS_ASSERT_EQUALS( x, a + a );
 
121
    }
 
122
 
 
123
    void testNormalize(void)
 
124
    {
 
125
        Point x(small_left);
 
126
        x.normalize();
 
127
        TS_ASSERT_EQUALS( x, Point(-1.0, 0.0) );
 
128
 
 
129
        x = smallish_3_neg4;
 
130
        x.normalize();
 
131
        TS_ASSERT_EQUALS( x, Point(0.6, -0.8) );
 
132
    }
 
133
 
 
134
};
 
135
 
 
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 :