1 // Copyright (c) 2015 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 // Encode every code-point using this module and verify that it matches the
22 // encoding generated using Windows WideCharToMultiByte.
24 #include "UnicodeEncoding.h"
32 static void correctnessByCode()
37 for (unsigned int code = 0; code < 0x110000; ++code) {
39 // Surrogate pair reserved region.
40 const bool isReserved = (code >= 0xD800 && code <= 0xDFFF);
42 int mblen1 = encodeUtf8(mbstr1, code);
43 if (isReserved ? mblen1 != 0 : mblen1 <= 0) {
44 printf("Error: 0x%04X: mblen1=%d\n", code, mblen1);
48 int wlen = encodeUtf16(wch, code);
49 if (isReserved ? wlen != 0 : wlen <= 0) {
50 printf("Error: 0x%04X: wlen=%d\n", code, wlen);
58 if (mblen1 != utf8CharLength(mbstr1[0])) {
59 printf("Error: 0x%04X: mblen1=%d, utf8CharLength(mbstr1[0])=%d\n",
60 code, mblen1, utf8CharLength(mbstr1[0]));
64 if (code != decodeUtf8(mbstr1)) {
65 printf("Error: 0x%04X: decodeUtf8(mbstr1)=%u\n",
66 code, decodeUtf8(mbstr1));
70 int mblen2 = WideCharToMultiByte(CP_UTF8, 0, wch, wlen, mbstr2, 4, NULL, NULL);
71 if (mblen1 != mblen2) {
72 printf("Error: 0x%04X: mblen1=%d, mblen2=%d\n", code, mblen1, mblen2);
76 if (memcmp(mbstr1, mbstr2, mblen1) != 0) {
77 printf("Error: 0x%04x: encodings are different\n", code);
83 static const char *encodingStr(char (&output)[128], char (&buf)[4])
85 sprintf(output, "Encoding %02X %02X %02X %02X",
86 static_cast<uint8_t>(buf[0]),
87 static_cast<uint8_t>(buf[1]),
88 static_cast<uint8_t>(buf[2]),
89 static_cast<uint8_t>(buf[3]));
93 // This test can take a couple of minutes to run.
94 static void correctnessByUtf8Encoding()
96 for (uint64_t encoding = 0; encoding <= 0xFFFFFFFF; ++encoding) {
100 mb[1] = encoding >> 8;
101 mb[2] = encoding >> 16;
102 mb[3] = encoding >> 24;
104 const int mblen = utf8CharLength(mb[0]);
110 const uint32_t code1 = decodeUtf8(mb);
112 const int wslen1 = encodeUtf16(ws1, code1);
114 // Test using Windows. We can't decode a codepoint directly; we have
115 // to do UTF8->UTF16, then decode the surrogate pair.
117 const int wslen2 = MultiByteToWideChar(
118 CP_UTF8, MB_ERR_INVALID_CHARS, mb, mblen, ws2, 2);
119 const uint32_t code2 =
120 (wslen2 == 1 ? ws2[0] :
121 wslen2 == 2 ? decodeSurrogatePair(ws2[0], ws2[1]) :
122 static_cast<uint32_t>(-1));
124 // Verify that the two implementations match.
126 if (code1 != code2) {
127 printf("%s: code1=0x%04x code2=0x%04x\n",
128 encodingStr(prefix, mb),
132 if (wslen1 != wslen2) {
133 printf("%s: wslen1=%d wslen2=%d\n",
134 encodingStr(prefix, mb),
138 if (memcmp(ws1, ws2, wslen1 * sizeof(wchar_t)) != 0) {
139 printf("%s: ws1 != ws2\n", encodingStr(prefix, mb));
145 wchar_t g_wch_TEST[] = { 0xD840, 0xDC00 };
147 wchar_t *volatile g_pwch = g_wch_TEST;
148 char *volatile g_pch = g_ch_TEST;
149 unsigned int volatile g_code = 0xA2000;
151 static void performance()
154 clock_t start = clock();
155 for (long long i = 0; i < 250000000LL; ++i) {
156 int mblen = WideCharToMultiByte(CP_UTF8, 0, g_pwch, 2, g_pch, 4, NULL, NULL);
159 clock_t stop = clock();
160 printf("%.3fns per char\n", (double)(stop - start) / CLOCKS_PER_SEC * 4.0);
164 clock_t start = clock();
165 for (long long i = 0; i < 3000000000LL; ++i) {
166 int mblen = encodeUtf8(g_pch, g_code);
169 clock_t stop = clock();
170 printf("%.3fns per char\n", (double)(stop - start) / CLOCKS_PER_SEC / 3.0);
176 printf("Testing correctnessByCode...\n");
180 printf("Testing correctnessByUtf8Encoding... (may take a couple minutes)\n");
182 correctnessByUtf8Encoding();
184 printf("Testing performance...\n");