Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

358 Zeilen
10 KiB

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