]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: add tests for array implementation
authorLucas De Marchi <lucas.demarchi@intel.com>
Wed, 8 Oct 2014 04:12:15 +0000 (01:12 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Thu, 9 Oct 2014 04:26:39 +0000 (01:26 -0300)
Makefile.am
testsuite/.gitignore
testsuite/test-array.c [new file with mode: 0644]

index 3a69ffdb5be5b12f35770835cef97a1d8117b40b..87702ee21c60d5738c911ff521713f16f30b3998 100644 (file)
@@ -267,6 +267,7 @@ testsuite_libtestsuite_la_LIBADD = -lrt
 
 TESTSUITE = \
        testsuite/test-hash \
+       testsuite/test-array \
        testsuite/test-init testsuite/test-testsuite testsuite/test-loaded \
        testsuite/test-modinfo testsuite/test-util testsuite/test-new-module \
        testsuite/test-modprobe testsuite/test-blacklist \
@@ -282,6 +283,9 @@ testsuite_test_testsuite_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 testsuite_test_hash_LDADD = $(TESTSUITE_LDADD)
 testsuite_test_hash_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 
+testsuite_test_array_LDADD = $(TESTSUITE_LDADD)
+testsuite_test_array_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
+
 testsuite_test_init_LDADD = $(TESTSUITE_LDADD)
 testsuite_test_init_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 testsuite_test_loaded_LDADD = $(TESTSUITE_LDADD)
index 377c89c07957725bb9c88803366b7d02673284fb..022606a77013fa8dd046e8d33c1a4f730e44d271 100644 (file)
@@ -2,6 +2,7 @@
 *.la
 *.so
 /.dirstamp
+/test-array
 /test-util
 /test-blacklist
 /test-dependencies
@@ -15,6 +16,8 @@
 /test-hash
 /rootfs
 /stamp-rootfs
+/test-array.log
+/test-array.trs
 /test-util.log
 /test-util.trs
 /test-blacklist.log
diff --git a/testsuite/test-array.c b/testsuite/test-array.c
new file mode 100644 (file)
index 0000000..adf58d7
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C)  2014 Intel Corporation. All rights reserved.
+ *
+ * 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <shared/array.h>
+
+#include "testsuite.h"
+
+static int test_array_append1(const struct test *t)
+{
+       struct array array;
+       const char *c1 = "test1";
+
+       array_init(&array, 2);
+       array_append(&array, c1);
+       assert_return(array.count == 1, EXIT_FAILURE);
+       assert_return(array.array[0] == c1, EXIT_FAILURE);
+       array_free_array(&array);
+
+       return 0;
+}
+static DEFINE_TEST(test_array_append1,
+               .description = "test simple array append");
+
+
+static int test_array_append2(const struct test *t)
+{
+       struct array array;
+       const char *c1 = "test1";
+       const char *c2 = "test2";
+       const char *c3 = "test3";
+
+       array_init(&array, 2);
+       array_append(&array, c1);
+       array_append(&array, c2);
+       array_append(&array, c3);
+       assert_return(array.count == 3, EXIT_FAILURE);
+       assert_return(array.array[0] == c1, EXIT_FAILURE);
+       assert_return(array.array[1] == c2, EXIT_FAILURE);
+       assert_return(array.array[2] == c3, EXIT_FAILURE);
+       array_free_array(&array);
+
+       return 0;
+}
+static DEFINE_TEST(test_array_append2,
+               .description = "test array append over step");
+
+static int test_array_append_unique(const struct test *t)
+{
+       struct array array;
+       const char *c1 = "test1";
+       const char *c2 = "test2";
+       const char *c3 = "test3";
+
+       array_init(&array, 2);
+       array_append_unique(&array, c1);
+       array_append_unique(&array, c2);
+       array_append_unique(&array, c3);
+       array_append_unique(&array, c3);
+       array_append_unique(&array, c2);
+       array_append_unique(&array, c1);
+       assert_return(array.count == 3, EXIT_FAILURE);
+       assert_return(array.array[0] == c1, EXIT_FAILURE);
+       assert_return(array.array[1] == c2, EXIT_FAILURE);
+       assert_return(array.array[2] == c3, EXIT_FAILURE);
+       array_free_array(&array);
+
+       return 0;
+}
+static DEFINE_TEST(test_array_append_unique,
+               .description = "test array append unique");
+
+static int test_array_sort(const struct test *t)
+{
+       struct array array;
+       const char *c1 = "test1";
+       const char *c2 = "test2";
+       const char *c3 = "test3";
+
+       array_init(&array, 2);
+       array_append(&array, c1);
+       array_append(&array, c2);
+       array_append(&array, c3);
+       array_append(&array, c2);
+       array_append(&array, c3);
+       array_append(&array, c1);
+       array_sort(&array, (int (*)(const void *a, const void *b)) strcmp);
+       assert_return(array.count == 6, EXIT_FAILURE);
+       assert_return(array.array[0] == c1, EXIT_FAILURE);
+       assert_return(array.array[1] == c1, EXIT_FAILURE);
+       assert_return(array.array[2] == c2, EXIT_FAILURE);
+       assert_return(array.array[3] == c2, EXIT_FAILURE);
+       assert_return(array.array[4] == c3, EXIT_FAILURE);
+       assert_return(array.array[5] == c3, EXIT_FAILURE);
+       array_free_array(&array);
+
+       return 0;
+}
+static DEFINE_TEST(test_array_sort,
+               .description = "test array sort");
+
+static int test_array_remove_at(const struct test *t)
+{
+       struct array array;
+       const char *c1 = "test1";
+       const char *c2 = "test2";
+       const char *c3 = "test3";
+
+       array_init(&array, 2);
+       array_append(&array, c1);
+       array_append(&array, c2);
+       array_append(&array, c3);
+
+       array_remove_at(&array, 2);
+       assert_return(array.count == 2, EXIT_FAILURE);
+       assert_return(array.array[0] == c1, EXIT_FAILURE);
+       assert_return(array.array[1] == c2, EXIT_FAILURE);
+
+       array_remove_at(&array, 0);
+       assert_return(array.count == 1, EXIT_FAILURE);
+       assert_return(array.array[0] == c2, EXIT_FAILURE);
+
+       array_remove_at(&array, 0);
+       assert_return(array.count == 0, EXIT_FAILURE);
+
+       array_append(&array, c1);
+       array_append(&array, c2);
+       array_append(&array, c3);
+
+       array_remove_at(&array, 1);
+       assert_return(array.count == 2, EXIT_FAILURE);
+       assert_return(array.array[0] == c1, EXIT_FAILURE);
+       assert_return(array.array[1] == c3, EXIT_FAILURE);
+
+       array_free_array(&array);
+
+       return 0;
+}
+static DEFINE_TEST(test_array_remove_at,
+               .description = "test array remove at");
+
+static const struct test *tests[] = {
+       &stest_array_append1,
+       &stest_array_append2,
+       &stest_array_append_unique,
+       &stest_array_sort,
+       &stest_array_remove_at,
+       NULL,
+};
+
+TESTSUITE_MAIN(tests);