StackTests.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "tests/StackTests.h"
  2. #include "data/Stack.h"
  3. #include "test/Test.h"
  4. using String = Core::ArrayString<128>;
  5. template<typename T>
  6. static String build(const T& t) {
  7. String s;
  8. CORE_TEST_FALSE(s.append(t));
  9. return s;
  10. }
  11. template<typename T>
  12. static void testPushPopPeek() {
  13. T stack;
  14. CORE_TEST_NOT_NULL(stack.push(1));
  15. CORE_TEST_NOT_NULL(stack.push(2));
  16. CORE_TEST_NOT_NULL(stack.push(3));
  17. CORE_TEST_EQUAL(3, stack.peek());
  18. CORE_TEST_FALSE(stack.pop());
  19. CORE_TEST_EQUAL(2, stack.peek());
  20. CORE_TEST_FALSE(stack.pop());
  21. CORE_TEST_EQUAL(1, stack.peek());
  22. CORE_TEST_FALSE(stack.pop());
  23. CORE_TEST_TRUE(stack.isEmpty());
  24. }
  25. template<typename T>
  26. static void testBigPushPop(int amount) {
  27. T stack;
  28. for(int i = 0; i < amount; i++) {
  29. CORE_TEST_NOT_NULL(stack.push(i));
  30. }
  31. for(int i = 0; i < amount; i++) {
  32. CORE_TEST_FALSE(stack.pop());
  33. }
  34. CORE_TEST_TRUE(stack.isEmpty());
  35. }
  36. template<typename T>
  37. static void testToString1() {
  38. T stack;
  39. CORE_TEST_NOT_NULL(stack.push(1));
  40. CORE_TEST_NOT_NULL(stack.push(243));
  41. CORE_TEST_NOT_NULL(stack.push(-423));
  42. CORE_TEST_EQUAL(build("[1, 243, -423]"), build(stack));
  43. }
  44. template<typename T>
  45. static void testToString2() {
  46. T stack;
  47. CORE_TEST_NOT_NULL(stack.push(1));
  48. CORE_TEST_EQUAL(build("[1]"), build(stack));
  49. }
  50. template<typename T>
  51. static void testToString3() {
  52. T stack;
  53. CORE_TEST_EQUAL(build("[]"), build(stack));
  54. }
  55. template<typename T>
  56. static void testPop() {
  57. T stack;
  58. for(int i = 0; i < 100000; i++) {
  59. CORE_TEST_TRUE(stack.pop());
  60. }
  61. }
  62. template<typename T>
  63. static void testType(int amount) {
  64. testPushPopPeek<T>();
  65. testBigPushPop<T>(amount);
  66. testToString1<T>();
  67. testToString2<T>();
  68. testToString3<T>();
  69. testPop<T>();
  70. }
  71. void Core::StackTests::test() {
  72. testType<Core::ListStack<int>>(100000);
  73. testType<Core::ArrayStack<int, 100>>(100);
  74. }