BufferedValueTests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "tests/BufferedValueTests.h"
  2. #include "math/BufferedValue.h"
  3. #include "math/Vector.h"
  4. #include "tests/Test.h"
  5. const float eps = 0.0001f;
  6. template<int N, typename T>
  7. static void compareVectors(Test& test, const Vector<N, T>& wanted,
  8. const Vector<N, T>& actual, const char* text) {
  9. for(int i = 0; i < N; i++) {
  10. test.checkFloat(
  11. wanted[i], actual[i], eps,
  12. StringBuffer<100>(text).append(" (").append(i).append(")"));
  13. }
  14. }
  15. static void testInit(Test& test) {
  16. BufferedValue<float> b = 5.0f;
  17. test.checkFloat(5.0f, b.get(0.0f), eps, "init 1");
  18. test.checkFloat(5.0f, b.get(0.5f), eps, "init 2");
  19. test.checkFloat(5.0f, b.get(1.0f), eps, "init 3");
  20. test.checkFloat(5.0f, b, eps, "init 4");
  21. }
  22. static void testInterpolate(Test& test) {
  23. BufferedValue<float> b = 5.0f;
  24. b = 7.0f;
  25. test.checkFloat(5.0f, b.get(0.0f), eps, "interpolate 1");
  26. test.checkFloat(6.0f, b.get(0.5f), eps, "interpolate 2");
  27. test.checkFloat(7.0f, b.get(1.0f), eps, "interpolate 3");
  28. test.checkFloat(7.0f, b, eps, "interpolate 4");
  29. }
  30. static void testUpdate(Test& test) {
  31. BufferedValue<float> b = 5.0f;
  32. b = 7.0f;
  33. b.update();
  34. test.checkFloat(7.0f, b.get(0.0f), eps, "update 1");
  35. test.checkFloat(7.0f, b.get(0.5f), eps, "update 2");
  36. test.checkFloat(7.0f, b.get(1.0f), eps, "update 3");
  37. test.checkFloat(7.0f, b, eps, "update 4");
  38. }
  39. static void testCalculate(Test& test) {
  40. BufferedValue<float> b = 5.0f;
  41. b = 7.0f;
  42. b += 3.0f;
  43. test.checkFloat(5.0f, b.get(0.0f), eps, "calculate 1");
  44. test.checkFloat(7.5f, b.get(0.5f), eps, "calculate 2");
  45. test.checkFloat(10.0f, b.get(1.0f), eps, "calculate 3");
  46. test.checkFloat(10.0f, b, eps, "calculate 4");
  47. test.checkFloat(12.0f, b + 2.0f, eps, "calculate 5");
  48. }
  49. static void testVector(Test& test) {
  50. Vector2 base(5.0f, 6.0f);
  51. BufferedValue<Vector2> b = base;
  52. compareVectors(test, base, b.get(1.0f), "vector 1");
  53. b = Vector2(7.0f, 5.0);
  54. compareVectors(test, Vector2(7.0f, 5.0f), b.get(1.0f), "vector 2");
  55. b += Vector2(1.0f, 1.0f);
  56. compareVectors(test, Vector2(8.0f, 6.0f), b.get(1.0f), "vector 3");
  57. b -= Vector2(1.0f, 1.0f);
  58. compareVectors(test, Vector2(7.0f, 5.0f), b.get(1.0f), "vector 4");
  59. b *= Vector2(2.0f, 2.0f);
  60. compareVectors(test, Vector2(14.0f, 10.0f), b.get(1.0f), "vector 5");
  61. b /= Vector2(0.5f, 0.5f);
  62. compareVectors(test, Vector2(28.0f, 20.0f), b.get(1.0f), "vector 6");
  63. b = b + Vector2(1.0f, 1.0f);
  64. compareVectors(test, Vector2(29.0f, 21.0f), b.get(1.0f), "vector 7");
  65. b = b - Vector2(1.0f, 1.0f);
  66. compareVectors(test, Vector2(28.0f, 20.0f), b.get(1.0f), "vector 8");
  67. b = b * Vector2(2.0f, 2.0f);
  68. compareVectors(test, Vector2(56.0f, 40.0f), b.get(1.0f), "vector 9");
  69. b = b / Vector2(0.5f, 0.5f);
  70. compareVectors(test, Vector2(112.0f, 80.0f), b.get(1.0f), "vector 10");
  71. b = Vector2(1.0f, 1.0f) + b;
  72. compareVectors(test, Vector2(113.0f, 81.0f), b.get(1.0f), "vector 11");
  73. b = Vector2(1.0f, 1.0f) - b;
  74. compareVectors(test, Vector2(-112.0f, -80.0f), b.get(1.0f), "vector 12");
  75. b = Vector2(2.0f, 2.0f) * b;
  76. compareVectors(test, Vector2(-224.0f, -160.0f), b.get(1.0f), "vector 13");
  77. b = Vector2(224.0f, 160.0f) / b;
  78. compareVectors(test, Vector2(-1.0f, -1.0f), b.get(1.0f), "vector 14");
  79. b = -b;
  80. compareVectors(test, Vector2(1.0f, 1.0f), b.get(1.0f), "vector 15");
  81. b[0] += 3;
  82. compareVectors(test, Vector2(4.0f, 1.0f), b.get(1.0f), "vector 16");
  83. }
  84. void BufferedValueTests::test() {
  85. Test test("BufferedValue");
  86. testInit(test);
  87. testInterpolate(test);
  88. testUpdate(test);
  89. testCalculate(test);
  90. testVector(test);
  91. test.finalize();
  92. }