From 71b90230c575ecd1d2f85f8fb22c7e875bca8d3a Mon Sep 17 00:00:00 2001 Message-Id: <71b90230c575ecd1d2f85f8fb22c7e875bca8d3a.1715231560.git.mdw@distorted.org.uk> From: Mark Wooding Date: Thu, 29 Nov 2007 19:19:03 +0000 Subject: [PATCH] test + doxygen for wstat() Organization: Straylight/Edgeware From: Richard Kettlewell --- lib/test.c | 42 ++++++++++++++++++++++++++++++++++++++++-- lib/wstat.c | 7 +++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/test.c b/lib/test.c index 71847ff..5aa9f57 100644 --- a/lib/test.c +++ b/lib/test.c @@ -49,6 +49,7 @@ #include "filepart.h" #include "hash.h" #include "selection.h" +#include "syscalls.h" static int tests, errors; static int fail_first; @@ -108,15 +109,31 @@ static const char *format_utf32(const uint32_t *s) { if(w == 0) { \ fprintf(stderr, "%s:%d: %s returned 0\n", \ __FILE__, __LINE__, #GOT); \ - count_error(); \ + count_error(); \ } else if(strcmp(w, g)) { \ fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \ __FILE__, __LINE__, #GOT, format(g), format(w)); \ - count_error(); \ + count_error(); \ } \ ++tests; \ } while(0) +#define check_string_prefix(GOT, WANT) do { \ + const char *g = GOT; \ + const char *w = WANT; \ + \ + if(w == 0) { \ + fprintf(stderr, "%s:%d: %s returned 0\n", \ + __FILE__, __LINE__, #GOT); \ + count_error(); \ + } else if(strncmp(w, g, strlen(w))) { \ + fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \ + __FILE__, __LINE__, #GOT, format(g), format(w)); \ + count_error(); \ + } \ + ++tests; \ + } while(0) + static uint32_t *ucs4parse(const char *s) { struct dynstr_ucs4 d; char *e; @@ -777,6 +794,26 @@ static void test_selection(void) { insist(hash_count(h) == 0); } +static void test_wstat(void) { + pid_t pid; + int w; + + fprintf(stderr, "test_wstat\n"); + if(!(pid = xfork())) { + _exit(1); + } + while(waitpid(pid, &w, 0) < 0 && errno == EINTR) + ; + check_string(wstat(w), "exited with status 1"); + if(!(pid = xfork())) { + kill(getpid(), SIGTERM); + _exit(-1); + } + while(waitpid(pid, &w, 0) < 0 && errno == EINTR) + ; + check_string_prefix(wstat(w), "terminated by signal 15"); +} + int main(void) { fail_first = !!getenv("FAIL_FIRST"); insist('\n' == 0x0A); @@ -827,6 +864,7 @@ int main(void) { test_casefold(); test_words(); /* wstat.c */ + test_wstat(); /* signame.c */ test_signame(); /* cache.c */ diff --git a/lib/wstat.c b/lib/wstat.c index 6d4cd29..0fd124c 100644 --- a/lib/wstat.c +++ b/lib/wstat.c @@ -17,6 +17,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file lib/wstat.c + * @brief Convert wait status to text + */ #include @@ -30,6 +33,10 @@ #include "wstat.h" #include "printf.h" +/** @brief Convert exit status to text + * @param w Exit status (e.g. from waitpid()) + * @return Allocated string containing description of status + */ const char *wstat(int w) { int n; char *r; -- [mdw]