Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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