/* splinter -- registry tests. */ #include #include #include "splinter.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static int noop(const char *args, struct sp_buf *out, void *ctx) { (void)args; (void)ctx; return sp_buf_puts(out, "noop"); } int main(void) { struct sp_registry r; struct sp_tool t; const struct sp_tool *found; memset(&r, 0, sizeof(r)); memset(&t, 0, sizeof(t)); CHECK(sp_registry_count(&r) == 0); CHECK(sp_registry_find(&r, "x") == NULL); t.name = "alpha"; t.description = "first"; t.auth = SP_AUTH_READ; t.run = noop; CHECK(sp_registry_add(&r, &t) == 0); t.name = "beta"; t.description = "second"; t.auth = SP_AUTH_MUTATE; t.run = noop; CHECK(sp_registry_add(&r, &t) == 0); CHECK(sp_registry_count(&r) == 2); found = sp_registry_find(&r, "alpha"); CHECK(found != NULL); CHECK(found != NULL && found->auth == SP_AUTH_READ); CHECK(found != NULL && strcmp(found->description, "first") == 0); found = sp_registry_find(&r, "beta"); CHECK(found != NULL); CHECK(found != NULL && found->auth == SP_AUTH_MUTATE); CHECK(sp_registry_find(&r, "gamma") == NULL); CHECK(sp_registry_find(&r, NULL) == NULL); /* growth beyond the initial eight slots */ { int i; char name[16]; for (i = 0; i < 40; i++) { snprintf(name, sizeof(name), "t%d", i); t.name = name; CHECK(sp_registry_add(&r, &t) == 0); } CHECK(sp_registry_count(&r) == 42); CHECK(sp_registry_find(&r, "t39") != NULL); } sp_registry_free(&r); CHECK(sp_registry_count(&r) == 0); CHECK(r.tools == NULL); if (failures == 0) printf("test_tool: ok\n"); return failures == 0 ? 0 : 1; }