2014-12-22 14:21:24 +01:00
|
|
|
#include <platform.h>
|
|
|
|
#include "parser.h"
|
|
|
|
|
2016-06-07 21:51:28 +02:00
|
|
|
#include <string.h>
|
2016-11-14 22:21:32 +01:00
|
|
|
#include <errno.h>
|
2014-12-22 14:21:24 +01:00
|
|
|
#include <CuTest.h>
|
|
|
|
|
2016-11-14 22:21:32 +01:00
|
|
|
static void test_atoip(CuTest *tc) {
|
|
|
|
CuAssertIntEquals(tc, 0, atoip("ALLES"));
|
|
|
|
CuAssertIntEquals(tc, 0, errno);
|
|
|
|
CuAssertIntEquals(tc, 42, atoip("42"));
|
|
|
|
CuAssertIntEquals(tc, 0, atoip("-1"));
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:47:09 +02:00
|
|
|
static void test_parse_token(CuTest *tc) {
|
|
|
|
char lbuf[8];
|
|
|
|
const char *tok;
|
|
|
|
const char *str, *orig;
|
|
|
|
|
|
|
|
orig = str = "SHORT TOKEN";
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, (void *)(orig+5), (void *)str);
|
|
|
|
CuAssertStrEquals(tc, "SHORT", tok);
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
2016-06-07 21:51:28 +02:00
|
|
|
CuAssertPtrEquals(tc, (void *)(orig + strlen(orig)), (void *)str);
|
2016-06-07 21:47:09 +02:00
|
|
|
CuAssertStrEquals(tc, "TOKEN", tok);
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_parse_token_limit(CuTest *tc) {
|
|
|
|
char lbuf[8];
|
|
|
|
const char *tok;
|
|
|
|
const char *str, *orig;
|
|
|
|
|
|
|
|
orig = str = "LONG_TOKEN";
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
2016-06-07 21:51:28 +02:00
|
|
|
CuAssertPtrEquals(tc, (void *)(orig + strlen(orig)), (void *)str);
|
2016-06-07 21:47:09 +02:00
|
|
|
CuAssertStrEquals(tc, tok, "LONG_TO");
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_parse_token_limit_utf8(CuTest *tc) {
|
|
|
|
char lbuf[8];
|
|
|
|
const char *tok;
|
|
|
|
const char *orig = "a\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f"; /* auml ouml uuml szlig, 8 bytes long */
|
|
|
|
const char *str = orig+1;
|
|
|
|
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, (void *)(orig + strlen(orig)), (void *)str);
|
|
|
|
CuAssertStrEquals(tc, tok, "\xc3\xa4\xc3\xb6\xc3\xbc"); // just three letters fit, 6 bytes long
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)tok);
|
|
|
|
|
|
|
|
str = orig; // now with an extra byte in the front, maxing out lbuf exactly
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, (void *)(orig + strlen(orig)), (void *)str);
|
|
|
|
CuAssertStrEquals(tc, tok, "a\xc3\xa4\xc3\xb6\xc3\xbc");
|
|
|
|
tok = parse_token(&str, lbuf, sizeof(lbuf));
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)tok);
|
|
|
|
}
|
|
|
|
|
2014-12-22 16:28:17 +01:00
|
|
|
static void test_gettoken(CuTest *tc) {
|
|
|
|
char token[128];
|
2014-12-22 14:21:24 +01:00
|
|
|
init_tokens_str("HELP ONE TWO THREE");
|
2014-12-22 16:28:17 +01:00
|
|
|
CuAssertStrEquals(tc, "HELP", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "HELP", token);
|
|
|
|
CuAssertStrEquals(tc, "ONE", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "TWO", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "THREE", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "", token);
|
2014-12-22 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 15:55:55 +01:00
|
|
|
static void test_gettoken_short(CuTest *tc) {
|
|
|
|
char token[3];
|
|
|
|
init_tokens_str("HELP ONE TWO THREE");
|
|
|
|
CuAssertStrEquals(tc, "HE", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "HE", token);
|
|
|
|
CuAssertStrEquals(tc, "ON", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "TW", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "TH", gettoken(token, sizeof(token)));
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)gettoken(token, sizeof(token)));
|
|
|
|
CuAssertStrEquals(tc, "", token);
|
|
|
|
}
|
|
|
|
|
2014-12-22 16:31:10 +01:00
|
|
|
static void test_skip_token(CuTest *tc) {
|
|
|
|
char token[128];
|
|
|
|
init_tokens_str("HELP ONE TWO THREE");
|
|
|
|
skip_token();
|
|
|
|
CuAssertStrEquals(tc, "ONE", gettoken(token, sizeof(token)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_getintegers(CuTest *tc) {
|
|
|
|
init_tokens_str("ii 666 666 -42 -42");
|
|
|
|
CuAssertIntEquals(tc, 666, getid());
|
|
|
|
CuAssertIntEquals(tc, 666, getint());
|
|
|
|
CuAssertIntEquals(tc, 666, getuint());
|
|
|
|
CuAssertIntEquals(tc, -42, getint());
|
|
|
|
CuAssertIntEquals(tc, 0, getuint());
|
|
|
|
CuAssertIntEquals(tc, 0, getint());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_getstrtoken(CuTest *tc) {
|
|
|
|
init_tokens_str("HELP ONE TWO THREE");
|
|
|
|
CuAssertStrEquals(tc, "HELP", getstrtoken());
|
|
|
|
CuAssertStrEquals(tc, "ONE", getstrtoken());
|
|
|
|
CuAssertStrEquals(tc, "TWO", getstrtoken());
|
|
|
|
CuAssertStrEquals(tc, "THREE", getstrtoken());
|
|
|
|
CuAssertPtrEquals(tc, NULL, (void *)getstrtoken());
|
|
|
|
}
|
|
|
|
|
2014-12-22 14:21:24 +01:00
|
|
|
CuSuite *get_parser_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
2014-12-23 09:23:37 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_atoip);
|
2014-12-22 16:31:10 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_skip_token);
|
2016-06-07 21:47:09 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_parse_token);
|
|
|
|
SUITE_ADD_TEST(suite, test_parse_token_limit);
|
|
|
|
SUITE_ADD_TEST(suite, test_parse_token_limit_utf8);
|
2014-12-22 16:28:17 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_gettoken);
|
2014-12-24 15:55:55 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_gettoken_short);
|
2014-12-22 16:31:10 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_getintegers);
|
|
|
|
SUITE_ADD_TEST(suite, test_getstrtoken);
|
2014-12-22 14:21:24 +01:00
|
|
|
return suite;
|
|
|
|
}
|