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 wcsrtombs112testing wcsrtombs213testing wcstombs14done15end:16*/1718#define NELEM(x) (sizeof(x)/sizeof(x[0]))1920static char str[20];21static wchar_t wcs[20];2223static struct wcstests {24 char *s;25 char *sexp;2627 wchar_t *wcs;28 wchar_t *wcsexp;2930 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},4041 {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},4546 {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},4950 {str, "\x31", (wchar_t[]) {0x31, 0xD800}, wcs, 20,-1, EILSEQ, 0},51};5253static void54tests_wcsrtombs(void)55{56 size_t r;57 const wchar_t *wc;58 mbstate_t st;59 struct wcstests *tp;6061 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));6667 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 }7677 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));8384 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 }9495}9697static void98tests_wcstombs(void)99{100 size_t r;101 wchar_t *wc;102 struct wcstests *tp;103104 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));109110 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}116117int118main(void)119{120 puts("testing");121 tests_wcsrtombs();122 tests_wcstombs();123 puts("done");124 return 0;125}