2012-06-21 15:31:28 +01:00
|
|
|
#include "ioutil.h"
|
|
|
|
|
|
|
|
#include <check.h>
|
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
START_TEST(test_read_until_newline_returns_line_length_plus_null)
|
2012-06-21 15:31:28 +01:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
int fds[2];
|
|
|
|
int nread;
|
|
|
|
char buf[5] = { 0 };
|
|
|
|
pipe(fds);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
write(fds[1], "1234\n", 5);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
nread = read_until_newline(fds[0], buf, 5);
|
|
|
|
|
|
|
|
ck_assert_int_eq(5, nread);
|
2018-02-20 10:05:35 +00:00
|
|
|
}
|
2018-02-20 10:13:42 +00:00
|
|
|
END_TEST
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
START_TEST(test_read_until_newline_inserts_null)
|
2018-02-20 10:13:42 +00:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
int fds[2];
|
|
|
|
int nread;
|
|
|
|
char buf[5] = { 0 };
|
|
|
|
pipe(fds);
|
|
|
|
|
|
|
|
write(fds[1], "1234\n", 5);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
nread = read_until_newline(fds[0], buf, 5);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
ck_assert_int_eq('\0', buf[4]);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 10:05:35 +00:00
|
|
|
}
|
2018-02-20 10:13:42 +00:00
|
|
|
END_TEST
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
START_TEST(test_read_empty_line_inserts_null)
|
2012-06-21 15:31:28 +01:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
int fds[2];
|
|
|
|
int nread;
|
|
|
|
char buf[5] = { 0 };
|
|
|
|
pipe(fds);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
write(fds[1], "\n", 1);
|
|
|
|
nread = read_until_newline(fds[0], buf, 1);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
ck_assert_int_eq('\0', buf[0]);
|
|
|
|
ck_assert_int_eq(1, nread);
|
2012-06-21 15:31:28 +01:00
|
|
|
}
|
2018-02-20 10:13:42 +00:00
|
|
|
END_TEST
|
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
START_TEST(test_read_eof_returns_err)
|
2012-06-21 15:31:28 +01:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
int fds[2];
|
|
|
|
int nread;
|
|
|
|
char buf[5] = { 0 };
|
|
|
|
pipe(fds);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
close(fds[1]);
|
|
|
|
nread = read_until_newline(fds[0], buf, 5);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
ck_assert_int_eq(-1, nread);
|
2012-06-21 15:31:28 +01:00
|
|
|
}
|
2018-02-20 10:13:42 +00:00
|
|
|
END_TEST
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
START_TEST(test_read_eof_fills_line)
|
2012-06-21 15:31:28 +01:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
int fds[2];
|
|
|
|
int nread;
|
|
|
|
char buf[5] = { 0 };
|
|
|
|
pipe(fds);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
write(fds[1], "1234", 4);
|
|
|
|
close(fds[1]);
|
|
|
|
nread = read_until_newline(fds[0], buf, 5);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
ck_assert_int_eq(-1, nread);
|
|
|
|
ck_assert_int_eq('4', buf[3]);
|
2012-06-21 15:31:28 +01:00
|
|
|
}
|
2018-02-20 10:13:42 +00:00
|
|
|
END_TEST
|
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
START_TEST(test_read_lines_until_blankline)
|
2012-06-21 15:31:28 +01:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
char **lines = NULL;
|
|
|
|
int fds[2];
|
|
|
|
int nlines;
|
|
|
|
pipe(fds);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
write(fds[1], "a\nb\nc\n\n", 7);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
nlines = read_lines_until_blankline(fds[0], 256, &lines);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
ck_assert_int_eq(3, nlines);
|
2012-06-21 15:31:28 +01:00
|
|
|
}
|
2018-02-20 10:13:42 +00:00
|
|
|
END_TEST
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
Suite * ioutil_suite(void)
|
2012-06-21 15:31:28 +01:00
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
Suite *s = suite_create("ioutil");
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
TCase *tc_read_until_newline = tcase_create("read_until_newline");
|
|
|
|
TCase *tc_read_lines_until_blankline =
|
|
|
|
tcase_create("read_lines_until_blankline");
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
tcase_add_test(tc_read_until_newline,
|
|
|
|
test_read_until_newline_returns_line_length_plus_null);
|
|
|
|
tcase_add_test(tc_read_until_newline,
|
|
|
|
test_read_until_newline_inserts_null);
|
|
|
|
tcase_add_test(tc_read_until_newline,
|
|
|
|
test_read_empty_line_inserts_null);
|
|
|
|
tcase_add_test(tc_read_until_newline, test_read_eof_returns_err);
|
|
|
|
tcase_add_test(tc_read_until_newline, test_read_eof_fills_line);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
tcase_add_test(tc_read_lines_until_blankline,
|
|
|
|
test_read_lines_until_blankline);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
suite_add_tcase(s, tc_read_until_newline);
|
|
|
|
suite_add_tcase(s, tc_read_lines_until_blankline);
|
2012-06-21 15:31:28 +01:00
|
|
|
|
2018-02-20 11:02:33 +00:00
|
|
|
return s;
|
2012-06-21 15:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2018-02-20 11:02:33 +00:00
|
|
|
int number_failed;
|
|
|
|
|
|
|
|
Suite *s = ioutil_suite();
|
|
|
|
SRunner *sr = srunner_create(s);
|
|
|
|
srunner_run_all(sr, CK_NORMAL);
|
|
|
|
number_failed = srunner_ntests_failed(sr);
|
|
|
|
srunner_free(sr);
|
|
|
|
return (number_failed == 0) ? 0 : 1;
|
2012-06-21 15:31:28 +01:00
|
|
|
}
|