1/*
2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "UnicodeCharTest.h"
8
9#include <String.h>
10#include <UnicodeChar.h>
11
12#include <cppunit/TestCaller.h>
13#include <cppunit/TestSuite.h>
14
15
16UnicodeCharTest::UnicodeCharTest()
17{
18}
19
20
21UnicodeCharTest::~UnicodeCharTest()
22{
23}
24
25
26void
27UnicodeCharTest::TestAscii()
28{
29	Result results[] = {
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'}
70	};
71
72	for (int32 i = 30; i < 70; i++) {
73		NextSubTest();
74		_TestChar(i, results[i - 30]);
75	}
76}
77
78
79void
80UnicodeCharTest::TestISO8859()
81{
82	uint32 chars[] = {(uint8)'\xe4', (uint8)'\xd6', (uint8)'\xdf',
83		(uint8)'\xe8', (uint8)'\xe1', (uint8)'\xe9', 0};
84
85	Result results[] = {
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}
92	};
93
94	for(int i = 0; chars[i] != 0; i++) {
95		NextSubTest();
96		_TestChar(chars[i], results[i]);
97	}
98}
99
100
101void
102UnicodeCharTest::TestUTF8()
103{
104	const char *utf8chars[] = {"��", "��", "��", "��", "��", "��", NULL};
105
106	Result results[] = {
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}
113	};
114
115	for(int i = 0; utf8chars[i] != 0; i++) {
116		NextSubTest();
117		_TestChar(BUnicodeChar::FromUTF8(utf8chars[i]), results[i]);
118	}
119}
120
121
122/*static*/ void
123UnicodeCharTest::AddTests(BTestSuite& parent)
124{
125	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UnicodeCharTest");
126
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));
133
134	parent.addTest("UnicodeCharTest", &suite);
135}
136
137
138void
139UnicodeCharTest::_ToString(uint32 c, char *text)
140{
141	BUnicodeChar::ToUTF8(c, &text);
142	text[0] = '\0';
143}
144
145
146void
147UnicodeCharTest::_TestChar(uint32 i, Result result)
148{
149	char text[16];
150
151	_ToString(i, text);
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));
161}
162