scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

0040-wcrtomb.c (2157B)


      1 #include <assert.h>
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <wchar.h>
      7 
      8 /*
      9 output:
     10 testing
     11 testing wcrtomb1
     12 testing wcrtomb2
     13 testing wctomb
     14 done
     15 end:
     16 */
     17 
     18 #define NELEM(x) (sizeof(x)/sizeof(x[0]))
     19 
     20 static char str[MB_CUR_MAX+1];
     21 
     22 static 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},
     36 
     37 	{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 };
     43 
     44 void
     45 tests_wcrtomb(void)
     46 {
     47 	struct wctest *tp;
     48 	int r;
     49 	mbstate_t s;
     50 
     51 	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 	}
     61 
     62 	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 }
     75 
     76 void
     77 tests_wctomb(void)
     78 {
     79 	struct wctest *tp;
     80 	int r;
     81 
     82 	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 		assert(tp->syserr == errno);
     89 		if (tp->s && tp->r != -1)
     90 			assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));
     91 	}
     92 }
     93 
     94 int
     95 main(void)
     96 {
     97 	puts("testing");
     98 	tests_wcrtomb();
     99 	tests_wctomb();
    100 	puts("done");
    101 	return 0;
    102 }