1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-24 22:59:49 +05:30

C: add test for asprintf.c

This commit is contained in:
Intel A80486DX2-66 2024-01-28 15:46:26 +03:00
parent 8cd483c79e
commit 1ec1b4a9e7
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -34,3 +34,33 @@ ssize_t asprintf(char** strp, char* format, ...) {
return result; return result;
} }
#ifdef TEST
# include <inttypes.h>
# include <math.h>
# include <stdbool.h>
# define PRINT_ASSERT(caption, test) do { \
bool test_result = (test); \
printf("[test] " caption ": %s\n", test_result ? "OK" : "failed"); \
if (test_result == false) \
exit(EXIT_FAILURE); \
} while (0);
int main(void) {
const char* world = " world";
char* output;
size_t result = asprintf(&output, "Hello%s! -- Unsigned 32-bit type is "
"constrained to lie\nwithin the range of integer values from "
"%" PRIu32 " to %" PRIu32 " inclusive.", world, 0,
(uint32_t) (powf(2.0f, 32.0f) - 1.0f));
PRINT_ASSERT("segmentation fault", true);
PRINT_ASSERT("no errors", errno == 0);
PRINT_ASSERT("zero termination test", output[result] == '\0');
printf("[asprintf (%zu)] '%s'\n", result, output);
return 0;
}
#endif