blob: e74f67f1432524ef4a91020923460b61188f2b21 [file] [log] [blame]
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "PhoneticStringUtils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace android;
class TestExecutor {
public:
TestExecutor() : m_total_count(0), m_success_count(0), m_success(true) {}
bool DoAllTests();
private:
void DoOneTest(void (TestExecutor::*test)());
void testGetCodePointFromUtf8();
void testGetPhoneticallySortableCodePointAscii();
void testGetPhoneticallySortableCodePointKana();
void testGetPhoneticallySortableCodePointWhitespaceOnly();
void testGetPhoneticallySortableCodePointSimpleCompare();
void testGetUtf8FromCodePoint();
void testGetPhoneticallySortableString();
void testGetNormalizedString();
// Note: When adding a test, do not forget to add it to DoOneTest().
int m_total_count;
int m_success_count;
bool m_success;
};
#define ASSERT_EQ_VALUE(input, expected) \
({ \
if ((expected) != (input)) { \
printf("0x%X(result) != 0x%X(expected)\n", input, expected); \
m_success = false; \
return; \
} \
})
#define EXPECT_EQ_VALUE(input, expected) \
({ \
if ((expected) != (input)) { \
printf("0x%X(result) != 0x%X(expected)\n", input, expected); \
m_success = false; \
} \
})
bool TestExecutor::DoAllTests() {
DoOneTest(&TestExecutor::testGetCodePointFromUtf8);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointAscii);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointKana);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointWhitespaceOnly);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare);
DoOneTest(&TestExecutor::testGetUtf8FromCodePoint);
DoOneTest(&TestExecutor::testGetPhoneticallySortableString);
DoOneTest(&TestExecutor::testGetNormalizedString);
printf("Test total: %d\nSuccess: %d\nFailure: %d\n",
m_total_count, m_success_count, m_total_count - m_success_count);
bool success = m_total_count == m_success_count;
printf("\n%s\n", success ? "Success" : "Failure");
return success;
}
void TestExecutor::DoOneTest(void (TestExecutor::*test)()) {
m_success = true;
(this->*test)();
++m_total_count;
m_success_count += m_success ? 1 : 0;
}
void TestExecutor::testGetCodePointFromUtf8() {
printf("testGetCodePointFromUtf8()\n");
int next;
EXPECT_EQ_VALUE(GetCodePointFromUtf8("a", 1, 0, &next), 97);
EXPECT_EQ_VALUE(next, 1);
// Japanese hiragana "a"
EXPECT_EQ_VALUE(GetCodePointFromUtf8("\xE3\x81\x82", 3, 0, &next), 0x3042);
EXPECT_EQ_VALUE(next, 3);
// Japanese fullwidth katakana "a" with ascii a
EXPECT_EQ_VALUE(GetCodePointFromUtf8("a\xE3\x82\xA2", 4, 1, &next), 0x30A2);
EXPECT_EQ_VALUE(next, 4);
// 2 PUA
ASSERT_EQ_VALUE(GetCodePointFromUtf8("\xF3\xBE\x80\x80\xF3\xBE\x80\x88",
8, 0, &next), 0xFE000);
ASSERT_EQ_VALUE(next, 4);
ASSERT_EQ_VALUE(GetCodePointFromUtf8("\xF3\xBE\x80\x80\xF3\xBE\x80\x88",
8, next, &next), 0xFE008);
ASSERT_EQ_VALUE(next, 8);
}
void TestExecutor::testGetPhoneticallySortableCodePointAscii() {
printf("testGetPhoneticallySortableCodePoint()\n");
int halfwidth[94];
int fullwidth[94];
int i, codepoint;
bool next_is_consumed;
for (i = 0, codepoint = 0x0021; codepoint <= 0x007E; ++i, ++codepoint) {
halfwidth[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
&next_is_consumed);
if (halfwidth[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
m_success = false;
return;
}
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint);
m_success = false;
return;
}
}
for (i = 0, codepoint = 0xFF01; codepoint <= 0xFF5E; ++i, ++codepoint) {
fullwidth[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
&next_is_consumed);
if (fullwidth[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
m_success = false;
return;
}
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint);
m_success = false;
return;
}
}
for (i = 0; i < 94; i++) {
EXPECT_EQ_VALUE(halfwidth[i], fullwidth[i]);
}
}
void TestExecutor::testGetPhoneticallySortableCodePointKana() {
printf("testGetPhoneticallySortableCodePointKana()\n");
int hiragana[86];
int fullwidth_katakana[86];
int i, codepoint;
bool next_is_consumed;
for (i = 0, codepoint = 0x3041; codepoint <= 0x3096; ++i, ++codepoint) {
hiragana[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
&next_is_consumed);
if (hiragana[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
m_success = false;
return;
}
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint);
m_success = false;
return;
}
}
for (i = 0, codepoint = 0x30A1; codepoint <= 0x30F6; ++i, ++codepoint) {
fullwidth_katakana[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
&next_is_consumed);
if (fullwidth_katakana[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
m_success = false;
return;
}
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint);
m_success = false;
return;
}
}
// hankaku-katakana space do not have some characters corresponding to
// zenkaku-hiragana (e.g. xwa, xka, xku). To make test easier, insert
// zenkaku-katakana version of them into this array (See the value 0x30??).
int halfwidth_katakana[] = {
0xFF67, 0xFF71, 0xFF68, 0xFF72, 0xFF69, 0xFF73, 0xFF6A, 0xFF74, 0xFF6B,
0xFF75, 0xFF76, 0xFF76, 0xFF9E, 0xFF77, 0xFF77, 0xFF9E, 0xFF78, 0xFF78,
0xFF9E, 0xFF79, 0xFF79, 0xFF9E, 0xFF7A, 0xFF7A, 0xFF9E, 0xFF7B, 0xFF7B,
0xFF9E, 0xFF7C, 0xFF7C, 0xFF9E, 0xFF7D, 0xFF7D, 0xFF9E, 0xFF7E, 0xFF7E,
0xFF9E, 0xFF7F, 0xFF7F, 0xFF9E, 0xFF80, 0xFF80, 0xFF9E, 0xFF81, 0xFF81,
0xFF9E, 0xFF6F, 0xFF82, 0xFF82, 0xFF9E, 0xFF83, 0xFF83, 0xFF9E, 0xFF84,
0xFF84, 0xFF9E, 0xFF85, 0xFF86, 0xFF87, 0xFF88, 0xFF89, 0xFF8A, 0xFF8A,
0xFF9E, 0xFF8A, 0xFF9F, 0xFF8B, 0xFF8B, 0xFF9E, 0xFF8B, 0xFF9F, 0xFF8C,
0xFF8C, 0xFF9E, 0xFF8C, 0xFF9F, 0xFF8D, 0xFF8D, 0xFF9E, 0xFF8D, 0xFF9F,
0xFF8E, 0xFF8E, 0xFF9E, 0xFF8E, 0xFF9F, 0xFF8F, 0xFF90, 0xFF91, 0xFF92,
0xFF93, 0xFF6C, 0xFF94, 0xFF6D, 0xFF95, 0xFF6E, 0xFF96, 0xFF97, 0xFF98,
0xFF99, 0xFF9A, 0xFF9B, 0x30EE, 0xFF9C, 0x30F0, 0x30F1, 0xFF66, 0xFF9D,
0xFF73, 0xFF9E, 0x30F5, 0x30F6};
int len = sizeof(halfwidth_katakana)/sizeof(int);
int halfwidth_katakana_result[86];
int j;
for (i = 0, j = 0; i < len && j < 86; ++i, ++j) {
int codepoint = halfwidth_katakana[i];
int next_codepoint = i + 1 < len ? halfwidth_katakana[i + 1] : -1;
halfwidth_katakana_result[j] =
GetPhoneticallySortableCodePoint(codepoint, next_codepoint,
&next_is_consumed);
// Consume voiced mark/half-voiced mark.
if (next_is_consumed) {
++i;
}
}
ASSERT_EQ_VALUE(i, len);
ASSERT_EQ_VALUE(j, 86);
for (i = 0; i < 86; ++i) {
EXPECT_EQ_VALUE(fullwidth_katakana[i], hiragana[i]);
EXPECT_EQ_VALUE(halfwidth_katakana_result[i], hiragana[i]);
}
}
void TestExecutor::testGetPhoneticallySortableCodePointWhitespaceOnly() {
printf("testGetPhoneticallySortableCodePointWhitespaceOnly()\n");
// Halfwidth space
int result = GetPhoneticallySortableCodePoint(0x0020, 0x0061, NULL);
ASSERT_EQ_VALUE(result, -1);
// Fullwidth space
result = GetPhoneticallySortableCodePoint(0x3000, 0x0062, NULL);
ASSERT_EQ_VALUE(result, -1);
// tab
result = GetPhoneticallySortableCodePoint(0x0009, 0x0062, NULL);
ASSERT_EQ_VALUE(result, -1);
}
void TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare() {
printf("testGetPhoneticallySortableCodePointSimpleCompare()\n");
int codepoints[] = {
0x3042, 0x30AB, 0xFF7B, 0x305F, 0x30CA, 0xFF8A, 0x30D0, 0x3071,
0x307E, 0x30E4, 0xFF97, 0x308F, 0x3093, 0x3094, 'A', 'Z',
'0', '9', '!', '/', ':', '?', '[', '`', '{', '~'};
size_t len = sizeof(codepoints)/sizeof(int);
bool next_is_consumed;
for (size_t i = 0; i < len - 1; ++i) {
int codepoint_a =
GetPhoneticallySortableCodePoint(codepoints[i], -1,
&next_is_consumed);
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint_a);
m_success = false;
return;
}
int codepoint_b =
GetPhoneticallySortableCodePoint(codepoints[i + 1], -1,
&next_is_consumed);
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint_b);
m_success = false;
return;
}
if (codepoint_a >= codepoint_b) {
printf("0x%04X (from 0x%04X) >= 0x%04X (from 0x%04X)\n",
codepoint_a, codepoints[i], codepoint_b, codepoints[i + 1]);
m_success = false;
return;
}
}
}
#define EXPECT_EQ_CODEPOINT_UTF8_WITH_INDEX(codepoint, expected, i) \
({ \
index = i; \
if (!GetUtf8FromCodePoint(codepoint, dst, 10, &index)) { \
printf("GetUtf8FromCodePoint() returned false at 0x%04X\n", codepoint); \
m_success = false; \
} else if (index >= 10) { \
printf("index (%d) >= 10\n", index); \
m_success = false; \
} else { \
dst[index] = '\0'; \
if (strcmp(dst + i, expected) != 0) { \
printf("Failed at codepoint 0x%04X\n", codepoint); \
for (const char *ch = dst; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("!= "); \
for (const char *ch = expected; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("\n"); \
m_success = false; \
} \
} \
})
#define EXPECT_EQ_CODEPOINT_UTF8(codepoint, expected) \
EXPECT_EQ_CODEPOINT_UTF8_WITH_INDEX(codepoint, expected, 0)
void TestExecutor::testGetUtf8FromCodePoint() {
printf("testGetUtf8FromCodePoint()\n");
size_t index = 0;
char dst[10];
EXPECT_EQ_CODEPOINT_UTF8('a', "\x61");
// Armenian capital letter AYB (2 bytes in UTF8)
EXPECT_EQ_CODEPOINT_UTF8(0x0530, "\xD4\xB0");
// Japanese 'a' (3 bytes in UTF8)
EXPECT_EQ_CODEPOINT_UTF8(0x3042, "\xE3\x81\x82");
// Kanji
EXPECT_EQ_CODEPOINT_UTF8(0x65E5, "\xE6\x97\xA5");
// PUA (4 byets in UTF8)
EXPECT_EQ_CODEPOINT_UTF8(0xFE016, "\xF3\xBE\x80\x96");
EXPECT_EQ_CODEPOINT_UTF8(0xFE972, "\xF3\xBE\xA5\xB2");
EXPECT_EQ_CODEPOINT_UTF8_WITH_INDEX(0x058F, "\xD6\x8F", 3);
index = 0;
if (GetUtf8FromCodePoint(0x3043, dst, 2, &index)) {
printf("GetUtf8FromCodePont() returned true even when destination length"
"is not enough\n");
m_success = false;
}
}
#define EXPECT_EQ_UTF8_UTF8(src, expected) \
({ \
if (!GetPhoneticallySortableString(src, &dst, &len)) { \
printf("GetPhoneticallySortableString() returned false.\n"); \
m_success = false; \
} else { \
if (strcmp(dst, expected) != 0) { \
for (const char *ch = dst; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("!= "); \
for (const char *ch = expected; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("\n"); \
m_success = false; \
} \
free(dst); \
} \
})
void TestExecutor::testGetPhoneticallySortableString() {
printf("testGetPhoneticallySortableString()\n");
char *dst;
size_t len;
// halfwidth alphabets -> fullwidth alphabets.
EXPECT_EQ_UTF8_UTF8("ABCD",
"\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3\xEF\xBC\xA4");
// halfwidth/fullwidth-katakana -> hiragana
EXPECT_EQ_UTF8_UTF8(
"\xE3\x81\x82\xE3\x82\xA4\xE3\x81\x86\xEF\xBD\xB4\xE3\x82\xAA",
"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A");
// whitespace -> string which should be placed at last
EXPECT_EQ_UTF8_UTF8(" \t", "\xF0\x9F\xBF\xBD");
}
#undef EXPECT_EQ_UTF8_UTF8
#define EXPECT_EQ_UTF8_UTF8(src, expected) \
({ \
if (!GetNormalizedString(src, &dst, &len)) { \
printf("GetPhoneticallySortableString() returned false.\n"); \
m_success = false; \
} else { \
if (strcmp(dst, expected) != 0) { \
for (const char *ch = dst; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("!= "); \
for (const char *ch = expected; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("\n"); \
m_success = false; \
} \
free(dst); \
} \
})
void TestExecutor::testGetNormalizedString() {
printf("testGetNormalizedString()\n");
char *dst;
size_t len;
// halfwidth alphabets/symbols -> keep it as is.
EXPECT_EQ_UTF8_UTF8("ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%^&'()",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%^&'()");
EXPECT_EQ_UTF8_UTF8("abcdefghijklmnopqrstuvwxyz[]{}\\@/",
"abcdefghijklmnopqrstuvwxyz[]{}\\@/");
// halfwidth/fullwidth-katakana -> hiragana
EXPECT_EQ_UTF8_UTF8(
"\xE3\x81\x82\xE3\x82\xA4\xE3\x81\x86\xEF\xBD\xB4\xE3\x82\xAA",
"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A");
// whitespace -> keep it as is.
EXPECT_EQ_UTF8_UTF8(" \t", " \t");
}
int main() {
TestExecutor executor;
if(executor.DoAllTests()) {
return 0;
} else {
return 1;
}
}