1279377Simp/*- 2279377Simp * Copyright (c) 2002-2004 Tim J. Robbins 3279377Simp * All rights reserved. 4279377Simp * 5279377Simp * Redistribution and use in source and binary forms, with or without 6279377Simp * modification, are permitted provided that the following conditions 7279377Simp * are met: 8279377Simp * 1. Redistributions of source code must retain the above copyright 9279377Simp * notice, this list of conditions and the following disclaimer. 10279377Simp * 2. Redistributions in binary form must reproduce the above copyright 11279377Simp * notice, this list of conditions and the following disclaimer in the 12279377Simp * documentation and/or other materials provided with the distribution. 13279377Simp * 14279377Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17279377Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24279377Simp * SUCH DAMAGE. 25279377Simp */ 26279377Simp 27279377Simp/* 28279377Simp * Test program for mbtowc(), as specified by IEEE Std. 1003.1-2001 and 29279377Simp * ISO/IEC 9899:1990. 30279377Simp * 31279377Simp * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and 32279377Simp * "ja_JP.eucJP". Other encodings are not tested. 33279377Simp */ 34279377Simp 35279377Simp#include <sys/cdefs.h> 36279377Simp__FBSDID("$FreeBSD$"); 37279377Simp 38279377Simp#include <assert.h> 39279377Simp#include <limits.h> 40279377Simp#include <locale.h> 41279377Simp#include <stdio.h> 42279377Simp#include <stdlib.h> 43279377Simp#include <string.h> 44279377Simp 45279377Simpint 46279377Simpmain(int argc, char *argv[]) 47279377Simp{ 48279377Simp size_t len; 49279377Simp wchar_t wc; 50279377Simp char buf[MB_LEN_MAX + 1]; 51279377Simp 52279377Simp /* 53279377Simp * C/POSIX locale. 54279377Simp */ 55279377Simp 56279377Simp printf("1..1\n"); 57279377Simp 58279377Simp assert(MB_CUR_MAX == 1); 59279377Simp 60279377Simp /* No shift states in C locale. */ 61279377Simp assert(mbtowc(NULL, NULL, 0) == 0); 62279377Simp 63279377Simp /* Null wide character. */ 64279377Simp wc = 0xcccc; 65279377Simp memset(buf, 0, sizeof(buf)); 66279377Simp assert(mbtowc(&wc, buf, 1) == 0); 67279377Simp assert(wc == 0); 68279377Simp 69279377Simp /* Latin letter A. */ 70279377Simp buf[0] = 'A'; 71279377Simp assert(mbtowc(&wc, buf, 1) == 1); 72279377Simp assert(wc == L'A'); 73279377Simp 74279377Simp /* Incomplete character sequence. */ 75279377Simp wc = L'z'; 76279377Simp buf[0] = '\0'; 77279377Simp assert(mbtowc(&wc, buf, 0) == -1); 78279377Simp assert(wc == L'z'); 79279377Simp assert(mbtowc(NULL, NULL, 0) == 0); 80279377Simp 81279377Simp /* 82279377Simp * Japanese (EUC) locale. 83279377Simp */ 84279377Simp 85279377Simp assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); 86279377Simp assert(MB_CUR_MAX > 1); 87279377Simp 88279377Simp /* Null wide character */ 89279377Simp memset(buf, 0xcc, sizeof(buf)); 90295436Sandrew buf[0] = 0; 91295436Sandrew wc = 0xcccc; 92295436Sandrew assert(mbtowc(&wc, buf, 1) == 0); 93295436Sandrew assert(wc == 0); 94295436Sandrew 95295436Sandrew /* Latin letter A. */ 96295436Sandrew buf[0] = 'A'; 97295436Sandrew assert(mbtowc(&wc, buf, 1) == 1); 98295436Sandrew assert(wc == L'A'); 99295436Sandrew 100295436Sandrew /* Incomplete character sequence (zero length). */ 101295436Sandrew wc = L'z'; 102295436Sandrew buf[0] = '\0'; 103295436Sandrew assert(mbtowc(&wc, buf, 0) == -1); 104295436Sandrew assert(wc == L'z'); 105295436Sandrew assert(mbtowc(NULL, NULL, 0) == 0); 106295436Sandrew 107295436Sandrew /* Incomplete character sequence (truncated double-byte). */ 108295436Sandrew memset(buf, 0xcc, sizeof(buf)); 109295436Sandrew buf[0] = 0xa3; 110295436Sandrew buf[1] = 0x00; 111295436Sandrew wc = L'z'; 112295436Sandrew assert(mbtowc(&wc, buf, 1) == -1); 113295436Sandrew assert(wc == L'z'); 114295436Sandrew assert(mbtowc(NULL, NULL, 0) == 0); 115295436Sandrew 116295436Sandrew /* Same as above, but complete. */ 117295436Sandrew buf[1] = 0xc1; 118295436Sandrew assert(mbtowc(&wc, buf, 2) == 2); 119295436Sandrew assert(wc == 0xa3c1); 120295436Sandrew 121295436Sandrew printf("ok 1 - mbtowc()\n"); 122295436Sandrew 123295436Sandrew return (0); 124295436Sandrew} 125295436Sandrew