sc and str tests (#49)

* sc and str tests
This commit is contained in:
Tezc 2021-02-17 04:17:39 +03:00 committed by GitHub
parent 88c2224025
commit e1b43ac245
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 4 deletions

View File

@ -38,7 +38,7 @@ if(SC_BUILD_TEST)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
target_link_options(${PROJECT_NAME}_test PRIVATE
-Wl,--wrap=snprintf)
-Wl,--wrap=snprintf,--wrap=strtoll)
endif ()
endif ()

View File

@ -163,6 +163,8 @@ void test_rand()
#ifdef SC_HAVE_WRAP
#include <errno.h>
int fail_snprintf;
int __wrap_snprintf(char *str, size_t size, const char *format, ...)
@ -181,9 +183,27 @@ int __wrap_snprintf(char *str, size_t size, const char *format, ...)
return fail_snprintf;
}
int fail_strtoll;
int fail_strtoll_errno;
extern long long int __real_strtoll (const char *__restrict nptr,
char **__restrict endptr, int base);
extern long long int __wrap_strtoll (const char *__restrict nptr,
char **__restrict endptr, int base)
{
if (fail_strtoll) {
errno = fail_strtoll_errno;
*endptr = (char*) nptr;
return 0;
}
return __real_strtoll(nptr, endptr, base);
}
void fail_test()
{
char* t;
int64_t val;
char buf[32];
fail_snprintf = -1;
@ -205,6 +225,17 @@ void fail_test()
t = sc_bytes_to_size(buf, sizeof(buf), 313);
assert(t == NULL);
fail_snprintf = 0;
fail_strtoll = 1;
val = sc_size_to_bytes("10kb");
assert(val == -1);
fail_strtoll = 0;
fail_strtoll = 1;
fail_strtoll_errno = 100;
val = sc_size_to_bytes("10kb");
assert(val == -1);
fail_strtoll = 0;
}
#else

View File

@ -205,7 +205,8 @@ bool sc_str_append(char **str, const char *param)
len = strlen(param);
alloc = sc_str_bytes(meta->len + len);
if (alloc > SC_SIZE_MAX || (meta = sc_str_realloc(meta, alloc)) == NULL) {
if (len > SC_SIZE_MAX - meta->len ||
(meta = sc_str_realloc(meta, alloc)) == NULL) {
return false;
}

View File

@ -46,6 +46,7 @@ size_t __wrap_strlen(const char* str)
}
bool fail_vsnprintf;
int fail_vnsprintf_value = -1;
int fail_vsnprintf_at = -1;
extern int __real_vsnprintf(char *str, size_t size, const char *format,
va_list ap);
@ -56,7 +57,7 @@ int __wrap_vsnprintf(char *str, size_t size, const char *format, va_list ap)
return __real_vsnprintf(str, size, format, ap);
}
return -1;
return fail_vnsprintf_value;
}
void test1()
@ -201,6 +202,40 @@ void test2()
assert(sc_str_replace(&c, "*", "2") == false);
fail_strlen = INT32_MAX;
sc_str_destroy(c);
c = sc_str_create("n1n1");
assert(sc_str_substring(&c, -1, -3) == false);
assert(sc_str_substring(&c, 5, 4) == false);
assert(sc_str_substring(&c, 2, 6) == false);
assert(sc_str_substring(&c, 5, 7) == false);
assert(sc_str_substring(&c, 2, 1) == false);
fail_strlen = 1;
assert(sc_str_append(&c, "12") == false);
fail_strlen = UINT32_MAX;
fail_realloc = true;
assert(sc_str_append(&c, "12") == false);
fail_realloc = false;
sc_str_destroy(c);
fail_vsnprintf_at = 2;
fail_vnsprintf_value = -1;
char *tmp = malloc(1500);
for (int i =0 ; i < 1500; i++) {
tmp[i] = '3';
}
tmp[1499] = '\0';
assert(sc_str_create_fmt("%s", tmp) == NULL);
fail_vsnprintf_at = 2;
fail_vnsprintf_value = 9000;
assert(sc_str_create_fmt("%s", tmp) == NULL);
fail_vsnprintf_at = -1;
fail_vnsprintf_value = -1;
free(tmp);
}
#endif
@ -248,9 +283,21 @@ void test3()
assert(count == 4);
assert(strcmp(str, tokens) == 0);
assert(sc_str_len(str) == (int64_t) strlen(tokens));
save = NULL;
while ((token = sc_str_token_begin(str, &save, "-")) != NULL) {
}
sc_str_token_end(str, &save);
sc_str_destroy(str);
str = sc_str_create(NULL);
save = NULL;
while ((token = sc_str_token_begin(str, &save, "")) != NULL) {
assert(strcmp(token, "token") == 0);
}
sc_str_token_end(str, &save);
sc_str_destroy(str);
}
void test4()