1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 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://www.wtfpl.net/ 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 | |
---|
29 | for (int i = -10000; i < 10000; i++) |
---|
30 | { |
---|
31 | double f = (double)i * (1.0 / 1000.0); |
---|
32 | #if defined __GNUC__ && !defined __SNC__ |
---|
33 | double a = __builtin_sin(f); |
---|
34 | #else |
---|
35 | double a = std::sin(f); |
---|
36 | #endif |
---|
37 | double b = lol_sin(f); |
---|
38 | LOLUNIT_SET_CONTEXT(f); |
---|
39 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
40 | } |
---|
41 | |
---|
42 | for (int i = -10000; i < 10000; i++) |
---|
43 | { |
---|
44 | double f = (double)i * (1.0 / 100000.0); |
---|
45 | #if defined __GNUC__ && !defined __SNC__ |
---|
46 | double a = __builtin_sin(f); |
---|
47 | #else |
---|
48 | double a = std::sin(f); |
---|
49 | #endif |
---|
50 | double b = lol_sin(f); |
---|
51 | LOLUNIT_SET_CONTEXT(f); |
---|
52 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | LOLUNIT_TEST(Cos) |
---|
57 | { |
---|
58 | using std::fabs; |
---|
59 | |
---|
60 | for (int i = -10000; i < 10000; i++) |
---|
61 | { |
---|
62 | double f = (double)i * (1.0 / 1000.0); |
---|
63 | #if defined __GNUC__ && !defined __SNC__ |
---|
64 | double a = __builtin_cos(f); |
---|
65 | #else |
---|
66 | double a = std::cos(f); |
---|
67 | #endif |
---|
68 | double b = lol_cos(f); |
---|
69 | LOLUNIT_SET_CONTEXT(f); |
---|
70 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
71 | } |
---|
72 | |
---|
73 | for (int i = -10000; i < 10000; i++) |
---|
74 | { |
---|
75 | double f = (double)i * (1.0 / 100000.0); |
---|
76 | #if defined __GNUC__ && !defined __SNC__ |
---|
77 | double a = __builtin_cos(f); |
---|
78 | #else |
---|
79 | double a = std::cos(f); |
---|
80 | #endif |
---|
81 | double b = lol_cos(f); |
---|
82 | LOLUNIT_SET_CONTEXT(f); |
---|
83 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | LOLUNIT_TEST(SinCos) |
---|
88 | { |
---|
89 | using std::fabs; |
---|
90 | |
---|
91 | for (int i = -10000; i < 10000; i++) |
---|
92 | { |
---|
93 | double f = (double)i * (1.0 / 1000.0); |
---|
94 | #if defined __GNUC__ && !defined __SNC__ |
---|
95 | double a1 = __builtin_sin(f); |
---|
96 | double a2 = __builtin_cos(f); |
---|
97 | #else |
---|
98 | double a1 = std::sin(f); |
---|
99 | double a2 = std::cos(f); |
---|
100 | #endif |
---|
101 | double b1, b2; |
---|
102 | lol_sincos(f, &b1, &b2); |
---|
103 | LOLUNIT_SET_CONTEXT(f); |
---|
104 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11); |
---|
105 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11); |
---|
106 | } |
---|
107 | |
---|
108 | for (int i = -10000; i < 10000; i++) |
---|
109 | { |
---|
110 | double f = (double)i * (1.0 / 100000.0); |
---|
111 | #if defined __GNUC__ && !defined __SNC__ |
---|
112 | double a1 = __builtin_sin(f); |
---|
113 | double a2 = __builtin_cos(f); |
---|
114 | #else |
---|
115 | double a1 = std::sin(f); |
---|
116 | double a2 = std::cos(f); |
---|
117 | #endif |
---|
118 | double b1, b2; |
---|
119 | lol_sincos(f, &b1, &b2); |
---|
120 | LOLUNIT_SET_CONTEXT(f); |
---|
121 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11); |
---|
122 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | LOLUNIT_TEST(Tan) |
---|
127 | { |
---|
128 | using std::fabs; |
---|
129 | |
---|
130 | for (int i = -100000; i < 100000; i++) |
---|
131 | { |
---|
132 | double f = (double)i * (1.0 / 10000.0); |
---|
133 | #if defined __GNUC__ && !defined __SNC__ |
---|
134 | double a = __builtin_tan(f); |
---|
135 | #else |
---|
136 | double a = std::tan(f); |
---|
137 | #endif |
---|
138 | double b = lol_tan(f); |
---|
139 | LOLUNIT_SET_CONTEXT(f); |
---|
140 | if (fabs(a) > 1e4) |
---|
141 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11); |
---|
142 | else if (fabs(a) > 1.0) |
---|
143 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11); |
---|
144 | else |
---|
145 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
146 | } |
---|
147 | |
---|
148 | for (int i = -10000; i < 10000; i++) |
---|
149 | { |
---|
150 | double f = (double)i * (1.0 / 100000.0); |
---|
151 | #if defined __GNUC__ && !defined __SNC__ |
---|
152 | double a = __builtin_tan(f); |
---|
153 | #else |
---|
154 | double a = std::tan(f); |
---|
155 | #endif |
---|
156 | double b = lol_tan(f); |
---|
157 | LOLUNIT_SET_CONTEXT(f); |
---|
158 | if (fabs(a) > 1e4) |
---|
159 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11); |
---|
160 | else if (fabs(a) > 1.0) |
---|
161 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11); |
---|
162 | else |
---|
163 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11); |
---|
164 | } |
---|
165 | } |
---|
166 | }; |
---|
167 | |
---|
168 | } /* namespace lol */ |
---|
169 | |
---|