new test framework (one main per test)
[libc-test] / src / multibyte / mbc.c
index 6b61806..56c546b 100644 (file)
@@ -6,80 +6,80 @@
 #include <langinfo.h>
 #include "test.h"
 
-/* r = place to store result
+/*
  * f = function call to test (or any expression)
  * x = expected result
- * m = message to print on failure (with formats for r & x)
-**/
-
-#define TEST(r, f, x, m) ( \
+ * m = message to print on failure
+ */
+#define T(f, x, m) (void)( \
        memset(&st, 0, sizeof st), \
-       ((r) = (f)) == (x) || \
-               (error("%s failed (" m ")\n", #f, r, x), 0) )
-
-#define TEST_S(s, x, m) ( \
-       !strcmp((s),(x)) || \
-               (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
+       (i = (f)) == (x) || \
+               error("%s failed (%s) got %d want %d\n", #f, m, i, x) )
+#define TCHAR(f, x, m) (void)( \
+       memset(&st, 0, sizeof st), \
+       (i = (f)) == (x) || \
+               error("%s failed (%s) got 0x%04x want 0x%04x\n", #f, m, i, x) )
 
-void test_mbc(void) {
+int main(void)
+{
        const char *cs;
        int i;
        mbstate_t st, st2;
        wchar_t wc, wcs[32];
-       
+
+       (void)(
        setlocale(LC_CTYPE, "en_US.UTF-8") ||
        setlocale(LC_CTYPE, "en_GB.UTF-8") ||
        setlocale(LC_CTYPE, "en.UTF-8") ||
        setlocale(LC_CTYPE, "POSIX.UTF-8") ||
        setlocale(LC_CTYPE, "C.UTF-8") ||
        setlocale(LC_CTYPE, "UTF-8") ||
-       setlocale(LC_CTYPE, "");
+       setlocale(LC_CTYPE, "") );
+
+       T(mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len");
+       T(mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len");
+       T(mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs");
 
-       TEST(i, mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len, %d != %d");
-       TEST(i, mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len, %d != %d");
-       TEST(i, mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs, %d != %d");
+       if (strcmp(nl_langinfo(CODESET), "UTF-8"))
+               return error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET));
 
-       if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
-               error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET));
-               return;
-       }
-       
-       TEST(i, mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error %d != %d");
-       TEST(i, mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial, %d != %d");
+       T(mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error");
+       T(mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial");
 
-       TEST(i, mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul %d != %d");
-       TEST(i, mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash %d != %d");
-       TEST(i, mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash %d != %d");
-       TEST(i, mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash %d != %d");
-       TEST(i, mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash %d != %d");
-       TEST(i, mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash %d != %d");
-       TEST(i, mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080 %d != %d");
-       TEST(i, mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF %d != %d");
-       TEST(i, mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800 %d != %d");
-       TEST(i, mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD %d != %d");
+       T(mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul");
+       T(mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash");
+       T(mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash");
+       T(mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash");
+       T(mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash");
+       T(mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash");
+       T(mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080");
+       T(mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF");
+       T(mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800");
+       T(mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD");
 
-       TEST(i, mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate, %d != %d");
-       TEST(i, mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE, %d != %d");
-       TEST(i, mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF, %d != %d");
-       TEST(i, mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE, %d != %d");
-       TEST(i, mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF, %d != %d");
+       T(mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate");
+       T(mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE");
+       T(mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF");
+       T(mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE");
+       T(mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF");
 
-       TEST(i, mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length %d != %d");
-       TEST(i, (mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char %04x != %04x");
-       TEST(i, mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length %d != %d");
-       TEST(i, (mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char %04x != %04x");
-       TEST(i, mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length %d != %d");
-       TEST(i, (mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char %04x != %04x");
+       T(mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length");
+       TCHAR((mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char");
+       T(mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length");
+       TCHAR((mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char");
+       T(mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length");
+       TCHAR((mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char");
 
        memset(&st2, 0, sizeof st2);
-       TEST(i, mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte, %d != %d");
-       TEST(i, mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume, %d != %d");
-       TEST(i, wc, 0x80, "wrong char %04x != %04x");
+       T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte");
+       T(mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume");
+       TCHAR(wc, 0x80, "wrong char");
 
        memset(&st2, 0, sizeof st2);
-       TEST(i, mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte, %d != %d");
-       TEST(i, mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume, %d != %d");
-       TEST(i, wcs[0], 0xa0, "wrong char %04x != %04x");
-       TEST(i, wcs[1], 'a', "wrong char %04x != %04x");
-       TEST(i, !cs, 1, "wrong final position %d != %d");
+       T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte");
+       T(mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume");
+       TCHAR(wcs[0], 0xa0, "wrong char");
+       TCHAR(wcs[1], 'a', "wrong char");
+       T(!cs, 1, "wrong final position");
+       return test_status;
 }