2021-07-10 15:10:00 -04:00
|
|
|
#include "greatest.h"
|
|
|
|
#include "../input.h"
|
|
|
|
|
|
|
|
TEST one_pass(void) {
|
|
|
|
struct libscomm_input in;
|
2021-07-10 15:17:56 -04:00
|
|
|
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);
|
2021-07-10 15:17:56 -04:00
|
|
|
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);
|
2021-07-10 15:17:56 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (GREATEST_IS_VERBOSE())
|
|
|
|
fprintf(stderr, "%s", s);
|
|
|
|
|
|
|
|
libscomm_reset(&in);
|
|
|
|
ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE);
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2021-07-10 15:17:56 -04:00
|
|
|
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;
|
2021-07-10 15:17:56 -04:00
|
|
|
size_t i;
|
2021-07-10 15:28:08 -04:00
|
|
|
int ostate;
|
2021-07-10 15:17:56 -04:00
|
|
|
|
|
|
|
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(in.len, 0);
|
|
|
|
|
|
|
|
/* state values are opaque. */
|
|
|
|
ostate = in.state;
|
|
|
|
libscomm_reset(&in);
|
|
|
|
ASSERT_EQ(ostate, in.state);
|
|
|
|
PASS();
|
2021-07-10 15:17:56 -04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2021-07-10 15:10:00 -04:00
|
|
|
GREATEST_MAIN_DEFS();
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
GREATEST_MAIN_BEGIN();
|
|
|
|
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 15:10:00 -04:00
|
|
|
GREATEST_MAIN_END();
|
|
|
|
}
|