2015-01-12 22:53:21 +01:00
|
|
|
#include <platform.h>
|
2014-10-19 06:42:40 +02:00
|
|
|
#include <CuTest.h>
|
|
|
|
#include "unicode.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-11-08 22:54:47 +01:00
|
|
|
static void test_unicode_mkname(CuTest * tc)
|
|
|
|
{
|
|
|
|
char buffer[32];
|
2016-11-09 14:58:51 +01:00
|
|
|
CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO W0Rld"));
|
|
|
|
CuAssertStrEquals(tc, "HeLlO W0Rld", buffer);
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
CuAssertIntEquals(tc, 1, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO\nW0Rld"));
|
2016-11-08 22:54:47 +01:00
|
|
|
CuAssertStrEquals(tc, "HeLlOW0Rld", buffer);
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
buffer[5] = 'X';
|
|
|
|
CuAssertIntEquals(tc, ENOMEM, unicode_utf8_mkname(buffer, 5, "HeLl\n W0Rld"));
|
|
|
|
CuAssertStrEquals(tc, "HeLl X", buffer);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-10-02 13:19:39 +02:00
|
|
|
const unsigned char uchar_str[] = { 0xc3, 0x98, 0xc5, 0xb8, 0xc2, 0x9d, 'l', 0 }; // ØŸl
|
|
|
|
utf8_t *utf8_str = (utf8_t *)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
|
|
|
}
|
|
|
|
|
2014-10-19 06:42:40 +02:00
|
|
|
CuSuite *get_unicode_suite(void)
|
|
|
|
{
|
2015-01-12 22:53:21 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_unicode_tolower);
|
2016-11-08 22:54:47 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_mkname);
|
2016-10-02 13:12:47 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_unicode_utf8_to_other);
|
2015-01-12 22:53:21 +01:00
|
|
|
return suite;
|
2014-10-19 06:42:40 +02:00
|
|
|
}
|