"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.2/test/libponyrt/ds/list.cc" (3 Feb 2020, 6551 Bytes) of package /linux/misc/ponyc-0.33.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "list.cc": 0.33.1_vs_0.33.2.

    1 #include <platform.h>
    2 #include <gtest/gtest.h>
    3 
    4 #include <stdlib.h>
    5 
    6 #include <ds/list.h>
    7 
    8 typedef struct elem_t elem_t;
    9 
   10 DECLARE_LIST(testlist, testlist_t, elem_t);
   11 
   12 class ListTest: public testing::Test
   13 {
   14   public:
   15     static elem_t* times2(elem_t* e, void* arg);
   16     static bool compare(elem_t* a, elem_t* b);
   17 };
   18 
   19 DEFINE_LIST(testlist, testlist_t, elem_t, ListTest::compare, NULL);
   20 
   21 struct elem_t
   22 {
   23   uint32_t val; /* only needed for map_fn test */
   24 };
   25 
   26 bool ListTest::compare(elem_t* a, elem_t* b)
   27 {
   28   return a == b;
   29 }
   30 
   31 elem_t* ListTest::times2(elem_t* e, void* arg)
   32 {
   33   (void)arg;
   34   e->val = e->val << 1;
   35   return e;
   36 }
   37 
   38 /** Calling ponyint_list_append with NULL as argument
   39  *  returns a new list with a single element.
   40  */
   41 TEST_F(ListTest, InitializeList)
   42 {
   43   elem_t e;
   44 
   45   testlist_t* list = testlist_append(NULL, &e);
   46 
   47   ASSERT_TRUE(list != NULL);
   48   ASSERT_EQ((size_t)1, testlist_length(list));
   49 
   50   testlist_free(list);
   51 }
   52 
   53 /** A call to ponyint_list_length returns the number of elements
   54  *  until the end of the list is reached.
   55  */
   56 TEST_F(ListTest, ListLength)
   57 {
   58   elem_t e1;
   59   elem_t e2;
   60   elem_t e3;
   61 
   62   testlist_t* list = testlist_append(NULL, &e1);
   63   list = testlist_append(list, &e2);
   64   list = testlist_append(list, &e3);
   65 
   66   ASSERT_EQ((size_t)3, testlist_length(list));
   67 
   68   testlist_free(list);
   69 }
   70 
   71 /** A call to ponyint_list_append appends an element to
   72  *  the end of the list.
   73  *
   74  *  After pushing an element, the resulting
   75  *  pointer still points to the head of the list.
   76  *
   77  *  If the end is reached, testlist_next returns NULL.
   78  */
   79 TEST_F(ListTest, AppendElement)
   80 {
   81   elem_t e1;
   82   elem_t e2;
   83 
   84   testlist_t* list = testlist_append(NULL, &e1);
   85   testlist_t* n = testlist_append(list, &e2);
   86 
   87   ASSERT_EQ(n, list);
   88   ASSERT_EQ(&e1, testlist_data(n));
   89 
   90   n = testlist_next(n);
   91 
   92   ASSERT_EQ(&e2, testlist_data(n));
   93 
   94   n = testlist_next(n);
   95 
   96   ASSERT_EQ(NULL, n);
   97 
   98   testlist_free(list);
   99 }
  100 
  101 /** A call to ponyint_list_push prepends an element to the list.
  102  *
  103  *  The pointer returned points to the new head.
  104  */
  105 TEST_F(ListTest, PushElement)
  106 {
  107   elem_t e1;
  108   elem_t e2;
  109 
  110   testlist_t* list = testlist_append(NULL, &e1);
  111   list = testlist_push(list, &e2);
  112 
  113   ASSERT_EQ(&e2, testlist_data(list));
  114 
  115   testlist_t* n = testlist_next(list);
  116 
  117   ASSERT_EQ(&e1, testlist_data(n));
  118 
  119   testlist_free(list);
  120 }
  121 
  122 /** A call to ponyint_list_pop provides the head of the list.
  123  *  The second element prior to ponyint_list_pop becomes the new head.
  124  */
  125 TEST_F(ListTest, ListPop)
  126 {
  127   elem_t* p = NULL;
  128 
  129   elem_t e1;
  130   elem_t e2;
  131 
  132   testlist_t* list = testlist_append(NULL, &e1);
  133   list = testlist_append(list, &e2);
  134   list = testlist_pop(list, &p);
  135 
  136   ASSERT_EQ(p, &e1);
  137 
  138   ASSERT_EQ(
  139     &e2,
  140     testlist_data(
  141       testlist_index(list, 0)
  142     )
  143   );
  144 
  145   testlist_free(list);
  146 }
  147 
  148 /** A call to ponyint_list_index (with index > 0) advances
  149  *  the list pointer to the element at position
  150  *  "index" (starting from 0).
  151  */
  152 TEST_F(ListTest, ListIndexAbsolute)
  153 {
  154   ssize_t i = 1;
  155 
  156   elem_t e1;
  157   elem_t e2;
  158 
  159   testlist_t* list = testlist_append(NULL, &e1);
  160   list = testlist_append(list, &e2);
  161 
  162   testlist_t* n = testlist_index(list, i);
  163 
  164   ASSERT_EQ(&e2, testlist_data(n));
  165   testlist_free(list);
  166 }
  167 
  168 /** A call to ponyint_list_index with a negative index
  169  *  allows to traverse the list relative to its end.
  170  */
  171 TEST_F(ListTest, ListIndexRelative)
  172 {
  173   ssize_t i = -2;
  174 
  175   elem_t e1;
  176   elem_t e2;
  177   elem_t e3;
  178 
  179   testlist_t* list = testlist_append(NULL, &e1);
  180   list = testlist_append(list, &e2);
  181   list = testlist_append(list, &e3);
  182 
  183   //should advance list to the second-to-last element
  184   testlist_t* n = testlist_index(list, i);
  185 
  186   ASSERT_EQ(&e2, testlist_data(n));
  187 
  188   testlist_free(list);
  189 }
  190 
  191 /** A call to ponyint_list_find returns a matching element according
  192  *  to the provided compare function.
  193  *
  194  *  If an element is not in the list, ponyint_list_find returns NULL.
  195  */
  196 TEST_F(ListTest, ListFind)
  197 {
  198   elem_t e1;
  199   elem_t e2;
  200   elem_t e3;
  201 
  202   testlist_t* list = testlist_append(NULL, &e1);
  203   list = testlist_append(list, &e2);
  204 
  205   ASSERT_EQ(
  206     &e2,
  207     testlist_find(list, &e2)
  208   );
  209 
  210   ASSERT_EQ(
  211     NULL,
  212     testlist_find(list, &e3)
  213   );
  214 
  215   testlist_free(list);
  216 }
  217 
  218 /** A call to ponyint_list_findindex returns the position
  219  *  of an element within the list, -1 otherwise.
  220  */
  221 TEST_F(ListTest, ListFindIndex)
  222 {
  223   elem_t e1;
  224   elem_t e2;
  225   elem_t e3;
  226 
  227   testlist_t* list = testlist_append(NULL, &e1);
  228   list = testlist_append(list, &e2);
  229 
  230   ASSERT_EQ(
  231     1,
  232     testlist_findindex(list, &e2)
  233   );
  234 
  235   ASSERT_EQ(
  236     -1,
  237      testlist_findindex(list, &e3)
  238   );
  239 
  240   testlist_free(list);
  241 }
  242 
  243 /** Lists where elements are pair-wise equivalent
  244  *  (starting from the provided head, according to
  245  *  the compare function), are equivalent.
  246  */
  247 TEST_F(ListTest, ListEquals)
  248 {
  249   testlist_t* a = NULL;
  250   testlist_t* b = NULL;
  251 
  252   elem_t e1;
  253   elem_t e2;
  254 
  255   a = testlist_append(a, &e1);
  256   a = testlist_append(a, &e2);
  257 
  258   b = testlist_append(b, &e1);
  259   b = testlist_append(b, &e2);
  260 
  261   ASSERT_TRUE(
  262     testlist_equals(a,b)
  263   );
  264 
  265   testlist_free(a);
  266   testlist_free(b);
  267 }
  268 
  269 /** Lists where one is a pair-wise prefix of
  270  *  the other, are subsets.
  271  */
  272 TEST_F(ListTest, ListSubset)
  273 {
  274   testlist_t* a = NULL;
  275   testlist_t* b = NULL;
  276 
  277   elem_t e1;
  278   elem_t e2;
  279 
  280   a = testlist_append(a, &e1);
  281   a = testlist_append(a, &e2);
  282   b = testlist_append(b, &e1);
  283 
  284   ASSERT_TRUE(
  285     testlist_subset(b, a)
  286   );
  287 
  288   testlist_free(a);
  289   testlist_free(b);
  290 }
  291 
  292 /** A call to ponyint_list_reverse the returns a pointer
  293  *  to a new list of reversed order.
  294  */
  295 TEST_F(ListTest, ListReverse)
  296 {
  297   elem_t* e = NULL;
  298 
  299   elem_t e1;
  300   elem_t e2;
  301   elem_t e3;
  302 
  303   testlist_t* list = testlist_append(NULL, &e1);
  304   list = testlist_append(list, &e2);
  305   list = testlist_append(list, &e3);
  306 
  307   testlist_t* r = testlist_reverse(list);
  308 
  309   r = testlist_pop(r, &e);
  310 
  311   ASSERT_EQ(e, &e3);
  312 
  313   r = testlist_pop(r, &e);
  314 
  315   ASSERT_EQ(e, &e2);
  316 
  317   testlist_pop(r, &e);
  318 
  319   ASSERT_EQ(e, &e1);
  320 }
  321 
  322 /** A call to ponyint_list_map invokes the provided map function
  323  *  for each element within the list.
  324  */
  325 TEST_F(ListTest, ListMap)
  326 {
  327   elem_t* c = NULL;
  328 
  329   elem_t e1;
  330   elem_t e2;
  331   elem_t e3;
  332 
  333   e1.val = 1;
  334   e2.val = 2;
  335   e3.val = 3;
  336 
  337   testlist_t* list = testlist_append(NULL, &e1);
  338   list = testlist_append(list, &e2);
  339   list = testlist_append(list, &e3);
  340 
  341   testlist_t* mapped = testlist_map(list, times2, NULL);
  342   testlist_t* m = mapped;
  343 
  344   for(uint32_t i = 1; i < 3; i++)
  345   {
  346     c = testlist_data(m);
  347     ASSERT_EQ(i << 1, c->val);
  348     m = testlist_next(m);
  349   }
  350 
  351   testlist_free(list);
  352   testlist_free(mapped);
  353 }