Skip to content

Commit

Permalink
Add unit test for seek history serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
laomaiweng committed Sep 10, 2023
1 parent 7c6a8ce commit e1ba294
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unit/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ if get_option('enable_tests')
'serialize_config',
'serialize_debug',
'serialize_flag',
'serialize_seek',
'serialize_spaces',
'serialize_types',
'skiplist',
Expand Down
98 changes: 98 additions & 0 deletions test/unit/test_serialize_seek.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-FileCopyrightText: 2023 Quentin Minster <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only

#include <rz_core.h>
#include "minunit.h"
#include "test_sdb.h"

Sdb *get_ref_sdb() {
Sdb *ref_sdb = sdb_new0();

sdb_set(ref_sdb, "-1", "{\"offset\":16,\"cursor\":1,\"current\":false}", 0);
sdb_set(ref_sdb, "0", "{\"offset\":32,\"cursor\":2,\"current\":true}", 0);
sdb_set(ref_sdb, "1", "{\"offset\":48,\"cursor\":3,\"current\":false}", 0);
sdb_set(ref_sdb, "2", "{\"offset\":64,\"cursor\":4,\"current\":false}", 0);

return ref_sdb;
}

bool test_seek_serialize_save() {
RzCore *core = rz_core_new();
mu_assert_notnull(core, "core null");
rz_core_file_open(core, "malloc://0x3000", RZ_PERM_R, 0);

bool sought = rz_core_seek(core, 0x10, false);
rz_print_set_cursor(core->print, true, 0, 1);
mu_assert_true(sought, "failed to seek 0x10");
sought = rz_core_seek_and_save(core, 0x20, false);
rz_print_set_cursor(core->print, true, 0, 2);
mu_assert_true(sought, "failed to seek 0x20");
sought = rz_core_seek_and_save(core, 0x30, false);
rz_print_set_cursor(core->print, true, 0, 3);
mu_assert_true(sought, "failed to seek 0x30");
sought = rz_core_seek_and_save(core, 0x40, false);
rz_print_set_cursor(core->print, true, 0, 4);
mu_assert_true(sought, "failed to seek 0x40");
sought = rz_core_seek_undo(core);
mu_assert_true(sought, "failed first undo seek");
sought = rz_core_seek_undo(core);
mu_assert_true(sought, "failed second undo seek");

Sdb *save_sdb = sdb_new0();
mu_assert_notnull(save_sdb, "sdb null");
rz_serialize_core_seek_save(save_sdb, core);
Sdb *ref = get_ref_sdb();
mu_assert_notnull(ref, "ref sdb null");
assert_sdb_eq(save_sdb, ref, "saved sdb not same");

rz_core_file_close(core->file);
rz_core_free(core);
sdb_free(save_sdb);
sdb_free(ref);

mu_end;
}

bool test_seek_serialize_load() {
RzCore *core = rz_core_new();
mu_assert_notnull(core, "core null");
rz_core_file_open(core, "malloc://0x3000", RZ_PERM_R, 0);

// enable the cursor so we can check the deserialized value
rz_print_set_cursor(core->print, true, 0, 0);

Sdb *ref = get_ref_sdb();
Sdb *load_sdb = sdb_new0();
rz_serialize_core_seek_load(ref, core, NULL);

mu_assert_eq(rz_vector_len(&core->seek_history.undos), 1, "bad number of undos");
RzCoreSeekItem *item = rz_vector_index_ptr(&core->seek_history.undos, 0);
mu_assert_eq(item->offset, 0x10, "bad undo offset");
mu_assert_eq(item->cursor, 1, "bad undo cursor");

mu_assert_eq(rz_vector_len(&core->seek_history.redos), 2, "bad number of redos");
item = rz_vector_index_ptr(&core->seek_history.redos, 1);
mu_assert_eq(item->offset, 0x30, "bad first redo offset");
mu_assert_eq(item->cursor, 3, "bad first redo cursor");
item = rz_vector_index_ptr(&core->seek_history.redos, 0);
mu_assert_eq(item->offset, 0x40, "bad second redo offset");
mu_assert_eq(item->cursor, 4, "bad second redo cursor");

// core offset not restored from current seek history item, so not checked
mu_assert_eq(rz_print_get_cursor(core->print), 2, "bad current cursor");

rz_core_file_close(core->file);
rz_core_free(core);
sdb_free(load_sdb);
sdb_free(ref);

mu_end;
}

int all_tests() {
mu_run_test(test_seek_serialize_save);
mu_run_test(test_seek_serialize_load);
return tests_passed != tests_run;
}

mu_main(all_tests)
37 changes: 37 additions & 0 deletions test/unit/test_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,42 @@ static bool test_vector_push_front(void) {
mu_end;
}

static bool test_vector_swap(void) {
RzVector v;
init_test_vector(&v, 3, 0, NULL, NULL);

rz_vector_swap(&v, 0, 2);
mu_assert_eq(v.len, 3UL, "rz_vector_swap (valid indexes) => len == 3");
ut32 e = *((ut32 *)rz_vector_index_ptr(&v, 0));
mu_assert_eq(e, 2, "rz_vector_swap (valid indexes) => content");
e = *((ut32 *)rz_vector_index_ptr(&v, 1));
mu_assert_eq(e, 1, "rz_vector_swap (valid indexes) => old content");
e = *((ut32 *)rz_vector_index_ptr(&v, 2));
mu_assert_eq(e, 0, "rz_vector_swap (valid indexes) => content");

rz_vector_swap(&v, 2, 2);
mu_assert_eq(v.len, 3UL, "rz_vector_swap (same index) => len == 3");
e = *((ut32 *)rz_vector_index_ptr(&v, 0));
mu_assert_eq(e, 2, "rz_vector_swap (same index) => old content");
e = *((ut32 *)rz_vector_index_ptr(&v, 1));
mu_assert_eq(e, 1, "rz_vector_swap (same index) => old content");
e = *((ut32 *)rz_vector_index_ptr(&v, 2));
mu_assert_eq(e, 0, "rz_vector_swap (same index) => content");

rz_vector_swap(&v, 3, 2);
mu_assert_eq(v.len, 3UL, "rz_vector_swap (bad index) => len == 3");
e = *((ut32 *)rz_vector_index_ptr(&v, 0));
mu_assert_eq(e, 2, "rz_vector_swap (bad index) => old content");
e = *((ut32 *)rz_vector_index_ptr(&v, 1));
mu_assert_eq(e, 1, "rz_vector_swap (bad index) => old content");
e = *((ut32 *)rz_vector_index_ptr(&v, 2));
mu_assert_eq(e, 0, "rz_vector_swap (bad index) => old content");

rz_vector_clear(&v);

mu_end;
}

static bool test_vector_reserve(void) {
RzVector v;
rz_vector_init(&v, 4, NULL, NULL);
Expand Down Expand Up @@ -1343,6 +1379,7 @@ static int all_tests(void) {
mu_run_test(test_vector_pop_front);
mu_run_test(test_vector_push);
mu_run_test(test_vector_push_front);
mu_run_test(test_vector_swap);
mu_run_test(test_vector_reserve);
mu_run_test(test_vector_shrink);
mu_run_test(test_vector_flush);
Expand Down

0 comments on commit e1ba294

Please sign in to comment.