test/test_list.cpp

changeset 606
314e9288af2f
parent 602
3b071ea0e9cf
child 610
de5d3ee6435f
     1.1 --- a/test/test_list.cpp	Sun Nov 06 16:11:11 2022 +0100
     1.2 +++ b/test/test_list.cpp	Sat Nov 12 15:56:58 2022 +0100
     1.3 @@ -27,6 +27,7 @@
     1.4   */
     1.5  
     1.6  #include "cx/linked_list.h"
     1.7 +#include "cx/array_list.h"
     1.8  #include "cx/utils.h"
     1.9  #include "cx/compare.h"
    1.10  #include "util_allocator.h"
    1.11 @@ -563,11 +564,12 @@
    1.12      int_test_data<testdata_len> testdata;
    1.13  
    1.14      auto autofree(CxList *list) const -> CxList * {
    1.15 -        lists.insert(list);
    1.16 +        if (list != nullptr) lists.insert(list);
    1.17          return list;
    1.18      }
    1.19  
    1.20      auto linkedListFromTestData() const -> CxList * {
    1.21 +        // TODO: replace with cxListAddArray
    1.22          return autofree(
    1.23                  cxLinkedListFromArray(
    1.24                          &testingAllocator,
    1.25 @@ -585,17 +587,23 @@
    1.26          return list;
    1.27      }
    1.28  
    1.29 +    auto arrayListFromTestData() const -> CxList * {
    1.30 +        auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len));
    1.31 +        // TODO: replace with cxListAddArray
    1.32 +        cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
    1.33 +        return list;
    1.34 +    }
    1.35 +
    1.36      void verifyCreate(CxList *list) const {
    1.37          EXPECT_EQ(list->content_destructor_type, CX_DESTRUCTOR_NONE);
    1.38          EXPECT_EQ(list->size, 0);
    1.39 -        EXPECT_EQ(list->capacity, (size_t) -1);
    1.40          EXPECT_EQ(list->allocator, &testingAllocator);
    1.41          EXPECT_EQ(list->cmpfunc, cx_cmp_int);
    1.42      }
    1.43  
    1.44      void verifyAdd(
    1.45              CxList *list,
    1.46 -            bool write_through
    1.47 +            bool as_pointer
    1.48      ) {
    1.49          auto len = testdata_len;
    1.50          cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
    1.51 @@ -603,7 +611,7 @@
    1.52          EXPECT_GE(list->capacity, list->size);
    1.53          cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
    1.54          cx_for_n (i, len) ++testdata.data[i];
    1.55 -        if (write_through) {
    1.56 +        if (as_pointer) {
    1.57              cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
    1.58          } else {
    1.59              cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i] - 1);
    1.60 @@ -769,15 +777,22 @@
    1.61  class PointerLinkedList : public HighLevelTest {
    1.62  };
    1.63  
    1.64 +class ArrayList : public HighLevelTest {
    1.65 +};
    1.66 +
    1.67  TEST_F(LinkedList, cxLinkedListCreate) {
    1.68      CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
    1.69 +    ASSERT_NE(list, nullptr);
    1.70      EXPECT_EQ(list->itemsize, sizeof(int));
    1.71 +    EXPECT_EQ(list->capacity, (size_t) -1);
    1.72      verifyCreate(list);
    1.73  }
    1.74  
    1.75  TEST_F(PointerLinkedList, cxPointerLinkedListCreate) {
    1.76      CxList *list = autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int));
    1.77 +    ASSERT_NE(list, nullptr);
    1.78      EXPECT_EQ(list->itemsize, sizeof(void *));
    1.79 +    EXPECT_EQ(list->capacity, (size_t) -1);
    1.80      verifyCreate(list);
    1.81  }
    1.82  
    1.83 @@ -786,9 +801,21 @@
    1.84      cx_for_n (i, testdata_len) cxListAdd(expected, &testdata.data[i]);
    1.85      CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int),
    1.86                                                    testdata_len, testdata.data.data()));
    1.87 +    ASSERT_NE(list, nullptr);
    1.88 +    EXPECT_EQ(list->itemsize, sizeof(int));
    1.89 +    EXPECT_EQ(list->capacity, (size_t) -1);
    1.90 +    EXPECT_EQ(list->size, testdata_len);
    1.91      EXPECT_EQ(cxListCompare(list, expected), 0);
    1.92  }
    1.93  
    1.94 +TEST_F(ArrayList, cxArrayListCreate) {
    1.95 +    CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
    1.96 +    ASSERT_NE(list, nullptr);
    1.97 +    EXPECT_EQ(list->itemsize, sizeof(int));
    1.98 +    EXPECT_EQ(list->capacity, 8);
    1.99 +    verifyCreate(list);
   1.100 +}
   1.101 +
   1.102  TEST_F(LinkedList, cxListAdd) {
   1.103      CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
   1.104      verifyAdd(list, false);
   1.105 @@ -799,6 +826,12 @@
   1.106      verifyAdd(list, true);
   1.107  }
   1.108  
   1.109 +TEST_F(ArrayList, cxListAdd) {
   1.110 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.111 +    CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
   1.112 +    verifyAdd(list, false);
   1.113 +}
   1.114 +
   1.115  TEST_F(LinkedList, cxListInsert) {
   1.116      verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
   1.117  }
   1.118 @@ -807,6 +840,11 @@
   1.119      verifyInsert(autofree(cxPointerLinkedListCreate(&testingAllocator, cx_cmp_int)));
   1.120  }
   1.121  
   1.122 +TEST_F(ArrayList, cxListInsert) {
   1.123 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.124 +    verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
   1.125 +}
   1.126 +
   1.127  TEST_F(LinkedList, cxListRemove) {
   1.128      verifyRemove(linkedListFromTestData());
   1.129  }
   1.130 @@ -815,6 +853,11 @@
   1.131      verifyRemove(pointerLinkedListFromTestData());
   1.132  }
   1.133  
   1.134 +TEST_F(ArrayList, cxListRemove) {
   1.135 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.136 +    verifyRemove(arrayListFromTestData());
   1.137 +}
   1.138 +
   1.139  TEST_F(LinkedList, cxListAt) {
   1.140      verifyAt(linkedListFromTestData());
   1.141  }
   1.142 @@ -823,6 +866,11 @@
   1.143      verifyAt(pointerLinkedListFromTestData());
   1.144  }
   1.145  
   1.146 +TEST_F(ArrayList, cxListAt) {
   1.147 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.148 +    verifyAt(arrayListFromTestData());
   1.149 +}
   1.150 +
   1.151  TEST_F(LinkedList, cxListFind) {
   1.152      verifyFind(linkedListFromTestData());
   1.153  }
   1.154 @@ -831,6 +879,11 @@
   1.155      verifyFind(pointerLinkedListFromTestData());
   1.156  }
   1.157  
   1.158 +TEST_F(ArrayList, cxListFind) {
   1.159 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.160 +    verifyFind(arrayListFromTestData());
   1.161 +}
   1.162 +
   1.163  TEST_F(LinkedList, cxListSort) {
   1.164      verifySort(linkedListFromTestData());
   1.165  }
   1.166 @@ -839,6 +892,11 @@
   1.167      verifySort(pointerLinkedListFromTestData());
   1.168  }
   1.169  
   1.170 +TEST_F(ArrayList, cxListSort) {
   1.171 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.172 +    verifySort(arrayListFromTestData());
   1.173 +}
   1.174 +
   1.175  TEST_F(LinkedList, Iterator) {
   1.176      verifyIterator(linkedListFromTestData());
   1.177  }
   1.178 @@ -847,6 +905,11 @@
   1.179      verifyIterator(pointerLinkedListFromTestData());
   1.180  }
   1.181  
   1.182 +TEST_F(ArrayList, Iterator) {
   1.183 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.184 +    verifyIterator(arrayListFromTestData());
   1.185 +}
   1.186 +
   1.187  TEST_F(LinkedList, InsertViaIterator) {
   1.188      int fivenums[] = {0, 1, 2, 3, 4, 5};
   1.189      CxList *list = autofree(cxLinkedListFromArray(&testingAllocator, cx_cmp_int, sizeof(int), 5, fivenums));
   1.190 @@ -860,6 +923,15 @@
   1.191      verifyInsertViaIterator(list);
   1.192  }
   1.193  
   1.194 +TEST_F(ArrayList, InsertViaIterator) {
   1.195 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.196 +    int fivenums[] = {0, 1, 2, 3, 4, 5};
   1.197 +    CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
   1.198 +    // TODO: replace with cxListAddArray
   1.199 +    cx_for_n (i, 5) cxListAdd(list, &fivenums[i]);
   1.200 +    verifyInsertViaIterator(list);
   1.201 +}
   1.202 +
   1.203  TEST_F(LinkedList, cxListReverse) {
   1.204      verifyReverse(linkedListFromTestData());
   1.205  }
   1.206 @@ -868,6 +940,11 @@
   1.207      verifyReverse(pointerLinkedListFromTestData());
   1.208  }
   1.209  
   1.210 +TEST_F(ArrayList, cxListReverse) {
   1.211 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.212 +    verifyReverse(arrayListFromTestData());
   1.213 +}
   1.214 +
   1.215  TEST_F(LinkedList, cxListCompare) {
   1.216      auto left = linkedListFromTestData();
   1.217      auto right = linkedListFromTestData();
   1.218 @@ -880,6 +957,13 @@
   1.219      verifyCompare(left, right);
   1.220  }
   1.221  
   1.222 +TEST_F(LinkedList, cxListCompareWithArrayList) {
   1.223 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.224 +    auto left = linkedListFromTestData();
   1.225 +    auto right = arrayListFromTestData();
   1.226 +    verifyCompare(left, right);
   1.227 +}
   1.228 +
   1.229  TEST_F(PointerLinkedList, cxListCompare) {
   1.230      auto left = pointerLinkedListFromTestData();
   1.231      auto right = pointerLinkedListFromTestData();
   1.232 @@ -892,6 +976,34 @@
   1.233      verifyCompare(left, right);
   1.234  }
   1.235  
   1.236 +TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
   1.237 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.238 +    auto left = pointerLinkedListFromTestData();
   1.239 +    auto right = arrayListFromTestData();
   1.240 +    verifyCompare(left, right);
   1.241 +}
   1.242 +
   1.243 +TEST_F(ArrayList, cxListCompare) {
   1.244 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.245 +    auto left = arrayListFromTestData();
   1.246 +    auto right = arrayListFromTestData();
   1.247 +    verifyCompare(left, right);
   1.248 +}
   1.249 +
   1.250 +TEST_F(ArrayList, cxListCompareWithPtrList) {
   1.251 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.252 +    auto left = arrayListFromTestData();
   1.253 +    auto right = pointerLinkedListFromTestData();
   1.254 +    verifyCompare(left, right);
   1.255 +}
   1.256 +
   1.257 +TEST_F(ArrayList, cxListCompareWithNormalList) {
   1.258 +    ASSERT_EQ(1,0); // TODO: remove when implemented
   1.259 +    auto left = arrayListFromTestData();
   1.260 +    auto right = linkedListFromTestData();
   1.261 +    verifyCompare(left, right);
   1.262 +}
   1.263 +
   1.264  TEST_F(PointerLinkedList, NoDestructor) {
   1.265      void *item = cxMalloc(&testingAllocator, sizeof(int));
   1.266      auto list = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_int);

mercurial