test_utf.cpp 7.9 KB

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