From 440fbfe2de9037d4d5b7a9c81d8ab7f86af84c79 Mon Sep 17 00:00:00 2001 From: quentin Date: Wed, 6 Sep 2023 19:23:45 +0200 Subject: [PATCH] Add unit test for seek history serialization --- test/unit/meson.build | 1 + test/unit/test_serialize_seek.c | 98 +++++++++++++++++++++++++++++++++ test/unit/test_vector.c | 37 +++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 test/unit/test_serialize_seek.c diff --git a/test/unit/meson.build b/test/unit/meson.build index a0d559d2b89..9fdadc09098 100644 --- a/test/unit/meson.build +++ b/test/unit/meson.build @@ -87,6 +87,7 @@ if get_option('enable_tests') 'serialize_config', 'serialize_debug', 'serialize_flag', + 'serialize_seek', 'serialize_spaces', 'serialize_types', 'skiplist', diff --git a/test/unit/test_serialize_seek.c b/test/unit/test_serialize_seek.c new file mode 100644 index 00000000000..fb12c85408d --- /dev/null +++ b/test/unit/test_serialize_seek.c @@ -0,0 +1,98 @@ +// SPDX-FileCopyrightText: 2023 Quentin Minster +// SPDX-License-Identifier: LGPL-3.0-only + +#include +#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) diff --git a/test/unit/test_vector.c b/test/unit/test_vector.c index edda8b29547..0d27ce5fed0 100644 --- a/test/unit/test_vector.c +++ b/test/unit/test_vector.c @@ -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); @@ -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);