BoxTests.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "tests/BoxTests.h"
  2. #include "math/Box.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. typedef StringBuffer<250> String;
  6. static void testInit(Test& test) {
  7. Box box(Vector3(1.0f, 2.0f, 3.0f));
  8. test.checkEqual(String("Box([0.00, 0.00, 0.00], [1.00, 2.00, 3.00])"),
  9. String(box), "init 1");
  10. test.checkEqual(String("[0.00, 0.00, 0.00]"), String(box.getMin()),
  11. "get min 1");
  12. test.checkEqual(String("[1.00, 2.00, 3.00]"), String(box.getMax()),
  13. "get max 1");
  14. box = Box(Vector3(-1.0f, -2.0f, -3.0f));
  15. test.checkEqual(String("Box([-1.00, -2.00, -3.00], [0.00, 0.00, 0.00])"),
  16. String(box), "init 2");
  17. test.checkEqual(String("[-1.00, -2.00, -3.00]"), String(box.getMin()),
  18. "get min 2");
  19. test.checkEqual(String("[0.00, 0.00, 0.00]"), String(box.getMax()),
  20. "get max 2");
  21. }
  22. static void testOffset(Test& test) {
  23. Box box(Vector3(1.0f, 2.0f, 3.0f));
  24. test.checkEqual(String("Box([7.00, -4.00, 6.00], [8.00, -2.00, 9.00])"),
  25. String(box.offset(Vector3(7.0f, -4.0f, 6.0f))), "expand");
  26. }
  27. static void testCollidesWith(Test& test) {
  28. Box boxA(Vector3(1.0f, 2.0f, 3.0f));
  29. Box boxB(Vector3(-1.0f, -2.0f, -3.0f));
  30. Box boxC(Vector3(2.0f, 2.0f, 2.0f));
  31. boxC = boxC.offset(Vector3(-1.0f, -1.0f, -1.0f));
  32. test.checkTrue(boxC.collidesWith(boxA), "collides with 1");
  33. test.checkTrue(boxC.collidesWith(boxB), "collides with 2");
  34. test.checkTrue(boxA.collidesWith(boxC), "collides with 3");
  35. test.checkTrue(boxB.collidesWith(boxC), "collides with 4");
  36. test.checkFalse(boxA.collidesWith(boxB), "collides with 5");
  37. test.checkFalse(boxB.collidesWith(boxA), "collides with 6");
  38. }
  39. static void testExpand(Test& test) {
  40. Box box(Vector3(1.0f, 2.0f, 3.0f));
  41. test.checkEqual(String("Box([0.00, -4.00, 0.00], [8.00, 2.00, 9.00])"),
  42. String(box.expand(Vector3(7.0f, -4.0f, 6.0f))), "expand");
  43. test.checkEqual(String("Box([-7.00, 0.00, -6.00], [1.00, 6.00, 3.00])"),
  44. String(box.expand(Vector3(-7.0f, 4.0f, -6.0f))), "expand");
  45. }
  46. static void testGrow(Test& test) {
  47. Box box(Vector3(1.0f, 2.0f, 3.0f));
  48. test.checkEqual(String("Box([-2.00, -1.00, -3.00], [3.00, 3.00, 6.00])"),
  49. String(box.grow(Vector3(4.0f, 2.0f, 6.0f))), "expand");
  50. test.checkEqual(String("Box([0.50, 1.00, 1.50], [0.50, 1.00, 1.50])"),
  51. String(box.grow(Vector3(-4.0f, -2.0f, -6.0f))), "expand");
  52. test.checkEqual(String("Box([0.05, 1.00, 0.50], [0.95, 1.00, 2.50])"),
  53. String(box.grow(Vector3(-0.1f, -4.0f, -1.0f))), "expand");
  54. }
  55. void BoxTests::test() {
  56. Test test("Box");
  57. testInit(test);
  58. testOffset(test);
  59. testCollidesWith(test);
  60. testExpand(test);
  61. testGrow(test);
  62. test.finalize();
  63. }