StackTests.cpp 1.7 KB

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