blob: 3524b4b7952fa455a40d5106b05632f9f34f846c [file] [log] [blame]
Alexander Kornienkoece0bec2013-08-07 00:07:07 +00001//===- 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
13namespace llvm {
14namespace sys {
15namespace locale {
16namespace {
17
Alexander Kornienko31a4f1f2013-08-07 00:41:18 +000018// FIXME: WIN32 implementation is incorrect. We should consider using the one
19// from LocaleGeneric.inc for WIN32.
20#ifndef _WIN32
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000021TEST(Locale, columnWidth) {
Alexander Kornienko6214ae52013-08-07 02:08:31 +000022 // FIXME: This test fails with MacOSX implementation of columnWidth.
23#ifndef __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000024 EXPECT_EQ(0, columnWidth(""));
25 EXPECT_EQ(1, columnWidth(" "));
26 EXPECT_EQ(1, columnWidth("a"));
27 EXPECT_EQ(1, columnWidth("~"));
28
29 EXPECT_EQ(6, columnWidth("abcdef"));
30
31 EXPECT_EQ(-1, columnWidth("\x01"));
32 EXPECT_EQ(-1, columnWidth("aaaaaaaaaa\x01"));
33 EXPECT_EQ(-1, columnWidth("\342\200\213")); // 200B ZERO WIDTH SPACE
34
35 EXPECT_EQ(0, columnWidth("\314\200")); // 0300 COMBINING GRAVE ACCENT
36 EXPECT_EQ(1, columnWidth("\340\270\201")); // 0E01 THAI CHARACTER KO KAI
37 EXPECT_EQ(2, columnWidth("\344\270\200")); // CJK UNIFIED IDEOGRAPH-4E00
38
39 EXPECT_EQ(4, columnWidth("\344\270\200\344\270\200"));
40 EXPECT_EQ(3, columnWidth("q\344\270\200"));
41 EXPECT_EQ(3, columnWidth("\314\200\340\270\201\344\270\200"));
42
43 // Invalid UTF-8 strings, columnWidth should error out.
44 EXPECT_EQ(-2, columnWidth("\344"));
45 EXPECT_EQ(-2, columnWidth("\344\270"));
46 EXPECT_EQ(-2, columnWidth("\344\270\033"));
47 EXPECT_EQ(-2, columnWidth("\344\270\300"));
48 EXPECT_EQ(-2, columnWidth("\377\366\355"));
49
50 EXPECT_EQ(-2, columnWidth("qwer\344"));
51 EXPECT_EQ(-2, columnWidth("qwer\344\270"));
52 EXPECT_EQ(-2, columnWidth("qwer\344\270\033"));
53 EXPECT_EQ(-2, columnWidth("qwer\344\270\300"));
54 EXPECT_EQ(-2, columnWidth("qwer\377\366\355"));
55
56 // UTF-8 sequences longer than 4 bytes correspond to unallocated Unicode
57 // characters.
58 EXPECT_EQ(-2, columnWidth("\370\200\200\200\200")); // U+200000
59 EXPECT_EQ(-2, columnWidth("\374\200\200\200\200\200")); // U+4000000
Alexander Kornienko074ce332013-08-07 01:23:28 +000060#endif // __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000061}
62
63TEST(Locale, isPrint) {
64 EXPECT_EQ(false, isPrint(0)); // <control-0000>-<control-001F>
65 EXPECT_EQ(false, isPrint(0x01));
66 EXPECT_EQ(false, isPrint(0x1F));
67 EXPECT_EQ(true, isPrint(' '));
68 EXPECT_EQ(true, isPrint('A'));
69 EXPECT_EQ(true, isPrint('~'));
70 EXPECT_EQ(false, isPrint(0x7F)); // <control-007F>..<control-009F>
71 EXPECT_EQ(false, isPrint(0x90));
72 EXPECT_EQ(false, isPrint(0x9F));
73
74 EXPECT_EQ(true, isPrint(0xAC));
Alexander Kornienko31a4f1f2013-08-07 00:41:18 +000075 // FIXME: Figure out if we want to treat SOFT HYPHEN as printable character.
Alexander Kornienko074ce332013-08-07 01:23:28 +000076#ifndef __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000077 EXPECT_EQ(false, isPrint(0xAD)); // SOFT HYPHEN
Alexander Kornienko074ce332013-08-07 01:23:28 +000078#endif // __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000079 EXPECT_EQ(true, isPrint(0xAE));
80
Alexander Kornienko31a4f1f2013-08-07 00:41:18 +000081 // MacOS implementation doesn't think it's printable.
Alexander Kornienko074ce332013-08-07 01:23:28 +000082#ifndef __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000083 EXPECT_EQ(true, isPrint(0x0377)); // GREEK SMALL LETTER PAMPHYLIAN DIGAMMA
Alexander Kornienko074ce332013-08-07 01:23:28 +000084#endif // __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000085 EXPECT_EQ(false, isPrint(0x0378)); // <reserved-0378>..<reserved-0379>
86
87 EXPECT_EQ(false, isPrint(0x0600)); // ARABIC NUMBER SIGN
88
89 EXPECT_EQ(false, isPrint(0x1FFFF)); // <reserved-1F774>..<noncharacter-1FFFF>
90 EXPECT_EQ(true, isPrint(0x20000)); // CJK UNIFIED IDEOGRAPH-20000
91
92 EXPECT_EQ(false, isPrint(0x10FFFF)); // noncharacter
93}
94
Alexander Kornienko31a4f1f2013-08-07 00:41:18 +000095#endif // _WIN32
96
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000097} // namespace
98} // namespace locale
99} // namespace sys
100} // namespace llvm