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