StackTests.cpp 2.3 KB

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