StackTests.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "tests/StackTests.hpp"
  2. #include "core/data/Stack.hpp"
  3. #include "test/Test.hpp"
  4. template<typename T>
  5. static void testPushPopPeek() {
  6. T stack;
  7. CORE_TEST_ERROR(stack.push(1));
  8. CORE_TEST_ERROR(stack.push(2));
  9. CORE_TEST_ERROR(stack.push(3));
  10. CORE_TEST_EQUAL(3, stack.peek());
  11. CORE_TEST_ERROR(stack.pop());
  12. CORE_TEST_EQUAL(2, stack.peek());
  13. CORE_TEST_ERROR(stack.pop());
  14. CORE_TEST_EQUAL(1, stack.peek());
  15. CORE_TEST_ERROR(stack.pop());
  16. CORE_TEST_TRUE(stack.isEmpty());
  17. }
  18. template<typename T>
  19. static void testBigPushPop(int amount) {
  20. T stack;
  21. for(int i = 0; i < amount; i++) {
  22. CORE_TEST_ERROR(stack.push(i));
  23. }
  24. for(int i = 0; i < amount; i++) {
  25. CORE_TEST_ERROR(stack.pop());
  26. }
  27. CORE_TEST_TRUE(stack.isEmpty());
  28. }
  29. template<typename T>
  30. static void testToString1() {
  31. T stack;
  32. CORE_TEST_ERROR(stack.push(1));
  33. CORE_TEST_ERROR(stack.push(243));
  34. CORE_TEST_ERROR(stack.push(-423));
  35. CORE_TEST_STRING("[1, 243, -423]", stack);
  36. }
  37. template<typename T>
  38. static void testToString2() {
  39. T stack;
  40. CORE_TEST_ERROR(stack.push(1));
  41. CORE_TEST_STRING("[1]", stack);
  42. }
  43. template<typename T>
  44. static void testToString3() {
  45. T stack;
  46. CORE_TEST_STRING("[]", stack);
  47. }
  48. template<typename T>
  49. static void testPop() {
  50. T stack;
  51. for(int i = 0; i < 100000; i++) {
  52. CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, stack.pop());
  53. }
  54. }
  55. template<typename T>
  56. static void testType(int amount) {
  57. testPushPopPeek<T>();
  58. testBigPushPop<T>(amount);
  59. testToString1<T>();
  60. testToString2<T>();
  61. testToString3<T>();
  62. testPop<T>();
  63. }
  64. void Core::StackTests::test() {
  65. testType<Core::ListStack<int>>(100000);
  66. testType<Core::ArrayStack<int, 100>>(100);
  67. }