void clar__assert_equal_file( const char *expected_data, size_t expected_bytes, int ignore_cr, const char *path, const char *file, int line) { char buf[4000]; ssize_t bytes, total_bytes = 0; int fd = p_open(path, O_RDONLY | O_BINARY); cl_assert(fd >= 0); if (expected_data && !expected_bytes) expected_bytes = strlen(expected_data); while ((bytes = p_read(fd, buf, sizeof(buf))) != 0) { clar__assert( bytes > 0, file, line, "error reading from file", path, 1); if (ignore_cr) bytes = strip_cr_from_buf(buf, bytes); if (memcmp(expected_data, buf, bytes) != 0) { int pos; for (pos = 0; pos < bytes && expected_data[pos] == buf[pos]; ++pos) /* find differing byte offset */; p_snprintf( buf, sizeof(buf), "file content mismatch at byte %"PRIdZ, (ssize_t)(total_bytes + pos)); p_close(fd); clar__fail(file, line, path, buf, 1); } expected_data += bytes; total_bytes += bytes; } p_close(fd); clar__assert(!bytes, file, line, "error reading from file", path, 1); clar__assert_equal(file, line, "mismatched file length", 1, "%"PRIuZ, (size_t)expected_bytes, (size_t)total_bytes); }
static void test_file_contents_internal( const char *path, const char *expectedcontents, bool strip_cr) { int fd; char data[1024] = {0}; git_buf buf = GIT_BUF_INIT; size_t expectedlen = strlen(expectedcontents); fd = p_open(path, O_RDONLY); cl_assert(fd >= 0); buf.ptr = data; buf.size = p_read(fd, buf.ptr, 1024); cl_git_pass(p_close(fd)); if (strip_cr) strip_cr_from_buf(&buf); cl_assert_equal_i((int)expectedlen, (int)buf.size); cl_assert_equal_s(expectedcontents, buf.ptr); }