Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

166 wiersze
4.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(TrigTest)
  19. {
  20. public:
  21. void setUp() {}
  22. void tearDown() {}
  23. LOLUNIT_TEST(test_sin)
  24. {
  25. for (int i = -10000; i < 10000; i++)
  26. {
  27. double f = (double)i * (1.0 / 1000.0);
  28. #if defined __GNUC__
  29. double a = __builtin_sin(f);
  30. #else
  31. double a = sin(f);
  32. #endif
  33. double b = lol_sin(f);
  34. LOLUNIT_SET_CONTEXT(f);
  35. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  36. }
  37. for (int i = -10000; i < 10000; i++)
  38. {
  39. double f = (double)i * (1.0 / 100000.0);
  40. #if defined __GNUC__
  41. double a = __builtin_sin(f);
  42. #else
  43. double a = sin(f);
  44. #endif
  45. double b = lol_sin(f);
  46. LOLUNIT_SET_CONTEXT(f);
  47. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  48. }
  49. }
  50. LOLUNIT_TEST(test_cos)
  51. {
  52. for (int i = -10000; i < 10000; i++)
  53. {
  54. double f = (double)i * (1.0 / 1000.0);
  55. #if defined __GNUC__
  56. double a = __builtin_cos(f);
  57. #else
  58. double a = cos(f);
  59. #endif
  60. double b = lol_cos(f);
  61. LOLUNIT_SET_CONTEXT(f);
  62. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  63. }
  64. for (int i = -10000; i < 10000; i++)
  65. {
  66. double f = (double)i * (1.0 / 100000.0);
  67. #if defined __GNUC__
  68. double a = __builtin_cos(f);
  69. #else
  70. double a = cos(f);
  71. #endif
  72. double b = lol_cos(f);
  73. LOLUNIT_SET_CONTEXT(f);
  74. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  75. }
  76. }
  77. LOLUNIT_TEST(test_sincos)
  78. {
  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 = sin(f);
  87. double a2 = 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 = sin(f);
  103. double a2 = 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(test_tan)
  113. {
  114. for (int i = -100000; i < 100000; i++)
  115. {
  116. double f = (double)i * (1.0 / 10000.0);
  117. #if defined __GNUC__
  118. double a = __builtin_tan(f);
  119. #else
  120. double a = tan(f);
  121. #endif
  122. double b = lol_tan(f);
  123. LOLUNIT_SET_CONTEXT(f);
  124. if (fabs(a) > 1e4)
  125. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
  126. else if (fabs(a) > 1.0)
  127. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
  128. else
  129. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  130. }
  131. for (int i = -10000; i < 10000; i++)
  132. {
  133. double f = (double)i * (1.0 / 100000.0);
  134. #if defined __GNUC__
  135. double a = __builtin_tan(f);
  136. #else
  137. double a = tan(f);
  138. #endif
  139. double b = lol_tan(f);
  140. LOLUNIT_SET_CONTEXT(f);
  141. if (fabs(a) > 1e4)
  142. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
  143. else if (fabs(a) > 1.0)
  144. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
  145. else
  146. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  147. }
  148. }
  149. };
  150. } /* namespace lol */