#include "tests/StackTests.h" #include "data/Stack.h" #include "test/Test.h" using String = Core::ArrayString<128>; template static String build(Core::Test& test, const T& t) { String s; test.checkFalse(s.append(t), "append works"); return s; } template static void testPushPopPeek(Core::Test& test) { T stack; test.checkFalse(stack.push(1), "push works 1"); test.checkFalse(stack.push(2), "push works 2"); test.checkFalse(stack.push(3), "push works 3"); test.checkEqual(3, stack.peek(), "push pop peek 1"); test.checkFalse(stack.pop(), "pop without error 1"); test.checkEqual(2, stack.peek(), "push pop peek 2"); test.checkFalse(stack.pop(), "pop without error 2"); test.checkEqual(1, stack.peek(), "push pop peek 3"); test.checkFalse(stack.pop(), "pop without error 3"); test.checkTrue(stack.isEmpty(), "empty after popping all"); } template static void testBigPushPop(Core::Test& test, int amount) { T stack; for(int i = 0; i < amount; i++) { test.checkFalse(stack.push(i), "big push works"); } for(int i = 0; i < amount; i++) { test.checkFalse(stack.pop(), "big push and pop"); } test.checkTrue(stack.isEmpty(), "empty after all pops"); } template static void testToString1(Core::Test& test) { T stack; test.checkFalse(stack.push(1), "to string push works 1"); test.checkFalse(stack.push(243), "to string push works 1"); test.checkFalse(stack.push(-423), "to string push works 1"); test.checkEqual(build(test, "[1, 243, -423]"), build(test, stack), "to string 1"); } template static void testToString2(Core::Test& test) { T stack; test.checkFalse(stack.push(1), "to string 2 push works 1"); test.checkEqual(build(test, "[1]"), build(test, stack), "to string 2"); } template static void testToString3(Core::Test& test) { T stack; test.checkEqual(build(test, "[]"), build(test, stack), "to string 3"); } template static void testPop(Core::Test& test) { T stack; for(int i = 0; i < 1000000; i++) { test.checkTrue(stack.pop(), "popping empty stack is safe"); } } template static void testType(Core::Test& test, int amount) { testPushPopPeek(test); testBigPushPop(test, amount); testToString1(test); testToString2(test); testToString3(test); testPop(test); } void Core::StackTests::test() { Test test("Stack"); testType>(test, 1000000); testType>(test, 100); test.finalize(); }