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(RealTest) |
---|
24 | { |
---|
25 | LOLUNIT_TEST(Constants) |
---|
26 | { |
---|
27 | double a0 = real::R_0; |
---|
28 | double a1 = real::R_1; |
---|
29 | double a2 = real::R_2; |
---|
30 | double a10 = real::R_10; |
---|
31 | |
---|
32 | LOLUNIT_ASSERT_EQUAL(a0, 0.0); |
---|
33 | LOLUNIT_ASSERT_EQUAL(a1, 1.0); |
---|
34 | LOLUNIT_ASSERT_EQUAL(a2, 2.0); |
---|
35 | LOLUNIT_ASSERT_EQUAL(a10, 10.0); |
---|
36 | |
---|
37 | double b0 = log(real::R_E); |
---|
38 | LOLUNIT_ASSERT_EQUAL(b0, 1.0); |
---|
39 | |
---|
40 | double b1 = exp(re(real::R_LOG2E)); |
---|
41 | LOLUNIT_ASSERT_EQUAL(b1, 2.0); |
---|
42 | |
---|
43 | double b2 = exp(re(real::R_LOG10E)); |
---|
44 | LOLUNIT_ASSERT_EQUAL(b2, 10.0); |
---|
45 | |
---|
46 | double b3 = exp(real::R_LN2); |
---|
47 | LOLUNIT_ASSERT_EQUAL(b3, 2.0); |
---|
48 | |
---|
49 | double b4 = exp(real::R_LN10); |
---|
50 | LOLUNIT_ASSERT_EQUAL(b4, 10.0); |
---|
51 | |
---|
52 | double b5 = sin(real::R_PI); |
---|
53 | double b6 = cos(real::R_PI); |
---|
54 | LOLUNIT_ASSERT_DOUBLES_EQUAL(b5, 0.0, 1e-100); |
---|
55 | LOLUNIT_ASSERT_EQUAL(b6, -1.0); |
---|
56 | |
---|
57 | double b7 = sin(real::R_PI_2); |
---|
58 | double b8 = cos(real::R_PI_2); |
---|
59 | LOLUNIT_ASSERT_EQUAL(b7, 1.0); |
---|
60 | LOLUNIT_ASSERT_DOUBLES_EQUAL(b8, 0.0, 1e-100); |
---|
61 | |
---|
62 | double b9 = sin(real::R_PI_4) * sin(real::R_PI_4); |
---|
63 | double b10 = cos(real::R_PI_4) * cos(real::R_PI_4); |
---|
64 | LOLUNIT_ASSERT_EQUAL(b9, 0.5); |
---|
65 | LOLUNIT_ASSERT_EQUAL(b10, 0.5); |
---|
66 | } |
---|
67 | |
---|
68 | LOLUNIT_TEST(FloatToReal) |
---|
69 | { |
---|
70 | float a1 = real(0.0f); |
---|
71 | float a2 = real(-0.0f); |
---|
72 | float a3 = real(1.0f); |
---|
73 | float a4 = real(-1.0f); |
---|
74 | float a5 = real(1.5f); |
---|
75 | float a6 = real(12345678.0f); |
---|
76 | |
---|
77 | LOLUNIT_ASSERT_EQUAL(a1, 0.0f); |
---|
78 | LOLUNIT_ASSERT_EQUAL(a2, -0.0f); |
---|
79 | LOLUNIT_ASSERT_EQUAL(a3, 1.0f); |
---|
80 | LOLUNIT_ASSERT_EQUAL(a4, -1.0f); |
---|
81 | LOLUNIT_ASSERT_EQUAL(a5, 1.5f); |
---|
82 | LOLUNIT_ASSERT_EQUAL(a6, 12345678.0f); |
---|
83 | } |
---|
84 | |
---|
85 | LOLUNIT_TEST(DoubleToReal) |
---|
86 | { |
---|
87 | double a1 = real(0.0); |
---|
88 | double a2 = real(-0.0); |
---|
89 | double a3 = real(1.0); |
---|
90 | double a4 = real(-1.0); |
---|
91 | double a5 = real(1.5); |
---|
92 | double a6 = real(1234567876543210.0); |
---|
93 | |
---|
94 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, 0.0, 0.0); |
---|
95 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, -0.0, 0.0); |
---|
96 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a3, 1.0, 0.0); |
---|
97 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a4, -1.0, 0.0); |
---|
98 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a5, 1.5, 0.0); |
---|
99 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 1234567876543210.0, 0.0); |
---|
100 | } |
---|
101 | |
---|
102 | LOLUNIT_TEST(UnaryMinus) |
---|
103 | { |
---|
104 | float a1 = - real(1.0f); |
---|
105 | float a2 = - real(-1.0f); |
---|
106 | float a3 = - real(0.0f); |
---|
107 | float a4 = - real(-0.0f); |
---|
108 | |
---|
109 | LOLUNIT_ASSERT_EQUAL(a1, -1.0f); |
---|
110 | LOLUNIT_ASSERT_EQUAL(a2, 1.0f); |
---|
111 | LOLUNIT_ASSERT_EQUAL(a3, -0.0f); |
---|
112 | LOLUNIT_ASSERT_EQUAL(a4, 0.0f); |
---|
113 | } |
---|
114 | |
---|
115 | LOLUNIT_TEST(Comparison) |
---|
116 | { |
---|
117 | LOLUNIT_ASSERT(real(1.0f) > real(0.5f)); |
---|
118 | LOLUNIT_ASSERT(real(1.0f) >= real(0.5f)); |
---|
119 | LOLUNIT_ASSERT(real(1.0f) >= real(1.0f)); |
---|
120 | |
---|
121 | LOLUNIT_ASSERT(real(-1.0f) < real(-0.5f)); |
---|
122 | LOLUNIT_ASSERT(real(-1.0f) <= real(-0.5f)); |
---|
123 | LOLUNIT_ASSERT(real(-1.0f) <= real(-1.0f)); |
---|
124 | |
---|
125 | LOLUNIT_ASSERT(real(-1.0f) < real(0.5f)); |
---|
126 | LOLUNIT_ASSERT(real(-0.5f) < real(1.0f)); |
---|
127 | LOLUNIT_ASSERT(real(-1.0f) <= real(0.5f)); |
---|
128 | LOLUNIT_ASSERT(real(-0.5f) <= real(1.0f)); |
---|
129 | |
---|
130 | LOLUNIT_ASSERT(real(1.0f) > real(-0.5f)); |
---|
131 | LOLUNIT_ASSERT(real(0.5f) > real(-1.0f)); |
---|
132 | LOLUNIT_ASSERT(real(1.0f) >= real(-0.5f)); |
---|
133 | LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f)); |
---|
134 | } |
---|
135 | |
---|
136 | LOLUNIT_TEST(Addition) |
---|
137 | { |
---|
138 | float a1 = real(1.0f) + real(0.0f); |
---|
139 | float a2 = real(0.0f) + real(1.0f); |
---|
140 | float a3 = real(1.0f) + real(1.0f); |
---|
141 | float a4 = real(-1.0f) + real(-1.0f); |
---|
142 | float a5 = real(1.0f) + real(0.125f); |
---|
143 | double a6 = real(3.13609818956293918) |
---|
144 | + real(0.00005972154828114); |
---|
145 | float a7 = real(1.0f) + real(-0.125f); |
---|
146 | double a8 = real(0.10000000002) + real(-2.0e-11); |
---|
147 | |
---|
148 | LOLUNIT_ASSERT_EQUAL(a1, 1.0f); |
---|
149 | LOLUNIT_ASSERT_EQUAL(a2, 1.0f); |
---|
150 | LOLUNIT_ASSERT_EQUAL(a3, 2.0f); |
---|
151 | LOLUNIT_ASSERT_EQUAL(a4, -2.0f); |
---|
152 | LOLUNIT_ASSERT_EQUAL(a5, 1.125f); |
---|
153 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 3.1361579, 0.000001); |
---|
154 | LOLUNIT_ASSERT_EQUAL(a7, 0.875f); |
---|
155 | LOLUNIT_ASSERT_DOUBLES_EQUAL(a8, 0.1, 1.0e-13); |
---|
156 | } |
---|
157 | |
---|
158 | LOLUNIT_TEST(Subtraction) |
---|
159 | { |
---|
160 | float a1 = real(1.0f) + real(1e20f) - real(1e20f); |
---|
161 | |
---|
162 | LOLUNIT_ASSERT_EQUAL(a1, 1.0f); |
---|
163 | } |
---|
164 | |
---|
165 | LOLUNIT_TEST(Multiplication) |
---|
166 | { |
---|
167 | real x(1.25f); |
---|
168 | real y(1.5f); |
---|
169 | real z(1.99999f); |
---|
170 | real w(-1.5f); |
---|
171 | |
---|
172 | float m1 = x * x; |
---|
173 | float m2 = y * y; |
---|
174 | float m3 = z * z; |
---|
175 | float m4 = w * w; |
---|
176 | |
---|
177 | LOLUNIT_ASSERT_EQUAL(m1, 1.25f * 1.25f); |
---|
178 | LOLUNIT_ASSERT_EQUAL(m2, 1.5f * 1.5f); |
---|
179 | LOLUNIT_ASSERT_EQUAL(m3, 1.99999f * 1.99999f); |
---|
180 | LOLUNIT_ASSERT_EQUAL(m4, -1.5f * -1.5f); |
---|
181 | } |
---|
182 | |
---|
183 | LOLUNIT_TEST(Division) |
---|
184 | { |
---|
185 | real a1(1.0f); |
---|
186 | real a2(2.0f); |
---|
187 | |
---|
188 | float m1 = a1 / a1; |
---|
189 | float m2 = a2 / a1; |
---|
190 | float m3 = a1 / a2; |
---|
191 | float m4 = a2 / a2; |
---|
192 | float m5 = a1 / -a2; |
---|
193 | |
---|
194 | LOLUNIT_ASSERT_EQUAL(m1, 1.0f); |
---|
195 | LOLUNIT_ASSERT_EQUAL(m2, 2.0f); |
---|
196 | LOLUNIT_ASSERT_EQUAL(m3, 0.5f); |
---|
197 | LOLUNIT_ASSERT_EQUAL(m4, 1.0f); |
---|
198 | LOLUNIT_ASSERT_EQUAL(m5, -0.5f); |
---|
199 | } |
---|
200 | |
---|
201 | LOLUNIT_TEST(Shift) |
---|
202 | { |
---|
203 | real a1(1.5); |
---|
204 | real a2(-1.5); |
---|
205 | real a3(0.0); |
---|
206 | |
---|
207 | LOLUNIT_ASSERT_EQUAL((double)(a1 >> 7), 0.01171875); |
---|
208 | LOLUNIT_ASSERT_EQUAL((double)(a1 >> -7), 192.0); |
---|
209 | LOLUNIT_ASSERT_EQUAL((double)(a1 << 7), 192.0); |
---|
210 | LOLUNIT_ASSERT_EQUAL((double)(a1 << -7), 0.01171875); |
---|
211 | |
---|
212 | LOLUNIT_ASSERT_EQUAL((double)(a2 >> 7), -0.01171875); |
---|
213 | LOLUNIT_ASSERT_EQUAL((double)(a2 >> -7), -192.0); |
---|
214 | LOLUNIT_ASSERT_EQUAL((double)(a2 << 7), -192.0); |
---|
215 | LOLUNIT_ASSERT_EQUAL((double)(a2 << -7), -0.01171875); |
---|
216 | |
---|
217 | LOLUNIT_ASSERT_EQUAL((double)(a3 >> 7), 0.0); |
---|
218 | LOLUNIT_ASSERT_EQUAL((double)(a3 >> -7), 0.0); |
---|
219 | LOLUNIT_ASSERT_EQUAL((double)(a3 << 7), 0.0); |
---|
220 | LOLUNIT_ASSERT_EQUAL((double)(a3 << -7), 0.0); |
---|
221 | } |
---|
222 | |
---|
223 | LOLUNIT_TEST(Bool) |
---|
224 | { |
---|
225 | real a = 0.0; |
---|
226 | LOLUNIT_ASSERT(!a); |
---|
227 | |
---|
228 | a = -0.0; |
---|
229 | LOLUNIT_ASSERT(!a); |
---|
230 | |
---|
231 | a = 1234.0; |
---|
232 | LOLUNIT_ASSERT(a); |
---|
233 | LOLUNIT_ASSERT(!!a); |
---|
234 | |
---|
235 | a = -1234.0; |
---|
236 | LOLUNIT_ASSERT(a); |
---|
237 | LOLUNIT_ASSERT(!!a); |
---|
238 | } |
---|
239 | }; |
---|
240 | |
---|
241 | } /* namespace lol */ |
---|
242 | |
---|