#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);
	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(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);

	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);
	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();
}

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);

	GREATEST_MAIN_END();
}