123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- #include "../../src/ErrorSimulator.hpp"
- #include "../Tests.hpp"
- #include "core/data/LinkedList.hpp"
- template class Core::LinkedList<int>;
- using IntList = Core::LinkedList<int>;
- struct LinkedListTester final {
- int a;
- LinkedListTester(int a_) : a(a_) {
- }
- LinkedListTester(const LinkedListTester&) = delete;
- LinkedListTester(LinkedListTester&&) = delete;
- LinkedListTester& operator=(const LinkedListTester&) = delete;
- LinkedListTester& operator=(LinkedListTester&&) = delete;
- };
- static void testWithoutCopyOrMove() {
- Core::LinkedList<LinkedListTester> list;
- CORE_TEST_ERROR(list.add(3));
- }
- static void testAdd() {
- IntList list;
- CORE_TEST_ERROR(list.add(5));
- auto iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(5, *iter);
- }
- 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));
- auto iter = list.begin();
- CORE_TEST_EQUAL(4, *iter);
- CORE_TEST_EQUAL(3, *(++iter));
- CORE_TEST_EQUAL(2, *(++iter));
- CORE_TEST_EQUAL(3, list.getLength());
- }
- 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());
- CORE_TEST_FALSE(list.begin() != list.end());
- }
- 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));
- }
- auto iter = list.begin();
- for(int i = 0; i < list.getLength(); i++) {
- CORE_TEST_EQUAL(i, *iter);
- ++iter;
- }
- 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());
- auto iterA = list.begin();
- auto iterB = copy.begin();
- for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
- CORE_TEST_EQUAL(*iterA, *iterB);
- ++iterA;
- ++iterB;
- }
- }
- static void testMove() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(3));
- const IntList move(Core::move(list));
- CORE_TEST_EQUAL(0, list.getLength());
- CORE_TEST_EQUAL(3, move.getLength());
- auto iter = move.begin();
- CORE_TEST_EQUAL(1, *iter);
- CORE_TEST_EQUAL(2, *(++iter));
- CORE_TEST_EQUAL(3, *(++iter));
- }
- 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());
- auto iter = move.begin();
- CORE_TEST_EQUAL(1, *iter);
- CORE_TEST_EQUAL(2, *(++iter));
- CORE_TEST_EQUAL(3, *(++iter));
- }
- 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 testRemove() {
- IntList list;
- IntList::Node* a = nullptr;
- IntList::Node* b = nullptr;
- IntList::Node* c = nullptr;
- IntList::Node* d = nullptr;
- CORE_TEST_ERROR(list.put(a, 4));
- CORE_TEST_ERROR(list.put(b, 3));
- CORE_TEST_ERROR(list.put(c, 2));
- CORE_TEST_ERROR(list.put(d, 1));
- CORE_TEST_NOT_NULL(a);
- CORE_TEST_NOT_NULL(b);
- CORE_TEST_NOT_NULL(c);
- CORE_TEST_NOT_NULL(c);
- list.remove(b);
- auto iter = list.begin();
- CORE_TEST_EQUAL(4, *iter);
- CORE_TEST_EQUAL(2, *(++iter));
- CORE_TEST_EQUAL(1, *(++iter));
- CORE_TEST_EQUAL(3, list.getLength());
- list.remove(a);
- iter = list.begin();
- CORE_TEST_EQUAL(2, *iter);
- CORE_TEST_EQUAL(1, *(++iter));
- CORE_TEST_EQUAL(2, list.getLength());
- list.remove(d);
- iter = list.begin();
- CORE_TEST_EQUAL(2, *iter);
- CORE_TEST_EQUAL(1, list.getLength());
- list.remove(c);
- CORE_TEST_FALSE(list.begin() != list.end());
- CORE_TEST_EQUAL(0, list.getLength());
- CORE_TEST_NULL(a);
- CORE_TEST_NULL(b);
- CORE_TEST_NULL(c);
- CORE_TEST_NULL(c);
- }
- static void testRemoveFirst() {
- IntList list;
- CORE_TEST_ERROR(list.add(4));
- CORE_TEST_ERROR(list.add(3));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(1));
- list.removeFirst();
- auto iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(3, *iter);
- ++iter;
- }
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(2, *iter);
- ++iter;
- }
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(1, *iter);
- }
- CORE_TEST_EQUAL(3, list.getLength());
- list.removeFirst();
- iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(2, *iter);
- ++iter;
- }
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(1, *iter);
- }
- CORE_TEST_EQUAL(2, list.getLength());
- list.removeFirst();
- iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(1, *iter);
- }
- CORE_TEST_EQUAL(1, list.getLength());
- list.removeFirst();
- CORE_TEST_FALSE(list.begin() != list.end());
- CORE_TEST_EQUAL(0, list.getLength());
- list.removeFirst();
- CORE_TEST_FALSE(list.begin() != list.end());
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testRemoveLast() {
- IntList list;
- CORE_TEST_ERROR(list.add(4));
- CORE_TEST_ERROR(list.add(3));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(1));
- list.removeLast();
- auto iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(4, *iter);
- ++iter;
- }
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(3, *iter);
- ++iter;
- }
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(2, *iter);
- }
- CORE_TEST_EQUAL(3, list.getLength());
- list.removeLast();
- iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(4, *iter);
- ++iter;
- }
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(3, *iter);
- }
- CORE_TEST_EQUAL(2, list.getLength());
- list.removeLast();
- iter = list.begin();
- if(CORE_TEST_TRUE(iter != list.end())) {
- CORE_TEST_EQUAL(4, *iter);
- }
- CORE_TEST_EQUAL(1, list.getLength());
- list.removeLast();
- CORE_TEST_FALSE(list.begin() != list.end());
- CORE_TEST_EQUAL(0, list.getLength());
- list.removeLast();
- CORE_TEST_FALSE(list.begin() != list.end());
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testOutOfMemory() {
- #ifdef ERROR_SIMULATOR
- IntList list;
- int memFails = 0;
- for(int i = 0; i < 40; i++) {
- Core::Fail::leftAllocations = i;
- Core::Error e = list.add(1);
- if(e == Core::Error::OUT_OF_MEMORY) {
- memFails++;
- }
- }
- if(CORE_TEST_TRUE(list.getLength() > 0)) {
- CORE_TEST_EQUAL(1, *list.begin());
- }
- Core::Fail::leftAllocations = -1;
- CORE_TEST_TRUE(memFails != 0);
- #endif
- }
- void Core::testLinkedList(bool light, bool outOfMemoryTest) {
- testWithoutCopyOrMove();
- testAdd();
- testMultipleAdd();
- testClear();
- testBigAdd(light);
- testCopy();
- testMove();
- testMoveAssignment();
- testToString1();
- testToString2();
- testToString3();
- testRemove();
- testRemoveFirst();
- testRemoveLast();
- if(outOfMemoryTest) {
- testOutOfMemory();
- }
- }
|