123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #include "../../src/ErrorSimulator.hpp"
- #include "../Tests.hpp"
- #include "core/data/List.hpp"
- #include "core/math/Math.hpp"
- template class Core::List<int>;
- using IntList = Core::List<int>;
- 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();
- }
|