BufferedValueTests.cpp 3.1 KB

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