StackTests.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. stack.push(1).push(2).push(3);
  9. CORE_TEST_EQUAL(3, stack.peek());
  10. CORE_TEST_EQUAL(3, static_cast<const T&>(stack).peek());
  11. stack.pop();
  12. CORE_TEST_EQUAL(2, stack.peek());
  13. CORE_TEST_EQUAL(2, static_cast<const T&>(stack).peek());
  14. stack.pop();
  15. CORE_TEST_EQUAL(1, stack.peek());
  16. CORE_TEST_EQUAL(1, static_cast<const T&>(stack).peek());
  17. stack.pop();
  18. CORE_TEST_TRUE(stack.isEmpty());
  19. }
  20. template<typename T>
  21. static void testBigPushPop(int amount) {
  22. T stack;
  23. for(int i = 0; i < amount; i++) {
  24. stack.push(i);
  25. }
  26. for(int i = 0; i < amount; i++) {
  27. stack.pop();
  28. }
  29. CORE_TEST_TRUE(stack.isEmpty());
  30. }
  31. template<typename T>
  32. static void testToString() {
  33. T stack;
  34. stack.push(1).push(243).push(-423);
  35. CORE_TEST_STRING("[1, 243, -423]", stack);
  36. CORE_TEST_STRING("[1]", T().push(1));
  37. CORE_TEST_STRING("[]", T());
  38. }
  39. template<typename T>
  40. static void testClear() {
  41. T stack;
  42. stack.push(1).push(2).push(3);
  43. stack.clear();
  44. CORE_TEST_TRUE(stack.isEmpty());
  45. }
  46. template<typename T>
  47. static void testType(int amount) {
  48. testPushPopPeek<T>();
  49. testBigPushPop<T>(amount);
  50. testToString<T>();
  51. testClear<T>();
  52. }
  53. void Core::testStack(bool light) {
  54. testType<Core::ListStack<int>>(light ? 10000 : 100000);
  55. testType<Core::ArrayStack<int, 100>>(100);
  56. }