StackTests.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "tests/StackTests.h"
  2. #include "data/Stack.h"
  3. #include "tests/Test.h"
  4. #include "utils/StringBuffer.h"
  5. typedef Stack<int, 10> IntStack;
  6. typedef StringBuffer<50> String;
  7. static void testPushPopPeek(Test& test) {
  8. IntStack stack;
  9. stack.push(1).push(2).push(3);
  10. test.checkEqual(3, stack.peek(), "push pop peek 1");
  11. test.checkEqual(false, stack.pop(), "pop without error 1");
  12. test.checkEqual(2, stack.peek(), "push pop peek 2");
  13. test.checkEqual(false, stack.pop(), "pop without error 2");
  14. test.checkEqual(1, stack.peek(), "push pop peek 3");
  15. test.checkEqual(false, stack.pop(), "pop without error 3");
  16. test.checkEqual(true, stack.isEmpty(), "empty after popping all");
  17. }
  18. static void testBigPushPop(Test& test) {
  19. IntStack stack;
  20. for(int i = 0; i < 1000000; i++) {
  21. stack.push(i);
  22. }
  23. for(int i = 0; i < 1000000; i++) {
  24. test.checkEqual(false, stack.pop(), "big push and pop");
  25. }
  26. test.checkEqual(true, stack.isEmpty(), "empty after all pops");
  27. }
  28. static void testToString1(Test& test) {
  29. IntStack stack;
  30. stack.push(1);
  31. stack.push(243);
  32. stack.push(-423);
  33. test.checkEqual(String("[1, 243, -423]"), String(stack), "to string 1");
  34. }
  35. static void testToString2(Test& test) {
  36. IntStack stack;
  37. stack.push(1);
  38. test.checkEqual(String("[1]"), String(stack), "to string 2");
  39. }
  40. static void testToString3(Test& test) {
  41. IntStack stack;
  42. test.checkEqual(String("[]"), String(stack), "to string 3");
  43. }
  44. static void testPop(Test& test) {
  45. IntStack stack;
  46. for(int i = 0; i < 1000000; i++) {
  47. test.checkEqual(true, stack.pop(), "popping empty stack is safe");
  48. }
  49. }
  50. void StackTests::test() {
  51. Test test("Stack");
  52. testPushPopPeek(test);
  53. testBigPushPop(test);
  54. testToString1(test);
  55. testToString2(test);
  56. testToString3(test);
  57. testPop(test);
  58. test.finalize();
  59. }