#include "sim_api.h" #include "sim_event.h" #include "sim_protocol.h" #include #include #include #include _Static_assert(sizeof(int32_t) == 4, "contract_int32_width"); _Static_assert(sizeof(uint32_t) == 4, "contract_uint32_width"); _Static_assert(sizeof(uint64_t) == 8, "contract_uint64_width"); _Static_assert(offsetof(sim_event_record, seq) == 0, "contract_event_seq_offset"); _Static_assert(offsetof(sim_event_record, tick) == 4, "contract_event_tick_offset"); _Static_assert(offsetof(sim_event_record, payload_hash) == 8, "contract_event_hash_offset"); _Static_assert(offsetof(sim_event_record, kind) == 16, "contract_event_kind_offset"); _Static_assert(sizeof(sim_event_record) == 48, "contract_event_record_size"); static void test_protocol_contract(void) { sim_protocol_reset(); assert(sim_protocol_accept(0, 1, "HELLO", NULL, 0) == SIM_PROTOCOL_ERR_SCHEMA); assert(sim_protocol_accept(1, 1, "HELLO", NULL, 0) == SIM_PROTOCOL_OK); assert(sim_protocol_accept(1, 3, "SESSION_ATTACH", NULL, 0) == SIM_PROTOCOL_ERR_SEQUENCE); assert(sim_protocol_accept(1, 2, "SESSION_ATTACH", NULL, 0) == SIM_PROTOCOL_OK); assert(sim_protocol_accept(1, 3, "MDI_EXECUTE", NULL, 0) == SIM_PROTOCOL_ERR_OPERATION_ID); assert(sim_protocol_accept(1, 3, "MDI_EXECUTE", "op-3", 1) == SIM_PROTOCOL_OK); assert(sim_protocol_accept(1, 4, "PING", NULL, 0) == SIM_PROTOCOL_ERR_TICK); assert(sim_protocol_last_seq() == 3 && sim_protocol_tick() == 1); } static void test_event_contract(void) { const uint8_t payload[] = {'c', 'o', 'n', 't', 'r', 'a', 'c', 't'}; uint8_t exported[sizeof(uint32_t) * 3u + sizeof(sim_event_record) * SIM_EVENT_CAPACITY + 16u] = {0}; uint8_t record[sizeof(sim_event_record)] = {0}; uint32_t export_size; uint32_t index; sim_event_reset(); assert(sim_event_append(1, 0, "SESSION_READY", payload, sizeof(payload)) == SIM_EVENT_OK); assert(sim_event_append(3, 0, "GAP", NULL, 0) == SIM_EVENT_ERR_SEQUENCE); assert(sim_event_append(2, 1, "SIM_STEP", NULL, 0) == SIM_EVENT_OK); assert(sim_event_read(0, NULL, 0) == sizeof(sim_event_record)); assert(sim_event_read(0, record, sizeof(record)) == sizeof(sim_event_record)); assert(((sim_event_record *)record)->seq == 1); export_size = sim_event_export(NULL, 0); assert(export_size > sizeof(sim_event_record) && export_size <= sizeof(exported)); assert(sim_event_export(exported, sizeof(exported)) == export_size); sim_event_reset(); assert(sim_event_import(exported, export_size) == SIM_EVENT_OK); assert(sim_event_count() == 2 && sim_event_cursor() == 2); assert(sim_event_import(exported, export_size - 1) == SIM_EVENT_ERR_ARGUMENT); sim_event_reset(); for (index = 1; index <= SIM_EVENT_CAPACITY; ++index) { assert(sim_event_append(index, index, "STEP", NULL, 0) == SIM_EVENT_OK); } assert(sim_event_append(SIM_EVENT_CAPACITY + 1U, SIM_EVENT_CAPACITY + 1U, "FULL", NULL, 0) == SIM_EVENT_ERR_FULL); } static void test_api_contract(void) { uint8_t state[32] = {0}; uint8_t snapshot[sizeof(uint64_t)] = {0}; uint64_t tick = 0; int32_t state_size; sim_destroy(); assert(sim_read_snapshot(NULL, 0) == sizeof(uint64_t)); assert(sim_create(NULL, 0) == SIM_OK); assert(sim_load_workspace("/workspace") == SIM_OK); assert(sim_step(4) == SIM_OK); assert(sim_read_snapshot(snapshot, sizeof(snapshot)) == sizeof(snapshot)); memcpy(&tick, snapshot, sizeof(tick)); assert(tick == 4); assert(sim_export_state(NULL, 0) == SIM_ERR_BUFFER_TOO_SMALL); state_size = sim_export_state(state, sizeof(state)); assert(state_size > 0 && state_size <= (int32_t)sizeof(state)); assert(sim_step(2) == SIM_OK); assert(sim_import_state(state, (uint32_t)state_size) == SIM_OK); assert(sim_read_snapshot(snapshot, sizeof(snapshot)) == sizeof(snapshot)); memcpy(&tick, snapshot, sizeof(tick)); assert(tick == 4); sim_destroy(); } int main(void) { test_protocol_contract(); test_event_contract(); test_api_contract(); return 0; }