MatrixStackTests.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "tests/MatrixStackTests.h"
  2. #include "tests/Test.h"
  3. #include "math/MatrixStack.h"
  4. #include "utils/StringBuffer.h"
  5. typedef MatrixStack<5> Matrices;
  6. typedef StringBuffer<500> String;
  7. static void testInit(Test& test) {
  8. Matrix m;
  9. Matrices stack;
  10. for(int i = 0; i < 16; i++) {
  11. test.checkEqual(m.getValues()[i], stack.peek().getValues()[i], "contains identity matrix");
  12. }
  13. }
  14. static void testPop(Test& test) {
  15. Matrices stack;
  16. stack.peek().scale(2.0f);
  17. stack.push();
  18. test.checkEqual(false, stack.pop(), "pop returns false when there is something to pop");
  19. test.checkEqual(true, stack.pop(), "pop returns true when the lowest matrix was popped");
  20. Matrix m;
  21. for(int i = 0; i < 16; i++) {
  22. test.checkEqual(m.getValues()[i], stack.peek().getValues()[i], "contains identity matrix");
  23. }
  24. }
  25. static void testPush(Test& test) {
  26. Matrices stack;
  27. for(int i = 0; i < 4; i++) {
  28. test.checkEqual(false, stack.push(), "push returns false without overflow");
  29. }
  30. for(int i = 0; i < 1000000; i++) {
  31. test.checkEqual(true, stack.push(), "push returns true with overflow");
  32. }
  33. test.checkEqual(true, true, "survives overflow");
  34. }
  35. static void testToString1(Test& test) {
  36. Matrices stack;
  37. stack.peek().scale(2.0f);
  38. stack.push();
  39. stack.peek().scale(3.0f);
  40. stack.push();
  41. stack.peek().scale(4.0f);
  42. test.checkEqual(String("["
  43. "[[2.00, 0.00, 0.00, 0.00], "
  44. "[0.00, 2.00, 0.00, 0.00], "
  45. "[0.00, 0.00, 2.00, 0.00], "
  46. "[0.00, 0.00, 0.00, 1.00]], "
  47. "[[6.00, 0.00, 0.00, 0.00], "
  48. "[0.00, 6.00, 0.00, 0.00], "
  49. "[0.00, 0.00, 6.00, 0.00], "
  50. "[0.00, 0.00, 0.00, 1.00]], "
  51. "[[24.00, 0.00, 0.00, 0.00], "
  52. "[0.00, 24.00, 0.00, 0.00], "
  53. "[0.00, 0.00, 24.00, 0.00], "
  54. "[0.00, 0.00, 0.00, 1.00]]"
  55. "]"), String(stack), "to string 1");
  56. }
  57. static void testToString2(Test& test) {
  58. Matrices stack;
  59. stack.peek().scale(2.0f);
  60. stack.push();
  61. stack.peek().scale(3.0f);
  62. test.checkEqual(String("["
  63. "[[2.00, 0.00, 0.00, 0.00], "
  64. "[0.00, 2.00, 0.00, 0.00], "
  65. "[0.00, 0.00, 2.00, 0.00], "
  66. "[0.00, 0.00, 0.00, 1.00]], "
  67. "[[6.00, 0.00, 0.00, 0.00], "
  68. "[0.00, 6.00, 0.00, 0.00], "
  69. "[0.00, 0.00, 6.00, 0.00], "
  70. "[0.00, 0.00, 0.00, 1.00]]"
  71. "]"), String(stack), "to string 2");
  72. }
  73. static void testToString3(Test& test) {
  74. Matrices stack;
  75. test.checkEqual(String("["
  76. "[[1.00, 0.00, 0.00, 0.00], "
  77. "[0.00, 1.00, 0.00, 0.00], "
  78. "[0.00, 0.00, 1.00, 0.00], "
  79. "[0.00, 0.00, 0.00, 1.00]]"
  80. "]"), String(stack), "to string 3");
  81. }
  82. void MatrixStackTests::test() {
  83. Test test("MatrixStack");
  84. testInit(test);
  85. testPop(test);
  86. testPush(test);
  87. testToString1(test);
  88. testToString2(test);
  89. testToString3(test);
  90. test.finalize();
  91. }