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.
 
 
 

360 line
11 KiB

  1. //
  2. // Lol Engine — Unit tests
  3. //
  4. // Copyright © 2010—2014 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cmath>
  14. #include <lolunit.h>
  15. namespace lol
  16. {
  17. lolunit_declare_fixture(RealTest)
  18. {
  19. lolunit_declare_test(Constants)
  20. {
  21. double a0 = real::R_0();
  22. double a1 = real::R_1();
  23. double a2 = real::R_2();
  24. double a10 = real::R_10();
  25. lolunit_assert_equal(a0, 0.0);
  26. lolunit_assert_equal(a1, 1.0);
  27. lolunit_assert_equal(a2, 2.0);
  28. lolunit_assert_equal(a10, 10.0);
  29. double b1 = log(real::R_E());
  30. double b2 = log2(real::R_2());
  31. lolunit_assert_equal(b1, 1.0);
  32. lolunit_assert_equal(b2, 1.0);
  33. double c1 = exp(re(real::R_LOG2E()));
  34. double c2 = log(exp2(real::R_LOG2E()));
  35. lolunit_assert_equal(c1, 2.0);
  36. lolunit_assert_equal(c2, 1.0);
  37. double d1 = exp(re(real::R_LOG10E()));
  38. lolunit_assert_equal(d1, 10.0);
  39. double e1 = exp(real::R_LN2());
  40. lolunit_assert_equal(e1, 2.0);
  41. double f1 = exp(real::R_LN10());
  42. lolunit_assert_equal(f1, 10.0);
  43. double g1 = sin(real::R_PI());
  44. double g2 = cos(real::R_PI());
  45. lolunit_assert_doubles_equal(g1, 0.0, 1e-100);
  46. lolunit_assert_equal(g2, -1.0);
  47. double h1 = sin(real::R_PI_2());
  48. double h2 = cos(real::R_PI_2());
  49. lolunit_assert_equal(h1, 1.0);
  50. lolunit_assert_doubles_equal(h2, 0.0, 1e-100);
  51. double i1 = sin(real::R_PI_4()) * sin(real::R_PI_4());
  52. double i2 = cos(real::R_PI_4()) * cos(real::R_PI_4());
  53. lolunit_assert_equal(i1, 0.5);
  54. lolunit_assert_equal(i2, 0.5);
  55. }
  56. lolunit_declare_test(FloatToReal)
  57. {
  58. float a1 = real(0.0f);
  59. float a2 = real(-0.0f);
  60. float a3 = real(1.0f);
  61. float a4 = real(-1.0f);
  62. float a5 = real(1.5f);
  63. float a6 = real(12345678.0f);
  64. lolunit_assert_equal(a1, 0.0f);
  65. lolunit_assert_equal(a2, -0.0f);
  66. lolunit_assert_equal(a3, 1.0f);
  67. lolunit_assert_equal(a4, -1.0f);
  68. lolunit_assert_equal(a5, 1.5f);
  69. lolunit_assert_equal(a6, 12345678.0f);
  70. }
  71. lolunit_declare_test(DoubleToReal)
  72. {
  73. double a1 = real(0.0);
  74. double a2 = real(-0.0);
  75. double a3 = real(1.0);
  76. double a4 = real(-1.0);
  77. double a5 = real(1.5);
  78. double a6 = real(1234567876543210.0);
  79. lolunit_assert_doubles_equal(a1, 0.0, 0.0);
  80. lolunit_assert_doubles_equal(a2, -0.0, 0.0);
  81. lolunit_assert_doubles_equal(a3, 1.0, 0.0);
  82. lolunit_assert_doubles_equal(a4, -1.0, 0.0);
  83. lolunit_assert_doubles_equal(a5, 1.5, 0.0);
  84. lolunit_assert_doubles_equal(a6, 1234567876543210.0, 0.0);
  85. }
  86. lolunit_declare_test(Init)
  87. {
  88. real r;
  89. float f1 = (float)r;
  90. lolunit_assert_equal(f1, 0.0f);
  91. rcmplx q;
  92. float f2 = (float)q.x;
  93. float f3 = (float)q.y;
  94. lolunit_assert_equal(f2, 0.0f);
  95. lolunit_assert_equal(f3, 0.0f);
  96. }
  97. lolunit_declare_test(StringToReal)
  98. {
  99. float a1 = real("0");
  100. float a2 = real("1");
  101. float a3 = real("-1");
  102. /* 2^-128 * 2^128 */
  103. float a4 = real("0.0000000000000000000000000000000000000029387358770"
  104. "557187699218413430556141945466638919302188037718792"
  105. "6569604314863681793212890625")
  106. * real("340282366920938463463374607431768211456");
  107. lolunit_assert_equal(a1, 0.0f);
  108. lolunit_assert_equal(a2, 1.0f);
  109. lolunit_assert_equal(a3, -1.0f);
  110. lolunit_assert_equal(a4, 1.0f);
  111. }
  112. lolunit_declare_test(UnaryMinus)
  113. {
  114. float a1 = - real(1.0f);
  115. float a2 = - real(-1.0f);
  116. float a3 = - real(0.0f);
  117. float a4 = - real(-0.0f);
  118. lolunit_assert_equal(a1, -1.0f);
  119. lolunit_assert_equal(a2, 1.0f);
  120. lolunit_assert_equal(a3, -0.0f);
  121. lolunit_assert_equal(a4, 0.0f);
  122. }
  123. lolunit_declare_test(Comparison)
  124. {
  125. lolunit_assert(real(1.0f) > real(0.5f));
  126. lolunit_assert(real(1.0f) >= real(0.5f));
  127. lolunit_assert(real(1.0f) >= real(1.0f));
  128. lolunit_assert(real(-1.0f) < real(-0.5f));
  129. lolunit_assert(real(-1.0f) <= real(-0.5f));
  130. lolunit_assert(real(-1.0f) <= real(-1.0f));
  131. lolunit_assert(real(-1.0f) < real(0.5f));
  132. lolunit_assert(real(-0.5f) < real(1.0f));
  133. lolunit_assert(real(-1.0f) <= real(0.5f));
  134. lolunit_assert(real(-0.5f) <= real(1.0f));
  135. lolunit_assert(real(1.0f) > real(-0.5f));
  136. lolunit_assert(real(0.5f) > real(-1.0f));
  137. lolunit_assert(real(1.0f) >= real(-0.5f));
  138. lolunit_assert(real(0.5f) >= real(-1.0f));
  139. }
  140. lolunit_declare_test(Addition)
  141. {
  142. float a1 = real(1.0f) + real(0.0f);
  143. float a2 = real(0.0f) + real(1.0f);
  144. float a3 = real(1.0f) + real(1.0f);
  145. float a4 = real(-1.0f) + real(-1.0f);
  146. float a5 = real(1.0f) + real(0.125f);
  147. double a6 = real(3.13609818956293918)
  148. + real(0.00005972154828114);
  149. float a7 = real(1.0f) + real(-0.125f);
  150. double a8 = real(0.10000000002) + real(-2.0e-11);
  151. lolunit_assert_equal(a1, 1.0f);
  152. lolunit_assert_equal(a2, 1.0f);
  153. lolunit_assert_equal(a3, 2.0f);
  154. lolunit_assert_equal(a4, -2.0f);
  155. lolunit_assert_equal(a5, 1.125f);
  156. lolunit_assert_doubles_equal(a6, 3.1361579, 0.000001);
  157. lolunit_assert_equal(a7, 0.875f);
  158. lolunit_assert_doubles_equal(a8, 0.1, 1.0e-13);
  159. }
  160. lolunit_declare_test(Subtraction)
  161. {
  162. float a1 = real(1.0f) + real(1e20f) - real(1e20f);
  163. lolunit_assert_equal(a1, 1.0f);
  164. }
  165. lolunit_declare_test(Multiplication)
  166. {
  167. real x(1.25f);
  168. real y(1.5f);
  169. real z(1.99999f);
  170. real w(-1.5f);
  171. float m1 = x * x;
  172. float m2 = y * y;
  173. float m3 = z * z;
  174. float m4 = w * w;
  175. lolunit_assert_equal(m1, 1.25f * 1.25f);
  176. lolunit_assert_equal(m2, 1.5f * 1.5f);
  177. lolunit_assert_equal(m3, 1.99999f * 1.99999f);
  178. lolunit_assert_equal(m4, -1.5f * -1.5f);
  179. }
  180. lolunit_declare_test(ExactDivision)
  181. {
  182. float m1 = real::R_1() / real::R_1();
  183. float m2 = real::R_2() / real::R_1();
  184. float m3 = real::R_1() / real::R_2();
  185. float m4 = real::R_2() / real::R_2();
  186. float m5 = real::R_1() / -real::R_2();
  187. lolunit_assert_equal(m1, 1.0f);
  188. lolunit_assert_equal(m2, 2.0f);
  189. lolunit_assert_equal(m3, 0.5f);
  190. lolunit_assert_equal(m4, 1.0f);
  191. lolunit_assert_equal(m5, -0.5f);
  192. }
  193. lolunit_declare_test(InexactDivision)
  194. {
  195. /* 1 / 3 * 3 should be close to 1... check that it does not differ
  196. * by more than 2^-k where k is the number of bits in the mantissa. */
  197. real a = real::R_1() / real::R_3() * real::R_3();
  198. real b = ldexp(real::R_1() - a, real::BIGITS * real::BIGIT_BITS);
  199. lolunit_assert_lequal((double)fabs(b), 1.0);
  200. }
  201. lolunit_declare_test(LoadExp)
  202. {
  203. real a1(1.5);
  204. real a2(-1.5);
  205. real a3(0.0);
  206. lolunit_assert_equal((double)ldexp(a1, 7), 192.0);
  207. lolunit_assert_equal((double)ldexp(a1, -7), 0.01171875);
  208. lolunit_assert_equal((double)ldexp(a2, 7), -192.0);
  209. lolunit_assert_equal((double)ldexp(a2, -7), -0.01171875);
  210. lolunit_assert_equal((double)ldexp(a3, 7), 0.0);
  211. lolunit_assert_equal((double)ldexp(a3, -7), 0.0);
  212. }
  213. lolunit_declare_test(Ulp)
  214. {
  215. real a1 = real::R_PI();
  216. lolunit_assert_not_equal((double)(a1 + ulp(a1) - a1), 0.0);
  217. lolunit_assert_equal((double)(a1 + ulp(a1) / 2 - a1), 0.0);
  218. }
  219. lolunit_declare_test(Bool)
  220. {
  221. real a = 0.0;
  222. lolunit_assert(!a);
  223. a = -0.0;
  224. lolunit_assert(!a);
  225. a = 1234.0;
  226. lolunit_assert(a);
  227. lolunit_assert(!!a);
  228. a = -1234.0;
  229. lolunit_assert(a);
  230. lolunit_assert(!!a);
  231. }
  232. lolunit_declare_test(AsinAcos)
  233. {
  234. double tests[] =
  235. {
  236. -1024.0, -1023.0, -513.0, -512.0, -511.0, -1.0, -0.0,
  237. 0.0, 1.0, 511.0, 512.0, 513.0, 1023.0, 1024.0
  238. };
  239. for (double test : tests)
  240. {
  241. double a = test / 1024;
  242. double b = sin(asin((real)a));
  243. double c = cos(acos((real)a));
  244. lolunit_set_context(a);
  245. lolunit_assert_doubles_equal(b, a, 1e-100);
  246. lolunit_assert_doubles_equal(c, a, 1e-100);
  247. }
  248. }
  249. lolunit_declare_test(FloorCeilEtc)
  250. {
  251. double tests[] =
  252. {
  253. -2.0, -2.0, -2.0, -2.0,
  254. -1.5, -2.0, -1.0, -2.0,
  255. -1.0, -1.0, -1.0, -1.0,
  256. -0.0, -0.0, -0.0, -0.0,
  257. 0.0, 0.0, 0.0, 0.0,
  258. 0.25, 0.0, 1.0, 0.0,
  259. 0.375, 0.0, 1.0, 0.0,
  260. 0.5, 0.0, 1.0, 1.0,
  261. 1.0, 1.0, 1.0, 1.0,
  262. 1.5, 1.0, 2.0, 2.0,
  263. 2.0, 2.0, 2.0, 2.0,
  264. 2.5, 2.0, 3.0, 3.0,
  265. 3.0, 3.0, 3.0, 3.0,
  266. 8192.0, 8192.0, 8192.0, 8192.0,
  267. 8192.03125, 8192.0, 8193.0, 8192.0,
  268. 8192.5, 8192.0, 8193.0, 8193.0,
  269. 8193.0, 8193.0, 8193.0, 8193.0,
  270. 549755813888.0, 549755813888.0, 549755813888.0, 549755813888.0,
  271. 549755813888.03125, 549755813888.0, 549755813889.0, 549755813888.0,
  272. 549755813888.5, 549755813888.0, 549755813889.0, 549755813889.0,
  273. 549755813889.0, 549755813889.0, 549755813889.0, 549755813889.0,
  274. };
  275. for (unsigned int n = 0; n < sizeof(tests) / sizeof(*tests); n += 4)
  276. {
  277. double a0 = floor((real)tests[n]);
  278. double b0 = tests[n + 1];
  279. double a1 = ceil((real)tests[n]);
  280. double b1 = tests[n + 2];
  281. double a2 = round((real)tests[n]);
  282. double b2 = tests[n + 3];
  283. lolunit_assert_equal(b0, a0);
  284. lolunit_assert_equal(b1, a1);
  285. lolunit_assert_equal(b2, a2);
  286. }
  287. }
  288. lolunit_declare_test(Pow)
  289. {
  290. double a1 = pow(-real::R_2(), real::R_2());
  291. double b1 = 4.0;
  292. lolunit_assert_doubles_equal(a1, b1, 1.0e-13);
  293. double a2 = pow(-real::R_2(), real::R_3());
  294. double b2 = -8.0;
  295. lolunit_assert_doubles_equal(a2, b2, 1.0e-13);
  296. }
  297. };
  298. } /* namespace lol */