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.
 
 
 

128 wiersze
3.6 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 <cppunit/extensions/HelperMacros.h>
  14. #include <cppunit/TestCaller.h>
  15. #include <cppunit/TestCase.h>
  16. #include <cppunit/TestSuite.h>
  17. #include <cmath>
  18. #include "core.h"
  19. namespace lol
  20. {
  21. class TrigTest : public CppUnit::TestCase
  22. {
  23. CPPUNIT_TEST_SUITE(TrigTest);
  24. CPPUNIT_TEST(test_sin);
  25. CPPUNIT_TEST_SUITE_END();
  26. public:
  27. TrigTest() : CppUnit::TestCase("Trigonometry Test") {}
  28. void setUp() {}
  29. void tearDown() {}
  30. void test_sin()
  31. {
  32. for (int i = -10000; i < 10000; i++)
  33. {
  34. double f = (double)i * (1.0 / 1000.0);
  35. double a = __builtin_sin(f);
  36. double b = lol_sin(f);
  37. CPPUNIT_ASSERT(fabs(a - b) <= fabs(f) * 1e-11);
  38. }
  39. for (int i = -10000; i < 10000; i++)
  40. {
  41. double f = (double)i * (1.0 / 100000.0);
  42. double a = __builtin_sin(f);
  43. double b = lol_sin(f);
  44. CPPUNIT_ASSERT(fabs(a - b) <= fabs(f) * 1e-11);
  45. }
  46. for (int i = -10000; i < 10000; i++)
  47. {
  48. double f = (double)i * (1.0 / 1000.0);
  49. double a = __builtin_cos(f);
  50. double b = lol_cos(f);
  51. CPPUNIT_ASSERT(fabs(a - b) <= fabs(f) * 1e-11);
  52. }
  53. for (int i = -10000; i < 10000; i++)
  54. {
  55. double f = (double)i * (1.0 / 100000.0);
  56. double a = __builtin_cos(f);
  57. double b = lol_cos(f);
  58. CPPUNIT_ASSERT(fabs(a - b) <= fabs(f) * 1e-11);
  59. }
  60. for (int i = -10000; i < 10000; i++)
  61. {
  62. double f = (double)i * (1.0 / 1000.0);
  63. double a1 = __builtin_sin(f);
  64. double a2 = __builtin_cos(f);
  65. double b1, b2;
  66. lol_sincos(f, &b1, &b2);
  67. CPPUNIT_ASSERT(fabs(a1 - b1) <= fabs(f) * 1e-11);
  68. CPPUNIT_ASSERT(fabs(a2 - b2) <= fabs(f) * 1e-11);
  69. }
  70. for (int i = -10000; i < 10000; i++)
  71. {
  72. double f = (double)i * (1.0 / 100000.0);
  73. double a1 = __builtin_sin(f);
  74. double a2 = __builtin_cos(f);
  75. double b1, b2;
  76. lol_sincos(f, &b1, &b2);
  77. CPPUNIT_ASSERT(fabs(a1 - b1) <= fabs(f) * 1e-11);
  78. CPPUNIT_ASSERT(fabs(a2 - b2) <= fabs(f) * 1e-11);
  79. }
  80. for (int i = -100000; i < 100000; i++)
  81. {
  82. double f = (double)i * (1.0 / 10000.0);
  83. double a = __builtin_tan(f);
  84. double b = lol_tan(f);
  85. if (fabs(a) > 1e4)
  86. CPPUNIT_ASSERT(fabs(a - b) <= fabs(a) * fabs(a) * 1e-11);
  87. else if (fabs(a) > 1.0)
  88. CPPUNIT_ASSERT(fabs(a - b) <= fabs(a) * 1e-11);
  89. else
  90. CPPUNIT_ASSERT(fabs(a - b) <= fabs(f) * 1e-11);
  91. }
  92. for (int i = -10000; i < 10000; i++)
  93. {
  94. double f = (double)i * (1.0 / 100000.0);
  95. double a = __builtin_tan(f);
  96. double b = lol_tan(f);
  97. if (fabs(a) > 1e4)
  98. CPPUNIT_ASSERT(fabs(a - b) <= fabs(a) * fabs(a) * 1e-11);
  99. else if (fabs(a) > 1.0)
  100. CPPUNIT_ASSERT(fabs(a - b) <= fabs(a) * 1e-11);
  101. else
  102. CPPUNIT_ASSERT(fabs(a - b) <= fabs(f) * 1e-11);
  103. }
  104. }
  105. };
  106. CPPUNIT_TEST_SUITE_REGISTRATION(TrigTest);
  107. } /* namespace lol */