Merge branch 'utf8'

This commit is contained in:
Patrick Pelissier committed 2017-12-13 21:58:33 +01:00
commit 349a2931d3
3 files changed
+275 -1

No files matched your search

+77
View File
@@ -32,6 +32,81 @@ static void test1(void)
}
}
static void test_utf8_basic(void)
{
string_t s;
string_init (s);
/* Test internal encode + decode for all characters */
for (string_unicode_t i = 1; i < 0x10ffff; i++) {
if (i < 0xD800U || i > 0xDFFFU) {
string_it_t it;
char buf[5] = {0};
stringi_utf8_encode (buf, i);
/* Low level access for internal testing */
it->ptr = buf;
bool b = string_end_p(it);
assert (b == false);
assert (it->u == i);
assert (string_get_cref(it) == i);
assert (*it->next_ptr == 0);
assert (stringi_utf8_length(buf) == 1);
assert (stringi_utf8_valid_str_p(buf) == true);
/* Higher level access */
string_clean(s);
string_push_u(s, i);
assert(string_utf8_p(s) == true);
assert(string_length_u(s) == 1);
}
}
/* Reject out of range value */
for (string_unicode_t i = 0x110000; i < 0x1fffff; i++) {
string_clean(s);
string_push_u(s, i);
assert(string_utf8_p(s) == false);
}
/* Test rejection of surrogate halves */
for (string_unicode_t i = 0xd800; i <= 0xdfff; i++) {
string_clean(s);
string_push_u(s, i);
assert(string_utf8_p(s) == false);
}
/* NOTE: Non-canonical representation are not rejected */
string_clear(s);
}
static void test_utf8_it(void)
{
string_t s;
string_init(s);
const unsigned int tab[] = { 45, 1458, 25623, 129, 24 };
for(int i = 0; i < 5; i++)
string_push_u (s, tab[i]);
assert (string_length_u(s) == 5);
string_it_t it;
int i = 0;
for(string_it(it, s) ; !string_end_p(it); string_next(it), i++) {
assert (i < 5);
assert (string_get_cref(it) == tab[i]);
}
string_set_str (s, "H€llo René Chaînôr¬");
assert(string_length_u(s) == 19);
string_unicode_t tab2[19];
i = 0;
for(string_it(it, s) ; !string_end_p(it); string_next(it), i++) {
assert (i < 19);
tab2[i] = string_get_cref(it);
}
assert (i == 19);
string_clean(s);
for(i = 0 ; i < 19; i++)
string_push_u(s, tab2[i]);
assert (string_equal_str_p(s, "H€llo René Chaînôr¬"));
string_clear(s);
}
int main(void)
{
@@ -341,5 +416,7 @@ int main(void)
string_clear (s1);
string_clear (s2);
test_utf8_basic();
test_utf8_it();
exit(0);
}