diff options
author | Lukas Slebodnik <lslebodn@redhat.com> | 2014-07-25 10:22:59 +0200 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2014-07-28 10:11:49 +0200 |
commit | 022c6b90bb37851c0e8704c0e5388ebc113c6470 (patch) | |
tree | bec16e36bf26ddefc4b1d8e5be99be2ea5812680 /src/tests | |
parent | 462db32918a05097652f8232cd6c8d78a826e63c (diff) | |
download | sssd-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')
-rw-r--r-- | src/tests/cmocka/test_string_utils.c | 150 | ||||
-rw-r--r-- | src/tests/cmocka/test_utils.c | 2 | ||||
-rw-r--r-- | src/tests/cmocka/test_utils.h | 4 |
3 files changed, 156 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); +} diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c index c1dfb66e1..9d6cbf35f 100644 --- a/src/tests/cmocka/test_utils.c +++ b/src/tests/cmocka/test_utils.c @@ -917,6 +917,8 @@ int main(int argc, const char *argv[]) unit_test_setup_teardown(test_expand_homedir_template, setup_homedir_ctx, teardown_homedir_ctx), unit_test(test_textual_public_key), + unit_test(test_replace_whitespaces), + unit_test(test_reverse_replace_whitespaces), }; /* Set debug level to invalid value so we can deside if -d 0 was used. */ diff --git a/src/tests/cmocka/test_utils.h b/src/tests/cmocka/test_utils.h index 8f6269305..f85ac2f2b 100644 --- a/src/tests/cmocka/test_utils.h +++ b/src/tests/cmocka/test_utils.h @@ -26,4 +26,8 @@ /* from src/tests/cmocka/test_sss_ssh.c */ void test_textual_public_key(void **state); +/* from src/tests/cmocka/test_string_utils.c */ +void test_replace_whitespaces(void **state); +void test_reverse_replace_whitespaces(void **state); + #endif /* __TESTS__CMOCKA__TEST_UTILS_H__ */ |