#include "../../src/ErrorSimulator.hpp" #include "../Tests.hpp" #include "core/data/List.hpp" #include "core/math/Math.hpp" template class Core::List; using IntList = Core::List; static void testAdd() { IntList list; if(CORE_TEST_ERROR(list.add(5))) { CORE_TEST_EQUAL(5, list[0]); } CORE_TEST_EQUAL(1, list.getLength()); } static void testMultipleAdd() { IntList list; CORE_TEST_ERROR(list.add(4)); CORE_TEST_ERROR(list.add(3)); CORE_TEST_ERROR(list.add(2)); CORE_TEST_EQUAL(4, list[0]); CORE_TEST_EQUAL(3, list[1]); CORE_TEST_EQUAL(2, list[2]); CORE_TEST_EQUAL(3, list.getLength()); } static void testAddReplace() { IntList list; if(CORE_TEST_ERROR(list.add(5))) { list[0] = 3; } CORE_TEST_EQUAL(3, list[0]); } static void testClear() { IntList list; CORE_TEST_ERROR(list.add(5)); CORE_TEST_ERROR(list.add(4)); list.clear(); CORE_TEST_EQUAL(0, list.getLength()); } static void testShrink() { IntList list; CORE_TEST_ERROR(list.add(5)); CORE_TEST_ERROR(list.add(4)); CORE_TEST_ERROR(list.add(3)); CORE_TEST_TRUE(list.getCapacity() >= 3); CORE_TEST_ERROR(list.shrink()); CORE_TEST_TRUE(list.getLength() == 3); CORE_TEST_TRUE(list.getCapacity() == 3); CORE_TEST_EQUAL(5, list[0]); CORE_TEST_EQUAL(4, list[1]); CORE_TEST_EQUAL(3, list[2]); } static void testBigAdd(bool light) { int limit = light ? 10000 : 100000; IntList list; for(int i = 0; i < limit; i++) { CORE_TEST_ERROR(list.add(i)); } for(int i = 0; i < list.getLength(); i++) { CORE_TEST_EQUAL(i, list[i]); } CORE_TEST_EQUAL(limit, list.getLength()); } static void testCopy() { IntList list; CORE_TEST_ERROR(list.add(1)); CORE_TEST_ERROR(list.add(2)); CORE_TEST_ERROR(list.add(3)); IntList copy; CORE_TEST_ERROR(copy.copyFrom(list)); CORE_TEST_EQUAL(list.getLength(), copy.getLength()); i64 limit = Core::Math::min(copy.getLength(), list.getLength()); for(i64 i = 0; i < limit; i++) { CORE_TEST_EQUAL(list[i], copy[i]); } } static void testMove() { IntList list; CORE_TEST_ERROR(list.add(1)); CORE_TEST_ERROR(list.add(2)); CORE_TEST_ERROR(list.add(3)); IntList move(Core::move(list)); CORE_TEST_EQUAL(0, list.getLength()); CORE_TEST_EQUAL(3, move.getLength()); CORE_TEST_EQUAL(1, move[0]); CORE_TEST_EQUAL(2, move[1]); CORE_TEST_EQUAL(3, move[2]); } static void testMoveAssignment() { IntList list; CORE_TEST_ERROR(list.add(1)); CORE_TEST_ERROR(list.add(2)); CORE_TEST_ERROR(list.add(3)); IntList move; move = Core::move(list); CORE_TEST_EQUAL(0, list.getLength()); CORE_TEST_EQUAL(3, move.getLength()); CORE_TEST_EQUAL(1, move[0]); CORE_TEST_EQUAL(2, move[1]); CORE_TEST_EQUAL(3, move[2]); } static void testToString1() { IntList list; CORE_TEST_ERROR(list.add(1)); CORE_TEST_ERROR(list.add(243)); CORE_TEST_ERROR(list.add(-423)); CORE_TEST_STRING("[1, 243, -423]", list); } static void testToString2() { IntList list; CORE_TEST_ERROR(list.add(1)); CORE_TEST_STRING("[1]", list); } static void testToString3() { IntList list; CORE_TEST_STRING("[]", list); } static void testRemoveBySwap() { IntList list; CORE_TEST_ERROR(list.add(4)); CORE_TEST_ERROR(list.add(3)); CORE_TEST_ERROR(list.add(2)); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(-1)); CORE_TEST_ERROR(list.removeBySwap(0)); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(2)); CORE_TEST_EQUAL(2, list[0]); CORE_TEST_EQUAL(3, list[1]); CORE_TEST_EQUAL(2, list.getLength()); CORE_TEST_ERROR(list.removeBySwap(1)); CORE_TEST_EQUAL(2, list[0]); CORE_TEST_EQUAL(1, list.getLength()); CORE_TEST_ERROR(list.removeBySwap(0)); CORE_TEST_EQUAL(0, list.getLength()); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.removeBySwap(0)); } static void testRemove() { IntList list; CORE_TEST_ERROR(list.add(4)); CORE_TEST_ERROR(list.add(3)); CORE_TEST_ERROR(list.add(2)); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(-1)); CORE_TEST_ERROR(list.remove(0)); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(2)); CORE_TEST_EQUAL(3, list[0]); CORE_TEST_EQUAL(2, list[1]); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(2)); CORE_TEST_EQUAL(2, list.getLength()); CORE_TEST_ERROR(list.remove(1)); CORE_TEST_EQUAL(3, list[0]); CORE_TEST_EQUAL(1, list.getLength()); CORE_TEST_ERROR(list.remove(0)); CORE_TEST_EQUAL(0, list.getLength()); CORE_TEST_EQUAL(Core::Error::INVALID_INDEX, list.remove(0)); } static void testResize() { IntList list; CORE_TEST_ERROR(list.resize(5, 10)); CORE_TEST_EQUAL(5, list.getLength()); for(int i = 0; i < 5; i++) { CORE_TEST_EQUAL(10, list[i]); } } static void testDefaultResize() { IntList list; CORE_TEST_ERROR(list.resize(5)); CORE_TEST_EQUAL(5, list.getLength()); for(int i = 0; i < 5; i++) { CORE_TEST_EQUAL(0, list[i]); } } static void testInvalidReserve() { IntList list; CORE_TEST_ERROR(list.reserve(0)); CORE_TEST_ERROR(list.reserve(-5)); } static void testShrinkExact() { IntList list; CORE_TEST_ERROR(list.resize(50)); CORE_TEST_ERROR(list.shrink()); } static void testShrinkResize() { IntList list; CORE_TEST_ERROR(list.resize(50)); CORE_TEST_ERROR(list.resize(20, 5)); CORE_TEST_ERROR(list.resize(10)); } static void testCopyEmpty() { IntList list; IntList copy; CORE_TEST_ERROR(copy.copyFrom(list)); } void Core::testList(bool light) { testAdd(); testMultipleAdd(); testAddReplace(); testClear(); testShrink(); testBigAdd(light); testCopy(); testMove(); testMoveAssignment(); testToString1(); testToString2(); testToString3(); testRemoveBySwap(); testRemove(); testResize(); testDefaultResize(); testInvalidReserve(); testShrinkExact(); testShrinkResize(); testCopyEmpty(); }