// vim:fileencoding=utf-8:foldmethod=marker #include "test_file.h" #define DST_CAPACITY 5 wp_intern WpAllocator arena = {0}; wp_intern WpStr8RO test_filename = wpStr8LitRoInitialiserList("wapptest.bin"); wp_intern WpStr8RO new_filename = wpStr8LitRoInitialiserList("wapptest2.bin"); wp_intern WpFile *test_fp = NULL; wp_intern WpI32Array src_array1 = wpArray(i32, 0, 1, 2, 3, 4); wp_intern WpI32Array src_array2 = wpArray(i32, 5, 6, 7, 8, 9); wp_intern WpI32Array src_array3 = wpArray(i32, 10, 11, 12, 13, 14); wp_intern WpI32Array dst_array = wpArrayWithCapacity(i32, DST_CAPACITY, false); wp_intern i32 dst_buf[DST_CAPACITY] = {0}; WpTestFuncResult test_wapp_file_open(void) { arena = wpMemArenaAllocatorInit(KiB(16)); test_fp = wpFileOpen(&arena, &test_filename, WP_ACCESS_WRITE_EX); return wpTesterResult(test_fp != NULL); } WpTestFuncResult test_wapp_file_get_current_position(void) { i64 pos = wpFileGetCurrentPosition(test_fp); return wpTesterResult(pos == 0); } WpTestFuncResult test_wapp_file_seek(void) { wpFileWriteArray((WpArray)src_array1, test_fp, wpArrayCount(src_array1)); i64 seek_result = wpFileSeek(test_fp, 0, WP_SEEK_END); b8 result = seek_result == (i64)(wpArrayCount(src_array1) * wpArrayItemSize(src_array1)); wpFileSeek(test_fp, 0, WP_SEEK_START); return wpTesterResult(result); } WpTestFuncResult test_wapp_file_get_length(void) { i64 length = wpFileGetLength(test_fp); return wpTesterResult(length == (i64)(wpArrayCount(src_array1) * wpArrayItemSize(src_array1))); } WpTestFuncResult test_wapp_file_read(void) { wpFileSeek(test_fp, 0, WP_SEEK_START); u64 byte_count = DST_CAPACITY * sizeof(i32); u64 count = wpFileRead((void *)dst_buf, test_fp, byte_count); b8 result = count == byte_count; // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings u64 index = 0; b8 running = true; while (running) { result = result && (dst_buf[index] == *wpArrayGet(i32, src_array1, index)); ++index; running = index < DST_CAPACITY; } return wpTesterResult(result); } WpTestFuncResult test_wapp_file_write(void) { wpFileSeek(test_fp, 0, WP_SEEK_END); u64 expected_count = wpArrayCount(src_array2) * wpArrayItemSize(src_array2); i64 count = wpFileWrite((void *)src_array2, test_fp, expected_count); return wpTesterResult(count >= 0 && (u64)count == expected_count); } WpTestFuncResult test_wapp_file_read_array(void) { wpFileSeek(test_fp, 0, WP_SEEK_START); u64 count = wpFileReadArray((WpArray)dst_array, test_fp, wpArrayCount(src_array1)); b8 result = count == wpArrayCount(src_array1) && wpArrayCount(dst_array) == wpArrayCount(src_array1); // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // MSVC Spectre mitigation warnings u64 index = 0; b8 running = true; while (running) { result = result && (*wpArrayGet(i32, dst_array, index) == *wpArrayGet(i32, src_array1, index)); ++index; running = index < wpArrayCount(dst_array); } return wpTesterResult(result); } WpTestFuncResult test_wapp_file_write_array(void) { wpFileSeek(test_fp, 0, WP_SEEK_END); i64 count = wpFileWriteArray((WpArray)src_array3, test_fp, wpArrayCount(src_array3)); return wpTesterResult(count >= 0 && (u64)count == wpArrayCount(src_array3)); } WpTestFuncResult test_wapp_file_flush(void) { i32 flush_result = wpFileFlush(test_fp); return wpTesterResult(flush_result == 0); } WpTestFuncResult test_wapp_file_close(void) { i32 close_result = wpFileClose(test_fp); return wpTesterResult(close_result == 0); } WpTestFuncResult test_wapp_file_rename(void) { i32 rename_result = wpFileRename(&test_filename, &new_filename); return wpTesterResult(rename_result == 0); } WpTestFuncResult test_wapp_file_remove(void) { i32 remove_result = wpFileRemove(&new_filename); return wpTesterResult(remove_result == 0); }