Skip to content

Commit

Permalink
Finish cleaning things up
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Dec 27, 2024
1 parent 5a80062 commit c6fdbdb
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 357 deletions.
398 changes: 198 additions & 200 deletions cli/etripator.c

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions cli/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 {
// ...
Expand All @@ -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);
}

Expand Down
39 changes: 14 additions & 25 deletions comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; k<section->data.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);
Expand Down
39 changes: 15 additions & 24 deletions label.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@
#include "label.h"
#include "message.h"

#define LABEL_ARRAY_INC 16
#include <assert.h>

/// 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.
Expand All @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions label.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions memory_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 39 additions & 37 deletions test/comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit c6fdbdb

Please sign in to comment.