UnicodeTest.cpp 3.4 KB

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