]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: add tests for strbuf
authorLucas De Marchi <lucas.demarchi@intel.com>
Mon, 13 Oct 2014 21:42:01 +0000 (18:42 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Mon, 13 Oct 2014 21:42:01 +0000 (18:42 -0300)
Makefile.am
testsuite/.gitignore
testsuite/test-strbuf.c [new file with mode: 0644]

index e2c53011035f19f2bc63eeb7f9e824405339ced4..356c4e2e06607473d2426f7a3ac948f31e5bff0e 100644 (file)
@@ -266,6 +266,7 @@ testsuite_libtestsuite_la_LIBADD = -lrt
 TESTSUITE = \
        testsuite/test-hash \
        testsuite/test-array \
+       testsuite/test-strbuf \
        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 \
@@ -284,6 +285,9 @@ testsuite_test_hash_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 testsuite_test_array_LDADD = $(TESTSUITE_LDADD)
 testsuite_test_array_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 
+testsuite_test_strbuf_LDADD = $(TESTSUITE_LDADD)
+testsuite_test_strbuf_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
+
 testsuite_test_init_LDADD = $(TESTSUITE_LDADD)
 testsuite_test_init_CPPFLAGS = $(TESTSUITE_CPPFLAGS)
 testsuite_test_loaded_LDADD = $(TESTSUITE_LDADD)
index 022606a77013fa8dd046e8d33c1a4f730e44d271..50acf6c256901608eba3c2258ffa2f085afa97a8 100644 (file)
@@ -2,6 +2,7 @@
 *.la
 *.so
 /.dirstamp
+/test-strbuf
 /test-array
 /test-util
 /test-blacklist
@@ -16,6 +17,8 @@
 /test-hash
 /rootfs
 /stamp-rootfs
+/test-strbuf.log
+/test-strbuf.trs
 /test-array.log
 /test-array.trs
 /test-util.log
diff --git a/testsuite/test-strbuf.c b/testsuite/test-strbuf.c
new file mode 100644 (file)
index 0000000..53d39e0
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * 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/strbuf.h>
+
+#include "testsuite.h"
+
+static const char *TEXT =
+       "this is a very long test that is longer than the size we initially se in the strbuf";
+
+static int test_strbuf_pushchar(const struct test *t)
+{
+       struct strbuf buf;
+       char *result1, *result2;
+       const char *c;
+
+       strbuf_init(&buf);
+
+       for (c = TEXT; *c != '\0'; c++)
+               strbuf_pushchar(&buf, *c);
+
+       result1 = (char *) strbuf_str(&buf);
+       assert_return(result1 == buf.bytes, EXIT_FAILURE);
+       assert_return(strcmp(result1, TEXT) == 0, EXIT_FAILURE);
+       result1 = strdup(result1);
+
+       result2 = strbuf_steal(&buf);
+       assert_return(strcmp(result1, result2) == 0, EXIT_FAILURE);
+
+       free(result1);
+       free(result2);
+
+       return 0;
+}
+DEFINE_TEST(test_strbuf_pushchar,
+               .description = "test strbuf_{pushchar, str, steal}");
+
+static int test_strbuf_pushchars(const struct test *t)
+{
+       struct strbuf buf;
+       char *result1, *saveptr = NULL, *str, *result2;
+       const char *c;
+       int lastwordlen = 0;
+
+       strbuf_init(&buf);
+       str = strdup(TEXT);
+       for (c = strtok_r(str, " ", &saveptr); c != NULL;
+            c = strtok_r(NULL, " ", &saveptr)) {
+               strbuf_pushchars(&buf, c);
+               strbuf_pushchar(&buf, ' ');
+               lastwordlen = strlen(c);
+       }
+
+       strbuf_popchar(&buf);
+       result1 = (char *) strbuf_str(&buf);
+       assert_return(result1 == buf.bytes, EXIT_FAILURE);
+       assert_return(strcmp(result1, TEXT) == 0, EXIT_FAILURE);
+
+       strbuf_popchars(&buf, lastwordlen);
+       result2 = strbuf_steal(&buf);
+       assert_return(strcmp(TEXT, result2) != 0, EXIT_FAILURE);
+       assert_return(strncmp(TEXT, result2, strlen(TEXT) - lastwordlen) == 0,
+                     EXIT_FAILURE);
+       assert_return(result2[strlen(TEXT) - lastwordlen] == '\0',
+                     EXIT_FAILURE);
+
+       free(str);
+       free(result2);
+
+       return 0;
+}
+DEFINE_TEST(test_strbuf_pushchars,
+               .description = "test strbuf_{pushchars, popchar, popchars}");
+
+
+TESTSUITE_MAIN();