linked-list: Remove unused replace() method
authorTobias Brunner <tobias@strongswan.org>
Tue, 16 Jul 2013 09:53:30 +0000 (11:53 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 17 Jul 2013 15:42:53 +0000 (17:42 +0200)
Its functionality can be replicated by calling insert_before() followed
by remove_at().  Not the other way around, though, because remove_at()
changes the enumerator position.

src/libstrongswan/collections/linked_list.c
src/libstrongswan/collections/linked_list.h
src/libstrongswan/tests/test_linked_list_enumerator.c

index 1ff8099..7594ddf 100644 (file)
@@ -316,20 +316,6 @@ METHOD(linked_list_t, insert_before, void,
        this->count++;
 }
 
-METHOD(linked_list_t, replace, void*,
-       private_linked_list_t *this, private_enumerator_t *enumerator,
-       void *item)
-{
-       void *old = NULL;
-
-       if (enumerator->current)
-       {
-               old = enumerator->current->value;
-               enumerator->current->value = item;
-       }
-       return old;
-}
-
 METHOD(linked_list_t, get_last, status_t,
        private_linked_list_t *this, void **item)
 {
@@ -556,7 +542,6 @@ linked_list_t *linked_list_create()
                        .insert_first = _insert_first,
                        .insert_last = _insert_last,
                        .insert_before = (void*)_insert_before,
-                       .replace = (void*)_replace,
                        .remove_first = _remove_first,
                        .remove_last = _remove_last,
                        .remove = _remove_,
index 81eca89..993ff10 100644 (file)
@@ -118,16 +118,6 @@ struct linked_list_t {
                                                  void *item);
 
        /**
-        * Replaces the item the enumerator currently points to with the given item.
-        *
-        * @param enumerator    enumerator with position
-        * @param item                  item value to replace current item with
-        * @return                              current item or NULL if the enumerator is at an
-        *                                              invalid position
-        */
-       void *(*replace)(linked_list_t *this, enumerator_t *enumerator, void *item);
-
-       /**
         * Remove an item from the list where the enumerator points to.
         *
         * @param enumerator enumerator with position
index 338a382..840e384 100644 (file)
@@ -215,13 +215,13 @@ START_TEST(test_insert_before_empty)
 END_TEST
 
 /*******************************************************************************
- * replace / remove_at
+ * remove_at
  */
 
-START_TEST(test_replace)
+START_TEST(test_remove_at)
 {
        enumerator_t *enumerator;
-       intptr_t x, y;
+       intptr_t x;
        int round;
 
        round = 1;
@@ -229,36 +229,50 @@ START_TEST(test_replace)
        while (enumerator->enumerate(enumerator, &x))
        {
                ck_assert_int_eq(round, x);
-               y = (intptr_t)list->replace(list, enumerator, (void*)(intptr_t)(6 - round));
-               ck_assert_int_eq(round, y);
+               if (round == 2)
+               {
+                       list->remove_at(list, enumerator);
+               }
                round++;
        }
+       ck_assert_int_eq(list->get_count(list), 4);
        list->reset_enumerator(list, enumerator);
-       round = 5;
+       round = 1;
        while (enumerator->enumerate(enumerator, &x))
        {
+               if (round == 2)
+               {       /* skip removed item */
+                       round++;
+               }
                ck_assert_int_eq(round, x);
-               round--;
+               round++;
        }
        enumerator->destroy(enumerator);
 }
 END_TEST
 
-START_TEST(test_replace_first)
+START_TEST(test_remove_at_ends)
 {
        enumerator_t *enumerator;
        intptr_t x;
 
        enumerator = list->create_enumerator(list);
-       x = (intptr_t)list->replace(list, enumerator, (void*)6);
-       ck_assert_int_eq(x, 0);
-       ck_assert(enumerator->enumerate(enumerator, &x));
+       list->remove_at(list, enumerator);
+       ck_assert_int_eq(list->get_count(list), 5);
+       ck_assert(list->get_first(list, (void*)&x) == SUCCESS);
        ck_assert_int_eq(x, 1);
+       while (enumerator->enumerate(enumerator, &x))
+       {
+       }
+       list->remove_at(list, enumerator);
+       ck_assert_int_eq(list->get_count(list), 5);
+       ck_assert(list->get_last(list, (void*)&x) == SUCCESS);
+       ck_assert_int_eq(x, 5);
        enumerator->destroy(enumerator);
 }
 END_TEST
 
-START_TEST(test_remove_at)
+START_TEST(test_insert_before_remove_at)
 {
        enumerator_t *enumerator;
        intptr_t x;
@@ -270,48 +284,46 @@ START_TEST(test_remove_at)
        {
                ck_assert_int_eq(round, x);
                if (round == 2)
-               {
+               {       /* this replaces the current item, as insert_before does not change
+                        * the enumerator position */
+                       list->insert_before(list, enumerator, (void*)42);
+                       list->remove_at(list, enumerator);
+               }
+               else if (round == 4)
+               {       /* this does not replace the item, as remove_at moves the enumerator
+                        * position to the previous item */
                        list->remove_at(list, enumerator);
+                       list->insert_before(list, enumerator, (void*)21);
                }
                round++;
        }
-       ck_assert_int_eq(list->get_count(list), 4);
+       ck_assert_int_eq(list->get_count(list), 5);
        list->reset_enumerator(list, enumerator);
        round = 1;
        while (enumerator->enumerate(enumerator, &x))
        {
                if (round == 2)
-               {       /* skip removed item */
-                       round++;
+               {       /* check replaced item */
+                       ck_assert_int_eq(42, x);
+               }
+               else if (round == 3)
+               {       /* check misplaced item */
+                       ck_assert_int_eq(21, x);
+               }
+               else if (round == 4)
+               {       /* check misplaced item */
+                       ck_assert_int_eq(3, x);
+               }
+               else
+               {
+                       ck_assert_int_eq(round, x);
                }
-               ck_assert_int_eq(round, x);
                round++;
        }
        enumerator->destroy(enumerator);
 }
 END_TEST
 
-START_TEST(test_remove_at_ends)
-{
-       enumerator_t *enumerator;
-       intptr_t x;
-
-       enumerator = list->create_enumerator(list);
-       list->remove_at(list, enumerator);
-       ck_assert_int_eq(list->get_count(list), 5);
-       ck_assert(list->get_first(list, (void*)&x) == SUCCESS);
-       ck_assert_int_eq(x, 1);
-       while (enumerator->enumerate(enumerator, &x))
-       {
-       }
-       list->remove_at(list, enumerator);
-       ck_assert_int_eq(list->get_count(list), 5);
-       ck_assert(list->get_last(list, (void*)&x) == SUCCESS);
-       ck_assert_int_eq(x, 5);
-       enumerator->destroy(enumerator);
-}
-END_TEST
-
 /*******************************************************************************
  * create list from enumerator
  */
@@ -366,10 +378,9 @@ Suite *linked_list_enumerator_suite_create()
 
        tc = tcase_create("modify");
        tcase_add_checked_fixture(tc, setup_list, teardown_list);
-       tcase_add_test(tc, test_replace);
-       tcase_add_test(tc, test_replace_first);
        tcase_add_test(tc, test_remove_at);
        tcase_add_test(tc, test_remove_at_ends);
+       tcase_add_test(tc, test_insert_before_remove_at);
        suite_add_tcase(s, tc);
 
        tc = tcase_create("create_from_enumerator");