From b0869165affce34b8b778134cea329eb20e6b815 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 14 Sep 2015 03:58:25 +0200 Subject: [PATCH] Added more file assertions and file comparison function --- CMakeLists.txt | 1 + include/criterion/redirect.h | 19 +++++++++++++ src/file.c | 53 ++++++++++++++++++++++++++++++++++++ src/posix-compat.c | 23 ---------------- 4 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 src/file.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f82d4fe..ef4b09e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,7 @@ set(SOURCE_FILES src/posix-compat.c src/theories.c src/asprintf.c + src/file.c src/main.c src/entry.c ) diff --git a/include/criterion/redirect.h b/include/criterion/redirect.h index e88fc89..2b8492f 100644 --- a/include/criterion/redirect.h +++ b/include/criterion/redirect.h @@ -50,6 +50,7 @@ CR_API CR_STDN FILE* cr_get_redirected_stderr(void); CR_API CR_STDN FILE* cr_get_redirected_stdin(void); CR_API int cr_file_match_str(CR_STDN FILE* f, const char *str); +CR_API int cr_file_match_file(CR_STDN FILE* f, CR_STDN FILE* ref); CR_END_C_API @@ -79,6 +80,12 @@ CR_END_C_API # define cr_assert_file_contents_neq_str(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_str, !=, __VA_ARGS__)) # define cr_expect_file_contents_neq_str(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_str, !=, __VA_ARGS__)) +# define cr_assert_file_contents_eq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_file, ==, __VA_ARGS__)) +# define cr_expect_file_contents_eq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_file, ==, __VA_ARGS__)) + +# define cr_assert_file_contents_neq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_file, !=, __VA_ARGS__)) +# define cr_expect_file_contents_neq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_file, !=, __VA_ARGS__)) + # define cr_assert_stdout_eq_str(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_str, ==, cr_get_redirected_stdout(), __VA_ARGS__)) # define cr_expect_stdout_eq_str(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_str, ==, cr_get_redirected_stdout(), __VA_ARGS__)) @@ -91,6 +98,18 @@ CR_END_C_API # define cr_assert_stderr_neq_str(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_str, !=, cr_get_redirected_stderr(), __VA_ARGS__)) # define cr_expect_stderr_neq_str(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_str, !=, cr_get_redirected_stderr(), __VA_ARGS__)) +# define cr_assert_stdout_eq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_file, ==, cr_get_redirected_stdout(), __VA_ARGS__)) +# define cr_expect_stdout_eq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_file, ==, cr_get_redirected_stdout(), __VA_ARGS__)) + +# define cr_assert_stdout_neq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_file, !=, cr_get_redirected_stdout(), __VA_ARGS__)) +# define cr_expect_stdout_neq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_file, !=, cr_get_redirected_stdout(), __VA_ARGS__)) + +# define cr_assert_stderr_eq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_file, ==, cr_get_redirected_stderr(), __VA_ARGS__)) +# define cr_expect_stderr_eq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_file, ==, cr_get_redirected_stderr(), __VA_ARGS__)) + +# define cr_assert_stderr_neq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_ABORT_, cr_file_match_file, !=, cr_get_redirected_stderr(), __VA_ARGS__)) +# define cr_expect_stderr_neq(...) CR_EXPAND(cr_assert_redir_op_va_(CR_FAIL_CONTINUES_, cr_file_match_file, !=, cr_get_redirected_stderr(), __VA_ARGS__)) + # ifdef __cplusplus namespace criterion { diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..e48e183 --- /dev/null +++ b/src/file.c @@ -0,0 +1,53 @@ +#include + +int cr_file_match_str(FILE* f, const char *str) { + size_t len = strlen(str); + + char buf[512]; + size_t read; + int matches = 0; + while ((read = fread(buf, 1, sizeof (buf), f)) > 0) { + matches = !strncmp(buf, str, read); + if (!matches || read > len) { + matches = 0; + break; + } + + len -= read; + str += read; + } + + // consume the rest of what's available + while (fread(buf, 1, sizeof (buf), f) > 0); + + return matches; +} + +int cr_file_match_file(FILE* f, FILE* ref) { + char buf1[512]; + char buf2[512]; + + fpos_t orig_pos; + fgetpos(ref, &orig_pos); + rewind(ref); + + size_t read1 = 1, read2 = 1; + int matches = 0; + while ((read1 = fread(buf1, 1, sizeof (buf1), f)) > 0 + && (read2 = fread(buf2, 1, sizeof (buf2), ref)) > 0) { + + if (read1 != read2) { + matches = 0; + break; + } + + matches = !memcmp(buf1, buf2, read1); + } + + // consume the rest of what's available + while (fread(buf1, 1, sizeof (buf1), f) > 0); + + fsetpos(ref, &orig_pos); + + return matches; +} diff --git a/src/posix-compat.c b/src/posix-compat.c index 21c2c33..72dd6db 100644 --- a/src/posix-compat.c +++ b/src/posix-compat.c @@ -581,26 +581,3 @@ FILE* cr_get_redirected_stdin(void) { } return f; } - -int cr_file_match_str(FILE* f, const char *str) { - size_t len = strlen(str); - - char buf[512]; - size_t read; - int matches = 0; - while ((read = fread(buf, 1, sizeof (buf), f)) > 0) { - matches = !strncmp(buf, str, read); - if (!matches || read > len) { - matches = 0; - break; - } - - len -= read; - str += read; - } - - // consume the rest of what's available - while (fread(buf, 1, sizeof (buf), f) > 0); - - return matches; -}