No empty .Rs/.Re
[netbsd-mini2440.git] / regress / lib / libc / locale / wctomb / test.c
blob4ca6eabcb2860e129a98ca53733d4e2d6a6275b4
1 /* $Id: test.c,v 1.3 2006/11/10 17:38:33 christos Exp $ */
3 /*-
4 * Copyright (c)2004 Citrus Project,
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <locale.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <wchar.h>
34 #include <err.h>
36 typedef size_t wcrtomb_t(char *, wchar_t, mbstate_t *);
38 wcrtomb_t _wctomb;
39 void dotest1(const char *, wcrtomb_t, wchar_t *, mbstate_t *);
41 size_t
42 _wctomb(char *s, wchar_t wc, mbstate_t *ps)
45 return wctomb(s, wc);
48 int
49 main(int argc, char *argv[])
51 wchar_t wcs[teststring_wclen + 2];
52 const char *pcs;
53 size_t sz;
54 mbstate_t st;
56 if (setlocale(LC_CTYPE, teststring_loc) == NULL)
57 exit(1);
59 pcs = teststring;
60 wcs[teststring_wclen] = L'X'; /* poison */
61 sz = mbsrtowcs(wcs, &pcs, teststring_wclen + 2, NULL);
62 if (sz != teststring_wclen)
63 exit(3);
64 if (wcs[teststring_wclen])
65 exit(4);
67 dotest1("wctomb", _wctomb, wcs, NULL);
68 memset(&st, 0, sizeof(st));
69 dotest1("wcrtomb", wcrtomb, wcs, &st);
70 dotest1("wcrtomb (internal state)", wcrtomb, wcs, NULL);
72 exit(0);
75 void
76 dotest1(const char *text, wcrtomb_t fn, wchar_t *wcs, mbstate_t *stp)
78 char *cs;
79 int ret;
80 int i;
82 cs = malloc(MB_CUR_MAX);
83 if (cs == NULL)
84 err(1, NULL);
85 printf("testing %s\n", text);
87 for (i = 0; i < teststring_wclen + 1; i++) {
88 ret = fn(cs, wcs[i], stp);
89 if (ret != teststring_mblen[i]) {
90 printf("\t[%d] %d != %d BAD\n",
91 i, ret, teststring_mblen[i]);
92 exit(5);
94 #if 0
95 printf("\t[%d] %d ok\n", i, ret);
96 #endif
98 free(cs);