123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- #include "tests/ListTests.h"
- #include "data/List.h"
- #include "test/Test.h"
- using IntList = Core::List<int>;
- 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;
- }
- static void testAdd(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(5), "add works 1");
- test.checkEqual(5, list[0], "contains added value");
- test.checkEqual(1, list.getLength(), "sizes is increased by add");
- }
- static void testMultipleAdd(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(4), "add works 2");
- test.checkFalse(list.add(3), "add works 3");
- test.checkFalse(list.add(2), "add works 4");
- test.checkEqual(4, list[0], "contains added value 1");
- test.checkEqual(3, list[1], "contains added value 2");
- test.checkEqual(2, list[2], "contains added value 3");
- test.checkEqual(3, list.getLength(), "sizes is increased by add");
- }
- static void testAddReplace(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(5), "add works 5");
- list[0] = 3;
- test.checkEqual(3, list[0], "value is overwritten");
- }
- static void testClear(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(5), "add works 6");
- test.checkFalse(list.add(4), "add works 7");
- list.clear();
- test.checkEqual(0, list.getLength(), "length is 0 after clear");
- }
- static void testShrink(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(5), "add works 8");
- test.checkFalse(list.add(4), "add works 9");
- test.checkFalse(list.add(3), "add works 10");
- test.checkTrue(list.getCapacity() >= 3, "capacity is >= 3 after adding");
- test.checkFalse(list.shrink(), "shrink works 1");
- test.checkTrue(list.getLength() == 3, "length is 3 after shrink");
- test.checkTrue(list.getCapacity() == 3, "capacity is 3 after shrink");
- test.checkEqual(5, list[0], "data ok after shrink 1");
- test.checkEqual(4, list[1], "data ok after shrink 2");
- test.checkEqual(3, list[2], "data ok after shrink 3");
- }
- static void testBigAdd(Core::Test& test) {
- IntList list;
- for(int i = 0; i < 1000000; i++) {
- test.checkFalse(list.add(i), "add works in big add");
- }
- for(int i = 0; i < list.getLength(); i++) {
- test.checkEqual(i, list[i], "big add");
- }
- test.checkEqual(1000000, list.getLength(), "big add length");
- }
- static void testCopy(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(1), "add works 11");
- test.checkFalse(list.add(2), "add works 12");
- test.checkFalse(list.add(3), "add works 13");
- IntList copy;
- test.checkFalse(copy.copyFrom(list), "copy works 1");
- test.checkEqual(list.getLength(), copy.getLength(), "copy has same length");
- for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
- test.checkEqual(list[i], copy[i], "copy has same values");
- }
- }
- static void testMove(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(1), "add works 14");
- test.checkFalse(list.add(2), "add works 15");
- test.checkFalse(list.add(3), "add works 16");
- IntList move(Core::move(list));
- test.checkEqual(0, list.getLength(), "moved has length 0");
- test.checkEqual(3, move.getLength(), "moved passes length");
- test.checkEqual(1, move[0], "moved passes values");
- test.checkEqual(2, move[1], "moved passes values");
- test.checkEqual(3, move[2], "moved passes values");
- }
- static void testMoveAssignment(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(1), "add works 17");
- test.checkFalse(list.add(2), "add works 18");
- test.checkFalse(list.add(3), "add works 19");
- IntList move;
- move = Core::move(list);
- test.checkEqual(0, list.getLength(), "assignment moved has length 0");
- test.checkEqual(3, move.getLength(), "assignment moved passes length");
- test.checkEqual(1, move[0], "assignment moved passes values");
- test.checkEqual(2, move[1], "assignment moved passes values");
- test.checkEqual(3, move[2], "assignment moved passes values");
- }
- static void testToString1(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(1), "add works 20");
- test.checkFalse(list.add(243), "add works 21");
- test.checkFalse(list.add(-423), "add works 22");
- test.checkEqual(build(test, "[1, 243, -423]"), build(test, list),
- "to string 1");
- }
- static void testToString2(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(1), "add works 23");
- test.checkEqual(build(test, "[1]"), build(test, list), "to string 2");
- }
- static void testToString3(Core::Test& test) {
- IntList list;
- test.checkEqual(build(test, "[]"), build(test, list), "to string 3");
- }
- static void testRemoveBySwap(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(4), "add works 24");
- test.checkFalse(list.add(3), "add works 25");
- test.checkFalse(list.add(2), "add works 26");
- test.checkTrue(list.removeBySwap(-1), "remove by swap does not work 1");
- test.checkFalse(list.removeBySwap(0), "remove by swap works 1");
- test.checkTrue(list.removeBySwap(2), "remove by swap does not work 2");
- test.checkEqual(2, list[0], "remove by swap 1");
- test.checkEqual(3, list[1], "remove by swap 2");
- test.checkEqual(2, list.getLength(), "remove by swap 3");
- test.checkFalse(list.removeBySwap(1), "remove by swap works 2");
- test.checkEqual(2, list[0], "remove by swap 4");
- test.checkEqual(1, list.getLength(), "remove by swap 5");
- test.checkFalse(list.removeBySwap(0), "remove by swap works 3");
- test.checkEqual(0, list.getLength(), "remove by swap 6");
- test.checkTrue(list.removeBySwap(0), "remove by swap does not work 3");
- }
- static void testRemove(Core::Test& test) {
- IntList list;
- test.checkFalse(list.add(4), "add works 27");
- test.checkFalse(list.add(3), "add works 28");
- test.checkFalse(list.add(2), "add works 29");
- test.checkTrue(list.remove(-1), "remove does not work 1");
- test.checkFalse(list.remove(0), "remove works 1");
- test.checkTrue(list.remove(2), "remove does not work 2");
- test.checkEqual(3, list[0], "remove 1");
- test.checkEqual(2, list[1], "remove 2");
- test.checkEqual(2, list.getLength(), "remove 3");
- test.checkFalse(list.remove(1), "remove works 2");
- test.checkEqual(3, list[0], "remove 4");
- test.checkEqual(1, list.getLength(), "remove 5");
- test.checkFalse(list.remove(0), "remove works 3");
- test.checkEqual(0, list.getLength(), "remove 6");
- test.checkTrue(list.remove(0), "remove does not work 3");
- }
- static void testResize(Core::Test& test) {
- IntList list;
- test.checkFalse(list.resize(5, 10), "resize works");
- test.checkEqual(5, list.getLength(), "correct size after resize");
- for(int i = 0; i < 5; i++) {
- test.checkEqual(10, list[i], "correct content after resize");
- }
- }
- static void testDefaultResize(Core::Test& test) {
- IntList list;
- test.checkFalse(list.resize(5), "default resize works 1");
- test.checkEqual(5, list.getLength(), "correct size after default resize");
- for(int i = 0; i < 5; i++) {
- test.checkEqual(0, list[i], "correct content after default resize");
- }
- }
- void Core::ListTests::test() {
- Core::Test test("List");
- testAdd(test);
- testMultipleAdd(test);
- testAddReplace(test);
- testClear(test);
- testShrink(test);
- testBigAdd(test);
- testCopy(test);
- testMove(test);
- testMoveAssignment(test);
- testToString1(test);
- testToString2(test);
- testToString3(test);
- testRemoveBySwap(test);
- testRemove(test);
- testResize(test);
- testDefaultResize(test);
- test.finalize();
- }
|