Files
SPHLite/tests/app/test_worker_dispatch.c
2026-07-05 17:00:13 +08:00

95 lines
3.4 KiB
C

#include "sph_worker_dispatch.h"
#include <stdio.h>
#include <string.h>
static int fail(const char *message)
{
fprintf(stderr, "%s\n", message);
return 1;
}
static int expect_actions(int command, const int *expected,
int expected_count, const char *label)
{
int actions[SPHLITE_WORKER_ACTION_CAPACITY];
int count = 0;
int i;
if (sphlite_worker_command_actions(command, actions,
SPHLITE_WORKER_ACTION_CAPACITY, &count) != 0) {
fprintf(stderr, "%s: command action query failed\n", label);
return 1;
}
if (count != expected_count) {
fprintf(stderr, "%s: action count mismatch\n", label);
return 1;
}
for (i = 0; i < expected_count; ++i) {
if (actions[i] != expected[i]) {
fprintf(stderr, "%s: action %d mismatch\n", label, i);
return 1;
}
}
return 0;
}
int main(void)
{
int actions[SPHLITE_WORKER_ACTION_CAPACITY];
int count = -1;
const int read_expected[] = {SPHLITE_WORKER_ACTION_READ_INPUT};
const int build_expected[] = {SPHLITE_WORKER_ACTION_BUILD_MODEL};
const int advance_expected[] = {SPHLITE_WORKER_ACTION_ADVANCE_EXPLICIT};
const int write_expected[] = {SPHLITE_WORKER_ACTION_WRITE_OUTPUT};
const int export_expected[] = {
SPHLITE_WORKER_ACTION_EXPORT_REGION_COUNT,
SPHLITE_WORKER_ACTION_EXPORT_REGION_INFO,
SPHLITE_WORKER_ACTION_EXPORT_PARTICLES,
SPHLITE_WORKER_ACTION_EXPORT_FACETS
};
const int import_expected[] = {
SPHLITE_WORKER_ACTION_IMPORT_PARTICLE_FORCE,
SPHLITE_WORKER_ACTION_IMPORT_FACET_PRESSURE,
SPHLITE_WORKER_ACTION_IMPORT_FACET_TRACTION,
SPHLITE_WORKER_ACTION_IMPORT_FACET_RESULTANT,
SPHLITE_WORKER_ACTION_IMPORT_CONTACT_STATE,
SPHLITE_WORKER_ACTION_IMPORT_MOTION_BOUNDARY
};
if (expect_actions(SPHLITE_WORKER_COMMAND_READ_INPUT, read_expected, 1,
"READ_INPUT") != 0) return 1;
if (expect_actions(SPHLITE_WORKER_COMMAND_BUILD_MODEL, build_expected, 1,
"BUILD_MODEL") != 0) return 1;
if (expect_actions(SPHLITE_WORKER_COMMAND_ADVANCE, advance_expected, 1,
"ADVANCE") != 0) return 1;
if (expect_actions(SPHLITE_WORKER_COMMAND_WRITE_OUTPUT, write_expected, 1,
"WRITE_OUTPUT") != 0) return 1;
if (expect_actions(SPHLITE_WORKER_COMMAND_EXPORT_INTERFACE,
export_expected, 4, "EXPORT_INTERFACE") != 0) return 1;
if (expect_actions(SPHLITE_WORKER_COMMAND_IMPORT_LOADS,
import_expected, 6, "IMPORT_LOADS") != 0) return 1;
if (strcmp(sphlite_worker_action_name(
SPHLITE_WORKER_ACTION_IMPORT_CONTACT_STATE),
"sphlite_coupling_apply_contact_state") != 0) {
return fail("action name mismatch");
}
if (sphlite_worker_command_actions(SPHLITE_WORKER_COMMAND_EXPORT_INTERFACE,
actions, 1, &count) == 0) {
return fail("small action buffer was accepted");
}
if (sphlite_worker_command_actions(SPHLITE_WORKER_COMMAND_UNKNOWN,
actions, SPHLITE_WORKER_ACTION_CAPACITY, &count) == 0 ||
count != 0) {
return fail("unknown command action path failed");
}
if (sphlite_worker_error_from_api_status(0) !=
SPHLITE_WORKER_ERROR_OK ||
sphlite_worker_error_from_api_status(-1) !=
SPHLITE_WORKER_ERROR_INTERNAL) {
return fail("API error mapping mismatch");
}
return 0;
}