From c6fdbdbd1b2959aa08162373c7964dea6d78bb30 Mon Sep 17 00:00:00 2001 From: MooZ Date: Fri, 27 Dec 2024 22:09:24 +0100 Subject: [PATCH] Finish cleaning things up --- cli/etripator.c | 398 ++++++++++++++++++++++++------------------------ cli/options.c | 8 +- comment.c | 39 ++--- comment.h | 14 +- decode.c | 6 - label.c | 39 ++--- label.h | 14 +- memory_map.h | 4 +- test/comment.c | 76 ++++----- test/label.c | 102 ++++++------- 10 files changed, 343 insertions(+), 357 deletions(-) diff --git a/cli/etripator.c b/cli/etripator.c index 36757db..b86fb8c 100644 --- a/cli/etripator.c +++ b/cli/etripator.c @@ -64,7 +64,7 @@ void exit_callback() { } // save found labels to a file named .YYmmddHHMMSS.lbl -static bool label_output(CommandLineOptions *options, LabelRepository *repository) { +static bool label_output(LabelRepository *repository, CommandLineOptions *options) { bool ret = false; char buffer[256U] = {0}; if (options->labels_out == NULL) { @@ -122,250 +122,248 @@ static bool log_cli(int argc, const char* argv[]) { return ret; } -/* ---------------------------------------------------------------- */ -int main(int argc, const char **argv) { - int ret = EXIT_FAILURE; - - atexit(exit_callback); - - message_printer_init(); - - if (file_message_printer_init() != true) { - fprintf(stderr, "Failed to setup file printer.\n"); - } else if (console_message_printer_init() != true) { - fprintf(stderr, "Failed to setup console printer.\n"); - } else if (log_cli(argc, argv) != true) { - fprintf(stderr, "Failed to log command line.\n"); - } else { - // [todo] - } - - CommandLineOptions options; - - FILE *out; - FILE *main_file; - int failure; - - MemoryMap map = {0}; - - SectionArray section_arr = {0}; - - failure = 1; - - section_array_reset(§ion_arr); - - /* Extract command line options */ - ret = cli_opt_get(&options, argc, argv); - if (ret <= 0) { - goto error_1; - } - - /* Read configuration file */ - if (options.cfg_filename) { - ret = section_load(§ion_arr, options.cfg_filename); - if (!ret) { - ERROR_MSG("Unable to read %s", options.cfg_filename); - goto error_1; +static bool load_sections(SectionArray *arr, const char *filename) { + bool ret = true; + if (filename) { + if (!section_load(arr, filename)) { + ERROR_MSG("Unable to read %s", filename); + ret = false; } } + return ret; +} - /* Initialize memory map */ - ret = memory_map_init(&map); - if (!ret) { - goto error_1; - } - - /* Read ROM */ - if (!options.cdrom) { - ret = rom_load(options.rom_filename, &map); - if (!ret) { - goto error_2; - } - - /* Get irq offsets */ - if (options.extract_irq) { - ret = irq_read(&map, §ion_arr); +static bool load_labels(LabelRepository *repository, const char **filename) { + bool ret = label_repository_create(repository); + if (filename != NULL) { + for(int i=0; ret && filename[i]; i++) { + ret = label_repository_load(repository, filename[i]); if (!ret) { - ERROR_MSG("An error occured while reading irq vector offsets"); - goto error_2; + ERROR_MSG("An error occured while loading labels from %s", filename[i]); } } - } else { - ret = cd_memory_map(&map); - if (!ret) { - goto error_2; - } + } + return ret; +} - if (options.extract_irq) { - IPL ipl; - ret = ipl_read(&ipl, options.rom_filename); - ret = ret && ipl_sections(&ipl, §ion_arr); +static bool load_comments(CommentRepository *repository, const char **filename) { + bool ret = comment_repository_create(repository); + if(filename != NULL) { + for(int i=0; ret && filename[i]; i++) { + ret = comment_repository_load(repository, filename[i]); if (!ret) { - ERROR_MSG("An error occured while setting up sections from IPL data."); - goto error_2; + ERROR_MSG("An error occured while loading comments from %s", filename[i]); } } - /* Data will be loaded during section disassembly */ } + return ret; +} - CommentRepository *comments_repository = comment_repository_create(); - /* Load comments */ - if(NULL != options.comments_in) { - for(int i=0; options.comments_in[i]; i++) { - ret = comment_repository_load(comments_repository, options.comments_in[i]); - if (!ret) { - ERROR_MSG("An error occured while loading comments from %s : %s", options.comments_in[i], strerror(errno)); - goto error_3; - } - } +static bool load_rom(MemoryMap *map, const CommandLineOptions *options) { + bool ret = false; + if (options->cdrom) { + ret = cd_memory_map(map); + /* Data will be loaded during section disassembly */ + } else { + ret = rom_load(options->rom_filename, map); } + return ret; +} - LabelRepository *repository = label_repository_create(); - /* Load labels */ - if (NULL != options.labels_in) { - for(int i=0; options.labels_in[i]; i++) { - ret = label_repository_load(repository, options.labels_in[i]); - if (!ret) { - ERROR_MSG("An error occured while loading labels from %s : %s", options.labels_in[i], strerror(errno)); - goto error_4; - } +static bool load_irq(MemoryMap *map, SectionArray *arr, const CommandLineOptions *options) { + bool ret = false; + if(!options->extract_irq) { + ret = true; + } else if (options->cdrom) { + IPL ipl = {0}; + if (!ipl_read(&ipl, options->rom_filename)) { + // ... + } else if (!ipl_sections(&ipl, arr)) { + ERROR_MSG("An error occured while setting up sections from IPL data."); + } else { + ret = true; } + } else if (!irq_read(map, arr)) { + ERROR_MSG("An error occured while reading irq vector offsets"); + } else { + ret = true; } + return ret; +} +static bool reset_output(SectionArray *arr) { + bool ret = true; /* For each section reset every existing files */ - for (int i = 0; i < section_arr.count; ++i) { - Section *section = §ion_arr.data[i]; - out = fopen(section->output, "wb"); - if (NULL == out) { + for (int i = 0; ret && (i < arr->count); ++i) { + Section *section = &arr->data[i]; + FILE *out = fopen(section->output, "wb"); + if (out == NULL) { ERROR_MSG("Can't open %s : %s", section->output, strerror(errno)); - goto error_4; + ret = false; + } else { + fclose(out); } - fclose(out); } + return ret; +} +static bool fill_label_reporitory(LabelRepository *labels, SectionArray *arr) { + bool ret = true; /* Add section name to label repository. */ - for (int i = 0; i < section_arr.count; ++i) { - Section *section = §ion_arr.data[i]; - ret = label_repository_add(repository, section->name, section->logical, section->page, section->description); + for (int i = 0; ret && (i < arr->count); ++i) { + Section *section = &arr->data[i]; + ret = label_repository_add(labels, section->name, section->logical, section->page, section->description); if (!ret) { ERROR_MSG("Failed to add section name (%s) to labels", section->name); - goto error_4; } } + return ret; +} - /* Sort & merge sections */ - section_array_tidy(§ion_arr); +static bool output_main(MemoryMap *map, LabelRepository *labels, const CommandLineOptions *options) { + bool ret = false; + FILE *out = fopen(options->main_filename, "w"); + if (out == NULL) { + ERROR_MSG("Unable to open %s : %s", options->main_filename, strerror(errno)); + } else { + label_dump(out, map, labels); + fclose(out); + ret = true; + } + return ret; +} - /* Disassemble and output */ +static bool code_extract(FILE *out, SectionArray *arr, int index, MemoryMap *map, LabelRepository *labels, CommentRepository *comments, int address) { + bool ret = false; + Section *current = &arr->data[index]; + if(current->size <= 0) { + current->size = compute_size(arr, index, arr->count, map); + } + if (!label_extract(current, map, labels)) { + // ... + } else { + /* Process opcodes */ + uint16_t logical = current->logical; + do { + (void)decode(out, &logical, current, map, labels, comments, address); + } while (logical < (current->logical+current->size)); + fputc('\n', out); + ret = true; + } + return true; +} + +static void print_header(FILE *out, Section *current, Section *previous) { + if(previous && ((previous->logical + previous->size) == current->logical)) { + // ... + } else if((current->type != SECTION_TYPE_DATA) || (current->data.type != DATA_TYPE_BINARY)) { + /* Print header */ + fprintf(out, "\t.%s\n" + "\t.bank $%03x\n" + "\t.org $%04x\n", + (current->type == SECTION_TYPE_CODE) ? "code" : "data", current->page, current->logical); + } +} + +static bool disassemble(SectionArray *arr, MemoryMap *map, LabelRepository *labels, CommentRepository *comments, CommandLineOptions *options) { + bool ret = true; Section *previous = NULL; Section *current = NULL; - for (int i = 0; i < section_arr.count; ++i, previous=current) { - current = §ion_arr.data[i]; - out = fopen(current->output, "ab"); - if (!out) { + for (int i = 0; ret && (i < arr->count); ++i, previous=current) { + current = &arr->data[i]; + FILE *out = fopen(current->output, "ab"); + if (out == NULL) { ERROR_MSG("Can't open %s : %s", current->output, strerror(errno)); - goto error_4; - } - - if (options.cdrom && (current->offset != ((current->page << 13) | (current->logical & 0x1fff)))) { - size_t offset = current->offset; - /* Copy CDROM data */ - ret = cd_load(options.rom_filename, current->offset, current->size, options.sector_size, current->page, current->logical, &map); - if (0 == ret) { - ERROR_MSG("Failed to load CD data (section %d)", i); - goto error_4; + } else { + if (options->cdrom && (current->offset != ((current->page << 13) | (current->logical & 0x1fff)))) { + size_t offset = current->offset; + /* Copy CDROM data */ + if (!cd_load(options->rom_filename, current->offset, current->size, options->sector_size, current->page, current->logical, map)) { + ERROR_MSG("Failed to load CD data (section %d)", i); + } else { + ret = false; + } } - } - - if((previous != NULL) && (current->logical < (previous->logical + previous->size)) - && (current->page == previous->page) - && (current->type != previous->type)) { - WARNING_MSG("Section %s and %s overlaps! %x %x.%x", current->name, previous->name); - } - if((previous != NULL) && (0 == strcmp(current->output, previous->output)) - && (current->page == previous->page) - && (current->logical <= (previous->logical + previous->size))) { - // "Merge" sections and adjust size if necessary. - if(current->size > 0) { - uint32_t end0 = previous->logical + previous->size; - uint32_t end1 = current->logical + current->size; - if(end1 > end0) { - current->size = end1 - end0; - current->logical = end0; - INFO_MSG("Section %s has been merged with %s!", current->name, previous->name); + if(ret) { + print_header(out, current, previous); + memory_map_mpr(map, current->mpr); + + if (current->type == SECTION_TYPE_CODE) { + ret = code_extract(out, arr, i, map, labels, comments, options->address); + } else { + ret = data_extract(out, current, map, labels, comments, options->address); } - else { - // The previous section overlaps the current one. - // We skip it as it has already been processed. - fclose(out); - continue; - } - } else { - current->logical = previous->logical + previous->size; - INFO_MSG("Section %s has been merged with %s!", current->name, previous->name); } - } else if((current->type != SECTION_TYPE_DATA) || (current->data.type != DATA_TYPE_BINARY)) { - /* Print header */ - fprintf(out, "\t.%s\n" - "\t.bank $%03x\n" - "\t.org $%04x\n", - (current->type == SECTION_TYPE_CODE) ? "code" : "data", current->page, current->logical); + fclose(out); } + } + return ret; +} - memory_map_mpr(&map, current->mpr); - - if (current->type == SECTION_TYPE_CODE) { - if(current->size <= 0) { - current->size = compute_size(§ion_arr, i, section_arr.count, &map); - } +/* ---------------------------------------------------------------- */ +int main(int argc, const char **argv) { + int ret = EXIT_FAILURE; - /* Extract labels */ - ret = label_extract(current, &map, repository); - if (!ret) { - goto error_4; - } - /* Process opcodes */ - uint16_t logical = current->logical; - do { - (void)decode(out, &logical, current, &map, repository, comments_repository, options.address); - } while (logical < (current->logical+current->size)); - fputc('\n', out); - } else { - ret = data_extract(out, current, &map, repository, comments_repository, options.address); - if (!ret) { - // [todo] - } - } + CommandLineOptions options = {0}; - fclose(out); - } + MemoryMap map = {0}; + SectionArray section_arr = {0}; - /* Open main asm file */ - main_file = fopen(options.main_filename, "a+"); // [todo] - if (!main_file) { - ERROR_MSG("Unable to open %s : %s", options.main_filename, strerror(errno)); - goto error_4; - } - label_dump(main_file, &map, repository); - fclose(main_file); + LabelRepository labels = {0}; + + CommentRepository comments = {0}; + + section_array_reset(§ion_arr); + + atexit(exit_callback); - /* Output labels */ - if (!label_output(&options, repository)) { - goto error_4; + message_printer_init(); + + if (file_message_printer_init() != true) { + fprintf(stderr, "Failed to setup file printer.\n"); + } else if (console_message_printer_init() != true) { + fprintf(stderr, "Failed to setup console printer.\n"); + } else if (log_cli(argc, argv) != true) { + fprintf(stderr, "Failed to log command line.\n"); + } else if (cli_opt_get(&options, argc, argv) != true) { + // ... + } else if (memory_map_init(&map) != true) { + // ... + } else if (!load_sections(§ion_arr, options.cfg_filename)) { + // ... + } else if (!load_labels(&labels, options.labels_in)) { + // ... + } else if (!load_comments(&comments, options.comments_in)) { + // ... + } else if (!load_rom(&map, &options)) { + // ... + } else if (!load_irq(&map, §ion_arr, &options)) { + // ... + } else if (!reset_output(§ion_arr)) { + // ... + } else if (!fill_label_reporitory(&labels, §ion_arr)) { + // ... + } else { + section_array_tidy(§ion_arr); + if(!output_main(&map, &labels, &options)) { + // ... + } else { + ret = EXIT_SUCCESS; + if(!disassemble(§ion_arr, &map, &labels, &comments, &options)) { + ret = EXIT_FAILURE; + } + if (label_output(&labels, &options)) { + ret = EXIT_FAILURE; + } + } } - failure = 0; -error_4: - label_repository_destroy(repository); -error_3: - comment_repository_destroy(comments_repository); -error_2: + label_repository_destroy(&labels); + comment_repository_destroy(&comments); memory_map_destroy(&map); -error_1: - cli_opt_release(&options); - section_array_delete(§ion_arr); + cli_opt_release(&options); - return failure; + return ret; } diff --git a/cli/options.c b/cli/options.c index c409d0e..0357d33 100644 --- a/cli/options.c +++ b/cli/options.c @@ -68,7 +68,7 @@ bool cli_opt_get(CommandLineOptions *options, int argc, const char** argv) { NULL }; - int ret = 0; + bool ret = false; char *dummy; struct payload_t labels_payload = { 0, 0, &options->labels_in }; @@ -112,7 +112,7 @@ bool cli_opt_get(CommandLineOptions *options, int argc, const char** argv) { /* Config file is optional with automatic irq vector extraction. */ options->cfg_filename = NULL; options->rom_filename = argv[0]; - ret = 1; + ret = true; } else { // ... @@ -121,10 +121,10 @@ bool cli_opt_get(CommandLineOptions *options, int argc, const char** argv) { else { options->cfg_filename = argv[0]; options->rom_filename = argv[1]; - ret = 1; + ret = true; } - if(ret == 0) { + if(!ret) { argparse_usage(&argparse); } diff --git a/comment.c b/comment.c index 9bc6d61..9fa0c21 100644 --- a/comment.c +++ b/comment.c @@ -41,13 +41,6 @@ #define COMMENT_ARRAY_INC 16 -/// Comment repository. -struct CommentRepositoryImpl { - size_t size; //< Size of comment repository. - size_t last; //< Last element in the repository. - Comment *comments; //< Comments. -}; - /// Get comment index by its address. /// \param [in] repository Coment repository. /// \param [in] logical Logical address. @@ -66,25 +59,21 @@ static int comment_repository_index(CommentRepository* repository, uint16_t logi } // Create comment repository. -CommentRepository* comment_repository_create() { - CommentRepository *repository; - repository = (CommentRepository*)malloc(sizeof(CommentRepository)); - if(repository == NULL) { - ERROR_MSG("Failed to create comment repository: %s", strerror(errno)); - } else { - repository->last = 0; - repository->comments = NULL; +bool comment_repository_create(CommentRepository *repository) { + assert(repository != NULL); + bool ret = true; - repository->size = COMMENT_ARRAY_INC; - repository->comments = (Comment*)malloc(repository->size * sizeof(Comment)); - if(repository->comments == NULL) { - ERROR_MSG("Failed to create comments: %s", strerror(errno)); - comment_repository_destroy(repository); - free(repository); - repository = NULL; - } - } - return repository; + repository->last = 0; + repository->comments = NULL; + + repository->size = COMMENT_ARRAY_INC; + repository->comments = (Comment*)malloc(repository->size * sizeof(Comment)); + if(repository->comments == NULL) { + ERROR_MSG("Failed to create comments: %s", strerror(errno)); + comment_repository_destroy(repository); + ret = false; + } + return ret; } // Release comment repository resources. diff --git a/comment.h b/comment.h index 7fca303..d909214 100644 --- a/comment.h +++ b/comment.h @@ -45,12 +45,18 @@ typedef struct { char* text; //< Comment text. } Comment; -typedef struct CommentRepositoryImpl CommentRepository; +/// Comment repository. +typedef struct { + size_t size; //< Size of comment repository. + size_t last; //< Last element in the repository. + Comment *comments; //< Comments. +} CommentRepository; /// Create comment repository. -/// \return A pointer to a comment repository. -/// \return NULL if an error occured. -CommentRepository* comment_repository_create(); +/// \param [in out] repository Comment repository. +/// \return true if the repository was succesfully initialized +/// \return false if an error occured +bool comment_repository_create(CommentRepository* repository); /// Release comment repository resources. /// \param [in,out] repository Comment repository. diff --git a/decode.c b/decode.c index 97a84d8..1b306c6 100644 --- a/decode.c +++ b/decode.c @@ -300,12 +300,6 @@ static int data_extract_string(FILE *out, Section *section, MemoryMap *map, Labe uint8_t line_page; Comment comment = {0}; - - fprintf(stdout, "----------------------------> "); - for(k=0; kdata.delimiter_size; k++) { - fprintf(stdout, "%c", section->data.delimiter[k]); - } - fprintf(stdout, "\n"); for (i = 0, j = 0, k = 0, logical = section->logical; i < section->size; i++, logical++) { uint8_t data = memory_map_read(map, logical); uint8_t page = memory_map_page(map, logical); diff --git a/label.c b/label.c index 07f64b4..4eeeec0 100644 --- a/label.c +++ b/label.c @@ -36,14 +36,9 @@ #include "label.h" #include "message.h" -#define LABEL_ARRAY_INC 16 +#include -/// Label repository. -struct LabelRepositoryImpl { - size_t size; //< Size of label repository. - size_t last; //< Last element in the repository. - Label *labels; //< Labels. -}; +#define LABEL_ARRAY_INC 16 /// Get label index by its address. /// \param [in] repository Label repository. @@ -63,23 +58,19 @@ static int label_repository_index(LabelRepository *repository, uint16_t logical, } // Create label repository. -LabelRepository* label_repository_create() { - LabelRepository *repository = (LabelRepository*)malloc(sizeof(LabelRepository)); - if(repository == NULL) { - ERROR_MSG("Failed to create label repository: %s", strerror(errno)); - } else { - repository->last = 0; - repository->labels = NULL; - repository->size = LABEL_ARRAY_INC; - repository->labels = (Label*)malloc(repository->size * sizeof(Label)); - if(repository->labels == NULL) { - ERROR_MSG("Failed to create label: %s", strerror(errno)); - label_repository_destroy(repository); - free(repository); - repository = NULL; - } - } - return repository; +bool label_repository_create(LabelRepository* repository) { + assert(repository != NULL); + bool ret = true; + repository->last = 0; + repository->labels = NULL; + repository->size = LABEL_ARRAY_INC; + repository->labels = (Label*)malloc(repository->size * sizeof(Label)); + if(repository->labels == NULL) { + ERROR_MSG("Failed to create label: %s", strerror(errno)); + label_repository_destroy(repository); + ret = false; + } + return ret; } // Delete label repository. diff --git a/label.h b/label.h index f140d4f..55dd1eb 100644 --- a/label.h +++ b/label.h @@ -46,12 +46,18 @@ typedef struct { char* description; //< Description (optional) } Label; -typedef struct LabelRepositoryImpl LabelRepository; +/// Label repository. +typedef struct { + size_t size; //< Size of label repository. + size_t last; //< Last element in the repository. + Label *labels; //< Labels. +} LabelRepository; /// Create label repository. -/// \return A pointer to a label repository. -/// \return NULL if an error occured. -LabelRepository* label_repository_create(); +/// \param [in out] repository Label repository. +/// \return true if the repository was succesfully initialized +/// \return false if an error occured +bool label_repository_create(LabelRepository* repository); /// Release label repository resources. /// \param [in,out] repository Label repository. diff --git a/memory_map.h b/memory_map.h index 16698ea..b4906fc 100644 --- a/memory_map.h +++ b/memory_map.h @@ -69,8 +69,8 @@ typedef struct { /// Initializes memory map. /// \param [in out] map Memory map. -/// \return true -/// \return false +/// \return true if the memory map was succesfully initialized +/// \return false if an error occured bool memory_map_init(MemoryMap *map); /// Releases resources used by the memory map. diff --git a/test/comment.c b/test/comment.c index 14d0216..717a341 100644 --- a/test/comment.c +++ b/test/comment.c @@ -50,91 +50,93 @@ void tear_down(void* fixture __attribute__((unused))) { MunitResult comment_add_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { Comment comment = {0}; - CommentRepository *repository = comment_repository_create(); - munit_assert_not_null(repository); + CommentRepository repository = {0}; + munit_assert_true(comment_repository_create(&repository)); - munit_assert_true(comment_repository_add(repository, 0x0002, 0x00, "comment #0")); - munit_assert_true(comment_repository_add(repository, 0x0001, 0x00, "comment #1")); - munit_assert_true(comment_repository_add(repository, 0x0003, 0x00, "comment #3")); - munit_assert_true(comment_repository_add(repository, 0x0001, 0x01, "comment #2")); + munit_assert_true(comment_repository_add(&repository, 0x0002, 0x00, "comment #0")); + munit_assert_true(comment_repository_add(&repository, 0x0001, 0x00, "comment #1")); + munit_assert_true(comment_repository_add(&repository, 0x0003, 0x00, "comment #3")); + munit_assert_true(comment_repository_add(&repository, 0x0001, 0x01, "comment #2")); - munit_assert_true(comment_repository_find(repository, 0x0001, 0x01, &comment)); + munit_assert_true(comment_repository_find(&repository, 0x0001, 0x01, &comment)); munit_assert_uint16(comment.logical, ==, 0x0001); munit_assert_uint8(comment.page, ==, 0x01); munit_assert_string_equal(comment.text, "comment #2"); - munit_assert_true(comment_repository_find(repository, 0x0001, 0x00, &comment)); + munit_assert_true(comment_repository_find(&repository, 0x0001, 0x00, &comment)); munit_assert_uint16(comment.logical, ==, 0x0001); munit_assert_uint8(comment.page, ==, 0x00); munit_assert_string_equal(comment.text, "comment #1"); - munit_assert_true(comment_repository_find(repository, 0x0003, 0x00, &comment)); + munit_assert_true(comment_repository_find(&repository, 0x0003, 0x00, &comment)); munit_assert_uint16(comment.logical, ==, 0x0003); munit_assert_uint8(comment.page, ==, 0x00); munit_assert_string_equal(comment.text, "comment #3"); - munit_assert_int(comment_repository_size(repository), ==, 4); + munit_assert_int(comment_repository_size(&repository), ==, 4); - comment_repository_destroy(repository); + comment_repository_destroy(&repository); return MUNIT_OK; } MunitResult comment_delete_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { Comment comment = {0}; - CommentRepository *repository = comment_repository_create(); - munit_assert_not_null(repository); + CommentRepository repository = {0}; + munit_assert_true(comment_repository_create(&repository)); - munit_assert_true(comment_repository_add(repository, 0x0002, 0x03, "comment #7")); - munit_assert_true(comment_repository_add(repository, 0x0002, 0x01, "comment #6")); - munit_assert_true(comment_repository_add(repository, 0x0002, 0x02, "comment #5")); - munit_assert_true(comment_repository_add(repository, 0x000a, 0x00, "comment #4")); - munit_assert_true(comment_repository_add(repository, 0x0008, 0x00, "comment #3")); - munit_assert_true(comment_repository_add(repository, 0x0004, 0x00, "comment #2")); - munit_assert_true(comment_repository_add(repository, 0x0002, 0x00, "comment #1")); - munit_assert_true(comment_repository_add(repository, 0x0000, 0x00, "comment #0")); - munit_assert_int(comment_repository_size(repository), ==, 8); + munit_assert_true(comment_repository_add(&repository, 0x0002, 0x03, "comment #7")); + munit_assert_true(comment_repository_add(&repository, 0x0002, 0x01, "comment #6")); + munit_assert_true(comment_repository_add(&repository, 0x0002, 0x02, "comment #5")); + munit_assert_true(comment_repository_add(&repository, 0x000a, 0x00, "comment #4")); + munit_assert_true(comment_repository_add(&repository, 0x0008, 0x00, "comment #3")); + munit_assert_true(comment_repository_add(&repository, 0x0004, 0x00, "comment #2")); + munit_assert_true(comment_repository_add(&repository, 0x0002, 0x00, "comment #1")); + munit_assert_true(comment_repository_add(&repository, 0x0000, 0x00, "comment #0")); + munit_assert_int(comment_repository_size(&repository), ==, 8); - comment_repository_delete(repository, 0x0001, 0x0009, 0x00); + comment_repository_delete(&repository, 0x0001, 0x0009, 0x00); - munit_assert_int(comment_repository_size(repository), ==, 5); + munit_assert_int(comment_repository_size(&repository), ==, 5); - comment_repository_destroy(repository); + comment_repository_destroy(&repository); return MUNIT_OK; } MunitResult comment_load_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { Comment comment = {0}; - CommentRepository *repository = comment_repository_create(); + CommentRepository repository = {0}; + + munit_assert_true(comment_repository_create(&repository)); - munit_assert_false(comment_repository_load(repository, "/not_here/comment.json")); - munit_assert_int(comment_repository_size(repository), ==, 0); + munit_assert_false(comment_repository_load(&repository, "/not_here/comment.json")); + munit_assert_int(comment_repository_size(&repository), ==, 0); - munit_assert_false(comment_repository_load(repository, "data/comment_1.json")); - munit_assert_int(comment_repository_size(repository), ==, 1); + munit_assert_false(comment_repository_load(&repository, "data/comment_1.json")); + munit_assert_int(comment_repository_size(&repository), ==, 1); - munit_assert_true(comment_repository_get(repository, 0, &comment)); + munit_assert_true(comment_repository_get(&repository, 0, &comment)); munit_assert_uint16(comment.logical, ==, 0xCAFEU); munit_assert_uint8(comment.page, ==, 0x0AU); munit_assert_string_equal(comment.text, "hello!"); - comment_repository_destroy(repository); + comment_repository_destroy(&repository); - munit_assert_true(comment_repository_load(repository, "data/comment_0.json")); - munit_assert_int(comment_repository_size(repository), ==, 2); + munit_assert_true(comment_repository_load(&repository, "data/comment_0.json")); + munit_assert_int(comment_repository_size(&repository), ==, 2); - munit_assert_true(comment_repository_find(repository, 0xC105U, 3, &comment)); + munit_assert_true(comment_repository_find(&repository, 0xC105U, 3, &comment)); munit_assert_uint16(comment.logical, ==, 0xC105U); munit_assert_uint8(comment.page, ==, 3); munit_assert_string_equal(comment.text, "line 0\nline 1\nline 2"); - munit_assert_true(comment_repository_find(repository, 0xEABCU, 0, &comment)); + munit_assert_true(comment_repository_find(&repository, 0xEABCU, 0, &comment)); munit_assert_uint16(comment.logical, ==, 0xEABCU); munit_assert_uint8(comment.page, ==, 0); munit_assert_string_equal(comment.text, "single line comment"); - comment_repository_destroy(repository); + comment_repository_destroy(&repository); return MUNIT_OK; } diff --git a/test/label.c b/test/label.c index 3843991..aa99e6d 100644 --- a/test/label.c +++ b/test/label.c @@ -50,117 +50,117 @@ void tear_down(void* fixture __attribute__((unused))) { MunitResult label_add_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { Label label = {}; - LabelRepository* repository = label_repository_create(); - munit_assert_not_null(repository); + LabelRepository repository = {0}; + munit_assert_true(label_repository_create(&repository)); - munit_assert_true(label_repository_add(repository, "l_0001", 0x0001, 0x00, NULL)); - munit_assert_true(label_repository_add(repository, "l_0020", 0x0020, 0x00, NULL)); - munit_assert_true(label_repository_add(repository, "l_000a", 0x000a, 0xb1, NULL)); - munit_assert_true(label_repository_add(repository, "l_cafe", 0xcafe, 0xf7, NULL)); - munit_assert_true(label_repository_add(repository, "l_0001", 0x0001, 0x00, NULL)); + munit_assert_true(label_repository_add(&repository, "l_0001", 0x0001, 0x00, NULL)); + munit_assert_true(label_repository_add(&repository, "l_0020", 0x0020, 0x00, NULL)); + munit_assert_true(label_repository_add(&repository, "l_000a", 0x000a, 0xb1, NULL)); + munit_assert_true(label_repository_add(&repository, "l_cafe", 0xcafe, 0xf7, NULL)); + munit_assert_true(label_repository_add(&repository, "l_0001", 0x0001, 0x00, NULL)); - munit_assert_int(label_repository_size(repository), ==, 4); + munit_assert_int(label_repository_size(&repository), ==, 4); - munit_assert_true(label_repository_find(repository, 0x000a, 0xb1, &label)); + munit_assert_true(label_repository_find(&repository, 0x000a, 0xb1, &label)); munit_assert_string_equal(label.name, "l_000a"); - munit_assert_true(label_repository_find(repository, 0x0020, 0x00, &label)); + munit_assert_true(label_repository_find(&repository, 0x0020, 0x00, &label)); munit_assert_string_equal(label.name, "l_0020"); - munit_assert_true(label_repository_find(repository, 0xcafe, 0xf7, &label)); + munit_assert_true(label_repository_find(&repository, 0xcafe, 0xf7, &label)); munit_assert_string_equal(label.name, "l_cafe"); - munit_assert_true(label_repository_find(repository, 0x0001, 0x00, &label)); + munit_assert_true(label_repository_find(&repository, 0x0001, 0x00, &label)); munit_assert_string_equal(label.name, "l_0001"); - munit_assert_false(label_repository_find(repository, 0xbeef, 0xac, &label)); + munit_assert_false(label_repository_find(&repository, 0xbeef, 0xac, &label)); - label_repository_destroy(repository); + label_repository_destroy(&repository); return MUNIT_OK; } MunitResult label_delete_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { Label label = {}; - LabelRepository* repository = label_repository_create(); - munit_assert_not_null(repository); - - munit_assert_true(label_repository_add(repository, "label01", 0x0110, 0x1a, NULL)); - munit_assert_true(label_repository_add(repository, "label02", 0x0220, 0x1a, NULL)); - munit_assert_true(label_repository_add(repository, "label03", 0x0330, 0x1b, "description")); - munit_assert_true(label_repository_add(repository, "label04", 0x0440, 0x1a, NULL)); - munit_assert_true(label_repository_add(repository, "label05", 0x0550, 0x1b, NULL)); - munit_assert_true(label_repository_add(repository, "label06", 0x0553, 0x1b, NULL)); - munit_assert_true(label_repository_add(repository, "label07", 0x0555, 0x1b, NULL)); - munit_assert_true(label_repository_add(repository, "label08", 0x0557, 0x1b, NULL)); + LabelRepository repository = {0}; + munit_assert_true(label_repository_create(&repository)); + + munit_assert_true(label_repository_add(&repository, "label01", 0x0110, 0x1a, NULL)); + munit_assert_true(label_repository_add(&repository, "label02", 0x0220, 0x1a, NULL)); + munit_assert_true(label_repository_add(&repository, "label03", 0x0330, 0x1b, "description")); + munit_assert_true(label_repository_add(&repository, "label04", 0x0440, 0x1a, NULL)); + munit_assert_true(label_repository_add(&repository, "label05", 0x0550, 0x1b, NULL)); + munit_assert_true(label_repository_add(&repository, "label06", 0x0553, 0x1b, NULL)); + munit_assert_true(label_repository_add(&repository, "label07", 0x0555, 0x1b, NULL)); + munit_assert_true(label_repository_add(&repository, "label08", 0x0557, 0x1b, NULL)); - munit_assert_int(label_repository_size(repository), ==, 8); + munit_assert_int(label_repository_size(&repository), ==, 8); - label_repository_delete(repository, 0x04a0, 0x0556, 0x1b); + label_repository_delete(&repository, 0x04a0, 0x0556, 0x1b); - munit_assert_int(label_repository_size(repository), ==, 5); + munit_assert_int(label_repository_size(&repository), ==, 5); - munit_assert_true(label_repository_find(repository, 0x0557, 0x1b, &label)); + munit_assert_true(label_repository_find(&repository, 0x0557, 0x1b, &label)); munit_assert_string_equal(label.name, "label08"); - munit_assert_true(label_repository_find(repository, 0x0440, 0x1a, &label)); + munit_assert_true(label_repository_find(&repository, 0x0440, 0x1a, &label)); munit_assert_string_equal(label.name, "label04"); - munit_assert_true(label_repository_find(repository, 0x0330, 0x1b, &label)); + munit_assert_true(label_repository_find(&repository, 0x0330, 0x1b, &label)); munit_assert_string_equal(label.name, "label03"); munit_assert_string_equal(label.description, "description"); - munit_assert_true(label_repository_find(repository, 0x0220, 0x1a, &label)); + munit_assert_true(label_repository_find(&repository, 0x0220, 0x1a, &label)); munit_assert_string_equal(label.name, "label02"); - munit_assert_true(label_repository_find(repository, 0x0110, 0x1a, &label)); + munit_assert_true(label_repository_find(&repository, 0x0110, 0x1a, &label)); munit_assert_string_equal(label.name, "label01"); - munit_assert_false(label_repository_find(repository, 0x0555, 0x1b, &label)); - munit_assert_false(label_repository_find(repository, 0x0553, 0x1b, &label)); - munit_assert_false(label_repository_find(repository, 0x0550, 0x1b, &label)); + munit_assert_false(label_repository_find(&repository, 0x0555, 0x1b, &label)); + munit_assert_false(label_repository_find(&repository, 0x0553, 0x1b, &label)); + munit_assert_false(label_repository_find(&repository, 0x0550, 0x1b, &label)); - label_repository_destroy(repository); + label_repository_destroy(&repository); return MUNIT_OK; } MunitResult label_load_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { Label label = {}; - LabelRepository* repository = label_repository_create(); - munit_assert_not_null(repository); + LabelRepository repository = {0}; + munit_assert_true(label_repository_create(&repository)); - munit_assert_false(label_repository_load(repository, "/not_here/label.json")); - munit_assert_int(label_repository_size(repository), ==, 0); + munit_assert_false(label_repository_load(&repository, "/not_here/label.json")); + munit_assert_int(label_repository_size(&repository), ==, 0); - munit_assert_false(label_repository_load(repository, "data/label_1.json")); - munit_assert_int(label_repository_size(repository), ==, 1); + munit_assert_false(label_repository_load(&repository, "data/label_1.json")); + munit_assert_int(label_repository_size(&repository), ==, 1); - munit_assert_true(label_repository_get(repository, 0, &label)); + munit_assert_true(label_repository_get(&repository, 0, &label)); munit_assert_uint16(label.logical, ==, 0x5030U); munit_assert_uint8(label.page, ==, 4); munit_assert_string_equal(label.name, "ok"); munit_assert_null(label.description); - label_repository_destroy(repository); + label_repository_destroy(&repository); - munit_assert_true(label_repository_load(repository, "data/label_0.json")); - munit_assert_int(label_repository_size(repository), ==, 3); + munit_assert_true(label_repository_load(&repository, "data/label_0.json")); + munit_assert_int(label_repository_size(&repository), ==, 3); - munit_assert_true(label_repository_find(repository, 0x31DC, 0xF8, &label)); + munit_assert_true(label_repository_find(&repository, 0x31DC, 0xF8, &label)); munit_assert_uint16(label.logical, ==, 0x31DCU); munit_assert_uint8(label.page, ==, 0xF8); munit_assert_string_equal(label.name, "var"); munit_assert_null(label.description); - munit_assert_true(label_repository_find(repository, 0xEABC, 0x00, &label)); + munit_assert_true(label_repository_find(&repository, 0xEABC, 0x00, &label)); munit_assert_uint16(label.logical, ==, 0xEABCU); munit_assert_uint8(label.page, ==, 0x00); munit_assert_string_equal(label.name, "do_something"); munit_assert_string_equal(label.description, "do something"); - munit_assert_true(label_repository_find(repository, 0xD6F7, 0x1F, &label)); + munit_assert_true(label_repository_find(&repository, 0xD6F7, 0x1F, &label)); munit_assert_uint16(label.logical, ==, 0xD6F7U); munit_assert_uint8(label.page, ==, 0x1F); munit_assert_string_equal(label.name, "run"); munit_assert_string_equal(label.description, "line0\nline1\nline2\nline3"); - label_repository_destroy(repository); + label_repository_destroy(&repository); return MUNIT_OK; }