12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- #include "tests/StackTests.h"
- #include "data/Stack.h"
- #include "test/Test.h"
- using String = Core::ArrayString<128>;
- template<typename T>
- static String build(Core::Test& test, const T& t) {
- String s;
- test.checkFalse(s.append(t), "append works");
- return s;
- }
- template<typename T>
- 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<typename T>
- 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<typename T>
- 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<typename T>
- 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<typename T>
- static void testToString3(Core::Test& test) {
- T stack;
- test.checkEqual(build(test, "[]"), build(test, stack), "to string 3");
- }
- template<typename T>
- 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<typename T>
- static void testType(Core::Test& test, int amount) {
- testPushPopPeek<T>(test);
- testBigPushPop<T>(test, amount);
- testToString1<T>(test);
- testToString2<T>(test);
- testToString3<T>(test);
- testPop<T>(test);
- }
- void Core::StackTests::test() {
- Test test("Stack");
- testType<Core::ListStack<int>>(test, 1000000);
- testType<Core::ArrayStack<int, 100>>(test, 100);
- test.finalize();
- }
|