[vlc-commits] vlc_arrays: add unit tests

Romain Vimont git at videolan.org
Thu Aug 9 14:04:55 CEST 2018


vlc | branch: master | Romain Vimont <rom1v at videolabs.io> | Wed Jul 18 15:51:55 2018 +0200| [2c63cf85dfa02e98565609ab95d47719719c9da7] | committer: Thomas Guillem

vlc_arrays: add unit tests

Add some tests for ARRAY_* macros.

Signed-off-by: Thomas Guillem <thomas at gllm.fr>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=2c63cf85dfa02e98565609ab95d47719719c9da7
---

 src/Makefile.am   |   4 +-
 src/test/arrays.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 563da0c6de..1802dedf58 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -523,7 +523,8 @@ check_PROGRAMS = \
 	test_utf8 \
 	test_xmlent \
 	test_headers \
-	test_mrl_helpers
+	test_mrl_helpers \
+	test_arrays
 
 TESTS = $(check_PROGRAMS) check_symbols
 
@@ -545,6 +546,7 @@ test_utf8_SOURCES = test/utf8.c
 test_xmlent_SOURCES = test/xmlent.c
 test_headers_SOURCES = test/headers.c
 test_mrl_helpers_SOURCES = test/mrl_helpers.c
+test_arrays_SOURCES = test/arrays.c
 
 AM_LDFLAGS = -no-install
 LDADD = libvlccore.la \
diff --git a/src/test/arrays.c b/src/test/arrays.c
new file mode 100644
index 0000000000..9d40a00650
--- /dev/null
+++ b/src/test/arrays.c
@@ -0,0 +1,126 @@
+/*****************************************************************************
+ * arrays.h : Test for ARRAY_* macros
+ *****************************************************************************
+ * Copyright (C) 2018 VLC authors and VideoLAN
+ *
+ * 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
+
+#undef NDEBUG
+
+#include <assert.h>
+
+#include <vlc_common.h>
+#include <vlc_arrays.h>
+
+static void test_array_insert_remove(void)
+{
+    DECL_ARRAY(int) array;
+    ARRAY_INIT(array);
+
+    ARRAY_APPEND(array, 42);
+    assert(array.i_size == 1);
+    assert(ARRAY_VAL(array, 0) == 42);
+
+    ARRAY_REMOVE(array, 0);
+    assert(array.i_size == 0);
+
+    ARRAY_APPEND(array, 43);
+    ARRAY_APPEND(array, 44);
+    ARRAY_APPEND(array, 45);
+    ARRAY_REMOVE(array, 1);
+    assert(array.i_size == 2);
+    assert(ARRAY_VAL(array, 0) == 43);
+    assert(ARRAY_VAL(array, 1) == 45);
+
+    ARRAY_INSERT(array, 100, 1);
+    assert(array.i_size == 3);
+    assert(ARRAY_VAL(array, 0) == 43);
+    assert(ARRAY_VAL(array, 1) == 100);
+    assert(ARRAY_VAL(array, 2) == 45);
+
+    ARRAY_RESET(array);
+}
+
+static void test_array_foreach(void)
+{
+    DECL_ARRAY(int) array;
+    ARRAY_INIT(array);
+
+    for (int i = 0; i < 10; ++i)
+        ARRAY_APPEND(array, i);
+
+    int count = 0;
+    int item;
+    FOREACH_ARRAY(item, array)
+        assert(item == count);
+        count++;
+    FOREACH_END()
+    assert(count == 10);
+
+    ARRAY_RESET(array);
+}
+
+static void test_array_bsearch(void)
+{
+    struct item {
+        int value;
+    };
+    struct item item;
+
+    DECL_ARRAY(struct item) array;
+    ARRAY_INIT(array);
+
+    ARRAY_APPEND(array, (struct item) { 1 });
+    ARRAY_APPEND(array, (struct item) { 2 });
+    ARRAY_APPEND(array, (struct item) { 3 });
+    ARRAY_APPEND(array, (struct item) { 5 });
+    ARRAY_APPEND(array, (struct item) { 8 });
+    ARRAY_APPEND(array, (struct item) { 13 });
+    ARRAY_APPEND(array, (struct item) { 21 });
+
+    int index;
+
+    ARRAY_BSEARCH(array, .value, int, 1, index);
+    assert(index == 0);
+
+    ARRAY_BSEARCH(array, .value, int, 2, index);
+    assert(index == 1);
+
+    ARRAY_BSEARCH(array, .value, int, 3, index);
+    assert(index == 2);
+
+    ARRAY_BSEARCH(array, .value, int, 8, index);
+    assert(index == 4);
+
+    ARRAY_BSEARCH(array, .value, int, 21, index);
+    assert(index == 6);
+
+    ARRAY_BSEARCH(array, .value, int, 4, index);
+    assert(index == -1);
+
+    ARRAY_RESET(array);
+}
+
+int main(void)
+{
+    test_array_insert_remove();
+    test_array_foreach();
+    test_array_bsearch();
+}



More information about the vlc-commits mailing list