libscomp/test/input.c

201 lines
4.5 KiB
C

#include "greatest.h"
#include "../input.h"
TEST one_pass(void) {
struct libscomm_input in;
char s[] = "Arg 1\tArg \r\v\t!\n";
char *p = s;
struct libscomm_line l;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(l.len, 3);
ASSERT_EQ(l.name, 0);
ASSERT_EQ(*p, 0);
ASSERT_EQ(p, &s[sizeof s - 1]);
ASSERT_STR_EQ(l.buf[0], "Arg 1");
ASSERT_STR_EQ(l.buf[1], "Arg \r\v");
ASSERT_STR_EQ(l.buf[2], "!");
PASS();
}
TEST line_limit(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[LIBSCOMM_MAXBUF + 1];
char *p = s;
int i;
for (i = 0; i < LIBSCOMM_MAXBUF - 1; i++) {
s[i] = (i + 1) % 64 ? 'a' : '\t';
}
s[LIBSCOMM_MAXBUF - 1] = '\n';
s[LIBSCOMM_MAXBUF] = 0;
#if 0
if (GREATEST_IS_VERBOSE())
fprintf(stderr, "%s", s);
#endif
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(p, &s[LIBSCOMM_MAXBUF]);
PASS();
}
TEST single_arg_too_large(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[LIBSCOMM_MAXBUF + 12];
char *p;
size_t i;
int ostate;
for (i = 0; i < sizeof s; i++)
s[i] = 'a';
s[sizeof s - 2] = '\n';
s[sizeof s - 1] = 0;
p = s;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_OVERFLOW);
ASSERT_EQ(p, &s[sizeof s - 1]);
ASSERT_EQ(in.len, 0);
/* state values are opaque. */
ostate = in.state;
libscomm_reset(&in);
ASSERT_EQ(ostate, in.state);
PASS();
}
TEST arg_limit(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[] = " 1\t 2\t 3\t 4\t 5\t 6\t 7\t 8\t 9\t 10\t 11\t 12\t 13\t 14\t 15\t 16\t 17\t 18\t 19\t 20\t 21\t 22\t 23\t 24\t 25\t 26\t 27\t 28\t 29\t 30\t 31\t 32\n";
char *p = s;
char tbuf[10];
int i;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(l.name, 0);
ASSERT_EQ(l.len, LIBSCOMM_MAXARG);
ASSERT_EQ(p, &s[sizeof s - 1]);
for (i = 1; i <= l.len; i++) {
sprintf(tbuf, " %d", i);
ASSERT_STR_EQ(l.buf[i-1], tbuf);
}
PASS();
}
TEST too_many_arg(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[] = " 1\t 2\t 3\t 4\t 5\t 6\t 7\t 8\t 9\t 10\t 11\t 12\t 13\t 14\t 15\t 16\t 17\t 18\t 19\t 20\t 21\t 22\t 23\t 24\t 25\t 26\t 27\t 28\t 29\t 30\t 31\t 32\t 33\n";
char *p = s;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_ARG_OVERFLOW);
ASSERT_EQ(p, &s[sizeof s - 1]);
PASS();
}
TEST two_in_one(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[] = "Arg 1\tArg 2\tArg 3\nArg 4\tArg 5\tArg 6\tArg 7\n";
char *p = s;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(l.name, 0);
ASSERT_EQ(l.len, 3);
ASSERT_STR_EQ(l.buf[0], "Arg 1");
ASSERT_STR_EQ(l.buf[1], "Arg 2");
ASSERT_STR_EQ(l.buf[2], "Arg 3");
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(l.name, 0);
ASSERT_EQ(l.len, 4);
ASSERT_STR_EQ(l.buf[0], "Arg 4");
ASSERT_STR_EQ(l.buf[1], "Arg 5");
ASSERT_STR_EQ(l.buf[2], "Arg 6");
ASSERT_STR_EQ(l.buf[3], "Arg 7");
PASS();
}
TEST label(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[] = ":N a m e\tArg 1\tArg 2\tArg 3\n";
char *p = s;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(l.name, 1);
ASSERT_EQ(l.len, 4);
ASSERT_STR_EQ(l.buf[0], ":N a m e");
ASSERT_STR_EQ(l.buf[1], "Arg 1");
ASSERT_STR_EQ(l.buf[2], "Arg 2");
ASSERT_STR_EQ(l.buf[3], "Arg 3");
PASS();
}
TEST split(void) {
struct libscomm_input in;
struct libscomm_line l;
char s1[] = ":123456\tArg 1\tArg 2\tAr";
char s2[] = "g 3\tArg 4\tArg 5\tArg 6";
char s3[] = "\n";
char *p = s1;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_MORE);
ASSERT_EQ(p, &s1[sizeof s1 - 1]);
p = s2;
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_MORE);
ASSERT_EQ(p, &s2[sizeof s2 - 1]);
p = s3;
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(p, &s3[sizeof s3 - 1]);
ASSERT_EQ(l.name, 1);
ASSERT_EQ(l.len, 7);
ASSERT_STR_EQ(l.buf[0], ":123456");
ASSERT_STR_EQ(l.buf[1], "Arg 1");
ASSERT_STR_EQ(l.buf[2], "Arg 2");
ASSERT_STR_EQ(l.buf[3], "Arg 3");
ASSERT_STR_EQ(l.buf[4], "Arg 4");
ASSERT_STR_EQ(l.buf[5], "Arg 5");
ASSERT_STR_EQ(l.buf[6], "Arg 6");
PASS();
}
GREATEST_MAIN_DEFS();
int main(int argc, char *argv[]) {
GREATEST_MAIN_BEGIN();
RUN_TEST(one_pass);
RUN_TEST(line_limit);
RUN_TEST(single_arg_too_large);
RUN_TEST(arg_limit);
RUN_TEST(too_many_arg);
RUN_TEST(two_in_one);
RUN_TEST(label);
RUN_TEST(split);
GREATEST_MAIN_END();
}