BufferedValueTests.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "tests/BufferedValueTests.hpp"
  2. #include "math/BufferedValue.hpp"
  3. #include "math/Vector.hpp"
  4. #include "test/Test.hpp"
  5. const float eps = 0.0001f;
  6. static void testInit() {
  7. Core::BufferedValue<float> b = 5.0f;
  8. CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
  9. CORE_TEST_FLOAT(5.0f, b.get(0.5f), eps);
  10. CORE_TEST_FLOAT(5.0f, b.get(1.0f), eps);
  11. CORE_TEST_FLOAT(5.0f, b, eps);
  12. }
  13. static void testInterpolate() {
  14. Core::BufferedValue<float> b = 5.0f;
  15. b = 7.0f;
  16. CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
  17. CORE_TEST_FLOAT(6.0f, b.get(0.5f), eps);
  18. CORE_TEST_FLOAT(7.0f, b.get(1.0f), eps);
  19. CORE_TEST_FLOAT(7.0f, b, eps);
  20. }
  21. static void testUpdate() {
  22. Core::BufferedValue<float> b = 5.0f;
  23. b = 7.0f;
  24. b.update();
  25. CORE_TEST_FLOAT(7.0f, b.get(0.0f), eps);
  26. CORE_TEST_FLOAT(7.0f, b.get(0.5f), eps);
  27. CORE_TEST_FLOAT(7.0f, b.get(1.0f), eps);
  28. CORE_TEST_FLOAT(7.0f, b, eps);
  29. }
  30. static void testCalculate() {
  31. Core::BufferedValue<float> b = 5.0f;
  32. b = 7.0f;
  33. b += 3.0f;
  34. CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
  35. CORE_TEST_FLOAT(7.5f, b.get(0.5f), eps);
  36. CORE_TEST_FLOAT(10.0f, b.get(1.0f), eps);
  37. CORE_TEST_FLOAT(10.0f, b, eps);
  38. CORE_TEST_FLOAT(12.0f, b + 2.0f, eps);
  39. }
  40. static void testVector() {
  41. Core::Vector2 base(5.0f, 6.0f);
  42. Core::BufferedValue<Core::Vector2> b = base;
  43. CORE_TEST_VECTOR(base, b.get(1.0f));
  44. b = Core::Vector2(7.0f, 5.0);
  45. CORE_TEST_VECTOR(Core::Vector2(7.0f, 5.0f), b.get(1.0f));
  46. b += Core::Vector2(1.0f, 1.0f);
  47. CORE_TEST_VECTOR(Core::Vector2(8.0f, 6.0f), b.get(1.0f));
  48. b -= Core::Vector2(1.0f, 1.0f);
  49. CORE_TEST_VECTOR(Core::Vector2(7.0f, 5.0f), b.get(1.0f));
  50. b *= Core::Vector2(2.0f, 2.0f);
  51. CORE_TEST_VECTOR(Core::Vector2(14.0f, 10.0f), b.get(1.0f));
  52. b /= Core::Vector2(0.5f, 0.5f);
  53. CORE_TEST_VECTOR(Core::Vector2(28.0f, 20.0f), b.get(1.0f));
  54. b = b + Core::Vector2(1.0f, 1.0f);
  55. CORE_TEST_VECTOR(Core::Vector2(29.0f, 21.0f), b.get(1.0f));
  56. b = b - Core::Vector2(1.0f, 1.0f);
  57. CORE_TEST_VECTOR(Core::Vector2(28.0f, 20.0f), b.get(1.0f));
  58. b = b * Core::Vector2(2.0f, 2.0f);
  59. CORE_TEST_VECTOR(Core::Vector2(56.0f, 40.0f), b.get(1.0f));
  60. b = b / Core::Vector2(0.5f, 0.5f);
  61. CORE_TEST_VECTOR(Core::Vector2(112.0f, 80.0f), b.get(1.0f));
  62. b = Core::Vector2(1.0f, 1.0f) + b;
  63. CORE_TEST_VECTOR(Core::Vector2(113.0f, 81.0f), b.get(1.0f));
  64. b = Core::Vector2(1.0f, 1.0f) - b;
  65. CORE_TEST_VECTOR(Core::Vector2(-112.0f, -80.0f), b.get(1.0f));
  66. b = Core::Vector2(2.0f, 2.0f) * b;
  67. CORE_TEST_VECTOR(Core::Vector2(-224.0f, -160.0f), b.get(1.0f));
  68. b = Core::Vector2(224.0f, 160.0f) / b;
  69. CORE_TEST_VECTOR(Core::Vector2(-1.0f, -1.0f), b.get(1.0f));
  70. b = -b;
  71. CORE_TEST_VECTOR(Core::Vector2(1.0f, 1.0f), b.get(1.0f));
  72. b[0] += 3;
  73. CORE_TEST_VECTOR(Core::Vector2(4.0f, 1.0f), b.get(1.0f));
  74. }
  75. void Core::BufferedValueTests::test() {
  76. testInit();
  77. testInterpolate();
  78. testUpdate();
  79. testCalculate();
  80. testVector();
  81. }