25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

170 lines
4.4 KiB

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