| 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 |
#include <check.h> |
| 34 |
#include <stdarg.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
|
| 38 |
#include "global.h" |
| 39 |
|
| 40 |
|
| 41 |
static void setup(void) { |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
static void teardown(void) { |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
static void check_split_string(const char *str, size_t array_size, ...) { |
| 50 |
char tmp[256]; |
| 51 |
char *array[64]; |
| 52 |
size_t result; |
| 53 |
size_t i; |
| 54 |
va_list arg; |
| 55 |
|
| 56 |
snprintf(tmp, sizeof(tmp), "%s", str); |
| 57 |
for(i = 0; i < sizeof(array)/sizeof(*array); i++) |
| 58 |
array[i] = NULL; |
| 59 |
result = split_string(tmp, array, array_size, ':'); |
| 60 |
fail_if(result > array_size, "result == %zu > %zu == array_size", result, array_size); |
| 61 |
va_start(arg, array_size); |
| 62 |
for(i = 0; i < sizeof(array)/sizeof(*array); i++) { |
| 63 |
const char *expected_result; |
| 64 |
|
| 65 |
expected_result = va_arg(arg, const char *); |
| 66 |
if (expected_result == NULL) |
| 67 |
break; |
| 68 |
|
| 69 |
if (i >= array_size) |
| 70 |
fail("internal error: too many arguments passed to check_split_string()"); |
| 71 |
if (i < result) |
| 72 |
fail_if(strcmp(array[i], expected_result) != 0, "strcmp(array[%zu] == %s, %s) != 0", i, array[i], expected_result); |
| 73 |
else |
| 74 |
fail_if(array[i] != NULL, "array[%zu] == NULL", i); |
| 75 |
} |
| 76 |
va_end(arg); |
| 77 |
fail_if(result != i, "%zu != %zu", result, i); |
| 78 |
} |
| 79 |
|
| 80 |
START_TEST(test_split_string) { |
| 81 |
check_split_string("", 0, NULL); |
| 82 |
check_split_string("", 5, "", NULL); |
| 83 |
check_split_string(":", 5, "", "", NULL); |
| 84 |
check_split_string("::", 5, "", "", "", NULL); |
| 85 |
check_split_string("abc:def:ghi", 0, NULL); |
| 86 |
check_split_string("abc:def:ghi", 1, "abc:def:ghi", NULL); |
| 87 |
check_split_string("abc:def:ghi", 2, "abc", "def:ghi", NULL); |
| 88 |
check_split_string("abc:def:ghi", 3, "abc", "def", "ghi", NULL); |
| 89 |
check_split_string("abc:def:ghi", 4, "abc", "def", "ghi", NULL); |
| 90 |
check_split_string("::abc::def::", 0, NULL); |
| 91 |
check_split_string("::abc::def::", 1, "::abc::def::", NULL); |
| 92 |
check_split_string("::abc::def::", 2, "", ":abc::def::", NULL); |
| 93 |
check_split_string("::abc::def::", 3, "", "", "abc::def::", NULL); |
| 94 |
check_split_string("::abc::def::", 4, "", "", "abc", ":def::", NULL); |
| 95 |
check_split_string("::abc::def::", 5, "", "", "abc", "", "def::", NULL); |
| 96 |
check_split_string("::abc::def::", 6, "", "", "abc", "", "def", ":", NULL); |
| 97 |
check_split_string("::abc::def::", 7, "", "", "abc", "", "def", "", "", NULL); |
| 98 |
check_split_string("::abc::def::", 8, "", "", "abc", "", "def", "", "", NULL); |
| 99 |
} |
| 100 |
END_TEST |
| 101 |
|
| 102 |
START_TEST(test_replace) { |
| 103 |
char init[MAX_BUF], replaced[MAX_BUF]; |
| 104 |
|
| 105 |
snprintf(init, sizeof(init), "first test"); |
| 106 |
replace(init, "first", "other", replaced, sizeof(replaced)); |
| 107 |
fail_if(strcmp(replaced, "other test") != 0, "%s != other test", replaced); |
| 108 |
|
| 109 |
snprintf(init, sizeof(init), "second test"); |
| 110 |
replace(init, "e", "a", replaced, sizeof(replaced)); |
| 111 |
fail_if(strcmp(replaced, "sacond tast") != 0, "%s != sacond tast", replaced); |
| 112 |
|
| 113 |
|
| 114 |
snprintf(init, sizeof(init), "long test"); |
| 115 |
replace(init, "long", "really long", replaced, strlen(init) + 1); |
| 116 |
fail_if(strcmp(replaced, "really lo") != 0, "%s != really lo", replaced); |
| 117 |
} |
| 118 |
END_TEST |
| 119 |
|
| 120 |
static Suite *utils_suite(void) { |
| 121 |
Suite *s = suite_create("utils"); |
| 122 |
TCase *tc_core = tcase_create("Core"); |
| 123 |
|
| 124 |
|
| 125 |
tcase_add_checked_fixture(tc_core, setup, teardown); |
| 126 |
|
| 127 |
suite_add_tcase(s, tc_core); |
| 128 |
tcase_add_test(tc_core, test_split_string); |
| 129 |
tcase_add_test(tc_core, test_replace); |
| 130 |
|
| 131 |
return s; |
| 132 |
} |
| 133 |
|
| 134 |
int main(void) { |
| 135 |
int nf; |
| 136 |
Suite *s = utils_suite(); |
| 137 |
SRunner *sr = srunner_create(s); |
| 138 |
|
| 139 |
srunner_set_fork_status(sr, CK_NOFORK); |
| 140 |
srunner_set_xml(sr, LOGDIR "/unit/common/utils.xml"); |
| 141 |
srunner_set_log(sr, LOGDIR "/unit/common/utils.out"); |
| 142 |
srunner_run_all(sr, CK_ENV); |
| 143 |
nf = srunner_ntests_failed(sr); |
| 144 |
srunner_free(sr); |
| 145 |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 146 |
} |