From 945bb3ff51b2ac7f874edede480191185dd03f47 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 21 Oct 2023 18:59:14 +0100 Subject: [PATCH] Add pckr_test --- src/pckr_test.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/pckr_test.c diff --git a/src/pckr_test.c b/src/pckr_test.c new file mode 100644 index 0000000..4e934a1 --- /dev/null +++ b/src/pckr_test.c @@ -0,0 +1,111 @@ +#include "aliases.h" +#include "io.h" +#include "pak.h" +#include +#include +#include + +#define ARRLEN(ARR) (sizeof ARR / sizeof ARR[0]) + +typedef struct { + const char *name; + const char *contents; +} asset_t; + +i32 main(i32 argc, char *argv[]) { + FILE *fp = fopen("assets.pak", "rb"); + if (!fp) { + printf("Failed to open file\n"); + + return EXIT_FAILURE; + } + + asset_t assets[] = { + (asset_t){.name = "file01", .contents = "Hello\n"}, + (asset_t){.name = "NAME", .contents = "Abdelrahman\n"}, + (asset_t){.name = "file02", .contents = "This is the second file\n"}, + }; + + u64 expected_pak_size = sizeof(pak_t); + + u64 expected_count = ARRLEN(assets); + + u64 expected_entries_size = 0; + + for (u64 i = 0; i < expected_count; ++i) { + u64 name_length = strlen(assets[i].name) + 1; + u64 contents_length = strlen(assets[i].contents) + 1; + + expected_pak_size += name_length + contents_length + sizeof(toc_entry_t) + + sizeof(pak_entry_t); + + expected_entries_size += name_length + sizeof(toc_entry_t); + } + + u64 real_size = get_file_length(fp); + + if (expected_pak_size != real_size) { + printf("Real size (%zu) doesn't match expected size (%zu)\n", real_size, + expected_pak_size); + + fclose(fp); + + return EXIT_FAILURE; + } + + pak_t pack = {0}; + fread((void *)&pack, sizeof(pak_t), 1, fp); + + if (pack.toc.count != expected_count) { + printf("Pack file count (%zu) doesn't match expected count (%zu)\n", + pack.toc.count, expected_count); + + fclose(fp); + + return EXIT_FAILURE; + } + + if (pack.toc.size != expected_entries_size) { + printf( + "Table of contents real size (%zu) doesn't match expected size (%zu)\n", + pack.toc.size, expected_entries_size); + + fclose(fp); + + return EXIT_FAILURE; + } + + toc_entry_t *entries[pack.toc.count]; + memset((void *)entries, 0, sizeof(toc_entry_t *) * 3); + + for (u64 i = 0; i < pack.toc.count; ++i) { + u64 pos = ftell(fp); + + u64 data[2] = {0}; + + fread(data, ARRLEN(data), 1, fp); + + u64 size = sizeof(toc_entry_t) + data[1]; + + entries[i] = malloc(size); + if (!(entries[i])) { + printf("Allocation failure\n"); + + fclose(fp); + + return EXIT_FAILURE; + } + + fseek(fp, pos, SEEK_SET); + + fread(&(entries[i]), size, 1, fp); + } + + for (u64 i = 0; i < pack.toc.count; ++i) { + free(entries[i]); + } + + fclose(fp); + + return EXIT_SUCCESS; +}