2019-08-01 18:40:42 +02:00
|
|
|
#ifdef _MSC_VER
|
2015-01-12 22:53:21 +01:00
|
|
|
#include <platform.h>
|
2019-08-01 18:40:42 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-19 06:42:40 +02:00
|
|
|
#include "unicode.h"
|
2019-08-01 18:40:42 +02:00
|
|
|
|
|
|
|
#include <CuTest.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-10-19 06:42:40 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-11-11 00:30:49 +01:00
|
|
|
static void test_unicode_trim(CuTest * tc)
|
|
|
|
{
|
|
|
|
char buffer[32];
|
|
|
|
|
2019-08-01 18:40:42 +02:00
|
|
|
strcpy(buffer, "Hello World");
|
2016-11-11 00:30:49 +01:00
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_trim(buffer));
|
2019-08-01 18:40:42 +02:00
|
|
|
CuAssertStrEquals(tc, "Hello World", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, " Hello World");
|
|
|
|
CuAssertIntEquals(tc, 2, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello World", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, "Hello World ");
|
|
|
|
CuAssertIntEquals(tc, 2, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello World", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, " Hello World ");
|
|
|
|
CuAssertIntEquals(tc, 2, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello World", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, "Hello\t\r\nWorld");
|
|
|
|
CuAssertIntEquals(tc, 3, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "HelloWorld", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, "LTR");
|
|
|
|
buffer[3] = -30;
|
|
|
|
buffer[4] = -128;
|
|
|
|
buffer[5] = -114;
|
|
|
|
buffer[6] = 0;
|
|
|
|
CuAssertIntEquals(tc, 3, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "LTR", buffer);
|
2016-11-11 00:30:49 +01:00
|
|
|
|
2016-11-11 01:34:59 +01:00
|
|
|
strcpy(buffer, " Hello Word ");
|
|
|
|
CuAssertIntEquals(tc, 4, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello Word", buffer);
|
|
|
|
|
2016-11-11 00:30:49 +01:00
|
|
|
strcpy(buffer, "Hello Word\n");
|
|
|
|
CuAssertIntEquals(tc, 1, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello Word", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, " Hello Word\t\n");
|
|
|
|
CuAssertIntEquals(tc, 4, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello Word", buffer);
|
|
|
|
|
|
|
|
strcpy(buffer, " \t Hello Word");
|
|
|
|
CuAssertIntEquals(tc, 3, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello Word", buffer);
|
2016-11-20 14:50:38 +01:00
|
|
|
|
2016-11-20 19:08:39 +01:00
|
|
|
buffer[9] = -61;
|
2016-11-20 14:50:38 +01:00
|
|
|
CuAssertIntEquals(tc, 1, unicode_utf8_trim(buffer));
|
|
|
|
CuAssertStrEquals(tc, "Hello Wor?", buffer);
|
2016-11-11 00:30:49 +01:00
|
|
|
}
|
|
|
|
|
2014-10-19 06:42:40 +02:00
|
|
|
static void test_unicode_tolower(CuTest * tc)
|
|
|
|
{
|
|
|
|
char buffer[32];
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_tolower(buffer, sizeof(buffer), "HeLlO W0Rld"));
|
|
|
|
CuAssertStrEquals(tc, "hello w0rld", buffer);
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
2015-01-12 22:53:21 +01:00
|
|
|
buffer[5] = 'X';
|
2014-10-19 06:42:40 +02:00
|
|
|
CuAssertIntEquals(tc, ENOMEM, unicode_utf8_tolower(buffer, 5, "HeLlO W0Rld"));
|
|
|
|
CuAssertStrEquals(tc, "helloX", buffer);
|
|
|
|
}
|
|
|
|
|
2016-10-02 13:12:47 +02:00
|
|
|
static void test_unicode_utf8_to_other(CuTest *tc)
|
2016-10-02 12:46:07 +02:00
|
|
|
{
|
2017-02-18 21:15:14 +01:00
|
|
|
const unsigned char uchar_str[] = { 0xc3, 0x98, 0xc5, 0xb8, 0xc2, 0x9d, 'l', 0 }; /* ØŸl */
|
2019-08-01 18:40:42 +02:00
|
|
|
char *utf8_str = (char *)uchar_str;
|
2016-10-02 13:12:47 +02:00
|
|
|
unsigned char ch;
|
2016-10-02 12:46:07 +02:00
|
|
|
size_t sz;
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp437(&ch, utf8_str, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 12:46:07 +02:00
|
|
|
CuAssertIntEquals(tc, '?', ch);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp437(&ch, utf8_str+2, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, '?', ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp437(&ch, utf8_str+4, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, '?', ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp437(&ch, utf8_str + 6, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 1, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 'l', ch);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp1252(&ch, utf8_str, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 216, ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp1252(&ch, utf8_str+2, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 120, ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp1252(&ch, utf8_str + 4, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 0x9d, ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_cp1252(&ch, utf8_str + 6, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 1, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 'l', ch);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_ascii(&ch, utf8_str, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, '?', ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_ascii(&ch, utf8_str + 2, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, '?', ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_ascii(&ch, utf8_str + 4, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 2, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, '?', ch);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_to_ascii(&ch, utf8_str + 6, &sz));
|
2016-10-09 10:06:51 +02:00
|
|
|
CuAssertIntEquals(tc, 1, (int)sz);
|
2016-10-02 13:12:47 +02:00
|
|
|
CuAssertIntEquals(tc, 'l', ch);
|
2016-10-02 12:46:07 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 14:50:38 +01:00
|
|
|
static void test_unicode_utf8_to_ucs(CuTest *tc) {
|
2019-08-01 18:40:42 +02:00
|
|
|
wint_t wc;
|
2016-11-20 14:50:38 +01:00
|
|
|
size_t sz;
|
|
|
|
|
2019-08-01 18:40:42 +02:00
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_decode(&wc, "a", &sz));
|
|
|
|
CuAssertIntEquals(tc, 'a', wc);
|
2016-11-20 14:50:38 +01:00
|
|
|
CuAssertIntEquals(tc, 1, sz);
|
|
|
|
}
|
|
|
|
|
2017-01-20 22:39:38 +01:00
|
|
|
static void test_unicode_bug2262(CuTest *tc) {
|
|
|
|
char name[7];
|
2019-08-01 18:40:42 +02:00
|
|
|
wint_t wc;
|
2017-01-20 22:39:38 +01:00
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
strcpy(name, "utende");
|
2019-08-01 18:40:42 +02:00
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_decode(&wc, name, &sz));
|
2017-01-20 22:39:38 +01:00
|
|
|
CuAssertIntEquals(tc, 1, sz);
|
2019-08-01 18:40:42 +02:00
|
|
|
CuAssertIntEquals(tc, 'u', wc);
|
2017-01-20 22:39:38 +01:00
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_trim(name));
|
|
|
|
|
2017-02-18 21:15:14 +01:00
|
|
|
name[0] = -4; /* latin1: ü should fail to decode */
|
2019-08-01 18:40:42 +02:00
|
|
|
CuAssertIntEquals(tc, EILSEQ, unicode_utf8_decode(&wc, name, &sz));
|
2017-01-20 22:39:38 +01:00
|
|
|
CuAssertIntEquals(tc, EILSEQ, unicode_utf8_trim(name));
|
|
|
|
}
|
|
|
|
|
2016-08-07 10:28:09 +02:00
|
|
|
static void test_unicode_compare(CuTest *tc)
|
|
|
|
{
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_strcasecmp("ABCDEFG", "abcdefg"));
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_strcasecmp("abcdefg123", "ABCDEFG123"));
|
|
|
|
CuAssertIntEquals(tc, 1, unicode_utf8_strcasecmp("bacdefg123", "ABCDEFG123"));
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:40:42 +02:00
|
|
|
static void test_unicode_trim_zwnj(CuTest *tc) {
|
|
|
|
const char zwnj[] = { 0xe2, 0x80, 0x8c, 0x00 };
|
2017-04-10 17:32:32 +02:00
|
|
|
char name[64];
|
2019-08-01 18:40:42 +02:00
|
|
|
char expect[64];
|
|
|
|
snprintf(name, sizeof(name), "%sA%sB%s ", zwnj, zwnj, zwnj);
|
|
|
|
snprintf(expect, sizeof(expect), "A%sB", zwnj);
|
|
|
|
CuAssertIntEquals(tc, 8, unicode_utf8_trim(name));
|
|
|
|
CuAssertStrEquals(tc, expect, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_unicode_trim_ltrm(CuTest *tc) {
|
|
|
|
const char ltrm[] = { 0xe2, 0x80, 0x8e, 0x00 };
|
|
|
|
char name[64];
|
|
|
|
char expect[64];
|
|
|
|
snprintf(name, sizeof(name), "%sBrot%szeit%s ", ltrm, ltrm, ltrm);
|
|
|
|
snprintf(expect, sizeof(expect), "Brot%szeit", ltrm);
|
|
|
|
CuAssertIntEquals(tc, 8, unicode_utf8_trim(name));
|
|
|
|
CuAssertStrEquals(tc, expect, name);
|
2017-04-10 17:32:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-08 18:23:31 +02:00
|
|
|
static void test_unicode_trim_emoji(CuTest *tc) {
|
|
|
|
const char clock[] = { 0xE2, 0x8F, 0xB0, 0x00 };
|
|
|
|
char name[64];
|
|
|
|
char expect[64];
|
|
|
|
snprintf(name, sizeof(name), "%s Alarm%sClock %s", clock, clock, clock);
|
|
|
|
strcpy(expect, name);
|
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_trim(name));
|
|
|
|
CuAssertStrEquals(tc, expect, name);
|
|
|
|
}
|
|
|
|
|
2014-10-19 06:42:40 +02:00
|
|
|
CuSuite *get_unicode_suite(void)
|
|
|
|
{
|
2015-01-12 22:53:21 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
2016-11-11 00:30:49 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_trim);
|
2019-08-01 18:40:42 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_trim_zwnj);
|
|
|
|
SUITE_ADD_TEST(suite, test_unicode_trim_ltrm);
|
2019-08-08 18:23:31 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_trim_emoji);
|
2016-10-02 13:12:47 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_utf8_to_other);
|
2016-11-20 14:50:38 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_utf8_to_ucs);
|
2016-08-07 10:28:09 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_compare);
|
2019-08-01 18:40:42 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_bug2262);
|
|
|
|
SUITE_ADD_TEST(suite, test_unicode_tolower);
|
2015-01-12 22:53:21 +01:00
|
|
|
return suite;
|
2014-10-19 06:42:40 +02:00
|
|
|
}
|