scc

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

0042-mbsrtowcs.c (2714B)


      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 mbsrtowcs1
     12 testing mbsrtowcs2
     13 testing mbstowcs
     14 done
     15 end:
     16 */
     17 
     18 #define NELEM(x) (sizeof(x)/sizeof(x[0]))
     19 
     20 static char str[20];
     21 static wchar_t wcs[20];
     22 static struct mbstests {
     23 	char *s;
     24 	char *sexp;
     25 
     26 	wchar_t *wcs;
     27 	wchar_t *wcsexp;
     28 
     29 	size_t n;
     30 	int r;
     31 	int syserr;
     32 	int mbstate;
     33 } tests[] = {
     34 	/* s       sexp    wcs  wcsexp                      n  r  syserr  mbstate */
     35 	{"\0",     NULL,   wcs, (wchar_t[]) {0},            1, 0,      0,       1},
     36 	{"\0",      str,   wcs, (wchar_t[]) {0},            0, 0,      0,       1},
     37 	{"\0",      str,  NULL, (wchar_t[]) {0},            1, 0,      0,       1},
     38 	{"\0",      str,  NULL, (wchar_t[]) {0},            0, 0,      0,       1},
     39 
     40 	{"\x31",   NULL,   wcs, (wchar_t[]) {0x31, 0},      2, 1,      0,       1},
     41 	{"\x31",  str+1,   wcs, (wchar_t[]) {0x31, 0},      1, 1,      0,       1},
     42 	{"\x31",    str,  NULL, (wchar_t[]) {0x31, 0},      1, 1,      0,       1},
     43 	{"\x31",    str,  NULL, (wchar_t[]) {0x31, 0},      0, 1,      0,       1},
     44 
     45 	{"\x21\xc2\xa1\xe2\x80\x94\xf0\x9f\x92\xa9",
     46                NULL,   wcs,
     47                (wchar_t[]) {0x21,0xa1,0x2014,0x1f4A9}, 20, 4,      0,       1},
     48 
     49 	{"\xf0\x9f",str,  wcs, NULL,                       20,-1, EILSEQ,       0},
     50 };
     51 
     52 void
     53 tests_mbsrtowcs(void)
     54 {
     55 	int r;
     56 	const char *s;
     57 	mbstate_t st;
     58 	struct mbstests *tp;
     59 
     60 	puts("testing mbsrtowcs1");
     61 	for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
     62 		errno = 0;
     63 		s = strcpy(str, tp->s);			
     64 		memset(wcs, -1, sizeof(wcs));
     65 
     66 		r = mbsrtowcs(tp->wcs, &s, tp->n, NULL);
     67 		assert(tp->r == r);
     68 		assert(tp->syserr == errno);
     69 		if (tp->r >= 0) {
     70 			assert(s == tp->sexp);
     71 			if (tp->wcs)
     72 				assert(!wcsncmp(tp->wcsexp, tp->wcs, tp->r));
     73 		}
     74 	}
     75 
     76 	puts("testing mbsrtowcs2");
     77 	for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
     78 		errno = 0;
     79 		s = strcpy(str, tp->s);			
     80 		memset(wcs, -1, sizeof(wcs));
     81 		memset(&st, 0, sizeof(st));
     82 
     83 		r = mbsrtowcs(tp->wcs, &s, tp->n, &st);
     84 		assert(tp->r == r);
     85 		assert(tp->syserr == errno);
     86 		if (tp->r >= 0) {
     87 			assert(s == tp->sexp);
     88 			if (tp->wcs)
     89 				assert(!wcsncmp(tp->wcsexp, tp->wcs, tp->r));
     90 			assert(mbsinit(&st) != 0 == tp->mbstate);
     91 		}
     92 	}
     93 }
     94 
     95 void
     96 tests_mbstowcs(void)
     97 {
     98 	int r;
     99 	struct mbstests *tp;
    100 
    101 	puts("testing mbstowcs");
    102 	for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
    103 		errno = 0;
    104 		memset(wcs, -1, sizeof(wcs));
    105 
    106 		r = mbstowcs(tp->wcs, tp->s, tp->n);
    107 		assert(tp->r == r);
    108 		if (tp->r >= 0 && tp->wcs)
    109 			assert(!wcsncmp(tp->wcsexp, tp->wcs, tp->r));
    110 	}
    111 }
    112 
    113 int
    114 main(void)
    115 {
    116 	puts("testing");
    117 	tests_mbsrtowcs();
    118 	tests_mbstowcs();
    119 	puts("done");
    120 	return 0;
    121 }