summaryrefslogtreecommitdiff
path: root/android/PhoneticStringUtilsTest.cpp
diff options
context:
space:
mode:
authorJean-Baptiste Queru <jbq@google.com>2009-07-29 14:56:59 -0700
committerJean-Baptiste Queru <jbq@google.com>2009-07-29 14:56:59 -0700
commit37a328e96c5feee101c4071b1729102258dd28bf (patch)
treeb1c0765d5145250a46523190f7375b3f3ed6effc /android/PhoneticStringUtilsTest.cpp
parent2a430a056ac731932633e24b8a1a079776948416 (diff)
parentb020ccd8772182ddfccba30b5adbc500982b59c1 (diff)
merge from donut
Diffstat (limited to 'android/PhoneticStringUtilsTest.cpp')
-rw-r--r--android/PhoneticStringUtilsTest.cpp110
1 files changed, 53 insertions, 57 deletions
diff --git a/android/PhoneticStringUtilsTest.cpp b/android/PhoneticStringUtilsTest.cpp
index e74f67f..356342e 100644
--- a/android/PhoneticStringUtilsTest.cpp
+++ b/android/PhoneticStringUtilsTest.cpp
@@ -20,6 +20,8 @@
#include <stdlib.h>
#include <string.h>
+#include <utils/String8.h>
+
using namespace android;
class TestExecutor {
@@ -29,12 +31,12 @@ class TestExecutor {
private:
void DoOneTest(void (TestExecutor::*test)());
- void testGetCodePointFromUtf8();
+ void testUtf32At();
void testGetPhoneticallySortableCodePointAscii();
void testGetPhoneticallySortableCodePointKana();
void testGetPhoneticallySortableCodePointWhitespaceOnly();
void testGetPhoneticallySortableCodePointSimpleCompare();
- void testGetUtf8FromCodePoint();
+ void testGetUtf8FromUtf32();
void testGetPhoneticallySortableString();
void testGetNormalizedString();
@@ -65,12 +67,12 @@ class TestExecutor {
bool TestExecutor::DoAllTests() {
- DoOneTest(&TestExecutor::testGetCodePointFromUtf8);
+ DoOneTest(&TestExecutor::testUtf32At);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointAscii);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointKana);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointWhitespaceOnly);
DoOneTest(&TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare);
- DoOneTest(&TestExecutor::testGetUtf8FromCodePoint);
+ DoOneTest(&TestExecutor::testGetUtf8FromUtf32);
DoOneTest(&TestExecutor::testGetPhoneticallySortableString);
DoOneTest(&TestExecutor::testGetNormalizedString);
@@ -92,36 +94,45 @@ void TestExecutor::DoOneTest(void (TestExecutor::*test)()) {
m_success_count += m_success ? 1 : 0;
}
-void TestExecutor::testGetCodePointFromUtf8() {
- printf("testGetCodePointFromUtf8()\n");
- int next;
+#define TEST_GET_UTF32AT(src, index, expected_next, expected_value) \
+ ({ \
+ size_t next; \
+ int32_t ret = utf32_at(src, strlen(src), index, &next); \
+ if (ret < 0) { \
+ printf("getUtf32At() returned negative value (src: %s, index: %d)\n", \
+ (src), (index)); \
+ m_success = false; \
+ } else if (next != (expected_next)) { \
+ printf("next is unexpected value (src: %s, actual: %u, expected: %u)\n", \
+ (src), next, (expected_next)); \
+ } else { \
+ EXPECT_EQ_VALUE(ret, (expected_value)); \
+ } \
+ })
+
+void TestExecutor::testUtf32At() {
+ printf("testUtf32At()\n");
- EXPECT_EQ_VALUE(GetCodePointFromUtf8("a", 1, 0, &next), 97);
- EXPECT_EQ_VALUE(next, 1);
+ TEST_GET_UTF32AT("a", 0, 1, 97);
// Japanese hiragana "a"
- EXPECT_EQ_VALUE(GetCodePointFromUtf8("\xE3\x81\x82", 3, 0, &next), 0x3042);
- EXPECT_EQ_VALUE(next, 3);
+ TEST_GET_UTF32AT("\xE3\x81\x82", 0, 3, 0x3042);
// Japanese fullwidth katakana "a" with ascii a
- EXPECT_EQ_VALUE(GetCodePointFromUtf8("a\xE3\x82\xA2", 4, 1, &next), 0x30A2);
- EXPECT_EQ_VALUE(next, 4);
+ TEST_GET_UTF32AT("a\xE3\x82\xA2", 1, 4, 0x30A2);
// 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);
+ TEST_GET_UTF32AT("\xF3\xBE\x80\x80\xF3\xBE\x80\x88", 0, 4, 0xFE000);
+ TEST_GET_UTF32AT("\xF3\xBE\x80\x80\xF3\xBE\x80\x88", 4, 8, 0xFE008);
}
void TestExecutor::testGetPhoneticallySortableCodePointAscii() {
printf("testGetPhoneticallySortableCodePoint()\n");
int halfwidth[94];
int fullwidth[94];
- int i, codepoint;
+ int i;
+ char32_t codepoint;
bool next_is_consumed;
for (i = 0, codepoint = 0x0021; codepoint <= 0x007E; ++i, ++codepoint) {
- halfwidth[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
+ halfwidth[i] = GetPhoneticallySortableCodePoint(codepoint, 0,
&next_is_consumed);
if (halfwidth[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
@@ -135,7 +146,7 @@ void TestExecutor::testGetPhoneticallySortableCodePointAscii() {
}
}
for (i = 0, codepoint = 0xFF01; codepoint <= 0xFF5E; ++i, ++codepoint) {
- fullwidth[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
+ fullwidth[i] = GetPhoneticallySortableCodePoint(codepoint, 0,
&next_is_consumed);
if (fullwidth[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
@@ -158,11 +169,12 @@ void TestExecutor::testGetPhoneticallySortableCodePointKana() {
printf("testGetPhoneticallySortableCodePointKana()\n");
int hiragana[86];
int fullwidth_katakana[86];
- int i, codepoint;
+ int i;
+ char32_t codepoint;
bool next_is_consumed;
for (i = 0, codepoint = 0x3041; codepoint <= 0x3096; ++i, ++codepoint) {
- hiragana[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
+ hiragana[i] = GetPhoneticallySortableCodePoint(codepoint, 0,
&next_is_consumed);
if (hiragana[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
@@ -177,7 +189,7 @@ void TestExecutor::testGetPhoneticallySortableCodePointKana() {
}
for (i = 0, codepoint = 0x30A1; codepoint <= 0x30F6; ++i, ++codepoint) {
- fullwidth_katakana[i] = GetPhoneticallySortableCodePoint(codepoint, -1,
+ fullwidth_katakana[i] = GetPhoneticallySortableCodePoint(codepoint, 0,
&next_is_consumed);
if (fullwidth_katakana[i] < 0) {
printf("returned value become negative at 0x%04X", codepoint);
@@ -194,7 +206,7 @@ void TestExecutor::testGetPhoneticallySortableCodePointKana() {
// 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[] = {
+ char32_t 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,
@@ -214,8 +226,8 @@ void TestExecutor::testGetPhoneticallySortableCodePointKana() {
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;
+ char32_t codepoint = halfwidth_katakana[i];
+ char32_t next_codepoint = i + 1 < len ? halfwidth_katakana[i + 1] : 0;
halfwidth_katakana_result[j] =
GetPhoneticallySortableCodePoint(codepoint, next_codepoint,
&next_is_consumed);
@@ -249,7 +261,7 @@ void TestExecutor::testGetPhoneticallySortableCodePointWhitespaceOnly() {
void TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare() {
printf("testGetPhoneticallySortableCodePointSimpleCompare()\n");
- int codepoints[] = {
+ char32_t codepoints[] = {
0x3042, 0x30AB, 0xFF7B, 0x305F, 0x30CA, 0xFF8A, 0x30D0, 0x3071,
0x307E, 0x30E4, 0xFF97, 0x308F, 0x3093, 0x3094, 'A', 'Z',
'0', '9', '!', '/', ':', '?', '[', '`', '{', '~'};
@@ -257,7 +269,7 @@ void TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare() {
bool next_is_consumed;
for (size_t i = 0; i < len - 1; ++i) {
int codepoint_a =
- GetPhoneticallySortableCodePoint(codepoints[i], -1,
+ GetPhoneticallySortableCodePoint(codepoints[i], 0,
&next_is_consumed);
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint_a);
@@ -265,7 +277,7 @@ void TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare() {
return;
}
int codepoint_b =
- GetPhoneticallySortableCodePoint(codepoints[i + 1], -1,
+ GetPhoneticallySortableCodePoint(codepoints[i + 1], 0,
&next_is_consumed);
if (next_is_consumed) {
printf("next_is_consumed become true at 0x%04X", codepoint_b);
@@ -282,20 +294,18 @@ void TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare() {
}
}
-#define EXPECT_EQ_CODEPOINT_UTF8_WITH_INDEX(codepoint, expected, i) \
+#define EXPECT_EQ_CODEPOINT_UTF8(codepoint, expected) \
({ \
- index = i; \
- if (!GetUtf8FromCodePoint(codepoint, dst, 10, &index)) { \
+ char32_t codepoints[1] = {codepoint}; \
+ status_t ret = string8.setTo(codepoints, 1); \
+ if (ret != NO_ERROR) { \
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) { \
+ const char* string = string8.string(); \
+ if (strcmp(string, expected) != 0) { \
printf("Failed at codepoint 0x%04X\n", codepoint); \
- for (const char *ch = dst; *ch != '\0'; ++ch) { \
+ for (const char *ch = string; *ch != '\0'; ++ch) { \
printf("0x%X ", *ch); \
} \
printf("!= "); \
@@ -308,14 +318,9 @@ void TestExecutor::testGetPhoneticallySortableCodePointSimpleCompare() {
} \
})
-#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];
+void TestExecutor::testGetUtf8FromUtf32() {
+ printf("testGetUtf8FromUtf32()\n");
+ String8 string8;
EXPECT_EQ_CODEPOINT_UTF8('a', "\x61");
// Armenian capital letter AYB (2 bytes in UTF8)
@@ -327,15 +332,6 @@ void TestExecutor::testGetUtf8FromCodePoint() {
// 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) \