2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT License.
7 #include "UnicodeCharTest.h"
10 #include <UnicodeChar.h>
12 #include <cppunit/TestCaller.h>
13 #include <cppunit/TestSuite.h>
16 UnicodeCharTest::UnicodeCharTest()
21 UnicodeCharTest::~UnicodeCharTest()
27 UnicodeCharTest::TestAscii()
30 {"\x1e", 0, 0, 0, 0, 1, 15, '\x1e', '\x1e'},
31 {"\x1f", 0, 0, 0, 0, 1, 15, '\x1f', '\x1f'},
32 {" ", 0, 0, 0, 0, 1, 12, ' ', ' '},
33 {"!", 0, 0, 0, 0, 1, 23, '!', '!'},
34 {"\"", 0, 0, 0, 0, 1, 23, '"', '"'},
35 {"#", 0, 0, 0, 0, 1, 23, '#', '#'},
36 {"$", 0, 0, 0, 0, 1, 25, '$', '$'},
37 {"%", 0, 0, 0, 0, 1, 23, '%', '%'},
38 {"&", 0, 0, 0, 0, 1, 23, '&', '&'},
39 {"'", 0, 0, 0, 0, 1, 23, '\'', '\''},
40 {"(", 0, 0, 0, 0, 1, 20, '(', '('},
41 {")", 0, 0, 0, 0, 1, 21, ')', ')'},
42 {"*", 0, 0, 0, 0, 1, 23, '*', '*'},
43 {"+", 0, 0, 0, 0, 1, 24, '+', '+'},
44 {",", 0, 0, 0, 0, 1, 23, ',', ','},
45 {"-", 0, 0, 0, 0, 1, 19, '-', '-'},
46 {".", 0, 0, 0, 0, 1, 23, '.', '.'},
47 {"/", 0, 0, 0, 0, 1, 23, '/', '/'},
48 {"0", 0, 1, 0, 0, 1, 9, '0', '0'},
49 {"1", 0, 1, 0, 0, 1, 9, '1', '1'},
50 {"2", 0, 1, 0, 0, 1, 9, '2', '2'},
51 {"3", 0, 1, 0, 0, 1, 9, '3', '3'},
52 {"4", 0, 1, 0, 0, 1, 9, '4', '4'},
53 {"5", 0, 1, 0, 0, 1, 9, '5', '5'},
54 {"6", 0, 1, 0, 0, 1, 9, '6', '6'},
55 {"7", 0, 1, 0, 0, 1, 9, '7', '7'},
56 {"8", 0, 1, 0, 0, 1, 9, '8', '8'},
57 {"9", 0, 1, 0, 0, 1, 9, '9', '9'},
58 {":", 0, 0, 0, 0, 1, 23, ':', ':'},
59 {";", 0, 0, 0, 0, 1, 23, ';', ';'},
60 {"<", 0, 0, 0, 0, 1, 24, '<', '<'},
61 {"=", 0, 0, 0, 0, 1, 24, '=', '='},
62 {">", 0, 0, 0, 0, 1, 24, '>', '>'},
63 {"?", 0, 0, 0, 0, 1, 23, '?', '?'},
64 {"@", 0, 0, 0, 0, 1, 23, '@', '@'},
65 {"A", 1, 1, 0, 1, 1, 1, 'A', 'a'},
66 {"B", 1, 1, 0, 1, 1, 1, 'B', 'b'},
67 {"C", 1, 1, 0, 1, 1, 1, 'C', 'c'},
68 {"D", 1, 1, 0, 1, 1, 1, 'D', 'd'},
69 {"E", 1, 1, 0, 1, 1, 1, 'E', 'e'}
72 for (int32 i
= 30; i
< 70; i
++) {
74 _TestChar(i
, results
[i
- 30]);
80 UnicodeCharTest::TestISO8859()
82 uint32 chars
[] = {(uint8
)'\xe4', (uint8
)'\xd6', (uint8
)'\xdf',
83 (uint8
)'\xe8', (uint8
)'\xe1', (uint8
)'\xe9', 0};
86 {"ä", 1, 1, 1, 0, 1, 2, 196, 228},
87 {"Ö", 1, 1, 0, 1, 1, 1, 214, 246},
88 {"ß", 1, 1, 1, 0, 1, 2, 223, 223},
89 {"è", 1, 1, 1, 0, 1, 2, 200, 232},
90 {"á", 1, 1, 1, 0, 1, 2, 193, 225},
91 {"é", 1, 1, 1, 0, 1, 2, 201, 233}
94 for(int i
= 0; chars
[i
] != 0; i
++) {
96 _TestChar(chars
[i
], results
[i
]);
102 UnicodeCharTest::TestUTF8()
104 const char *utf8chars
[] = {"à", "ß", "ñ", "é", "ç", "ä", NULL
};
107 {"à", 1, 1, 1, 0, 1, 2, 192, 224},
108 {"ß", 1, 1, 1, 0, 1, 2, 223, 223},
109 {"ñ", 1, 1, 1, 0, 1, 2, 209, 241},
110 {"é", 1, 1, 1, 0, 1, 2, 201, 233},
111 {"ç", 1, 1, 1, 0, 1, 2, 199, 231},
112 {"ä", 1, 1, 1, 0, 1, 2, 196, 228}
115 for(int i
= 0; utf8chars
[i
] != 0; i
++) {
117 _TestChar(BUnicodeChar::FromUTF8(utf8chars
[i
]), results
[i
]);
123 UnicodeCharTest::AddTests(BTestSuite
& parent
)
125 CppUnit::TestSuite
& suite
= *new CppUnit::TestSuite("UnicodeCharTest");
127 suite
.addTest(new CppUnit::TestCaller
<UnicodeCharTest
>(
128 "UnicodeCharTest::TestAscii", &UnicodeCharTest::TestAscii
));
129 suite
.addTest(new CppUnit::TestCaller
<UnicodeCharTest
>(
130 "UnicodeCharTest::TestISO8859", &UnicodeCharTest::TestISO8859
));
131 suite
.addTest(new CppUnit::TestCaller
<UnicodeCharTest
>(
132 "UnicodeCharTest::TestUTF8", &UnicodeCharTest::TestUTF8
));
134 parent
.addTest("UnicodeCharTest", &suite
);
139 UnicodeCharTest::_ToString(uint32 c
, char *text
)
141 BUnicodeChar::ToUTF8(c
, &text
);
147 UnicodeCharTest::_TestChar(uint32 i
, Result result
)
152 CPPUNIT_ASSERT_EQUAL(BString(result
.value
), text
);
153 CPPUNIT_ASSERT_EQUAL(result
.isAlpha
, BUnicodeChar::IsAlpha(i
));
154 CPPUNIT_ASSERT_EQUAL(result
.isAlNum
, BUnicodeChar::IsAlNum(i
));
155 CPPUNIT_ASSERT_EQUAL(result
.isLower
, BUnicodeChar::IsLower(i
));
156 CPPUNIT_ASSERT_EQUAL(result
.isUpper
, BUnicodeChar::IsUpper(i
));
157 CPPUNIT_ASSERT_EQUAL(result
.isDefined
, BUnicodeChar::IsDefined(i
));
158 CPPUNIT_ASSERT_EQUAL(result
.type
, BUnicodeChar::Type(i
));
159 CPPUNIT_ASSERT_EQUAL(result
.toUpper
, BUnicodeChar::ToUpper(i
));
160 CPPUNIT_ASSERT_EQUAL(result
.toLower
, BUnicodeChar::ToLower(i
));