0043-wcsrtombs.c (2698B)
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 wcsrtombs1 12 testing wcsrtombs2 13 testing wcstombs 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 23 static struct wcstests { 24 char *s; 25 char *sexp; 26 27 wchar_t *wcs; 28 wchar_t *wcsexp; 29 30 size_t n; 31 int r; 32 int syserr; 33 int mbstate; 34 } tests [] = { 35 /* s sexp wcs wcsexp n r syserr mbstate */ 36 {str, "\0", (wchar_t[]) {0}, NULL, 1, 0, 0, 1}, 37 {str, "\0", (wchar_t[]) {0}, wcs, 0, 0, 0, 1}, 38 {NULL, NULL, (wchar_t[]) {0}, NULL, 1, 0, 0, 1}, 39 {NULL, NULL, (wchar_t[]) {0}, NULL, 0, 0, 0, 1}, 40 41 {str, "\x31", (wchar_t[]) {0x31, 0}, NULL, 2, 1, 0, 1}, 42 {str, "\x31", (wchar_t[]) {0x31, 0}, wcs+1, 1, 1, 0, 1}, 43 {NULL, NULL, (wchar_t[]) {0x31, 0}, NULL, 1, 1, 0, 1}, 44 {NULL, NULL, (wchar_t[]) {0x31, 0}, NULL, 0, 1, 0, 1}, 45 46 {str, 47 "\x21\xc2\xa1\xe2\x80\x94\xf0\x9f\x92\xa9", 48 (wchar_t[]) {0x21,0xa1,0x2014,0x1f4A9, 0},NULL, 20,10, 0, 1}, 49 50 {str, "\x31", (wchar_t[]) {0x31, 0xD800}, wcs, 20,-1, EILSEQ, 0}, 51 }; 52 53 static void 54 tests_wcsrtombs(void) 55 { 56 size_t r; 57 wchar_t *wc; 58 mbstate_t st; 59 struct wcstests *tp; 60 61 puts("testing wcsrtombs1"); 62 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) { 63 errno = 0; 64 wc = wcscpy(wcs, tp->wcs); 65 memset(str, -1, sizeof(str)); 66 67 r = wcsrtombs(tp->s, &wc, tp->n, NULL); 68 assert(tp->r == r); 69 assert(tp->syserr == errno); 70 if (tp->r >= 0) { 71 assert(wc == tp->wcsexp); 72 if (tp->s) 73 assert(!strncmp(tp->sexp, tp->s, r)); 74 } 75 } 76 77 puts("testing wcsrtombs2"); 78 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) { 79 errno = 0; 80 wc = wcscpy(wcs, tp->wcs); 81 memset(str, -1, sizeof(str)); 82 memset(&st, 0, sizeof(st)); 83 84 r = wcsrtombs(tp->s, &wc, tp->n, &st); 85 assert(tp->r == r); 86 assert(tp->syserr == errno); 87 if (tp->r >= 0) { 88 assert(wc == tp->wcsexp); 89 if (tp->s) 90 assert(!strncmp(tp->sexp, tp->s, r)); 91 assert(mbsinit(&st) != 0 == tp->mbstate); 92 } 93 } 94 95 } 96 97 static void 98 tests_wcstombs(void) 99 { 100 size_t r; 101 wchar_t *wc; 102 struct wcstests *tp; 103 104 puts("testing wcstombs"); 105 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) { 106 errno = 0; 107 wc = wcscpy(wcs, tp->wcs); 108 memset(str, -1, sizeof(str)); 109 110 r = wcstombs(tp->s, wc, tp->n); 111 assert(tp->r == r); 112 if (tp->r >= 0 && tp->s) 113 assert(!strncmp(tp->sexp, tp->s, r)); 114 } 115 } 116 117 int 118 main(void) 119 { 120 puts("testing"); 121 tests_wcsrtombs(); 122 tests_wcstombs(); 123 puts("done"); 124 return 0; 125 }