StackTests.cpp 1.9 KB

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