X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/763d5e6ad88ef3ba1cd1d7742d060e4f1e54c6b8..ccf0aafa12134bad479faffbf56b2bc4b3e55b82:/lib/test.c diff --git a/lib/test.c b/lib/test.c index 23d6366..ed90183 100644 --- a/lib/test.c +++ b/lib/test.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder. - * Copyright (C) 2005 Richard Kettlewell + * Copyright (C) 2005, 2007 Richard Kettlewell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file lib/test.c @brief Library tests */ #include #include "types.h" @@ -26,6 +27,7 @@ #include #include #include +#include #include "utf8.h" #include "mem.h" @@ -35,11 +37,13 @@ #include "mime.h" #include "hex.h" #include "words.h" +#include "heap.h" static int tests, errors; +/** @brief Checks that @p expr is nonzero */ #define insist(expr) do { \ - if(!expr) { \ + if(!(expr)) { \ ++errors; \ fprintf(stderr, "%s:%d: error checking %s\n", \ __FILE__, __LINE__, #expr); \ @@ -113,6 +117,8 @@ static void test_utf8(void) { insist(!strcmp(u8, CHARS)); \ } while(0) + fprintf(stderr, "test_utf8\n"); + /* empty string */ U8("", ""); @@ -210,6 +216,8 @@ static void test_utf8(void) { static void test_mime(void) { char *t, *n, *v; + fprintf(stderr, "test_mime\n"); + t = n = v = 0; insist(!mime_content_type("text/plain", &t, &n, &v)); insist(!strcmp(t, "text/plain")); @@ -279,6 +287,8 @@ static void test_hex(void) { uint8_t *u; size_t ul; + fprintf(stderr, "test_hex\n"); + for(n = 0; n <= UCHAR_MAX; ++n) { if(!isxdigit(n)) insist(unhexdigitq(n) == -1); @@ -315,15 +325,18 @@ static void test_hex(void) { insist(memcmp(u, h, 4) == 0); u = unhex("", &ul); insist(ul == 0); - fprintf(stderr, "2 ERROR reports expected:\n"); + fprintf(stderr, "2 ERROR reports expected {\n"); insist(unhex("F", 0) == 0); insist(unhex("az", 0) == 0); + fprintf(stderr, "}\n"); } static void test_casefold(void) { uint32_t c, l, u[2]; const char *s, *ls; + fprintf(stderr, "test_casefold\n"); + for(c = 1; c < 256; ++c) { u[0] = c; u[1] = 0; @@ -361,6 +374,34 @@ static void test_casefold(void) { check_string(casefold(""), ""); } +/** @brief Less-than comparison function for integer heap */ +static inline int int_lt(int a, int b) { return a < b; } + +/** @struct iheap + * @brief A heap with @c int elements */ +HEAP_TYPE(iheap, int, int_lt); + +/** @brief Tests for @ref heap.h */ +static void test_heap(void) { + struct iheap h[1]; + int n; + int last = -1; + + fprintf(stderr, "test_heap\n"); + + iheap_init(h); + for(n = 0; n < 1000; ++n) + iheap_insert(h, random() % 100); + for(n = 0; n < 1000; ++n) { + const int latest = iheap_remove(h); + if(last > latest) + fprintf(stderr, "should have %d <= %d\n", last, latest); + insist(last <= latest); + last = latest; + } + putchar('\n'); +} + int main(void) { insist('\n' == 0x0A); insist('\r' == 0x0D); @@ -380,6 +421,8 @@ int main(void) { /* configuration.c */ /* event.c */ /* fprintf.c */ + /* heap.c */ + test_heap(); /* hex.c */ test_hex(); /* inputline.c */