Added more file assertions and file comparison function
This commit is contained in:
parent
9f850ef6fe
commit
b0869165af
4 changed files with 73 additions and 23 deletions
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
53
src/file.c
Normal file
53
src/file.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include <criterion/redirect.h>
|
||||
|
||||
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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue