1 #include <cassert>
2 #include <cmath>
3 #include <glib/gmacros.h>
4 #include <stdlib.h>
6 #include "utest/utest.h"
7 #include "libnr/nr-point-fns.h"
8 #include "isnan.h"
10 using NR::Point;
12 int main(int argc, char *argv[])
13 {
14 utest_start("nr-point-fns");
16 Point const p3n4(3.0, -4.0);
17 Point const p0(0.0, 0.0);
18 double const small = pow(2.0, -1070);
19 double const inf = 1e400;
20 double const nan = inf - inf;
22 Point const small_left(-small, 0.0);
23 Point const small_n3_4(-3.0 * small, 4.0 * small);
24 Point const part_nan(3., nan);
25 Point const inf_left(-inf, 5.0);
27 assert(isNaN(nan));
28 assert(!isNaN(small));
30 UTEST_TEST("L1") {
31 UTEST_ASSERT( NR::L1(p0) == 0.0 );
32 UTEST_ASSERT( NR::L1(p3n4) == 7.0 );
33 UTEST_ASSERT( NR::L1(small_left) == small );
34 UTEST_ASSERT( NR::L1(inf_left) == inf );
35 UTEST_ASSERT( NR::L1(small_n3_4) == 7.0 * small );
36 UTEST_ASSERT(isNaN(NR::L1(part_nan)));
37 }
39 UTEST_TEST("L2") {
40 UTEST_ASSERT( NR::L2(p0) == 0.0 );
41 UTEST_ASSERT( NR::L2(p3n4) == 5.0 );
42 UTEST_ASSERT( NR::L2(small_left) == small );
43 UTEST_ASSERT( NR::L2(inf_left) == inf );
44 UTEST_ASSERT( NR::L2(small_n3_4) == 5.0 * small );
45 UTEST_ASSERT(isNaN(NR::L2(part_nan)));
46 }
48 UTEST_TEST("LInfty") {
49 UTEST_ASSERT( NR::LInfty(p0) == 0.0 );
50 UTEST_ASSERT( NR::LInfty(p3n4) == 4.0 );
51 UTEST_ASSERT( NR::LInfty(small_left) == small );
52 UTEST_ASSERT( NR::LInfty(inf_left) == inf );
53 UTEST_ASSERT( NR::LInfty(small_n3_4) == 4.0 * small );
54 UTEST_ASSERT(isNaN(NR::LInfty(part_nan)));
55 }
57 UTEST_TEST("is_zero") {
58 UTEST_ASSERT(NR::is_zero(p0));
59 UTEST_ASSERT(!NR::is_zero(p3n4));
60 UTEST_ASSERT(!NR::is_zero(small_left));
61 UTEST_ASSERT(!NR::is_zero(inf_left));
62 UTEST_ASSERT(!NR::is_zero(small_n3_4));
63 UTEST_ASSERT(!NR::is_zero(part_nan));
64 }
66 UTEST_TEST("atan2") {
67 UTEST_ASSERT( NR::atan2(p3n4) == atan2(-4.0, 3.0) );
68 UTEST_ASSERT( NR::atan2(small_left) == atan2(0.0, -1.0) );
69 UTEST_ASSERT( NR::atan2(small_n3_4) == atan2(4.0, -3.0) );
70 }
72 UTEST_TEST("unit_vector") {
73 UTEST_ASSERT( NR::unit_vector(p3n4) == Point(.6, -0.8) );
74 UTEST_ASSERT( NR::unit_vector(small_left) == Point(-1.0, 0.0) );
75 UTEST_ASSERT( NR::unit_vector(small_n3_4) == Point(-.6, 0.8) );
76 }
78 UTEST_TEST("is_unit_vector") {
79 UTEST_ASSERT(!NR::is_unit_vector(p3n4));
80 UTEST_ASSERT(!NR::is_unit_vector(small_left));
81 UTEST_ASSERT(!NR::is_unit_vector(small_n3_4));
82 UTEST_ASSERT(!NR::is_unit_vector(part_nan));
83 UTEST_ASSERT(!NR::is_unit_vector(inf_left));
84 UTEST_ASSERT(!NR::is_unit_vector(Point(.5, 0.5)));
85 UTEST_ASSERT(NR::is_unit_vector(Point(.6, -0.8)));
86 UTEST_ASSERT(NR::is_unit_vector(Point(-.6, 0.8)));
87 UTEST_ASSERT(NR::is_unit_vector(Point(-1.0, 0.0)));
88 UTEST_ASSERT(NR::is_unit_vector(Point(1.0, 0.0)));
89 UTEST_ASSERT(NR::is_unit_vector(Point(0.0, -1.0)));
90 UTEST_ASSERT(NR::is_unit_vector(Point(0.0, 1.0)));
91 }
93 return ( utest_end()
94 ? EXIT_SUCCESS
95 : EXIT_FAILURE );
96 }
98 /*
99 Local Variables:
100 mode:c++
101 c-file-style:"stroustrup"
102 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
103 indent-tabs-mode:nil
104 fill-column:99
105 End:
106 */
107 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :