#include "tests/StackTests.h" #include "data/Stack.h" #include "test/Test.h" using String = Core::ArrayString<128>; template static String build(const T& t) { String s; CORE_TEST_FALSE(s.append(t)); return s; } template static void testPushPopPeek() { T stack; CORE_TEST_NOT_NULL(stack.push(1)); CORE_TEST_NOT_NULL(stack.push(2)); CORE_TEST_NOT_NULL(stack.push(3)); CORE_TEST_EQUAL(3, stack.peek()); CORE_TEST_FALSE(stack.pop()); CORE_TEST_EQUAL(2, stack.peek()); CORE_TEST_FALSE(stack.pop()); CORE_TEST_EQUAL(1, stack.peek()); CORE_TEST_FALSE(stack.pop()); CORE_TEST_TRUE(stack.isEmpty()); } template static void testBigPushPop(int amount) { T stack; for(int i = 0; i < amount; i++) { CORE_TEST_NOT_NULL(stack.push(i)); } for(int i = 0; i < amount; i++) { CORE_TEST_FALSE(stack.pop()); } CORE_TEST_TRUE(stack.isEmpty()); } template static void testToString1() { T stack; CORE_TEST_NOT_NULL(stack.push(1)); CORE_TEST_NOT_NULL(stack.push(243)); CORE_TEST_NOT_NULL(stack.push(-423)); CORE_TEST_EQUAL(build("[1, 243, -423]"), build(stack)); } template static void testToString2() { T stack; CORE_TEST_NOT_NULL(stack.push(1)); CORE_TEST_EQUAL(build("[1]"), build(stack)); } template static void testToString3() { T stack; CORE_TEST_EQUAL(build("[]"), build(stack)); } template static void testPop() { T stack; for(int i = 0; i < 100000; i++) { CORE_TEST_TRUE(stack.pop()); } } template static void testType(int amount) { testPushPopPeek(); testBigPushPop(amount); testToString1(); testToString2(); testToString3(); testPop(); } void Core::StackTests::test() { testType>(100000); testType>(100); }