d48eb559f0eec0288390d36da8e94dd267d27a6e
[oota-llvm.git] / unittests / Support / LocaleTest.cpp
1 //===- unittests/Support/LocaleTest.cpp - Locale.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
10 #include "llvm/Support/Locale.h"
11 #include "gtest/gtest.h"
12
13 namespace llvm {
14 namespace sys {
15 namespace locale {
16 namespace {
17
18 TEST(Locale, columnWidth) {
19   EXPECT_EQ(0, columnWidth(""));
20   EXPECT_EQ(1, columnWidth(" "));
21   EXPECT_EQ(1, columnWidth("a"));
22   EXPECT_EQ(1, columnWidth("~"));
23
24   EXPECT_EQ(6, columnWidth("abcdef"));
25
26   EXPECT_EQ(-1, columnWidth("\x01"));
27   EXPECT_EQ(-1, columnWidth("aaaaaaaaaa\x01"));
28   EXPECT_EQ(-1, columnWidth("\342\200\213")); // 200B ZERO WIDTH SPACE
29
30   EXPECT_EQ(0, columnWidth("\314\200")); // 0300 COMBINING GRAVE ACCENT
31   EXPECT_EQ(1, columnWidth("\340\270\201")); // 0E01 THAI CHARACTER KO KAI
32   EXPECT_EQ(2, columnWidth("\344\270\200")); // CJK UNIFIED IDEOGRAPH-4E00
33
34   EXPECT_EQ(4, columnWidth("\344\270\200\344\270\200"));
35   EXPECT_EQ(3, columnWidth("q\344\270\200"));
36   EXPECT_EQ(3, columnWidth("\314\200\340\270\201\344\270\200"));
37
38   // Invalid UTF-8 strings, columnWidth should error out.
39   EXPECT_EQ(-2, columnWidth("\344"));
40   EXPECT_EQ(-2, columnWidth("\344\270"));
41   EXPECT_EQ(-2, columnWidth("\344\270\033"));
42   EXPECT_EQ(-2, columnWidth("\344\270\300"));
43   EXPECT_EQ(-2, columnWidth("\377\366\355"));
44
45   EXPECT_EQ(-2, columnWidth("qwer\344"));
46   EXPECT_EQ(-2, columnWidth("qwer\344\270"));
47   EXPECT_EQ(-2, columnWidth("qwer\344\270\033"));
48   EXPECT_EQ(-2, columnWidth("qwer\344\270\300"));
49   EXPECT_EQ(-2, columnWidth("qwer\377\366\355"));
50
51   // UTF-8 sequences longer than 4 bytes correspond to unallocated Unicode
52   // characters.
53   EXPECT_EQ(-2, columnWidth("\370\200\200\200\200"));     // U+200000
54   EXPECT_EQ(-2, columnWidth("\374\200\200\200\200\200")); // U+4000000
55 }
56
57 TEST(Locale, isPrint) {
58   EXPECT_EQ(false, isPrint(0)); // <control-0000>-<control-001F>
59   EXPECT_EQ(false, isPrint(0x01));
60   EXPECT_EQ(false, isPrint(0x1F));
61   EXPECT_EQ(true, isPrint(' '));
62   EXPECT_EQ(true, isPrint('A'));
63   EXPECT_EQ(true, isPrint('~'));
64   EXPECT_EQ(false, isPrint(0x7F)); // <control-007F>..<control-009F>
65   EXPECT_EQ(false, isPrint(0x90));
66   EXPECT_EQ(false, isPrint(0x9F));
67
68   EXPECT_EQ(true, isPrint(0xAC));
69   EXPECT_EQ(false, isPrint(0xAD)); // SOFT HYPHEN
70   EXPECT_EQ(true, isPrint(0xAE));
71
72   EXPECT_EQ(true, isPrint(0x0377)); // GREEK SMALL LETTER PAMPHYLIAN DIGAMMA
73   EXPECT_EQ(false, isPrint(0x0378)); // <reserved-0378>..<reserved-0379>
74
75   EXPECT_EQ(false, isPrint(0x0600)); // ARABIC NUMBER SIGN
76
77   EXPECT_EQ(false, isPrint(0x1FFFF)); // <reserved-1F774>..<noncharacter-1FFFF>
78   EXPECT_EQ(true, isPrint(0x20000)); // CJK UNIFIED IDEOGRAPH-20000
79
80   EXPECT_EQ(false, isPrint(0x10FFFF)); // noncharacter
81 }
82
83 } // namespace
84 } // namespace locale
85 } // namespace sys
86 } // namespace llvm