1106686Stjr/*- 2106686Stjr * Copyright (c) 2002 Tim J. Robbins 3106686Stjr * All rights reserved. 4106686Stjr * 5106686Stjr * Redistribution and use in source and binary forms, with or without 6106686Stjr * modification, are permitted provided that the following conditions 7106686Stjr * are met: 8106686Stjr * 1. Redistributions of source code must retain the above copyright 9106686Stjr * notice, this list of conditions and the following disclaimer. 10106686Stjr * 2. Redistributions in binary form must reproduce the above copyright 11106686Stjr * notice, this list of conditions and the following disclaimer in the 12106686Stjr * documentation and/or other materials provided with the distribution. 13106686Stjr * 14106686Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15106686Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16106686Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17106686Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18106686Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19106686Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20106686Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21106686Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22106686Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23106686Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24106686Stjr * SUCH DAMAGE. 25106686Stjr */ 26106686Stjr 27106686Stjr/* 28106686Stjr * Test program for wcstombs(), as specified by IEEE Std. 1003.1-2001 and 29106686Stjr * ISO/IEC 9899:1999. 30106686Stjr * 31106686Stjr * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and 32106686Stjr * "ja_JP.eucJP". Other encodings are not tested. 33106686Stjr */ 34106686Stjr 35106686Stjr#include <sys/cdefs.h> 36106686Stjr__FBSDID("$FreeBSD$"); 37106686Stjr 38106686Stjr#include <assert.h> 39106686Stjr#include <errno.h> 40106686Stjr#include <limits.h> 41106686Stjr#include <locale.h> 42106686Stjr#include <stdio.h> 43106686Stjr#include <stdlib.h> 44106686Stjr#include <string.h> 45106686Stjr#include <wchar.h> 46106686Stjr 47106686Stjrint 48106686Stjrmain(int argc, char *argv[]) 49106686Stjr{ 50106686Stjr wchar_t srcbuf[128]; 51106686Stjr char dstbuf[128]; 52106686Stjr 53106686Stjr /* 54106686Stjr * C/POSIX locale. 55106686Stjr */ 56106686Stjr 57137587Snik printf("1..1\n"); 58137587Snik 59106686Stjr /* Simple null terminated string. */ 60106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 61106686Stjr wcscpy(srcbuf, L"hello"); 62106686Stjr memset(dstbuf, 0xcc, sizeof(dstbuf)); 63106686Stjr assert(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 5); 64106686Stjr assert(strcmp(dstbuf, "hello") == 0); 65106686Stjr assert((unsigned char)dstbuf[6] == 0xcc); 66106686Stjr 67106686Stjr /* Not enough space in destination buffer. */ 68106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 69106686Stjr wcscpy(srcbuf, L"hello"); 70106686Stjr memset(dstbuf, 0xcc, sizeof(dstbuf)); 71106686Stjr assert(wcstombs(dstbuf, srcbuf, 4) == 4); 72106686Stjr assert(memcmp(dstbuf, "hell", 4) == 0); 73106686Stjr assert((unsigned char)dstbuf[5] == 0xcc); 74106686Stjr 75106686Stjr /* Null terminated string, internal dest. buffer */ 76106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 77106686Stjr wcscpy(srcbuf, L"hello"); 78106686Stjr assert(wcstombs(NULL, srcbuf, sizeof(dstbuf)) == 5); 79106686Stjr 80106686Stjr /* Null terminated string, internal state. */ 81106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 82106686Stjr wcscpy(srcbuf, L"hello"); 83106686Stjr memset(dstbuf, 0xcc, sizeof(dstbuf)); 84106686Stjr assert(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 5); 85106686Stjr assert(strcmp(dstbuf, "hello") == 0); 86106686Stjr assert((unsigned char)dstbuf[6] == 0xcc); 87106686Stjr 88106686Stjr /* Null terminated string, internal state, internal dest. buffer. */ 89106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 90106686Stjr wcscpy(srcbuf, L"hello"); 91106686Stjr assert(wcstombs(NULL, srcbuf, 0) == 5); 92106686Stjr 93106686Stjr /* Empty source buffer. */ 94106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 95106686Stjr srcbuf[0] = L'\0'; 96106686Stjr memset(dstbuf, 0xcc, sizeof(dstbuf)); 97106686Stjr assert(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 0); 98106686Stjr assert(dstbuf[0] == L'\0'); 99106686Stjr 100106686Stjr /* Zero length destination buffer. */ 101106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 102106686Stjr wcscpy(srcbuf, L"hello"); 103106686Stjr memset(dstbuf, 0xcc, sizeof(dstbuf)); 104106686Stjr assert(wcstombs(dstbuf, srcbuf, 0) == 0); 105106686Stjr assert((unsigned char)dstbuf[0] == 0xcc); 106106686Stjr 107106686Stjr /* 108106686Stjr * Japanese (EUC) locale. 109106686Stjr */ 110106686Stjr 111106686Stjr assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); 112106686Stjr assert(MB_CUR_MAX > 1); 113106686Stjr 114106686Stjr wmemset(srcbuf, 0xcc, sizeof(srcbuf) / sizeof(*srcbuf)); 115106686Stjr srcbuf[0] = 0xA3C1; 116106686Stjr srcbuf[1] = 0x0020; 117106686Stjr srcbuf[2] = 0x0042; 118106686Stjr srcbuf[3] = 0x0020; 119106686Stjr srcbuf[4] = 0xA3C3; 120106686Stjr srcbuf[5] = 0x0000; 121106686Stjr memset(dstbuf, 0xcc, sizeof(dstbuf)); 122106686Stjr assert(wcstombs(dstbuf, srcbuf, sizeof(dstbuf)) == 7); 123106686Stjr assert(strcmp(dstbuf, "\xA3\xC1 B \xA3\xC3") == 0); 124106686Stjr assert((unsigned char)dstbuf[8] == 0xcc); 125106686Stjr 126137587Snik printf("ok 1 - wcstombs()\n"); 127106686Stjr 128106686Stjr return (0); 129106686Stjr} 130