StackTests.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "tests/StackTests.h"
  2. #include "data/Stack.h"
  3. #include "test/Test.h"
  4. using String = Core::ArrayString<128>;
  5. template<typename T>
  6. static String build(Core::Test& test, const T& t) {
  7. String s;
  8. test.checkFalse(s.append(t), "append works");
  9. return s;
  10. }
  11. template<typename T>
  12. static void testPushPopPeek(Core::Test& test) {
  13. T stack;
  14. test.checkFalse(stack.push(1), "push works 1");
  15. test.checkFalse(stack.push(2), "push works 2");
  16. test.checkFalse(stack.push(3), "push works 3");
  17. test.checkEqual(3, stack.peek(), "push pop peek 1");
  18. test.checkFalse(stack.pop(), "pop without error 1");
  19. test.checkEqual(2, stack.peek(), "push pop peek 2");
  20. test.checkFalse(stack.pop(), "pop without error 2");
  21. test.checkEqual(1, stack.peek(), "push pop peek 3");
  22. test.checkFalse(stack.pop(), "pop without error 3");
  23. test.checkTrue(stack.isEmpty(), "empty after popping all");
  24. }
  25. template<typename T>
  26. static void testBigPushPop(Core::Test& test, int amount) {
  27. T stack;
  28. for(int i = 0; i < amount; i++) {
  29. test.checkFalse(stack.push(i), "big push works");
  30. }
  31. for(int i = 0; i < amount; i++) {
  32. test.checkFalse(stack.pop(), "big push and pop");
  33. }
  34. test.checkTrue(stack.isEmpty(), "empty after all pops");
  35. }
  36. template<typename T>
  37. static void testToString1(Core::Test& test) {
  38. T stack;
  39. test.checkFalse(stack.push(1), "to string push works 1");
  40. test.checkFalse(stack.push(243), "to string push works 1");
  41. test.checkFalse(stack.push(-423), "to string push works 1");
  42. test.checkEqual(build(test, "[1, 243, -423]"), build(test, stack),
  43. "to string 1");
  44. }
  45. template<typename T>
  46. static void testToString2(Core::Test& test) {
  47. T stack;
  48. test.checkFalse(stack.push(1), "to string 2 push works 1");
  49. test.checkEqual(build(test, "[1]"), build(test, stack), "to string 2");
  50. }
  51. template<typename T>
  52. static void testToString3(Core::Test& test) {
  53. T stack;
  54. test.checkEqual(build(test, "[]"), build(test, stack), "to string 3");
  55. }
  56. template<typename T>
  57. static void testPop(Core::Test& test) {
  58. T stack;
  59. for(int i = 0; i < 1000000; i++) {
  60. test.checkTrue(stack.pop(), "popping empty stack is safe");
  61. }
  62. }
  63. template<typename T>
  64. static void testType(Core::Test& test, int amount) {
  65. testPushPopPeek<T>(test);
  66. testBigPushPop<T>(test, amount);
  67. testToString1<T>(test);
  68. testToString2<T>(test);
  69. testToString3<T>(test);
  70. testPop<T>(test);
  71. }
  72. void Core::StackTests::test() {
  73. Test test("Stack");
  74. testType<Core::ListStack<int>>(test, 1000000);
  75. testType<Core::ArrayStack<int, 100>>(test, 100);
  76. test.finalize();
  77. }