StackTests.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. stack.pop();
  14. CORE_TEST_EQUAL(2, stack.peek());
  15. CORE_TEST_EQUAL(2, static_cast<const T&>(stack).peek());
  16. stack.pop();
  17. CORE_TEST_EQUAL(1, stack.peek());
  18. CORE_TEST_EQUAL(1, static_cast<const T&>(stack).peek());
  19. 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. 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 testClear() {
  54. T stack;
  55. CORE_TEST_ERROR(stack.push(1));
  56. CORE_TEST_ERROR(stack.push(2));
  57. CORE_TEST_ERROR(stack.push(3));
  58. stack.clear();
  59. CORE_TEST_TRUE(stack.isEmpty());
  60. }
  61. template<typename T>
  62. static void testType(int amount) {
  63. testPushPopPeek<T>();
  64. testBigPushPop<T>(amount);
  65. testToString1<T>();
  66. testToString2<T>();
  67. testToString3<T>();
  68. testClear<T>();
  69. }
  70. void Core::testStack(bool light) {
  71. testType<Core::ListStack<int>>(light ? 10000 : 100000);
  72. testType<Core::ArrayStack<int, 100>>(100);
  73. }