X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fasprintf%2Ftest%2Frun.c;fp=ccan%2Fasprintf%2Ftest%2Frun.c;h=64c9990aedfd84c7f87691bc68a1880b279cea10;hb=fb1dfd092940905883ea6473162f5f6e36624da2;hp=0000000000000000000000000000000000000000;hpb=5079b95c1121e7a8e5b0b18b094ec7d8a539c7c7;p=ccan diff --git a/ccan/asprintf/test/run.c b/ccan/asprintf/test/run.c new file mode 100644 index 00000000..64c9990a --- /dev/null +++ b/ccan/asprintf/test/run.c @@ -0,0 +1,73 @@ +#include +/* Include the C files directly. */ + +/* Override vasprintf for testing. */ +#if HAVE_ASPRINTF +#define vasprintf my_vasprintf +static int my_vasprintf(char **strp, const char *fmt, va_list ap); +#else +#include +#include +#define vsnprintf my_vsnprintf +static int my_vsnprintf(char *str, size_t size, const char *format, va_list ap); +#endif + +#include +#include +#include +#include +#include + +static bool fail; + +#if HAVE_ASPRINTF +#undef vasprintf +static int my_vasprintf(char **strp, const char *fmt, va_list ap) +{ + if (fail) { + /* Set strp to crap. */ + *strp = (char *)(long)1; + return -1; + } + return vasprintf(strp, fmt, ap); +} +#else +#undef vsnprintf +static int my_vsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + if (fail) { + return -1; + } + return vsnprintf(str, size, format, ap); +} +#endif + +int main(void) +{ + char *p, nul = '\0'; + int ret; + + /* This is how many tests you plan to run */ + plan_tests(8); + + fail = false; + p = afmt("Test %u%cafter-nul", 1, nul); + ok1(p); + ok1(strlen(p) == strlen("Test 1")); + ok1(memcmp(p, "Test 1\0after-nul\0", 17) == 0); + free(p); + + ret = asprintf(&p, "Test %u%cafter-nul", 1, nul); + ok1(ret == 16); + ok1(p); + ok1(strlen(p) == strlen("Test 1")); + ok1(memcmp(p, "Test 1\0after-nul\0", 17) == 0); + free(p); + + fail = true; + p = afmt("Test %u%cafter-nul", 1, nul); + ok1(p == NULL); + + /* This exits depending on whether all tests passed */ + return exit_status(); +}