summaryrefslogtreecommitdiff
path: root/test/utils.c
blob: e74057ebdb50567fa11a2eb6feb1ff7e1e7350cd (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright 2016 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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/>.
 */

/**
 * \file
 * Tests for utility functions.
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <check.h>

#include "utils/string.h"
#include "utils/corestrings.h"

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))
#define SLEN(x) (sizeof((x)) - 1)

struct test_pairs {
	const unsigned long test;
	const char* res;
};

static const struct test_pairs human_friendly_bytesize_test_vec[] = {
	{ 0, "0.00Bytes" },
	{ 1024, "1024.00Bytes" },
	{ 1025, "1.00kBytes" },
	{ 1048576, "1024.00kBytes" },
	{ 1048577, "1.00MBytes" },
	{ 1073741824, "1024.00MBytes" },
	{ 1073741888, "1024.00MBytes" }, /* spot the rounding error */
	{ 1073741889, "1.00GBytes" },
	{ 2147483648, "2.00GBytes" },
	{ 3221225472, "3.00GBytes" },
	{ 4294967295, "4.00GBytes" },
};

START_TEST(human_friendly_bytesize_test)
{
	char *res_str;
	const struct test_pairs *tst = &human_friendly_bytesize_test_vec[_i];

	res_str = human_friendly_bytesize(tst->test);

	/* ensure result data is correct */
	ck_assert_str_eq(res_str, tst->res);
}
END_TEST

static TCase *human_friendly_bytesize_case_create(void)
{
	TCase *tc;
	tc = tcase_create("Human friendly bytesize");

	tcase_add_loop_test(tc, human_friendly_bytesize_test,
			    0, NELEMS(human_friendly_bytesize_test_vec));

	return tc;
}

struct test_strings {
	const char* test;
	const char* res;
};

static const struct test_strings squash_whitespace_test_vec[] = {
	{ "", "" },
	{ " ", " " },
	{ "    ", " " },
	{ " \n\r\t   ", " " },
	{ " a ", " a " },
	{ " a   b ", " a b " },
};

START_TEST(squash_whitespace_test)
{
	char *res_str;
	const struct test_strings *tst = &squash_whitespace_test_vec[_i];

	res_str = squash_whitespace(tst->test);
	ck_assert(res_str != NULL);

	/* ensure result data is correct */
	ck_assert_str_eq(res_str, tst->res);

	free(res_str);
}
END_TEST

START_TEST(squash_whitespace_api_test)
{
	char *res_str;

	res_str = squash_whitespace(NULL);
	ck_assert(res_str != NULL);

	free(res_str);
}
END_TEST

static TCase *squash_whitespace_case_create(void)
{
	TCase *tc;
	tc = tcase_create("Squash whitespace");

	tcase_add_test_raise_signal(tc, squash_whitespace_api_test, 6);

	tcase_add_loop_test(tc, squash_whitespace_test,
			    0, NELEMS(squash_whitespace_test_vec));

	return tc;
}


START_TEST(corestrings_init_fini_test)
{
	nserror res;

	res = corestrings_init();
	ck_assert_int_eq(res, NSERROR_OK);

	corestrings_fini();
}
END_TEST

START_TEST(corestrings_double_init_test)
{
	nserror res;

	res = corestrings_init();
	ck_assert_int_eq(res, NSERROR_OK);

	res = corestrings_init();
	ck_assert_int_eq(res, NSERROR_OK);

	corestrings_fini();
}
END_TEST

START_TEST(corestrings_double_fini_test)
{
	nserror res;

	res = corestrings_init();
	ck_assert_int_eq(res, NSERROR_OK);

	corestrings_fini();

	corestrings_fini();
}
END_TEST


static TCase *corestrings_case_create(void)
{
	TCase *tc;
	tc = tcase_create("Corestrings");

	tcase_add_test(tc, corestrings_init_fini_test);
	tcase_add_test(tc, corestrings_double_init_test);
	tcase_add_test(tc, corestrings_double_fini_test);

	return tc;
}

START_TEST(string_utils_squash_whitespace_test)
{
	char *res;
	res = squash_whitespace("   A string  with \t      \t   lots of whitespace    ");
	ck_assert(res != NULL);
	ck_assert_str_eq(res, " A string with lots of whitespace ");

	free(res);
}
END_TEST


START_TEST(string_utils_cnv_space2nbsp_test)
{
	char *res;
	char comparison[64];

	snprintf(comparison, 64,
		 "%c%cA%c%cstring%c%c%c%cwith%c%c%c%c%c%cwhitespace%c%c",
		 0xC2, 0xA0, 0xC2, 0xA0, 0xC2, 0xA0, 0xC2, 0xA0,
		 0xC2, 0xA0, 0xC2, 0xA0, 0xC2, 0xA0, 0xC2, 0xA0);

	res = cnv_space2nbsp(" A string  with \t whitespace ");
	ck_assert(res != NULL);
	ck_assert_str_eq(res, comparison);

	free(res);
}
END_TEST


START_TEST(string_utils_snstrjoin_test)
{
	nserror res;
	char *resstr = NULL;
	size_t resstrlen;

	res = snstrjoin(&resstr, &resstrlen, ',', 4, "1", "2", "3", "4");
	ck_assert_int_eq(res, NSERROR_OK);
	ck_assert(resstr != NULL);
	ck_assert_int_eq(resstrlen, 8);
	ck_assert_str_eq(resstr, "1,2,3,4");
	free(resstr);
}
END_TEST


static TCase *string_utils_case_create(void)
{
	TCase *tc;
	tc = tcase_create("String utilities");

	tcase_add_test(tc, string_utils_squash_whitespace_test);
	tcase_add_test(tc, string_utils_cnv_space2nbsp_test);
	tcase_add_test(tc, string_utils_snstrjoin_test);

	return tc;
}

static Suite *utils_suite_create(void)
{
	Suite *s;
	s = suite_create("String utils");

	suite_add_tcase(s, human_friendly_bytesize_case_create());
	suite_add_tcase(s, squash_whitespace_case_create());
	suite_add_tcase(s, corestrings_case_create());
	suite_add_tcase(s, string_utils_case_create());

	return s;
}

int main(int argc, char **argv)
{
	int number_failed;
	SRunner *sr;

	sr = srunner_create(utils_suite_create());

	srunner_run_all(sr, CK_ENV);

	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}