1#include <assert.h>2#include <errno.h>3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6#include <wchar.h>78/*9output:10testing11testing wcrtomb112testing wcrtomb213testing wctomb14done15end:16*/1718#define NELEM(x) (sizeof(x)/sizeof(x[0]))1920static char str[MB_CUR_MAX+1];2122static struct wctest {23 wchar_t wc;24 char *s;25 char exp[MB_CUR_MAX+1];26 int r;27 int syserr;28 int mbstate;29} tests[] = {30 {0, NULL, "", 1, 0, 1},31 {0, str, "\0", 1, 0, 1},32 {0x21, str, "\x21", 1, 0, 1},33 {0x00A1, str, "\xc2\xa1", 2, 0, 1},34 {0x2014, str, "\xe2\x80\x94", 3, 0, 1},35 {0x01F4A9, str, "\xf0\x9f\x92\xa9", 4, 0, 1},3637 {0xd800, str, "", -1, EILSEQ, 1},38 {0xDCFF, str, "", -1, EILSEQ, 1},39 {0xDD00, str, "\xed\xb4\x80", 3, 0, 1},40 {0x10ffff, str, "\xf4\x8f\xbf\xbf", 4, 0, 1},41 {0x110000, str, "", -1, EILSEQ, 1},42};4344void45tests_wcrtomb(void)46{47 struct wctest *tp;48 int r;49 mbstate_t s;5051 puts("testing wcrtomb1");52 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {53 memset(str, 0, MB_CUR_MAX+1);54 errno = 0;55 r = wcrtomb(tp->s, tp->wc, NULL);56 assert(tp->r == r);57 assert(tp->syserr == errno);58 if (tp->s && tp->r != -1)59 assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));60 }6162 puts("testing wcrtomb2");63 memset(&s, 0, sizeof(s));64 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {65 memset(str, 0, MB_CUR_MAX+1);66 errno = 0;67 r = wcrtomb(tp->s, tp->wc, &s);68 assert(tp->r == r);69 assert(tp->syserr == errno);70 if (tp->s && tp->r != -1)71 assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));72 assert(mbsinit(&s) != 0 == tp->mbstate);73 }74}7576void77tests_wctomb(void)78{79 struct wctest *tp;80 int r;8182 puts("testing wctomb");83 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {84 memset(str, 0, MB_CUR_MAX+1);85 errno = 0;86 r = wctomb(tp->s, tp->wc);87 assert(tp->r == r);88 if (tp->s && tp->r != -1)89 assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));90 }91}9293int94main(void)95{96 puts("testing");97 tests_wcrtomb();98 tests_wctomb();99 puts("done");100 return 0;101}