diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2020-04-09 13:46:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-09 13:46:36 -0400 |
commit | 0f0dd17bc5a2c3149684f2a50fbefae09e49be72 (patch) | |
tree | c7c207c995fcc69d9f0b0d338120676853ec3a49 /tests/framework.h | |
parent | bda58bb6886c1fa49a4ecfc5812f38921fb9e7f4 (diff) | |
parent | 5fab65a9f8c026ccd9e4c58cbe1d2aea06c28305 (diff) | |
download | spmc-0f0dd17bc5a2c3149684f2a50fbefae09e49be72.tar.gz |
Merge pull request #17 from jhunkeler/test-get_file_size
Test get file size
Diffstat (limited to 'tests/framework.h')
-rw-r--r-- | tests/framework.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/framework.h b/tests/framework.h index 7770ed7..c083b8a 100644 --- a/tests/framework.h +++ b/tests/framework.h @@ -73,6 +73,49 @@ size_t mock(const char *filename, void *data, size_t size, size_t nelem) { return written; } +/** + * Generate a temporary file of `size` in bytes, filled with `fill_byte`s + * @param size size of new file in bytes + * @param fill_byte byte to write `size` times + * @return temporary file path + */ +char *mock_size(size_t size, const char *fill_byte) { + FILE *fp = NULL; + char *buffer = NULL; + char filename[PATH_MAX] = {"mock_file_of_size.XXXXXX"}; + + if ((buffer = malloc(size)) == NULL) { + perror("unable to allocate buffer"); + exit(errno); + } + + if (fill_byte == NULL) { + return NULL; + } + + if ((mkstemp(filename)) < 0) { + perror("mktemp failed to create temporary file"); + exit(errno); + } + + if ((fp = fopen(filename, "w+")) == NULL) { + perror(filename); + exit(errno); + } + + memset(buffer, fill_byte[0], size); + size_t bytes = fwrite(buffer, sizeof(char), size, fp); + if (bytes == 0 && ferror(fp) != 0) { + fprintf(stderr, "%s: read failed\n", filename); + exit(1); + } + + fclose(fp); + free(buffer); + + return strdup(filename); +} + #define myassert(condition, ...) \ do { \ if (!(condition)) { \ |