blob: 81d31b9f613a7672188a166d567b5e645ccbd04e [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
Alexander Kornienko6cd4f2a2013-08-08 01:10:50 +000035 // 00AD SOFT HYPHEN is displayed on most terminals as a space or a dash. Some
36 // text editors display it only when a line is broken at it, some use it as a
37 // line-break hint, but don't display. We choose terminal-oriented
38 // interpretation.
39 EXPECT_EQ(1, columnWidth("\302\255"));
40
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000041 EXPECT_EQ(0, columnWidth("\314\200")); // 0300 COMBINING GRAVE ACCENT
42 EXPECT_EQ(1, columnWidth("\340\270\201")); // 0E01 THAI CHARACTER KO KAI
43 EXPECT_EQ(2, columnWidth("\344\270\200")); // CJK UNIFIED IDEOGRAPH-4E00
44
45 EXPECT_EQ(4, columnWidth("\344\270\200\344\270\200"));
46 EXPECT_EQ(3, columnWidth("q\344\270\200"));
47 EXPECT_EQ(3, columnWidth("\314\200\340\270\201\344\270\200"));
48
49 // Invalid UTF-8 strings, columnWidth should error out.
50 EXPECT_EQ(-2, columnWidth("\344"));
51 EXPECT_EQ(-2, columnWidth("\344\270"));
52 EXPECT_EQ(-2, columnWidth("\344\270\033"));
53 EXPECT_EQ(-2, columnWidth("\344\270\300"));
54 EXPECT_EQ(-2, columnWidth("\377\366\355"));
55
56 EXPECT_EQ(-2, columnWidth("qwer\344"));
57 EXPECT_EQ(-2, columnWidth("qwer\344\270"));
58 EXPECT_EQ(-2, columnWidth("qwer\344\270\033"));
59 EXPECT_EQ(-2, columnWidth("qwer\344\270\300"));
60 EXPECT_EQ(-2, columnWidth("qwer\377\366\355"));
61
62 // UTF-8 sequences longer than 4 bytes correspond to unallocated Unicode
63 // characters.
64 EXPECT_EQ(-2, columnWidth("\370\200\200\200\200")); // U+200000
65 EXPECT_EQ(-2, columnWidth("\374\200\200\200\200\200")); // U+4000000
Alexander Kornienko074ce332013-08-07 01:23:28 +000066#endif // __APPLE__
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000067}
68
69TEST(Locale, isPrint) {
Benjamin Kramer6ee05812013-08-08 11:17:39 +000070 EXPECT_FALSE(isPrint(0)); // <control-0000>-<control-001F>
71 EXPECT_FALSE(isPrint(0x01));
72 EXPECT_FALSE(isPrint(0x1F));
73 EXPECT_TRUE(isPrint(' '));
74 EXPECT_TRUE(isPrint('A'));
75 EXPECT_TRUE(isPrint('~'));
76 EXPECT_FALSE(isPrint(0x7F)); // <control-007F>..<control-009F>
77 EXPECT_FALSE(isPrint(0x90));
78 EXPECT_FALSE(isPrint(0x9F));
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000079
Benjamin Kramer6ee05812013-08-08 11:17:39 +000080 EXPECT_TRUE(isPrint(0xAC));
81 EXPECT_TRUE(isPrint(0xAD)); // SOFT HYPHEN is displayed on most terminals
82 // as either a space or a dash.
83 EXPECT_TRUE(isPrint(0xAE));
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000084
Alexander Kornienko31a4f1f2013-08-07 00:41:18 +000085 // MacOS implementation doesn't think it's printable.
Alexander Kornienko074ce332013-08-07 01:23:28 +000086#ifndef __APPLE__
Benjamin Kramer6ee05812013-08-08 11:17:39 +000087 EXPECT_TRUE(isPrint(0x0377)); // GREEK SMALL LETTER PAMPHYLIAN DIGAMMA
Alexander Kornienko074ce332013-08-07 01:23:28 +000088#endif // __APPLE__
Benjamin Kramer6ee05812013-08-08 11:17:39 +000089 EXPECT_FALSE(isPrint(0x0378)); // <reserved-0378>..<reserved-0379>
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000090
Benjamin Kramer6ee05812013-08-08 11:17:39 +000091 EXPECT_FALSE(isPrint(0x0600)); // ARABIC NUMBER SIGN
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000092
Benjamin Kramer6ee05812013-08-08 11:17:39 +000093 EXPECT_FALSE(isPrint(0x1FFFF)); // <reserved-1F774>..<noncharacter-1FFFF>
94 EXPECT_TRUE(isPrint(0x20000)); // CJK UNIFIED IDEOGRAPH-20000
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000095
Benjamin Kramer6ee05812013-08-08 11:17:39 +000096 EXPECT_FALSE(isPrint(0x10FFFF)); // noncharacter
Alexander Kornienkoece0bec2013-08-07 00:07:07 +000097}
98
Alexander Kornienko31a4f1f2013-08-07 00:41:18 +000099#endif // _WIN32
100
Alexander Kornienkoece0bec2013-08-07 00:07:07 +0000101} // namespace
102} // namespace locale
103} // namespace sys
104} // namespace llvm