diff options
author | Mattias Andrée <maandree@kth.se> | 2018-08-29 14:34:31 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-08-29 14:34:31 +0200 |
commit | adb6da8b949b934b2a91406a7fb470e91d8dcefc (patch) | |
tree | f2bcef69b786485644dda26b073cde9f08f4df77 /vweprintf.c | |
parent | Add tests (diff) | |
download | libsimple-adb6da8b949b934b2a91406a7fb470e91d8dcefc.tar.gz libsimple-adb6da8b949b934b2a91406a7fb470e91d8dcefc.tar.bz2 libsimple-adb6da8b949b934b2a91406a7fb470e91d8dcefc.tar.xz |
Add tests for [v]{we,e,en}printf
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'vweprintf.c')
-rw-r--r-- | vweprintf.c | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/vweprintf.c b/vweprintf.c index a46363d..491763b 100644 --- a/vweprintf.c +++ b/vweprintf.c @@ -7,7 +7,7 @@ extern char *argv0; void -libsimple_vweprintf(const char *fmt, va_list ap) /* TODO test (weprintf, enprintf, eprintf) */ +libsimple_vweprintf(const char *fmt, va_list ap) { int saved_errno = errno, r; const char *end = strchr(fmt, '\0'); @@ -60,9 +60,90 @@ libsimple_vweprintf(const char *fmt, va_list ap) /* TODO test (weprintf, enprint #else #include "test.h" +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic ignored "-Wformat-zero-length" +#endif + int main(void) { + char prefix[100]; + int i, j; + +#define T(...)\ + do {\ + stderr_n = 0;\ + if (j == 0) {\ + libsimple_weprintf(__VA_ARGS__);\ + } else if (j == 1) {\ + assert_exit(libsimple_enprintf(107, __VA_ARGS__));\ + assert(exit_status == 107);\ + } else {\ + libsimple_default_failure_exit = 108;\ + assert_exit(libsimple_eprintf(__VA_ARGS__));\ + assert(exit_status == 108);\ + }\ + } while (0) + + sprintf(prefix, "%s: ", argv0); + stderr_ok = 1; + + for (i = 0; i < 2; i++, argv0 = NULL, *prefix = '\0') { + for (j = 0; j < 3; j++) { + T("%s%s%s\n", "abc", "123", "xyz"); + assert_stderr("%s%s\n", prefix, "abc123xyz"); + T("%s%s%s\n", "abc", "123", "\n"); + assert_stderr("%s%s\n", prefix, "abc123\n"); + T("%s%s%s", "abc", "123", "xyz"); + assert_stderr("%s%s\n", prefix, "abc123xyz"); + T("%s%s%s", "abc", "123", "\n"); + assert_stderr("%s%s\n", prefix, "abc123\n"); + errno = EDOM; + T("%s%s%s:", "abc", "123", "\n"); + assert_stderr("%s%s: %s\n", prefix, "abc123\n", strerror(EDOM)); + errno = ERANGE; + T("%s%s%s:", "abc", "123", "\n"); + assert_stderr("%s%s: %s\n", prefix, "abc123\n", strerror(ERANGE)); + errno = EOVERFLOW; + T(""); + assert_stderr("%s%s\n", prefix, strerror(EOVERFLOW)); + + T("%s%s%s\n", "usage: ", "123", "xyz"); + assert_stderr("%s%s\n", prefix, "usage: 123xyz"); + T("%s%s%s\n", "usage: ", "123", "\n"); + assert_stderr("%s%s\n", prefix, "usage: 123\n"); + T("%s%s%s", "usage: ", "123", "xyz"); + assert_stderr("%s%s\n", prefix, "usage: 123xyz"); + T("%s%s%s", "usage: ", "123", "\n"); + assert_stderr("%s%s\n", prefix, "usage: 123\n"); + errno = EDOM; + T("%s%s%s:", "usage: ", "123", "\n"); + assert_stderr("%s%s: %s\n", prefix, "usage: 123\n", strerror(EDOM)); + errno = ERANGE; + T("%s%s%s:", "usage: ", "123", "\n"); + assert_stderr("%s%s: %s\n", prefix, "usage: 123\n", strerror(ERANGE)); + + T("usage: %s%s%s\n", "abc", "123", "xyz"); + assert_stderr("%s: %s\n", "usage", "abc123xyz"); + T("usage: %s%s%s\n", "abc", "123", "\n"); + assert_stderr("%s: %s\n", "usage", "abc123\n"); + T("usage: %s%s%s", "abc", "123", "xyz"); + assert_stderr("%s: %s\n", "usage", "abc123xyz"); + T("usage: %s%s%s", "abc", "123", "\n"); + assert_stderr("%s: %s\n", "usage", "abc123\n"); + errno = EDOM; + T("usage: %s%s%s:", "abc", "123", "\n"); + assert_stderr("%s: %s: %s\n", "usage", "abc123\n", strerror(EDOM)); + errno = ERANGE; + T("usage: %s%s%s:", "abc", "123", "\n"); + assert_stderr("%s: %s: %s\n", "usage", "abc123\n", strerror(ERANGE)); + T("usage: "); + assert_stderr("%s", "usage: \n"); + } + } + +#undef T + return 0; } |