charset_test.c
资源名称:tcpmp.rar [点击查看]
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:9k
源码类别:
Windows CE
开发平台:
C/C++
- /*
- * Copyright (C) 2001 Edmund Grimley Evans <edmundo@rano.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include <assert.h>
- #include <string.h>
- #include "charset.h"
- void test_any(struct charset *charset)
- {
- int wc;
- char s[2];
- assert(charset);
- /* Decoder */
- assert(charset_mbtowc(charset, 0, 0, 0) == 0);
- assert(charset_mbtowc(charset, 0, 0, 1) == 0);
- assert(charset_mbtowc(charset, 0, (char *)(-1), 0) == 0);
- assert(charset_mbtowc(charset, 0, "a", 0) == 0);
- assert(charset_mbtowc(charset, 0, "", 1) == 0);
- assert(charset_mbtowc(charset, 0, "b", 1) == 1);
- assert(charset_mbtowc(charset, 0, "", 2) == 0);
- assert(charset_mbtowc(charset, 0, "c", 2) == 1);
- wc = 'x';
- assert(charset_mbtowc(charset, &wc, "a", 0) == 0 && wc == 'x');
- assert(charset_mbtowc(charset, &wc, "", 1) == 0 && wc == 0);
- assert(charset_mbtowc(charset, &wc, "b", 1) == 1 && wc == 'b');
- assert(charset_mbtowc(charset, &wc, "", 2) == 0 && wc == 0);
- assert(charset_mbtowc(charset, &wc, "c", 2) == 1 && wc == 'c');
- /* Encoder */
- assert(charset_wctomb(charset, 0, 0) == 0);
- s[0] = s[1] = '.';
- assert(charset_wctomb(charset, s, 0) == 1 &&
- s[0] == ' ' && s[1] == '.');
- assert(charset_wctomb(charset, s, 'x') == 1 &&
- s[0] == 'x' && s[1] == '.');
- }
- void test_utf8()
- {
- struct charset *charset;
- int wc;
- char s[8];
- charset = charset_find("UTF-8");
- test_any(charset);
- /* Decoder */
- wc = 0;
- assert(charset_mbtowc(charset, &wc, "177", 1) == 1 && wc == 127);
- assert(charset_mbtowc(charset, &wc, "200", 2) == -1);
- assert(charset_mbtowc(charset, &wc, "301277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "302200", 1) == -1);
- assert(charset_mbtowc(charset, &wc, "302200", 2) == 2 && wc == 128);
- assert(charset_mbtowc(charset, &wc, "302200", 3) == 2 && wc == 128);
- assert(charset_mbtowc(charset, &wc, "340237200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "340240200", 9) == 3 &&
- wc == 1 << 11);
- assert(charset_mbtowc(charset, &wc, "360217277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360220200200", 9) == 4 &&
- wc == 1 << 16);
- assert(charset_mbtowc(charset, &wc, "370207277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "370210200200200", 9) == 5 &&
- wc == 1 << 21);
- assert(charset_mbtowc(charset, &wc, "374203277277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "374204200200200200", 9) == 6 &&
- wc == 1 << 26);
- assert(charset_mbtowc(charset, &wc, "375277277277277277", 9) == 6 &&
- wc == 0x7fffffff);
- assert(charset_mbtowc(charset, &wc, "302 00", 2) == -1);
- assert(charset_mbtowc(charset, &wc, "302300", 2) == -1);
- assert(charset_mbtowc(charset, &wc, "340 40200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "340340200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "340240 00", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "340240300", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360 20200200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360320200200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360220 00200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360220300200", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360220200 00", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "360220200300", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375 77277277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375377277277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375277 77277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375277377277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375277277277 77277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375277277277377277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375277277277277 77", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "375277277277277377", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "376277277277277277", 9) == -1);
- assert(charset_mbtowc(charset, &wc, "377277277277277277", 9) == -1);
- /* Encoder */
- strcpy(s, ".......");
- assert(charset_wctomb(charset, s, 1 << 31) == -1 &&
- !strcmp(s, "......."));
- assert(charset_wctomb(charset, s, 127) == 1 &&
- !strcmp(s, "177......"));
- assert(charset_wctomb(charset, s, 128) == 2 &&
- !strcmp(s, "302200....."));
- assert(charset_wctomb(charset, s, 0x7ff) == 2 &&
- !strcmp(s, "337277....."));
- assert(charset_wctomb(charset, s, 0x800) == 3 &&
- !strcmp(s, "340240200...."));
- assert(charset_wctomb(charset, s, 0xffff) == 3 &&
- !strcmp(s, "357277277...."));
- assert(charset_wctomb(charset, s, 0x10000) == 4 &&
- !strcmp(s, "360220200200..."));
- assert(charset_wctomb(charset, s, 0x1fffff) == 4 &&
- !strcmp(s, "367277277277..."));
- assert(charset_wctomb(charset, s, 0x200000) == 5 &&
- !strcmp(s, "370210200200200.."));
- assert(charset_wctomb(charset, s, 0x3ffffff) == 5 &&
- !strcmp(s, "373277277277277.."));
- assert(charset_wctomb(charset, s, 0x4000000) == 6 &&
- !strcmp(s, "374204200200200200."));
- assert(charset_wctomb(charset, s, 0x7fffffff) == 6 &&
- !strcmp(s, "375277277277277277."));
- }
- void test_ascii()
- {
- struct charset *charset;
- int wc;
- char s[3];
- charset = charset_find("us-ascii");
- test_any(charset);
- /* Decoder */
- wc = 0;
- assert(charset_mbtowc(charset, &wc, "177", 2) == 1 && wc == 127);
- assert(charset_mbtowc(charset, &wc, "200", 2) == -1);
- /* Encoder */
- strcpy(s, "..");
- assert(charset_wctomb(charset, s, 256) == -1 && !strcmp(s, ".."));
- assert(charset_wctomb(charset, s, 255) == -1);
- assert(charset_wctomb(charset, s, 128) == -1);
- assert(charset_wctomb(charset, s, 127) == 1 && !strcmp(s, "177."));
- }
- void test_iso1()
- {
- struct charset *charset;
- int wc;
- char s[3];
- charset = charset_find("iso-8859-1");
- test_any(charset);
- /* Decoder */
- wc = 0;
- assert(charset_mbtowc(charset, &wc, "302200", 9) == 1 && wc == 0xc2);
- /* Encoder */
- strcpy(s, "..");
- assert(charset_wctomb(charset, s, 256) == -1 && !strcmp(s, ".."));
- assert(charset_wctomb(charset, s, 255) == 1 && !strcmp(s, "377."));
- assert(charset_wctomb(charset, s, 128) == 1 && !strcmp(s, "200."));
- }
- void test_iso2()
- {
- struct charset *charset;
- int wc;
- char s[3];
- charset = charset_find("iso-8859-2");
- test_any(charset);
- /* Decoder */
- wc = 0;
- assert(charset_mbtowc(charset, &wc, "302200", 9) == 1 && wc == 0xc2);
- assert(charset_mbtowc(charset, &wc, "377", 2) == 1 && wc == 0x2d9);
- /* Encoder */
- strcpy(s, "..");
- assert(charset_wctomb(charset, s, 256) == -1 && !strcmp(s, ".."));
- assert(charset_wctomb(charset, s, 255) == -1 && !strcmp(s, ".."));
- assert(charset_wctomb(charset, s, 258) == 1 && !strcmp(s, "303."));
- assert(charset_wctomb(charset, s, 128) == 1 && !strcmp(s, "200."));
- }
- void test_convert()
- {
- const char *p;
- char *q, *r;
- char s[256];
- size_t n, n2;
- int i;
- p = "