/** Initialize an #sbp_state_t struct before use. * This resets the entire state, including all callbacks. * Remember to use this function to initialize the state before calling * sbp_process() for the first time. * * \param s State structure */ void sbp_state_init(sbp_state_t *s) { s->state = WAITING; /* Set the IO context pointer, passed to read and write functions, to NULL. */ s->io_context = 0; /* Clear the callbacks, if any, currently in s */ sbp_clear_callbacks(s); }
END_TEST START_TEST(test_callbacks) { sbp_state_t s; sbp_state_init(&s); /* Start with no callbacks registered. */ sbp_clear_callbacks(&s); fail_unless(sbp_find_callback(&s, 0x1234) == 0, "sbp_find_callback should return NULL if no callbacks registered"); fail_unless(sbp_register_callback(&s, 0x2233, &test_callback, 0, 0) == SBP_NULL_ERROR, "sbp_register_callback should return an error if node is NULL"); /* Add a first callback. */ static sbp_msg_callbacks_node_t n; int NUMBER = 42; fail_unless(sbp_register_callback(&s, 0x2233, 0, 0, &n) == SBP_NULL_ERROR, "sbp_register_callback should return an error if cb is NULL"); fail_unless(sbp_register_callback(&s, 0x2233, &test_callback, &NUMBER, &n) == SBP_OK, "sbp_register_callback should return success if everything is groovy"); fail_unless(sbp_register_callback(&s, 0x2233, &test_callback, 0, &n) == SBP_CALLBACK_ERROR, "sbp_register_callback should return SBP_CALLBACK_ERROR if a callback " "of the same type is already registered"); fail_unless(sbp_find_callback(&s, 0x1234) == 0, "sbp_find_callback should return NULL if callback not registered"); fail_unless(sbp_find_callback(&s, 0x2233) == &n, "sbp_find_callback didn't return the correct callback node pointer"); fail_unless(sbp_find_callback(&s, 0x2233)->context == &NUMBER, "sbp_find_callback didn't return the correct context pointer"); /* Add a second callback. */ static sbp_msg_callbacks_node_t m; int NUMBER2 = 84; fail_unless(sbp_register_callback(&s, 0x1234, &test_callback2, &NUMBER2, &m) == SBP_OK, "sbp_register_callback should return success if everything is groovy (2)"); fail_unless(sbp_find_callback(&s, 0x2233) == &n, "sbp_find_callback didn't return the correct callback function pointer (2)"); fail_unless(sbp_find_callback(&s, 0x2233)->context == &NUMBER, "sbp_find_callback didn't return the correct context pointer"); fail_unless(sbp_find_callback(&s, 0x1234) == &m, "sbp_find_callback didn't return the correct callback function pointer (3)"); fail_unless(sbp_find_callback(&s, 0x1234)->context == &NUMBER2, "sbp_find_callback didn't return the correct context pointer"); fail_unless(sbp_register_callback(&s, 0x1234, &test_callback, 0, &n) == SBP_CALLBACK_ERROR, "sbp_register_callback should return SBP_CALLBACK_ERROR if a callback " "of the same type is already registered (2)"); fail_unless(sbp_find_callback(&s, 0x7788) == 0, "sbp_find_callback should return NULL if callback not registered (2)"); /* Clear all the registered callbacks and check they can no longer be found. */ sbp_clear_callbacks(&s); fail_unless(sbp_find_callback(&s, 0x1234) == 0, "sbp_find_callback should return NULL if no callbacks registered (2)"); fail_unless(sbp_find_callback(&s, 0x2233) == 0, "sbp_find_callback should return NULL if no callbacks registered (3)"); }