scc

A fork of https://git.simple-cc.org/scc/ for Qute

git clone https://git.8pit.net/scc.git

  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/*
  9output:
 10testing
 11testing wcrtomb1
 12testing wcrtomb2
 13testing wctomb
 14done
 15end:
 16*/
 17
 18#define NELEM(x) (sizeof(x)/sizeof(x[0]))
 19
 20static char str[MB_CUR_MAX+1];
 21
 22static 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
 44void
 45tests_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
 76void
 77tests_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		if (tp->s && tp->r != -1)
 89			assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));
 90	}
 91}
 92
 93int
 94main(void)
 95{
 96	puts("testing");
 97	tests_wcrtomb();
 98	tests_wctomb();
 99	puts("done");
100	return 0;
101}