[vlc-devel] [PATCH 2/4] test: add tests for vlc_list

Rémi Denis-Courmont remi at remlab.net
Tue Jun 12 20:27:05 CEST 2018


---
 src/Makefile.am |   2 +
 src/test/list.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)
 create mode 100644 src/test/list.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 56552deab2..7a610db868 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -512,6 +512,7 @@ check_PROGRAMS = \
 	test_dictionary \
 	test_i18n_atof \
 	test_interrupt \
+	test_list \
 	test_md5 \
 	test_picture_pool \
 	test_sort \
@@ -532,6 +533,7 @@ test_dictionary_SOURCES = test/dictionary.c
 test_i18n_atof_SOURCES = test/i18n_atof.c
 test_interrupt_SOURCES = test/interrupt.c
 test_interrupt_LDADD = $(LDADD) $(LIBS_libvlccore) $(LIBPTHREAD)
+test_list_SOURCES = test/list.c
 test_md5_SOURCES = test/md5.c
 test_picture_pool_SOURCES = test/picture_pool.c
 test_sort_SOURCES = test/sort.c
diff --git a/src/test/list.c b/src/test/list.c
new file mode 100644
index 0000000000..0266b520cc
--- /dev/null
+++ b/src/test/list.c
@@ -0,0 +1,124 @@
+/*****************************************************************************
+ * list.c: Test for vlc_list
+ *****************************************************************************
+ * Copyright (C) 2018 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#undef NDEBUG
+#include <assert.h>
+
+#include <vlc_common.h>
+#include <vlc_list.h>
+
+struct test_elem
+{
+    int i;
+    struct vlc_list node;
+};
+
+static struct vlc_list *make_elem(int i)
+{
+    struct test_elem *e = malloc(sizeof (*e));
+    if (e == NULL)
+        abort();
+
+    e->i = i;
+    return &e->node;
+}
+
+int main (void)
+{
+    struct vlc_list head, *back;
+    struct test_elem *elem;
+    int count;
+
+    vlc_list_init(&head);
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+        assert(0); /* No iteration on an empty list */
+    assert(vlc_list_is_empty(&head));
+
+    vlc_list_init(&head); /* List can be reinitialized */
+    vlc_list_prepend(make_elem(1), &head);
+    count = 0;
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+        assert(elem->i == 1), count++;
+    assert(count == 1);
+
+    back = make_elem(2);
+    vlc_list_append(back, &head);
+    count = 0;
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+        assert(elem->i == count + 1), count++;
+    assert(count == 2);
+
+    vlc_list_prepend(make_elem(3), &head);
+    vlc_list_remove(head.prev); /* remove number 2 */
+    free(vlc_list_entry(back, struct test_elem, node));
+    count = 0;
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+        assert(elem->i == (count ? 1 : 3)), count++;
+    assert(count == 2);
+
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+    {
+        vlc_list_remove(&elem->node);
+        free(elem);
+    }
+    assert(vlc_list_is_empty(&head));
+
+    for (int i = 20; i < 30; i++)
+        vlc_list_append(make_elem(i), &head);
+    for (int i = 19; i >= 10; i--)
+        vlc_list_prepend(make_elem(i), &head);
+
+    count = 0;
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+        assert(elem->i == count + 10), count++;
+    assert(count == 20);
+
+    count = 0;
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+    {
+        if (count & 1)
+        {
+            vlc_list_remove(&elem->node);
+            free(elem);
+        }
+        count++;
+    }
+    assert(count == 20);
+
+    count = 0;
+    vlc_list_foreach(elem, it, &head, struct test_elem, node)
+    {
+        fprintf(stderr, "%d: seen %d\n", count, elem->i);
+        assert(elem->i == count * 2 + 10);
+        vlc_list_remove(&elem->node);
+        free(elem);
+        count++;
+    }
+    assert(count == 10);
+    assert(vlc_list_is_empty(&head));
+    return 0;
+}
-- 
2.17.1



More information about the vlc-devel mailing list