skip to content
==> tests/hello.c <==
1/* hello on the CPU simulator: init sets the magic, tasks compute arg * 3 + 7
2 * (wrapping), counters count, and a halted program relaunches where it was. */
3#include "mesh/check.h"
4#include "mesh/rt/sim_mailbox.h"
5
6#include "../src/hello.c"
7
8int main(void) {
9    uint8_t *mb = mesh_sim_mailbox();
10    mesh_sim_prog p = {.name = "hello", .view_bytes = 0x1000, .nodes = 2};
11    uint8_t *view = mesh_sim_install(mb, 3, &p);
12    uint8_t *ctl = mesh_sim_ctl(mb, 3);
13    mesh_sim_launch *l = mesh_sim_launch_prog(mb, 3, &p, mesh_main);
14    CHECK_U(mesh_sim_call(mb, 3, MESH_OP_INIT, 0), MESH_R_NONE);
15    CHECK_U(mesh_sim_u32(view), HELLO_MAGIC);
16    CHECK_U(mesh_sim_call(mb, 3, MESH_OP_TASK, 5), 22);
17    CHECK_U(mesh_sim_u32(view + 4), 5);
18    CHECK_U(mesh_sim_u32(view + 8), 22);
19    CHECK_U(mesh_sim_call(mb, 3, MESH_OP_TASK, 0xffffffffu), 4); /* wraps */
20    CHECK_U(mesh_sim_call(mb, 3, 9, 0), MESH_R_BAD_OP);
21    /* A batch of 40 tickets over both nodes. */
22    uint32_t t[40];
23    for (uint32_t i = 0; i < 40; i++) t[i] = mesh_sim_publish(mb, 3, MESH_OP_TASK, i);
24    for (uint32_t i = 0; i < 40; i++) {
25        uint32_t r = 0;
26        CHECK(mesh_sim_wait(mb, 3, t[i], &r, 10000));
27        CHECK_U(r, i * 3 + 7);
28    }
29    CHECK_U(mesh_sim_u32(ctl + MESH_P_COUNTERS), 42);     /* tasks: the 2 above and the 40 (not the bad op) */
30    CHECK_U(mesh_sim_u32(ctl + MESH_P_COUNTERS + 4), 1); /* inits */
31    CHECK_U(mesh_sim_u32(ctl + MESH_P_ONLINE), 2);
32    CHECK_U(mesh_sim_u32(ctl + MESH_P_DONE), 44);
33    mesh_sim_halt(mb, 3, l);
34    /* Relaunched: the next ticket runs (the ring was rewound to the tail). */
35    l = mesh_sim_launch_prog(mb, 3, &p, mesh_main);
36    CHECK_U(mesh_sim_call(mb, 3, MESH_OP_TASK, 1), 10);
37    CHECK_U(mesh_sim_u32(ctl + MESH_P_FAULT), 0);
38    mesh_sim_halt(mb, 3, l);
39    mesh_sim_mailbox_free(mb);
40    return mesh_check_done();
41}