summaryrefslogtreecommitdiffstats
path: root/src/tests/cmocka/test_string_utils.c
diff options
context:
space:
mode:
authorLukas Slebodnik <lslebodn@redhat.com>2014-07-25 10:22:59 +0200
committerJakub Hrozek <jhrozek@redhat.com>2014-07-28 10:11:49 +0200
commit022c6b90bb37851c0e8704c0e5388ebc113c6470 (patch)
treebec16e36bf26ddefc4b1d8e5be99be2ea5812680 /src/tests/cmocka/test_string_utils.c
parent462db32918a05097652f8232cd6c8d78a826e63c (diff)
downloadsssd-022c6b90bb37851c0e8704c0e5388ebc113c6470.tar.gz
sssd-022c6b90bb37851c0e8704c0e5388ebc113c6470.tar.xz
sssd-022c6b90bb37851c0e8704c0e5388ebc113c6470.zip
UTIL: Add functions for replacing whitespaces.
Reviewed-by: Pavel Reichl <preichl@redhat.com> Reviewed-by: Michal Židek <mzidek@redhat.com>
Diffstat (limited to 'src/tests/cmocka/test_string_utils.c')
-rw-r--r--src/tests/cmocka/test_string_utils.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/tests/cmocka/test_string_utils.c b/src/tests/cmocka/test_string_utils.c
new file mode 100644
index 000000000..d10c3be47
--- /dev/null
+++ b/src/tests/cmocka/test_string_utils.c
@@ -0,0 +1,150 @@
+/*
+ Authors:
+ Lukas Slebodnik <slebodnikl@redhat.com>
+
+ Copyright (C) 2014 Red Hat
+
+ 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
+ the Free Software Foundation; either version 3 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "util/util.h"
+#include "tests/cmocka/common_mock.h"
+
+void test_replace_whitespaces(void **state)
+{
+ TALLOC_CTX *mem_ctx;
+ const char *input_str = "Lorem ipsum dolor sit amet";
+ const char *res;
+ size_t i;
+
+ struct {
+ const char *input;
+ const char *output;
+ const char *replace_string;
+ } data_set[] = {
+ { "", "", "-" },
+ { " ", "-", "-" },
+ { "\t", "-", "-" },
+ { "\n", "-", "-" },
+ { " \t\n ", "----", "-" },
+ { "abcd", "abcd", "-" },
+ { "a b c d", "a-b-c-d", "-" },
+ { " a b c d ", "-a-b-c-d-", "-" },
+ { " ", "^", "^" },
+ { "\t", "^", "^" },
+ { "\n", "^", "^" },
+ { " \t\n ", "^^^^", "^" },
+ { "abcd", "abcd", "^" },
+ { "a b c d", "a^b^c^d", "^" },
+ { " a b c d ", "^a^b^c^d^", "^" },
+ { " ", "^", "^" },
+ { "\t", ";-)", ";-)" },
+ { "\n", ";-)", ";-)" },
+ { " \t\n ", ";-);-);-);-)", ";-)" },
+ { "abcd", "abcd", ";-)" },
+ { "a b c d", "a;-)b;-)c;-)d", ";-)" },
+ { " a b c d ", ";-)a;-)b;-)c;-)d;-)", ";-)" },
+ { " ", " ", " " },
+ { " ", " ", " " },
+ { "abcd", "abcd", " " },
+ { "a b c d", "a b c d", " " },
+ { " a b c d ", " a b c d ", " " },
+ { " ", " \t", " \t" },
+ { " ", " \t \t \t \t", " \t" },
+ { "abcd", "abcd", " \t" },
+ { "a b c d", "a \tb \tc \td", " \t" },
+ { " a b c d ", " \ta \tb \tc \td \t", " \t" },
+ { NULL, NULL, NULL },
+ };
+
+ mem_ctx = talloc_new(NULL);
+ assert_non_null(mem_ctx);
+ check_leaks_push(mem_ctx);
+
+ res = sss_replace_whitespaces(mem_ctx, input_str, NULL);
+ assert_true(res == input_str);
+
+ res = sss_replace_whitespaces(mem_ctx, input_str, "");
+ assert_true(res == input_str);
+
+ for (i=0; data_set[i].input != NULL; ++i) {
+ res = sss_replace_whitespaces(mem_ctx, data_set[i].input,
+ data_set[i].replace_string);
+ assert_non_null(res);
+ assert_string_equal(res, data_set[i].output);
+ talloc_zfree(res);
+ }
+
+ assert_true(check_leaks_pop(mem_ctx) == true);
+ talloc_free(mem_ctx);
+}
+
+void test_reverse_replace_whitespaces(void **state)
+{
+ TALLOC_CTX *mem_ctx;
+ char *input_str = discard_const_p(char, "Lorem ipsum dolor sit amet");
+ char *res;
+ size_t i;
+
+ struct {
+ const char *input;
+ const char *output;
+ const char *replace_string;
+ } data_set[] = {
+ { "", "", "-" },
+ { "-", " ", "-" },
+ { "----", " ", "-" },
+ { "abcd", "abcd", "-" },
+ { "a-b-c-d", "a b c d", "-" },
+ { "-a-b-c-d-", " a b c d ", "-" },
+ { "^", " ", "^" },
+ { "^^^^", " ", "^" },
+ { "abcd", "abcd", "^" },
+ { "a^b^c^d", "a b c d", "^" },
+ { "^a^b^c^d^", " a b c d ", "^" },
+ { ";-)", " ", ";-)" },
+ { ";-);-);-);-)", " ", ";-)" },
+ { "abcd", "abcd", ";-)" },
+ { "a;-)b;-)c;-)d", "a b c d", ";-)" },
+ { ";-)a;-)b;-)c;-)d;-)", " a b c d ", ";-)" },
+ { " ", " ", " " },
+ { " ", " ", " " },
+ { "abcd", "abcd", " " },
+ { "a b c d", "a b c d", " " },
+ { " a b c d ", " a b c d ", " " },
+ { NULL, NULL, NULL },
+ };
+
+ mem_ctx = talloc_new(NULL);
+ assert_non_null(mem_ctx);
+ check_leaks_push(mem_ctx);
+
+ res = sss_reverse_replace_whitespaces(mem_ctx, input_str, NULL);
+ assert_true(res == input_str);
+
+ res = sss_reverse_replace_whitespaces(mem_ctx, input_str, "");
+ assert_true(res == input_str);
+
+ for (i=0; data_set[i].input != NULL; ++i) {
+ input_str = discard_const_p(char, data_set[i].input);
+ res = sss_reverse_replace_whitespaces(mem_ctx, input_str,
+ data_set[i].replace_string);
+ assert_non_null(res);
+ assert_string_equal(res, data_set[i].output);
+ talloc_zfree(res);
+ }
+
+ assert_true(check_leaks_pop(mem_ctx) == true);
+ talloc_free(mem_ctx);
+}