You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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