Tests: add string-utils library unit tests
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 17 Feb 2017 03:01:25 +0000 (22:01 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 5 May 2017 15:30:31 +0000 (11:30 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/unit/Makefile.am
tests/unit/test_string_utils.c [new file with mode: 0644]

index f11b69b7c29b83c2e736410f1b95f5c1c607eba0..a38785a1e72014c5c7f39044d88f52831ccc0af0 100644 (file)
@@ -12,6 +12,7 @@ TESTS = test_kernel_data \
        test_uri \
        test_utils_parse_size_suffix \
        test_utils_expand_path \
+       test_string_utils \
        ini_config/test_ini_config
 
 if LTTNG_TOOLS_BUILD_WITH_LIBDL
@@ -24,6 +25,7 @@ endif
 LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la
 
 LIBCOMMON=$(top_builddir)/src/common/libcommon.la
+LIBSTRINGUTILS=$(top_builddir)/src/common/string-utils/libstring-utils.la
 LIBSESSIOND_COMM=$(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la
 LIBHASHTABLE=$(top_builddir)/src/common/hashtable/libhashtable.la
 LIBRELAYD=$(top_builddir)/src/common/relayd/librelayd.la
@@ -31,6 +33,7 @@ LIBRELAYD=$(top_builddir)/src/common/relayd/librelayd.la
 # Define test programs
 noinst_PROGRAMS = test_uri test_session test_kernel_data
 noinst_PROGRAMS += test_utils_parse_size_suffix test_utils_expand_path
+noinst_PROGRAMS += test_string_utils
 
 if HAVE_LIBLTTNG_UST_CTL
 noinst_PROGRAMS += test_ust_data
@@ -110,3 +113,7 @@ test_utils_parse_size_suffix_LDADD += $(UTILS_SUFFIX)
 test_utils_expand_path_SOURCES = test_utils_expand_path.c
 test_utils_expand_path_LDADD = $(LIBTAP) $(LIBHASHTABLE) $(LIBCOMMON)
 test_utils_expand_path_LDADD += $(UTILS_SUFFIX)
+
+# string utilities unit test
+test_string_utils_SOURCES = test_string_utils.c
+test_string_utils_LDADD = $(LIBTAP) $(LIBCOMMON) $(LIBSTRINGUTILS)
diff --git a/tests/unit/test_string_utils.c b/tests/unit/test_string_utils.c
new file mode 100644 (file)
index 0000000..9596af8
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) - 2017 Philippe Proulx <pproulx@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by as
+ * published by the Free Software Foundation; only version 2 of the License.
+ *
+ * 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 General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <assert.h>
+#include <string.h>
+#include <stdarg.h>
+#include <common/string-utils/string-utils.h>
+#include <tap/tap.h>
+
+/* Number of TAP tests in this file */
+#define NUM_TESTS 69
+
+static void test_one_split(const char *input, char delim, bool escape_delim,
+               ...)
+{
+       va_list vl;
+       char **substrings;
+       char * const *substring;
+       bool all_ok = true;
+
+       substrings = strutils_split(input, delim, escape_delim);
+       assert(substrings);
+       va_start(vl, escape_delim);
+
+       for (substring = substrings; *substring; substring++) {
+               const char *expected_substring = va_arg(vl, const char *);
+
+               diag("  got `%s`, expecting `%s`", *substring, expected_substring);
+
+               if (!expected_substring) {
+                       all_ok = false;
+                       break;
+               }
+
+               if (strcmp(*substring, expected_substring) != 0) {
+                       all_ok = false;
+                       break;
+               }
+       }
+
+       strutils_free_null_terminated_array_of_strings(substrings);
+       va_end(vl);
+       ok(all_ok, "strutils_split() produces the expected substrings: `%s` (delim. `%c`, escape `%d`)",
+               input, delim, escape_delim);
+}
+
+static void test_split(void)
+{
+       test_one_split("a/b/c/d/e", '/', false, "a", "b", "c", "d", "e", NULL);
+       test_one_split("a/b//d/e", '/', false, "a", "b", "", "d", "e", NULL);
+       test_one_split("/b/c/d/e", '/', false, "", "b", "c", "d", "e", NULL);
+       test_one_split("a/b/c/d/", '/', false, "a", "b", "c", "d", "", NULL);
+       test_one_split("/b/c/d/", '/', false, "", "b", "c", "d", "", NULL);
+       test_one_split("", '/', false, "", NULL);
+       test_one_split("/", '/', false, "", "", NULL);
+       test_one_split("//", '/', false, "", "", "", NULL);
+       test_one_split("hello+world", '+', false, "hello", "world", NULL);
+       test_one_split("hello\\+world", '+', false, "hello\\", "world", NULL);
+       test_one_split("hello\\+world", '+', true, "hello+world", NULL);
+       test_one_split("hello\\++world", '+', true, "hello+", "world", NULL);
+       test_one_split("hello\\\\++world", '+', true, "hello\\\\", "", "world", NULL);
+       test_one_split("hello+world\\", '+', false, "hello", "world\\", NULL);
+       test_one_split("hello+world\\", '+', true, "hello", "world\\", NULL);
+       test_one_split("\\+", '+', false, "\\", "", NULL);
+       test_one_split("\\+", '+', true, "+", NULL);
+}
+
+static void test_one_is_star_at_the_end_only_glob_pattern(const char *pattern, bool expected)
+{
+       ok(strutils_is_star_at_the_end_only_glob_pattern(pattern) == expected,
+               "strutils_is_star_at_the_end_only_glob_pattern() returns the expected result: `%s` -> %d",
+               pattern, expected);
+}
+
+static void test_is_star_at_the_end_only_glob_pattern(void)
+{
+       test_one_is_star_at_the_end_only_glob_pattern("allo*", true);
+       test_one_is_star_at_the_end_only_glob_pattern("allo\\\\*", true);
+       test_one_is_star_at_the_end_only_glob_pattern("allo", false);
+       test_one_is_star_at_the_end_only_glob_pattern("al*lo", false);
+       test_one_is_star_at_the_end_only_glob_pattern("al\\*lo", false);
+       test_one_is_star_at_the_end_only_glob_pattern("*allo", false);
+       test_one_is_star_at_the_end_only_glob_pattern("al*lo*", false);
+       test_one_is_star_at_the_end_only_glob_pattern("allo**", false);
+       test_one_is_star_at_the_end_only_glob_pattern("allo*\\*", false);
+       test_one_is_star_at_the_end_only_glob_pattern("allo\\*", false);
+}
+
+static void test_one_is_star_glob_pattern(const char *pattern, bool expected)
+{
+       ok(strutils_is_star_glob_pattern(pattern) == expected,
+               "strutils_is_star_glob_pattern() returns the expected result: `%s` -> %d",
+               pattern, expected);
+}
+
+static void test_is_star_glob_pattern(void)
+{
+       test_one_is_star_glob_pattern("allo*", true);
+       test_one_is_star_glob_pattern("*allo", true);
+       test_one_is_star_glob_pattern("*allo*", true);
+       test_one_is_star_glob_pattern("*al*lo*", true);
+       test_one_is_star_glob_pattern("al\\**lo", true);
+       test_one_is_star_glob_pattern("al\\*l*o", true);
+       test_one_is_star_glob_pattern("all*o\\", true);
+       test_one_is_star_glob_pattern("*", true);
+       test_one_is_star_glob_pattern("\\\\*", true);
+       test_one_is_star_glob_pattern("allo", false);
+       test_one_is_star_glob_pattern("allo\\*", false);
+       test_one_is_star_glob_pattern("al\\*lo", false);
+       test_one_is_star_glob_pattern("\\*allo", false);
+       test_one_is_star_glob_pattern("\\*", false);
+       test_one_is_star_glob_pattern("allo\\", false);
+}
+
+static void test_one_normalize_star_glob_pattern(const char *pattern,
+               const char *expected)
+{
+       char *rw_pattern = strdup(pattern);
+
+       assert(rw_pattern);
+       strutils_normalize_star_glob_pattern(rw_pattern);
+       ok(strcmp(rw_pattern, expected) == 0,
+               "strutils_normalize_star_glob_pattern() produces the expected result: `%s` -> `%s`",
+               pattern, expected);
+       free(rw_pattern);
+}
+
+static void test_normalize_star_glob_pattern(void)
+{
+       test_one_normalize_star_glob_pattern("salut", "salut");
+       test_one_normalize_star_glob_pattern("sal*ut", "sal*ut");
+       test_one_normalize_star_glob_pattern("sal**ut", "sal*ut");
+       test_one_normalize_star_glob_pattern("sal***ut", "sal*ut");
+       test_one_normalize_star_glob_pattern("*salut", "*salut");
+       test_one_normalize_star_glob_pattern("**salut", "*salut");
+       test_one_normalize_star_glob_pattern("***salut", "*salut");
+       test_one_normalize_star_glob_pattern("salut*", "salut*");
+       test_one_normalize_star_glob_pattern("salut**", "salut*");
+       test_one_normalize_star_glob_pattern("salut***", "salut*");
+       test_one_normalize_star_glob_pattern("sa\\*lut", "sa\\*lut");
+       test_one_normalize_star_glob_pattern("sa\\**lut", "sa\\**lut");
+       test_one_normalize_star_glob_pattern("sa*\\**lut", "sa*\\**lut");
+       test_one_normalize_star_glob_pattern("sa*\\***lut", "sa*\\**lut");
+       test_one_normalize_star_glob_pattern("\\*salu**t", "\\*salu*t");
+       test_one_normalize_star_glob_pattern("\\*salut**", "\\*salut*");
+       test_one_normalize_star_glob_pattern("\\*salut**\\*", "\\*salut*\\*");
+       test_one_normalize_star_glob_pattern("\\*salut", "\\*salut");
+       test_one_normalize_star_glob_pattern("\\***salut", "\\**salut");
+       test_one_normalize_star_glob_pattern("salut\\", "salut\\");
+       test_one_normalize_star_glob_pattern("salut\\**", "salut\\**");
+       test_one_normalize_star_glob_pattern("salut\\\\*", "salut\\\\*");
+       test_one_normalize_star_glob_pattern("salut\\\\***", "salut\\\\*");
+       test_one_normalize_star_glob_pattern("*", "*");
+       test_one_normalize_star_glob_pattern("**", "*");
+       test_one_normalize_star_glob_pattern("***", "*");
+       test_one_normalize_star_glob_pattern("**\\***", "*\\**");
+}
+
+int main(int argc, char **argv)
+{
+       plan_tests(NUM_TESTS);
+       diag("String utils unit tests");
+       test_normalize_star_glob_pattern();
+       test_is_star_glob_pattern();
+       test_is_star_at_the_end_only_glob_pattern();
+       test_split();
+
+       return exit_status();
+}
This page took 0.02802 seconds and 4 git commands to generate.