feat(raft-sim-conformance): add conformance-harness data types to raft.h (task 1.1)

This commit is contained in:
Rongsong Shen 2026-06-24 11:40:38 +00:00
parent 2b15dfb67a
commit 35c012ddc2
3 changed files with 179 additions and 1 deletions

View file

@ -3203,6 +3203,76 @@ static void test_raft_snapshot_returns_current_state(void)
#ifdef XCE_TESTING
/*
* test_sim_conformance_types_compile (raft-sim-conformance task 1.1)
*
* Structural / compile-reference test for the conformance harness data types
* added to the XCE_TESTING section of raft.h. This task is non-behavioral: it
* only declares new types and struct fields. The test proves the new types are
* usable (construct a raft_sim_msg_t / raft_sim_log_entry_t / raft_sim_action_t
* and read fields back) and that raft_sim_cluster_t / raft_sim_node_t carry the
* new fixed arrays (sizeof grows to include bag[]/trace[]/applied[]).
*/
static void test_sim_conformance_types_compile(void)
{
/* Bag message round-trips its fields. */
raft_sim_msg_t m;
memset(&m, 0, sizeof(m));
m.kind = RAFT_SIM_MSG_AE_REQ;
m.src = 1;
m.dst = 2;
m.term = 7;
m.entry_term = 7;
m.entry_value = 42;
m.entries_len = 1;
TEST_ASSERT_EQUAL_INT(RAFT_SIM_MSG_AE_REQ, m.kind);
TEST_ASSERT_EQUAL_UINT64(42, m.entry_value);
TEST_ASSERT_EQUAL_INT(1, m.entries_len);
/* Lightweight (term, value) log entry. */
raft_sim_log_entry_t e = { .term = 3, .value = 99 };
TEST_ASSERT_EQUAL_UINT64(3, e.term);
TEST_ASSERT_EQUAL_UINT64(99, e.value);
/* Replayable action record over the Next-action enum. */
raft_sim_action_t a;
memset(&a, 0, sizeof(a));
a.kind = RAFT_SIM_ACT_APPEND_ENTRIES;
a.actor = 0;
a.msg_index = -1;
a.term = 7;
a.index = 5;
TEST_ASSERT_EQUAL_INT(RAFT_SIM_ACT_APPEND_ENTRIES, a.kind);
TEST_ASSERT_EQUAL_UINT64(5, a.index);
/* New node + cluster fields exist and are addressable. */
raft_sim_node_t n;
memset(&n, 0, sizeof(n));
n.applied[0] = 11;
n.persisted_term = 4;
n.persisted_voted_for = 2;
TEST_ASSERT_EQUAL_UINT64(11, n.applied[0]);
TEST_ASSERT_EQUAL_INT(2, n.persisted_voted_for);
raft_sim_cluster_t c;
memset(&c, 0, sizeof(c));
c.seed = 0xDEADBEEFULL;
c.prng = c.seed;
c.bag_count = 0;
c.trace_len = 0;
c.bag[0] = m;
c.trace[0] = a;
TEST_ASSERT_EQUAL_UINT64(0xDEADBEEFULL, c.seed);
TEST_ASSERT_EQUAL_INT(RAFT_SIM_MSG_AE_REQ, c.bag[0].kind);
TEST_ASSERT_EQUAL_INT(RAFT_SIM_ACT_APPEND_ENTRIES, c.trace[0].kind);
/* sizeof grows to include the new fixed arrays (bounds are positive). */
TEST_ASSERT_TRUE(RAFT_SIM_MAX_MSGS > 0);
TEST_ASSERT_TRUE(RAFT_SIM_MAX_STEPS > 0);
TEST_ASSERT_TRUE(sizeof(c.bag) == sizeof(raft_sim_msg_t) * RAFT_SIM_MAX_MSGS);
TEST_ASSERT_TRUE(sizeof(c.trace) == sizeof(raft_sim_action_t) * RAFT_SIM_MAX_STEPS);
}
/*
* test_sim_init_3node
*
@ -6933,6 +7003,9 @@ void test_raft(void)
/* simulator init (task 4.1) */
#ifdef XCE_TESTING
/* conformance harness data types (raft-sim-conformance task 1.1) */
RUN_TEST(test_sim_conformance_types_compile);
RUN_TEST(test_sim_init_3node);
RUN_TEST(test_sim_trace_buf);