UnicodeTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //===- unittests/Support/UnicodeTest.cpp - Unicode.h tests ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Support/Unicode.h"
  10. #include "gtest/gtest.h"
  11. namespace llvm {
  12. namespace sys {
  13. namespace unicode {
  14. namespace {
  15. TEST(Unicode, columnWidthUTF8) {
  16. EXPECT_EQ(0, columnWidthUTF8(""));
  17. EXPECT_EQ(1, columnWidthUTF8(" "));
  18. EXPECT_EQ(1, columnWidthUTF8("a"));
  19. EXPECT_EQ(1, columnWidthUTF8("~"));
  20. EXPECT_EQ(6, columnWidthUTF8("abcdef"));
  21. EXPECT_EQ(-1, columnWidthUTF8("\x01"));
  22. EXPECT_EQ(-1, columnWidthUTF8("aaaaaaaaaa\x01"));
  23. EXPECT_EQ(-1, columnWidthUTF8("\342\200\213")); // 200B ZERO WIDTH SPACE
  24. // 00AD SOFT HYPHEN is displayed on most terminals as a space or a dash. Some
  25. // text editors display it only when a line is broken at it, some use it as a
  26. // line-break hint, but don't display. We choose terminal-oriented
  27. // interpretation.
  28. EXPECT_EQ(1, columnWidthUTF8("\302\255"));
  29. EXPECT_EQ(0, columnWidthUTF8("\314\200")); // 0300 COMBINING GRAVE ACCENT
  30. EXPECT_EQ(1, columnWidthUTF8("\340\270\201")); // 0E01 THAI CHARACTER KO KAI
  31. EXPECT_EQ(2, columnWidthUTF8("\344\270\200")); // CJK UNIFIED IDEOGRAPH-4E00
  32. EXPECT_EQ(4, columnWidthUTF8("\344\270\200\344\270\200"));
  33. EXPECT_EQ(3, columnWidthUTF8("q\344\270\200"));
  34. EXPECT_EQ(3, columnWidthUTF8("\314\200\340\270\201\344\270\200"));
  35. // Invalid UTF-8 strings, columnWidthUTF8 should error out.
  36. EXPECT_EQ(-2, columnWidthUTF8("\344"));
  37. EXPECT_EQ(-2, columnWidthUTF8("\344\270"));
  38. EXPECT_EQ(-2, columnWidthUTF8("\344\270\033"));
  39. EXPECT_EQ(-2, columnWidthUTF8("\344\270\300"));
  40. EXPECT_EQ(-2, columnWidthUTF8("\377\366\355"));
  41. EXPECT_EQ(-2, columnWidthUTF8("qwer\344"));
  42. EXPECT_EQ(-2, columnWidthUTF8("qwer\344\270"));
  43. EXPECT_EQ(-2, columnWidthUTF8("qwer\344\270\033"));
  44. EXPECT_EQ(-2, columnWidthUTF8("qwer\344\270\300"));
  45. EXPECT_EQ(-2, columnWidthUTF8("qwer\377\366\355"));
  46. // UTF-8 sequences longer than 4 bytes correspond to unallocated Unicode
  47. // characters.
  48. EXPECT_EQ(-2, columnWidthUTF8("\370\200\200\200\200")); // U+200000
  49. EXPECT_EQ(-2, columnWidthUTF8("\374\200\200\200\200\200")); // U+4000000
  50. }
  51. TEST(Unicode, isPrintable) {
  52. EXPECT_FALSE(isPrintable(0)); // <control-0000>-<control-001F>
  53. EXPECT_FALSE(isPrintable(0x01));
  54. EXPECT_FALSE(isPrintable(0x1F));
  55. EXPECT_TRUE(isPrintable(' '));
  56. EXPECT_TRUE(isPrintable('A'));
  57. EXPECT_TRUE(isPrintable('~'));
  58. EXPECT_FALSE(isPrintable(0x7F)); // <control-007F>..<control-009F>
  59. EXPECT_FALSE(isPrintable(0x90));
  60. EXPECT_FALSE(isPrintable(0x9F));
  61. EXPECT_TRUE(isPrintable(0xAC));
  62. EXPECT_TRUE(isPrintable(0xAD)); // SOFT HYPHEN is displayed on most terminals
  63. // as either a space or a dash.
  64. EXPECT_TRUE(isPrintable(0xAE));
  65. EXPECT_TRUE(isPrintable(0x0377)); // GREEK SMALL LETTER PAMPHYLIAN DIGAMMA
  66. EXPECT_FALSE(isPrintable(0x0378)); // <reserved-0378>..<reserved-0379>
  67. EXPECT_FALSE(isPrintable(0x0600)); // ARABIC NUMBER SIGN
  68. EXPECT_FALSE(isPrintable(0x1FFFF)); // <reserved-1F774>..<noncharacter-1FFFF>
  69. EXPECT_TRUE(isPrintable(0x20000)); // CJK UNIFIED IDEOGRAPH-20000
  70. EXPECT_FALSE(isPrintable(0x10FFFF)); // noncharacter
  71. }
  72. } // namespace
  73. } // namespace unicode
  74. } // namespace sys
  75. } // namespace llvm