Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

316 linhas
9.4 KiB

  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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #include "core.h"
  15. #include "lol/unit.h"
  16. namespace lol
  17. {
  18. LOLUNIT_FIXTURE(RealTest)
  19. {
  20. LOLUNIT_TEST(Constants)
  21. {
  22. double a0 = real::R_0;
  23. double a1 = real::R_1;
  24. double a2 = real::R_2;
  25. double a10 = real::R_10;
  26. LOLUNIT_ASSERT_EQUAL(a0, 0.0);
  27. LOLUNIT_ASSERT_EQUAL(a1, 1.0);
  28. LOLUNIT_ASSERT_EQUAL(a2, 2.0);
  29. LOLUNIT_ASSERT_EQUAL(a10, 10.0);
  30. double b1 = log(real::R_E);
  31. double b2 = log2(real::R_2);
  32. LOLUNIT_ASSERT_EQUAL(b1, 1.0);
  33. LOLUNIT_ASSERT_EQUAL(b2, 1.0);
  34. double c1 = exp(re(real::R_LOG2E));
  35. double c2 = log(exp2(real::R_LOG2E));
  36. LOLUNIT_ASSERT_EQUAL(c1, 2.0);
  37. LOLUNIT_ASSERT_EQUAL(c2, 1.0);
  38. double d1 = exp(re(real::R_LOG10E));
  39. LOLUNIT_ASSERT_EQUAL(d1, 10.0);
  40. double e1 = exp(real::R_LN2);
  41. LOLUNIT_ASSERT_EQUAL(e1, 2.0);
  42. double f1 = exp(real::R_LN10);
  43. LOLUNIT_ASSERT_EQUAL(f1, 10.0);
  44. double g1 = sin(real::R_PI);
  45. double g2 = cos(real::R_PI);
  46. LOLUNIT_ASSERT_DOUBLES_EQUAL(g1, 0.0, 1e-100);
  47. LOLUNIT_ASSERT_EQUAL(g2, -1.0);
  48. double h1 = sin(real::R_PI_2);
  49. double h2 = cos(real::R_PI_2);
  50. LOLUNIT_ASSERT_EQUAL(h1, 1.0);
  51. LOLUNIT_ASSERT_DOUBLES_EQUAL(h2, 0.0, 1e-100);
  52. double i1 = sin(real::R_PI_4) * sin(real::R_PI_4);
  53. double i2 = cos(real::R_PI_4) * cos(real::R_PI_4);
  54. LOLUNIT_ASSERT_EQUAL(i1, 0.5);
  55. LOLUNIT_ASSERT_EQUAL(i2, 0.5);
  56. }
  57. LOLUNIT_TEST(FloatToReal)
  58. {
  59. float a1 = real(0.0f);
  60. float a2 = real(-0.0f);
  61. float a3 = real(1.0f);
  62. float a4 = real(-1.0f);
  63. float a5 = real(1.5f);
  64. float a6 = real(12345678.0f);
  65. LOLUNIT_ASSERT_EQUAL(a1, 0.0f);
  66. LOLUNIT_ASSERT_EQUAL(a2, -0.0f);
  67. LOLUNIT_ASSERT_EQUAL(a3, 1.0f);
  68. LOLUNIT_ASSERT_EQUAL(a4, -1.0f);
  69. LOLUNIT_ASSERT_EQUAL(a5, 1.5f);
  70. LOLUNIT_ASSERT_EQUAL(a6, 12345678.0f);
  71. }
  72. LOLUNIT_TEST(DoubleToReal)
  73. {
  74. double a1 = real(0.0);
  75. double a2 = real(-0.0);
  76. double a3 = real(1.0);
  77. double a4 = real(-1.0);
  78. double a5 = real(1.5);
  79. double a6 = real(1234567876543210.0);
  80. LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, 0.0, 0.0);
  81. LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, -0.0, 0.0);
  82. LOLUNIT_ASSERT_DOUBLES_EQUAL(a3, 1.0, 0.0);
  83. LOLUNIT_ASSERT_DOUBLES_EQUAL(a4, -1.0, 0.0);
  84. LOLUNIT_ASSERT_DOUBLES_EQUAL(a5, 1.5, 0.0);
  85. LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 1234567876543210.0, 0.0);
  86. }
  87. LOLUNIT_TEST(UnaryMinus)
  88. {
  89. float a1 = - real(1.0f);
  90. float a2 = - real(-1.0f);
  91. float a3 = - real(0.0f);
  92. float a4 = - real(-0.0f);
  93. LOLUNIT_ASSERT_EQUAL(a1, -1.0f);
  94. LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
  95. LOLUNIT_ASSERT_EQUAL(a3, -0.0f);
  96. LOLUNIT_ASSERT_EQUAL(a4, 0.0f);
  97. }
  98. LOLUNIT_TEST(Comparison)
  99. {
  100. LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
  101. LOLUNIT_ASSERT(real(1.0f) >= real(0.5f));
  102. LOLUNIT_ASSERT(real(1.0f) >= real(1.0f));
  103. LOLUNIT_ASSERT(real(-1.0f) < real(-0.5f));
  104. LOLUNIT_ASSERT(real(-1.0f) <= real(-0.5f));
  105. LOLUNIT_ASSERT(real(-1.0f) <= real(-1.0f));
  106. LOLUNIT_ASSERT(real(-1.0f) < real(0.5f));
  107. LOLUNIT_ASSERT(real(-0.5f) < real(1.0f));
  108. LOLUNIT_ASSERT(real(-1.0f) <= real(0.5f));
  109. LOLUNIT_ASSERT(real(-0.5f) <= real(1.0f));
  110. LOLUNIT_ASSERT(real(1.0f) > real(-0.5f));
  111. LOLUNIT_ASSERT(real(0.5f) > real(-1.0f));
  112. LOLUNIT_ASSERT(real(1.0f) >= real(-0.5f));
  113. LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f));
  114. }
  115. LOLUNIT_TEST(Addition)
  116. {
  117. float a1 = real(1.0f) + real(0.0f);
  118. float a2 = real(0.0f) + real(1.0f);
  119. float a3 = real(1.0f) + real(1.0f);
  120. float a4 = real(-1.0f) + real(-1.0f);
  121. float a5 = real(1.0f) + real(0.125f);
  122. double a6 = real(3.13609818956293918)
  123. + real(0.00005972154828114);
  124. float a7 = real(1.0f) + real(-0.125f);
  125. double a8 = real(0.10000000002) + real(-2.0e-11);
  126. LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
  127. LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
  128. LOLUNIT_ASSERT_EQUAL(a3, 2.0f);
  129. LOLUNIT_ASSERT_EQUAL(a4, -2.0f);
  130. LOLUNIT_ASSERT_EQUAL(a5, 1.125f);
  131. LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 3.1361579, 0.000001);
  132. LOLUNIT_ASSERT_EQUAL(a7, 0.875f);
  133. LOLUNIT_ASSERT_DOUBLES_EQUAL(a8, 0.1, 1.0e-13);
  134. }
  135. LOLUNIT_TEST(Subtraction)
  136. {
  137. float a1 = real(1.0f) + real(1e20f) - real(1e20f);
  138. LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
  139. }
  140. LOLUNIT_TEST(Multiplication)
  141. {
  142. real x(1.25f);
  143. real y(1.5f);
  144. real z(1.99999f);
  145. real w(-1.5f);
  146. float m1 = x * x;
  147. float m2 = y * y;
  148. float m3 = z * z;
  149. float m4 = w * w;
  150. LOLUNIT_ASSERT_EQUAL(m1, 1.25f * 1.25f);
  151. LOLUNIT_ASSERT_EQUAL(m2, 1.5f * 1.5f);
  152. LOLUNIT_ASSERT_EQUAL(m3, 1.99999f * 1.99999f);
  153. LOLUNIT_ASSERT_EQUAL(m4, -1.5f * -1.5f);
  154. }
  155. LOLUNIT_TEST(ExactDivision)
  156. {
  157. float m1 = real::R_1 / real::R_1;
  158. float m2 = real::R_2 / real::R_1;
  159. float m3 = real::R_1 / real::R_2;
  160. float m4 = real::R_2 / real::R_2;
  161. float m5 = real::R_1 / -real::R_2;
  162. LOLUNIT_ASSERT_EQUAL(m1, 1.0f);
  163. LOLUNIT_ASSERT_EQUAL(m2, 2.0f);
  164. LOLUNIT_ASSERT_EQUAL(m3, 0.5f);
  165. LOLUNIT_ASSERT_EQUAL(m4, 1.0f);
  166. LOLUNIT_ASSERT_EQUAL(m5, -0.5f);
  167. }
  168. LOLUNIT_TEST(InexactDivision)
  169. {
  170. /* 1 / 3 * 3 should be close to 1... check that it does not differ
  171. * by more than 2^-k where k is the number of bits in the mantissa. */
  172. real a = real::R_1 / real::R_3 * real::R_3;
  173. real b = (real::R_1 - a) << (real::BIGITS * real::BIGIT_BITS);
  174. LOLUNIT_ASSERT_LEQUAL((double)fabs(b), 1.0);
  175. }
  176. LOLUNIT_TEST(Shift)
  177. {
  178. real a1(1.5);
  179. real a2(-1.5);
  180. real a3(0.0);
  181. LOLUNIT_ASSERT_EQUAL((double)(a1 >> 7), 0.01171875);
  182. LOLUNIT_ASSERT_EQUAL((double)(a1 >> -7), 192.0);
  183. LOLUNIT_ASSERT_EQUAL((double)(a1 << 7), 192.0);
  184. LOLUNIT_ASSERT_EQUAL((double)(a1 << -7), 0.01171875);
  185. LOLUNIT_ASSERT_EQUAL((double)(a2 >> 7), -0.01171875);
  186. LOLUNIT_ASSERT_EQUAL((double)(a2 >> -7), -192.0);
  187. LOLUNIT_ASSERT_EQUAL((double)(a2 << 7), -192.0);
  188. LOLUNIT_ASSERT_EQUAL((double)(a2 << -7), -0.01171875);
  189. LOLUNIT_ASSERT_EQUAL((double)(a3 >> 7), 0.0);
  190. LOLUNIT_ASSERT_EQUAL((double)(a3 >> -7), 0.0);
  191. LOLUNIT_ASSERT_EQUAL((double)(a3 << 7), 0.0);
  192. LOLUNIT_ASSERT_EQUAL((double)(a3 << -7), 0.0);
  193. }
  194. LOLUNIT_TEST(Bool)
  195. {
  196. real a = 0.0;
  197. LOLUNIT_ASSERT(!a);
  198. a = -0.0;
  199. LOLUNIT_ASSERT(!a);
  200. a = 1234.0;
  201. LOLUNIT_ASSERT(a);
  202. LOLUNIT_ASSERT(!!a);
  203. a = -1234.0;
  204. LOLUNIT_ASSERT(a);
  205. LOLUNIT_ASSERT(!!a);
  206. }
  207. LOLUNIT_TEST(AsinAcos)
  208. {
  209. double tests[] =
  210. {
  211. -1024.0, -1023.0, -513.0, -512.0, -511.0, -1.0, -0.0,
  212. 0.0, 1.0, 511.0, 512.0, 513.0, 1023.0, 1024.0
  213. };
  214. for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n++)
  215. {
  216. double a = tests[n] / 1024;
  217. double b = sin(asin((real)a));
  218. double c = cos(acos((real)a));
  219. LOLUNIT_SET_CONTEXT(a);
  220. LOLUNIT_ASSERT_DOUBLES_EQUAL(b, a, 1e-100);
  221. LOLUNIT_ASSERT_DOUBLES_EQUAL(c, a, 1e-100);
  222. }
  223. }
  224. LOLUNIT_TEST(FloorCeilEtc)
  225. {
  226. double tests[] =
  227. {
  228. -2.0, -2.0, -2.0, -2.0,
  229. -1.5, -2.0, -1.0, -2.0,
  230. -1.0, -1.0, -1.0, -1.0,
  231. -0.0, -0.0, -0.0, -0.0,
  232. 0.0, 0.0, 0.0, 0.0,
  233. 0.25, 0.0, 1.0, 0.0,
  234. 0.375, 0.0, 1.0, 0.0,
  235. 0.5, 0.0, 1.0, 1.0,
  236. 1.0, 1.0, 1.0, 1.0,
  237. 1.5, 1.0, 2.0, 2.0,
  238. 2.0, 2.0, 2.0, 2.0,
  239. 2.5, 2.0, 3.0, 3.0,
  240. 3.0, 3.0, 3.0, 3.0,
  241. 8192.0, 8192.0, 8192.0, 8192.0,
  242. 8192.03125, 8192.0, 8193.0, 8192.0,
  243. 8192.5, 8192.0, 8193.0, 8193.0,
  244. 8193.0, 8193.0, 8193.0, 8193.0,
  245. 549755813888.0, 549755813888.0, 549755813888.0, 549755813888.0,
  246. 549755813888.03125, 549755813888.0, 549755813889.0, 549755813888.0,
  247. 549755813888.5, 549755813888.0, 549755813889.0, 549755813889.0,
  248. 549755813889.0, 549755813889.0, 549755813889.0, 549755813889.0,
  249. };
  250. for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n += 4)
  251. {
  252. double a0 = floor((real)tests[n]);
  253. double b0 = tests[n + 1];
  254. double a1 = ceil((real)tests[n]);
  255. double b1 = tests[n + 2];
  256. double a2 = round((real)tests[n]);
  257. double b2 = tests[n + 3];
  258. LOLUNIT_ASSERT_EQUAL(b0, a0);
  259. LOLUNIT_ASSERT_EQUAL(b1, a1);
  260. LOLUNIT_ASSERT_EQUAL(b2, a2);
  261. }
  262. }
  263. };
  264. } /* namespace lol */