From 48e4d181912389c95dd91449dedf5dcb886996a4 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Mon, 13 Oct 2014 18:42:01 -0300 Subject: [PATCH] testsuite: add tests for strbuf --- Makefile.am | 4 ++ testsuite/.gitignore | 3 ++ testsuite/test-strbuf.c | 98 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 testsuite/test-strbuf.c diff --git a/Makefile.am b/Makefile.am index e2c5301..356c4e2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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) diff --git a/testsuite/.gitignore b/testsuite/.gitignore index 022606a..50acf6c 100644 --- a/testsuite/.gitignore +++ b/testsuite/.gitignore @@ -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 index 0000000..53d39e0 --- /dev/null +++ b/testsuite/test-strbuf.c @@ -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 +#include +#include +#include +#include +#include + +#include + +#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(); -- 2.39.2