1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "test_locale.hpp"
#include "test_locale_tools.hpp"
#include <boost/locale/utf.hpp>
#include <string.h>
using namespace boost::locale::utf;
char *make2(unsigned v)
{
static unsigned char buf[3] = {0};
buf[0] = 0xC0 | (v >> 6);
buf[1] = 0x80 | (v & 0x3F );
return reinterpret_cast<char*>(buf);
}
char *make3(unsigned v)
{
static unsigned char buf[4] = {0};
buf[0] = 0xE0 | ((v >> 12) ) ;
buf[1] = 0x80 | ((v >> 6) & 0x3F );
buf[2] = 0x80 | ((v >> 0) & 0x3F );
return reinterpret_cast<char*>(buf);
}
char *make4(unsigned v)
{
static unsigned char buf[5] = {0};
buf[0] = 0xF0 | ((v >> 18) ) ;
buf[1] = 0x80 | ((v >> 12) & 0x3F );
buf[2] = 0x80 | ((v >> 6) & 0x3F );
buf[3] = 0x80 | ((v >> 0) & 0x3F );
return reinterpret_cast<char*>(buf);
}
boost::uint32_t const *u32_seq(boost::uint32_t a)
{
static boost::uint32_t buf[2];
buf[0]=a;
buf[1]=0;
return buf;
}
boost::uint16_t const *u16_seq(boost::uint16_t a)
{
static boost::uint16_t buf[2];
buf[0]=a;
buf[1]=0;
return buf;
}
boost::uint16_t const *u16_seq(boost::uint16_t a,boost::uint16_t b)
{
static boost::uint16_t buf[3];
buf[0]=a;
buf[1]=b;
buf[2]=0;
return buf;
}
template<typename CharType>
void test_to(CharType const *s,unsigned codepoint)
{
CharType const *begin = s;
CharType const *end = begin;
while(*end)
end++;
typedef utf_traits<CharType> tr;
TEST(tr::max_width == 4 / sizeof(CharType));
TEST(tr::template decode<CharType const *>(begin,end) == codepoint);
if(codepoint == incomplete || codepoint != illegal)
TEST(end == begin);
if(codepoint == incomplete) {
TEST(*s== 0 || 0 < tr::trail_length(*s));
TEST(tr::trail_length(*s) + 1 > end - s);
}
if(codepoint != incomplete && codepoint != illegal) {
begin=s;
TEST(tr::is_lead(*begin));
TEST(!tr::is_trail(*begin));
begin++;
while(begin!=end) {
TEST(tr::is_trail(*begin));
TEST(!tr::is_lead(*begin));
begin++;
}
TEST(tr::width(codepoint)==end - s);
TEST(tr::trail_length(*s) == tr::width(codepoint) - 1);
begin = s;
TEST(tr::decode_valid(begin) == codepoint);
TEST(begin == end);
}
}
template<typename CharType>
void test_from(CharType const *str,unsigned codepoint)
{
CharType buf[5] = {1,1,1,1,1};
CharType *p=buf;
p = utf_traits<CharType>::template encode<CharType *>(codepoint,p);
CharType const *end = str;
while(*end)
end++;
TEST(end - str == p-buf );
TEST(*p);
*p=0;
TEST(memcmp(str,buf,sizeof(CharType) * (end-str))==0);
}
int main()
{
try {
std::cout << "Test UTF-8" << std::endl;
std::cout << "- From UTF-8" << std::endl;
std::cout << "-- Correct" << std::endl;
test_to("\x7f",0x7f);
test_to("\xc2\x80",0x80);
test_to("\xdf\xbf",0x7ff);
test_to("\xe0\xa0\x80",0x800);
test_to("\xef\xbf\xbf",0xffff);
test_to("\xf0\x90\x80\x80",0x10000);
test_to("\xf4\x8f\xbf\xbf",0x10ffff);
std::cout << "-- Too big" << std::endl;
test_to("\xf4\x9f\x80\x80",illegal); // 11 0000
test_to("\xfb\xbf\xbf\xbf",illegal); // 3ff ffff
test_to("\xf8\x90\x80\x80\x80",illegal); // 400 0000
test_to("\xfd\xbf\xbf\xbf\xbf\xbf",illegal); // 7fff ffff
std::cout << "-- Invalid length" << std::endl;
/// test that this actually works
test_to(make2(0x80),0x80);
test_to(make2(0x7ff),0x7ff);
test_to(make3(0x800),0x800);
test_to(make3(0xffff),0xffff);
test_to(make4(0x10000),0x10000);
test_to(make4(0x10ffff),0x10ffff);
test_to(make4(0x110000),illegal);
test_to(make4(0x1fffff),illegal);
test_to(make2(0),illegal);
test_to(make3(0),illegal);
test_to(make4(0),illegal);
test_to(make2(0x7f),illegal);
test_to(make3(0x7f),illegal);
test_to(make4(0x7f),illegal);
test_to(make3(0x80),illegal);
test_to(make4(0x80),illegal);
test_to(make3(0x7ff),illegal);
test_to(make4(0x7ff),illegal);
test_to(make4(0x8000),illegal);
test_to(make4(0xffff),illegal);
std::cout << "-- Invalid surrogate" << std::endl;
test_to(make3(0xd800),illegal);
test_to(make3(0xdbff),illegal);
test_to(make3(0xdc00),illegal);
test_to(make3(0xdfff),illegal);
test_to(make4(0xd800),illegal);
test_to(make4(0xdbff),illegal);
test_to(make4(0xdc00),illegal);
test_to(make4(0xdfff),illegal);
std::cout <<"-- Incomplete" << std::endl;
test_to("",incomplete);
test_to("\x80",illegal);
test_to("\xc2",incomplete);
test_to("\xdf",incomplete);
test_to("\xe0",incomplete);
test_to("\xe0\xa0",incomplete);
test_to("\xef\xbf",incomplete);
test_to("\xef",incomplete);
test_to("\xf0\x90\x80",incomplete);
test_to("\xf0\x90",incomplete);
test_to("\xf0",incomplete);
test_to("\xf4\x8f\xbf",incomplete);
test_to("\xf4\x8f",incomplete);
test_to("\xf4",incomplete);
std::cout << "- To UTF-8" << std::endl;
std::cout << "-- Test correct" << std::endl;
test_from("\x7f",0x7f);
test_from("\xc2\x80",0x80);
test_from("\xdf\xbf",0x7ff);
test_from("\xe0\xa0\x80",0x800);
test_from("\xef\xbf\xbf",0xffff);
test_from("\xf0\x90\x80\x80",0x10000);
test_from("\xf4\x8f\xbf\xbf",0x10ffff);
std::cout << "Test UTF-16" << std::endl;
std::cout << "- From UTF-16" << std::endl;
std::cout << "-- Correct" << std::endl;
test_to(u16_seq(0x10),0x10);
test_to(u16_seq(0xffff),0xffff);
test_to(u16_seq(0xD800,0xDC00),0x10000);
test_to(u16_seq(0xDBFF,0xDFFF),0x10FFFF);
std::cout << "-- Invalid surrogate" << std::endl;
test_to(u16_seq(0xDFFF),illegal);
test_to(u16_seq(0xDC00),illegal);
std::cout <<"-- Incomplete" << std::endl;
test_to(u16_seq(0),incomplete);
test_to(u16_seq(0xD800),incomplete);
test_to(u16_seq(0xDBFF),incomplete);
std::cout << "- To UTF-16" << std::endl;
std::cout << "-- Test correct" << std::endl;
test_to(u16_seq(0x10),0x10);
test_to(u16_seq(0xffff),0xffff);
test_to(u16_seq(0xD800,0xDC00),0x10000);
test_to(u16_seq(0xDBFF,0xDFFF),0x10FFFF);
std::cout << "Test UTF-32" << std::endl;
std::cout << "- From UTF-32" << std::endl;
std::cout << "-- Correct" << std::endl;
test_to(u32_seq(0x10),0x10);
test_to(u32_seq(0xffff),0xffff);
test_to(u32_seq(0x10000),0x10000);
test_to(u32_seq(0x10ffff),0x10ffff);
std::cout << "-- Invalid surrogate" << std::endl;
test_to(u32_seq(0xD800),illegal);
test_to(u32_seq(0xDBFF),illegal);
test_to(u32_seq(0xDFFF),illegal);
test_to(u32_seq(0xDC00),illegal);
test_to(u32_seq(0x110000),illegal);
std::cout <<"-- Incomplete" << std::endl;
test_to(u32_seq(0),incomplete);
std::cout << "- To UTF-32" << std::endl;
std::cout << "-- Test correct" << std::endl;
test_to(u32_seq(0x10),0x10);
test_to(u32_seq(0xffff),0xffff);
test_to(u32_seq(0x10ffff),0x10ffff);
}
catch(std::exception const &e) {
std::cerr << "Failed " << e.what() << std::endl;
return EXIT_FAILURE;
}
FINALIZE();
}
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
// boostinspect:noascii
|