1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/*
* Copyright (c) 2018 Balabit
*
* This library 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 library 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
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include <criterion/criterion.h>
#include <criterion/parameterized.h>
#include "str-utils.h"
/* this macro defines which strchr() implementation we are testing. It is
* extracted as a macro in order to make it easy to switch strchr()
* implementation, provided there would be more.
*/
#define strchr_under_test _strchr_optimized_for_single_char_haystack
typedef struct _StrChrTestData
{
gchar *str;
int c;
int ofs;
} StrChrTestData;
ParameterizedTestParameters(str_utils, test_str_utils_is_null)
{
static StrChrTestData test_data_list[] =
{
{"", 'x', 0},
{"\0x", 'x', 0},
{"a", 'x', 0},
{"abc", 'x', 0}
};
return cr_make_param_array(StrChrTestData, test_data_list, sizeof(test_data_list) / sizeof(test_data_list[0]));
}
ParameterizedTest(StrChrTestData *test_data, str_utils, test_str_utils_is_null)
{
cr_assert_null(strchr_under_test(test_data->str, test_data->c), "expected a NULL return");
}
ParameterizedTestParameters(str_utils, test_str_utils_find_char)
{
static StrChrTestData test_data_list[] =
{
{"", '\0', 0},
{"a", 'a', 0},
{"a", '\0', 1},
{"abc", 'a', 0},
{"abc", 'b', 1},
{"abc", 'c', 2},
{"abc", '\0', 3},
{"0123456789abcdef", '0', 0},
{"0123456789abcdef", '7', 7},
{"0123456789abcdef", 'f', 15}
};
return cr_make_param_array(StrChrTestData, test_data_list, sizeof(test_data_list) / sizeof(test_data_list[0]));
}
ParameterizedTest(StrChrTestData *test_data, str_utils, test_str_utils_find_char)
{
const char *result = strchr_under_test(test_data->str, test_data->c);
cr_assert_not_null(result, "expected a non-NULL return");
cr_assert(result - test_data->str <= strlen(test_data->str),
"Expected the strchr() return value to point into the input string or the terminating NUL, it points past the NUL");
cr_assert(result >= test_data->str,
"Expected the strchr() return value to point into the input string or the terminating NUL, it points before the start of the string");
cr_assert_eq((result - test_data->str), test_data->ofs,
"Expected the strchr() return value to point right to the specified offset");
}
Test(strsplit, when_tokens_not_limited_find_all_tokens)
{
gchar **tokens = strsplit(" ABB CCC DDDD 111", ' ', 0);
gint tokens_n = g_strv_length(tokens);
cr_expect_eq(tokens_n, 4);
cr_expect_str_eq(tokens[0], "ABB");
cr_expect_str_eq(tokens[1], "CCC");
cr_expect_str_eq(tokens[2], "DDDD");
cr_expect_str_eq(tokens[3], "111");
cr_expect_null(tokens[4]);
g_strfreev(tokens);
}
Test(strsplit, when_string_without_delim_return_the_string)
{
gchar **tokens = strsplit("111", ' ', 0);
gint tokens_n = g_strv_length(tokens);
cr_expect_eq(tokens_n, 1);
cr_expect_str_eq(tokens[0], "111");
cr_expect_null(tokens[1]);
g_strfreev(tokens);
}
Test(strsplit, when_empty_string_return_the_empty_string)
{
gchar **tokens = strsplit("", ' ', 0);
gint tokens_n = g_strv_length(tokens);
cr_expect_eq(tokens_n, 1);
cr_expect_str_eq(tokens[0], "");
cr_expect_null(tokens[1]);
g_strfreev(tokens);
}
Test(strsplit, when_null_str_or_null_delim_return_null)
{
gchar **tokens = strsplit(NULL, ' ', 0);
cr_expect_null(tokens);
tokens = strsplit(NULL, '\0', 0);
cr_expect_null(tokens);
tokens = strsplit("AAA ", '\0', 0);
cr_expect_null(tokens);
}
Test(strsplit, when_tokens_limited_join_remaining_tokens)
{
gchar **tokens = strsplit(" ABB CCC DDDD 111", ' ', 3);
gint tokens_n = g_strv_length(tokens);
cr_expect_eq(tokens_n, 3);
cr_expect_str_eq(tokens[0], "ABB");
cr_expect_str_eq(tokens[1], "CCC");
cr_expect_str_eq(tokens[2], "DDDD 111");
cr_expect_null(tokens[3]);
g_strfreev(tokens);
}
|