2017-12-29 06:13:28 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <platform.h>
|
|
|
|
#endif
|
2012-05-31 04:17:08 +02:00
|
|
|
#include <CuTest.h>
|
2012-05-15 09:06:27 +02:00
|
|
|
#include <ctype.h>
|
2012-05-15 05:08:11 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "umlaut.h"
|
|
|
|
|
2012-05-15 09:06:27 +02:00
|
|
|
static void test_transliterate(CuTest * tc)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
char buffer[32];
|
2012-05-15 09:06:27 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "", transliterate(buffer, sizeof(buffer), ""));
|
|
|
|
CuAssertStrEquals(tc, "herpderp", transliterate(buffer, sizeof(buffer), "herpderp"));
|
|
|
|
CuAssertStrEquals(tc, "herpderp", buffer);
|
2012-05-15 09:06:27 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertStrEquals(tc, "herpderp", transliterate(buffer, sizeof(buffer), "HERPDERP"));
|
|
|
|
CuAssertStrEquals(tc, "haerpdaerp", transliterate(buffer, sizeof(buffer), "h\xc3\xa4rpd\xc3\xa4rp"));
|
|
|
|
CuAssertStrEquals(tc, "aeoeuess", transliterate(buffer, sizeof(buffer), "\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f"));
|
|
|
|
CuAssertStrEquals(tc, "aeoeuess", transliterate(buffer, sizeof(buffer), "\xc3\x84\xc3\x96\xc3\x9c\xe1\xba\x9e"));
|
2012-05-19 22:10:19 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
/* handle buffer that is too small */
|
|
|
|
CuAssertStrEquals(tc, 0, transliterate(buffer, 1, "herpderp"));
|
|
|
|
CuAssertStrEquals(tc, "", buffer);
|
|
|
|
CuAssertStrEquals(tc, 0, transliterate(buffer, 3, "herpderp"));
|
|
|
|
CuAssertStrEquals(tc, "he", buffer);
|
|
|
|
CuAssertStrEquals(tc, 0, transliterate(buffer, 3, "h\xc3\xa4rpd\xc3\xa4rp"));
|
|
|
|
CuAssertStrEquals(tc, "h?", buffer);
|
2012-05-15 09:06:27 +02:00
|
|
|
}
|
2012-05-15 05:08:11 +02:00
|
|
|
|
2015-10-16 12:03:32 +02:00
|
|
|
static void test_transliterations(CuTest *tc) {
|
|
|
|
const char * umlauts = "\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f"; /* auml ouml uuml szlig nul */
|
2016-09-06 20:57:07 +02:00
|
|
|
struct tnode * tokens = 0;
|
2015-10-16 12:03:32 +02:00
|
|
|
variant id;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
id.i = 3;
|
|
|
|
addtoken(&tokens, umlauts, id);
|
|
|
|
/* transliteration is the real magic */
|
|
|
|
result = findtoken(tokens, "AEoeUEss", &id);
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, result);
|
|
|
|
CuAssertIntEquals(tc, 3, id.i);
|
|
|
|
|
|
|
|
result = findtoken(tokens, umlauts, &id);
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, result);
|
|
|
|
CuAssertIntEquals(tc, 3, id.i);
|
|
|
|
|
|
|
|
freetokens(tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_directions(CuTest * tc)
|
|
|
|
{
|
2016-09-06 20:57:07 +02:00
|
|
|
struct tnode * tokens = 0;
|
2015-10-16 12:03:32 +02:00
|
|
|
variant id;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
id.i = 2;
|
|
|
|
addtoken(&tokens, "nw", id);
|
|
|
|
addtoken(&tokens, "northwest", id);
|
|
|
|
|
|
|
|
result = findtoken(tokens, "northw", &id);
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, result);
|
|
|
|
CuAssertIntEquals(tc, 2, id.i);
|
|
|
|
freetokens(tokens);
|
|
|
|
}
|
|
|
|
|
2012-05-15 05:08:11 +02:00
|
|
|
static void test_umlaut(CuTest * tc)
|
|
|
|
{
|
2016-09-06 20:57:07 +02:00
|
|
|
struct tnode *tokens = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
variant id;
|
|
|
|
int result;
|
2012-05-15 05:08:11 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
/* don't crash on an empty set */
|
|
|
|
result = findtoken(tokens, "herpderp", &id);
|
|
|
|
CuAssertIntEquals(tc, E_TOK_NOMATCH, result);
|
2012-05-15 09:06:27 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
id.i = 1;
|
|
|
|
addtoken(&tokens, "herpderp", id);
|
|
|
|
id.i = 2;
|
|
|
|
addtoken(&tokens, "derp", id);
|
2015-10-16 12:03:32 +02:00
|
|
|
addtoken(&tokens, "d", id);
|
2012-05-15 05:08:11 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
/* we can find substrings if they are significant */
|
|
|
|
result = findtoken(tokens, "herp", &id);
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, result);
|
|
|
|
CuAssertIntEquals(tc, 1, id.i);
|
2012-05-15 05:08:11 +02:00
|
|
|
|
2015-10-14 13:42:12 +02:00
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "derp", &id));
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "Derp", &id));
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "dErp", &id));
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "deRp", &id));
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "derP", &id));
|
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "DERP", &id));
|
2015-01-30 20:37:14 +01:00
|
|
|
CuAssertIntEquals(tc, 2, id.i);
|
2012-05-15 05:08:11 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
result = findtoken(tokens, "herp-a-derp", &id);
|
|
|
|
CuAssertIntEquals(tc, E_TOK_NOMATCH, result);
|
2012-05-26 18:05:03 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
freetokens(tokens);
|
2012-05-15 05:08:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 18:46:02 +02:00
|
|
|
static void test_leak(CuTest *tc) {
|
2016-09-06 20:57:07 +02:00
|
|
|
struct tnode *tokens = NULL;
|
2016-09-05 18:46:02 +02:00
|
|
|
variant token;
|
|
|
|
|
|
|
|
token.i = 42;
|
2016-09-06 20:57:07 +02:00
|
|
|
addtoken(&tokens, "NO", token);
|
2016-09-05 20:05:36 +02:00
|
|
|
addtoken(&tokens, "nw", token);
|
2016-09-05 18:48:54 +02:00
|
|
|
CuAssertIntEquals(tc, E_TOK_SUCCESS, findtoken(tokens, "n", &token));
|
2016-09-05 18:46:02 +02:00
|
|
|
freetokens(tokens);
|
|
|
|
}
|
|
|
|
|
2012-05-15 05:08:11 +02:00
|
|
|
CuSuite *get_umlaut_suite(void)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_umlaut);
|
2015-10-16 12:03:32 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_directions);
|
2015-01-30 20:37:14 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_transliterate);
|
2015-10-16 12:03:32 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_transliterations);
|
2016-09-05 18:46:02 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_leak);
|
2015-01-30 20:37:14 +01:00
|
|
|
return suite;
|
2012-05-15 05:08:11 +02:00
|
|
|
}
|