Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

185 righe
5.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #include <lol/main.h>
  15. #include "lol/unit.h"
  16. namespace lol
  17. {
  18. LOLUNIT_FIXTURE(TrigTest)
  19. {
  20. LOLUNIT_TEST(AngleConversions)
  21. {
  22. LOLUNIT_ASSERT_DOUBLES_EQUAL(D_PI, radians(180.0), 1e-5);
  23. LOLUNIT_ASSERT_DOUBLES_EQUAL(D_PI_2, radians(90.0), 1e-5);
  24. LOLUNIT_ASSERT_DOUBLES_EQUAL(F_PI, radians(180.0f), 1e-5f);
  25. LOLUNIT_ASSERT_DOUBLES_EQUAL(F_PI_2, radians(90.0f), 1e-5f);
  26. LOLUNIT_ASSERT_DOUBLES_EQUAL(180.0, degrees(D_PI), 1e-5);
  27. LOLUNIT_ASSERT_DOUBLES_EQUAL(90.0, degrees(D_PI_2), 1e-5);
  28. LOLUNIT_ASSERT_DOUBLES_EQUAL(180.0f, degrees(F_PI), 1e-5f);
  29. LOLUNIT_ASSERT_DOUBLES_EQUAL(90.0f, degrees(F_PI_2), 1e-5f);
  30. }
  31. LOLUNIT_TEST(Sin)
  32. {
  33. using std::fabs;
  34. for (int i = -10000; i < 10000; i++)
  35. {
  36. double f = (double)i * (1.0 / 1000.0);
  37. #if defined __GNUC__ && !defined __SNC__
  38. double a = __builtin_sin(f);
  39. #else
  40. double a = std::sin(f);
  41. #endif
  42. double b = lol_sin(f);
  43. LOLUNIT_SET_CONTEXT(f);
  44. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  45. }
  46. for (int i = -10000; i < 10000; i++)
  47. {
  48. double f = (double)i * (1.0 / 100000.0);
  49. #if defined __GNUC__ && !defined __SNC__
  50. double a = __builtin_sin(f);
  51. #else
  52. double a = std::sin(f);
  53. #endif
  54. double b = lol_sin(f);
  55. LOLUNIT_SET_CONTEXT(f);
  56. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  57. }
  58. }
  59. LOLUNIT_TEST(Cos)
  60. {
  61. using std::fabs;
  62. for (int i = -10000; i < 10000; i++)
  63. {
  64. double f = (double)i * (1.0 / 1000.0);
  65. #if defined __GNUC__ && !defined __SNC__
  66. double a = __builtin_cos(f);
  67. #else
  68. double a = std::cos(f);
  69. #endif
  70. double b = lol_cos(f);
  71. LOLUNIT_SET_CONTEXT(f);
  72. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  73. }
  74. for (int i = -10000; i < 10000; i++)
  75. {
  76. double f = (double)i * (1.0 / 100000.0);
  77. #if defined __GNUC__ && !defined __SNC__
  78. double a = __builtin_cos(f);
  79. #else
  80. double a = std::cos(f);
  81. #endif
  82. double b = lol_cos(f);
  83. LOLUNIT_SET_CONTEXT(f);
  84. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  85. }
  86. }
  87. LOLUNIT_TEST(SinCos)
  88. {
  89. using std::fabs;
  90. for (int i = -10000; i < 10000; i++)
  91. {
  92. double f = (double)i * (1.0 / 1000.0);
  93. #if defined __GNUC__ && !defined __SNC__
  94. double a1 = __builtin_sin(f);
  95. double a2 = __builtin_cos(f);
  96. #else
  97. double a1 = std::sin(f);
  98. double a2 = std::cos(f);
  99. #endif
  100. double b1, b2;
  101. lol_sincos(f, &b1, &b2);
  102. LOLUNIT_SET_CONTEXT(f);
  103. LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
  104. LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
  105. }
  106. for (int i = -10000; i < 10000; i++)
  107. {
  108. double f = (double)i * (1.0 / 100000.0);
  109. #if defined __GNUC__ && !defined __SNC__
  110. double a1 = __builtin_sin(f);
  111. double a2 = __builtin_cos(f);
  112. #else
  113. double a1 = std::sin(f);
  114. double a2 = std::cos(f);
  115. #endif
  116. double b1, b2;
  117. lol_sincos(f, &b1, &b2);
  118. LOLUNIT_SET_CONTEXT(f);
  119. LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
  120. LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
  121. }
  122. }
  123. LOLUNIT_TEST(Tan)
  124. {
  125. using std::fabs;
  126. for (int i = -100000; i < 100000; i++)
  127. {
  128. double f = (double)i * (1.0 / 10000.0);
  129. #if defined __GNUC__ && !defined __SNC__
  130. double a = __builtin_tan(f);
  131. #else
  132. double a = std::tan(f);
  133. #endif
  134. double b = lol_tan(f);
  135. LOLUNIT_SET_CONTEXT(f);
  136. if (fabs(a) > 1e4)
  137. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
  138. else if (fabs(a) > 1.0)
  139. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
  140. else
  141. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  142. }
  143. for (int i = -10000; i < 10000; i++)
  144. {
  145. double f = (double)i * (1.0 / 100000.0);
  146. #if defined __GNUC__ && !defined __SNC__
  147. double a = __builtin_tan(f);
  148. #else
  149. double a = std::tan(f);
  150. #endif
  151. double b = lol_tan(f);
  152. LOLUNIT_SET_CONTEXT(f);
  153. if (fabs(a) > 1e4)
  154. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
  155. else if (fabs(a) > 1.0)
  156. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
  157. else
  158. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  159. }
  160. }
  161. };
  162. } /* namespace lol */