libscomp/test/input.c

201 lines
4.5 KiB
C
Raw Normal View History

2021-07-10 15:10:00 -04:00
#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;
2021-07-10 15:10:00 -04:00
struct libscomm_line l;
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
2021-07-10 15:10:00 -04:00
ASSERT_EQ(l.len, 3);
ASSERT_EQ(l.name, 0);
ASSERT_EQ(*p, 0);
ASSERT_EQ(p, &s[sizeof s - 1]);
2021-07-10 15:10:00 -04:00
ASSERT_STR_EQ(l.buf[0], "Arg 1");
ASSERT_STR_EQ(l.buf[1], "Arg \r\v");
ASSERT_STR_EQ(l.buf[2], "!");
PASS();
}
2021-07-10 16:06:19 -04:00
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;
2021-07-10 16:10:43 -04:00
#if 0
2021-07-10 16:06:19 -04:00
if (GREATEST_IS_VERBOSE())
fprintf(stderr, "%s", s);
2021-07-10 16:10:43 -04:00
#endif
2021-07-10 16:06:19 -04:00
libscomm_reset(&in);
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
ASSERT_EQ(p, &s[LIBSCOMM_MAXBUF]);
2021-07-10 16:06:19 -04:00
PASS();
}
TEST single_arg_too_large(void) {
struct libscomm_input in;
struct libscomm_line l;
char s[LIBSCOMM_MAXBUF + 12];
2021-07-10 15:28:08 -04:00
char *p;
size_t i;
2021-07-10 15:28:08 -04:00
int ostate;
for (i = 0; i < sizeof s; i++)
s[i] = 'a';
2021-07-10 15:28:08 -04:00
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]);
2021-07-10 15:28:08 -04:00
ASSERT_EQ(in.len, 0);
/* state values are opaque. */
ostate = in.state;
libscomm_reset(&in);
ASSERT_EQ(ostate, in.state);
PASS();
}
2021-07-10 15:43:39 -04:00
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]);
2021-07-10 15:43:39 -04:00
for (i = 1; i <= l.len; i++) {
sprintf(tbuf, " %d", i);
ASSERT_STR_EQ(l.buf[i-1], tbuf);
}
PASS();
}
2021-07-10 16:09:34 -04:00
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]);
2021-07-10 16:09:34 -04:00
PASS();
}
2021-07-10 16:16:57 -04:00
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();
}
2021-07-10 16:23:56 -04:00
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();
}
2021-07-10 16:33:27 -04:00
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();
}
2021-07-10 15:10:00 -04:00
GREATEST_MAIN_DEFS();
int main(int argc, char *argv[]) {
GREATEST_MAIN_BEGIN();
2021-07-10 16:10:43 -04:00
2021-07-10 15:10:00 -04:00
RUN_TEST(one_pass);
2021-07-10 16:06:19 -04:00
RUN_TEST(line_limit);
2021-07-10 15:28:08 -04:00
RUN_TEST(single_arg_too_large);
2021-07-10 15:43:39 -04:00
RUN_TEST(arg_limit);
2021-07-10 16:09:34 -04:00
RUN_TEST(too_many_arg);
2021-07-10 16:16:57 -04:00
RUN_TEST(two_in_one);
2021-07-10 16:23:56 -04:00
RUN_TEST(label);
2021-07-10 16:33:27 -04:00
RUN_TEST(split);
2021-07-10 16:10:43 -04:00
2021-07-10 15:10:00 -04:00
GREATEST_MAIN_END();
}