| 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 <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <check.h> |
| 36 |
#include "global.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static void setup(void) { |
| 48 |
init_hash_table(); |
| 49 |
} |
| 50 |
|
| 51 |
static void teardown(void) { |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
START_TEST(test_add_string) { |
| 56 |
const char *str1; |
| 57 |
const char *str2; |
| 58 |
const char *str3; |
| 59 |
char *temp; |
| 60 |
#ifndef MANY_CORES |
| 61 |
fail_unless(add_string(NULL) == NULL, "add_string should null when receiving a null as parameter."); |
| 62 |
#endif |
| 63 |
str1 = add_string("Hello world"); |
| 64 |
fail_unless(str1 != NULL, "add_string should not return null when receiving content."); |
| 65 |
temp = malloc(strlen(str1)+1); |
| 66 |
strcpy(temp, str1); |
| 67 |
str2 = add_string(temp); |
| 68 |
fail_unless(str2 == str1, "add_string should return same pointer for 2 same strings but str1 (%p -> '%s') != str2 (%p -> '%s').", str1, str1, str2, str2); |
| 69 |
str3 = add_string(""); |
| 70 |
fail_unless(str3 != NULL, "add_string should handle gracefully empty non null strings."); |
| 71 |
free(temp); |
| 72 |
} |
| 73 |
END_TEST |
| 74 |
|
| 75 |
START_TEST(test_add_refcount) { |
| 76 |
const char *str1; |
| 77 |
const char *str2; |
| 78 |
|
| 79 |
str1 = add_string("Crossfire Rulez"); |
| 80 |
str2 = add_refcount(str1); |
| 81 |
fail_unless(str1 == str2, "result of add_refcount (%p) should be the same as original pointer (%p).", str2, str1); |
| 82 |
fail_unless(query_refcount(str1) == 2, "add_refcount (%p) should have made refcount to value 2 but was %d", str1, query_refcount(str1)); |
| 83 |
} |
| 84 |
END_TEST |
| 85 |
|
| 86 |
START_TEST(test_query_refcount) { |
| 87 |
const char *str1; |
| 88 |
|
| 89 |
str1 = add_string("Hello World"); |
| 90 |
fail_unless(query_refcount(str1) == 1, "After add_string, query_refcount should return 1 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1); |
| 91 |
add_string("Hello World"); |
| 92 |
fail_unless(query_refcount(str1) == 2, "After twice add_string with same string, query_refcount should return 2 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1); |
| 93 |
add_refcount(str1); |
| 94 |
fail_unless(query_refcount(str1) == 3, "After call to add_refcount, query_refcount should now return 3 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1); |
| 95 |
} |
| 96 |
END_TEST |
| 97 |
|
| 98 |
START_TEST(test_find_string) { |
| 99 |
const char *str1; |
| 100 |
const char *str2; |
| 101 |
const char *result; |
| 102 |
|
| 103 |
str1 = add_string("Hello world"); |
| 104 |
str2 = add_string("Bonjour le monde"); |
| 105 |
result = find_string("Hello world"); |
| 106 |
fail_unless(str1 == result, "find_string for %s should return %p but returned %p(%s).", str1, str1, result, result); |
| 107 |
result = find_string("Bonjour le monde"); |
| 108 |
fail_unless(str2 == result, "find_string for %s should return %p but returned %p(%s).", str2, str2, result, result); |
| 109 |
result = find_string("Hola mundo"); |
| 110 |
fail_unless(result == NULL, "Searching for an inexistant string should return NULL but returned %p(%s)", result, result); |
| 111 |
str1 = add_string(""); |
| 112 |
result = find_string(""); |
| 113 |
fail_unless(result == str1, "Search for empty string should return it(%p), but returned %p", str1, result); |
| 114 |
free_string(str2); |
| 115 |
result = find_string("Bonjour le monde"); |
| 116 |
fail_unless(result == NULL, "add_string + free_string should mean i can't find the string anymore but find string returned %p(%s)", result, result); |
| 117 |
} |
| 118 |
END_TEST |
| 119 |
|
| 120 |
START_TEST(test_free_string) { |
| 121 |
const char *str1; |
| 122 |
const char *str2; |
| 123 |
|
| 124 |
str1 = add_string("Cr0ssf1r3 r|_|1z"); |
| 125 |
free_string(str1); |
| 126 |
str2 = find_string("Cr0ssf1r3 r|_|1z"); |
| 127 |
fail_unless(str2 == NULL, "find_String should return null after a free_string but it returned %p (%s)", str2, str2); |
| 128 |
str1 = add_string("bleh"); |
| 129 |
add_string("bleh"); |
| 130 |
free_string(str1); |
| 131 |
str2 = find_string("bleh"); |
| 132 |
fail_unless(str2 == str1, "find_string should return the string(%p) after a add_string, add_string, free_string but returned %p", str1, str2); |
| 133 |
free_string(str1); |
| 134 |
str2 = find_string("bleh"); |
| 135 |
fail_unless(str2 == NULL, "find_string should return null after add_string, add_string, free_string, free_string but returned %p", str2); |
| 136 |
} |
| 137 |
END_TEST |
| 138 |
|
| 139 |
START_TEST(test_buf_overflow) { |
| 140 |
int i; |
| 141 |
|
| 142 |
i = buf_overflow("1", "22", 3); |
| 143 |
fail_unless(i, "'1' +'22' can't fit in a 3 char buffer but buf_overflow told us there won't be any overflow"); |
| 144 |
i = buf_overflow("1", NULL, 1); |
| 145 |
fail_unless(i, "'1' +NULL can't fit in a 1 char buffer but buf_overflow told us there won't be any overflow"); |
| 146 |
i = buf_overflow("1", NULL, 2); |
| 147 |
fail_unless(!i, "'1' +NULL can fit in a 2 char buffer but buf_overflow told us it won't"); |
| 148 |
i = buf_overflow("", NULL, 1); |
| 149 |
fail_unless(!i, "EMPTY +NULL can fit in a 1 char buffer but buf_overflow told us it won't"); |
| 150 |
i = buf_overflow("", NULL, 0); |
| 151 |
fail_unless(i, "EMPTY +NULL can't fit in a 0 char buffer but buf_overflow told us there won't be any overflow"); |
| 152 |
} |
| 153 |
END_TEST |
| 154 |
|
| 155 |
static Suite *shstr_suite(void) { |
| 156 |
Suite *s = suite_create("shstr"); |
| 157 |
TCase *tc_core = tcase_create("Core"); |
| 158 |
|
| 159 |
|
| 160 |
tcase_add_checked_fixture(tc_core, setup, teardown); |
| 161 |
|
| 162 |
suite_add_tcase(s, tc_core); |
| 163 |
tcase_add_test(tc_core, test_add_string); |
| 164 |
tcase_add_test(tc_core, test_add_refcount); |
| 165 |
tcase_add_test(tc_core, test_query_refcount); |
| 166 |
tcase_add_test(tc_core, test_find_string); |
| 167 |
tcase_add_test(tc_core, test_free_string); |
| 168 |
tcase_add_test(tc_core, test_buf_overflow); |
| 169 |
|
| 170 |
return s; |
| 171 |
} |
| 172 |
|
| 173 |
int main(void) { |
| 174 |
int nf; |
| 175 |
Suite *s = shstr_suite(); |
| 176 |
SRunner *sr = srunner_create(s); |
| 177 |
|
| 178 |
srunner_set_xml(sr, LOGDIR "/unit/common/shstr.xml"); |
| 179 |
srunner_set_log(sr, LOGDIR "/unit/common/shstr.out"); |
| 180 |
srunner_run_all(sr, CK_ENV); |
| 181 |
nf = srunner_ntests_failed(sr); |
| 182 |
srunner_free(sr); |
| 183 |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 184 |
} |