1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cmath> |
---|
16 | |
---|
17 | #include "core.h" |
---|
18 | #include "lol/unit.h" |
---|
19 | |
---|
20 | namespace lol |
---|
21 | { |
---|
22 | |
---|
23 | LOLUNIT_FIXTURE(TrigTest) |
---|
24 | { |
---|
25 | LOLUNIT_TEST(Sin) |
---|
26 | { |
---|
27 | using std::fabs; |
---|
28 | using std::sin; |
---|
29 | |
---|
30 | for (int i = -10000; i < 10000; i++) |
---|
31 | { |
---|
32 | double f = (double)i * (1.0 / 1000.0); |
---|
33 | #if defined __GNUC__ |
---|
34 | double a = __builtin_sin(f); |
---|
35 | #else |
---|
36 | double a = sin(f); |
---|
37 | #endif |
---|
38 | double b = lol_sin(f); |
---|
39 | LOLUNIT_SET_CONTEXT(f); |
---|
40 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
41 | } |
---|
42 | |
---|
43 | for (int i = -10000; i < 10000; i++) |
---|
44 | { |
---|
45 | double f = (double)i * (1.0 / 100000.0); |
---|
46 | #if defined __GNUC__ |
---|
47 | double a = __builtin_sin(f); |
---|
48 | #else |
---|
49 | double a = sin(f); |
---|
50 | #endif |
---|
51 | double b = lol_sin(f); |
---|
52 | LOLUNIT_SET_CONTEXT(f); |
---|
53 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | LOLUNIT_TEST(Cos) |
---|
58 | { |
---|
59 | using std::fabs; |
---|
60 | using std::cos; |
---|
61 | |
---|
62 | for (int i = -10000; i < 10000; i++) |
---|
63 | { |
---|
64 | double f = (double)i * (1.0 / 1000.0); |
---|
65 | #if defined __GNUC__ |
---|
66 | double a = __builtin_cos(f); |
---|
67 | #else |
---|
68 | double a = cos(f); |
---|
69 | #endif |
---|
70 | double b = lol_cos(f); |
---|
71 | LOLUNIT_SET_CONTEXT(f); |
---|
72 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
73 | } |
---|
74 | |
---|
75 | for (int i = -10000; i < 10000; i++) |
---|
76 | { |
---|
77 | double f = (double)i * (1.0 / 100000.0); |
---|
78 | #if defined __GNUC__ |
---|
79 | double a = __builtin_cos(f); |
---|
80 | #else |
---|
81 | double a = cos(f); |
---|
82 | #endif |
---|
83 | double b = lol_cos(f); |
---|
84 | LOLUNIT_SET_CONTEXT(f); |
---|
85 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | LOLUNIT_TEST(SinCos) |
---|
90 | { |
---|
91 | using std::fabs; |
---|
92 | using std::sin; |
---|
93 | using std::cos; |
---|
94 | |
---|
95 | for (int i = -10000; i < 10000; i++) |
---|
96 | { |
---|
97 | double f = (double)i * (1.0 / 1000.0); |
---|
98 | #if defined __GNUC__ |
---|
99 | double a1 = __builtin_sin(f); |
---|
100 | double a2 = __builtin_cos(f); |
---|
101 | #else |
---|
102 | double a1 = sin(f); |
---|
103 | double a2 = cos(f); |
---|
104 | #endif |
---|
105 | double b1, b2; |
---|
106 | lol_sincos(f, &b1, &b2); |
---|
107 | LOLUNIT_SET_CONTEXT(f); |
---|
108 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11); |
---|
109 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11); |
---|
110 | } |
---|
111 | |
---|
112 | for (int i = -10000; i < 10000; i++) |
---|
113 | { |
---|
114 | double f = (double)i * (1.0 / 100000.0); |
---|
115 | #if defined __GNUC__ |
---|
116 | double a1 = __builtin_sin(f); |
---|
117 | double a2 = __builtin_cos(f); |
---|
118 | #else |
---|
119 | double a1 = sin(f); |
---|
120 | double a2 = cos(f); |
---|
121 | #endif |
---|
122 | double b1, b2; |
---|
123 | lol_sincos(f, &b1, &b2); |
---|
124 | LOLUNIT_SET_CONTEXT(f); |
---|
125 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11); |
---|
126 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | LOLUNIT_TEST(Tan) |
---|
131 | { |
---|
132 | using std::fabs; |
---|
133 | using std::tan; |
---|
134 | |
---|
135 | for (int i = -100000; i < 100000; i++) |
---|
136 | { |
---|
137 | double f = (double)i * (1.0 / 10000.0); |
---|
138 | #if defined __GNUC__ |
---|
139 | double a = __builtin_tan(f); |
---|
140 | #else |
---|
141 | double a = tan(f); |
---|
142 | #endif |
---|
143 | double b = lol_tan(f); |
---|
144 | LOLUNIT_SET_CONTEXT(f); |
---|
145 | if (fabs(a) > 1e4) |
---|
146 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11); |
---|
147 | else if (fabs(a) > 1.0) |
---|
148 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11); |
---|
149 | else |
---|
150 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
151 | } |
---|
152 | |
---|
153 | for (int i = -10000; i < 10000; i++) |
---|
154 | { |
---|
155 | double f = (double)i * (1.0 / 100000.0); |
---|
156 | #if defined __GNUC__ |
---|
157 | double a = __builtin_tan(f); |
---|
158 | #else |
---|
159 | double a = tan(f); |
---|
160 | #endif |
---|
161 | double b = lol_tan(f); |
---|
162 | LOLUNIT_SET_CONTEXT(f); |
---|
163 | if (fabs(a) > 1e4) |
---|
164 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11); |
---|
165 | else if (fabs(a) > 1.0) |
---|
166 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11); |
---|
167 | else |
---|
168 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
169 | } |
---|
170 | } |
---|
171 | }; |
---|
172 | |
---|
173 | } /* namespace lol */ |
---|
174 | |
---|