#include #include #include #include #include "unity.h" #include "message.h" struct message_t { char *message; uint16_t received_from, *sent_to; observer_func *observers; int message_length, num_sent_to, num_observers; }; void test_message_init(void) { const char message[] = "testing"; message_handle_t message_handle = message_init(message, strlen(message), 0); TEST_ASSERT_NOT_NULL(message_handle); TEST_ASSERT_EQUAL_INT(strlen(message), message_handle->message_length); TEST_ASSERT_EQUAL_INT(0, message_handle->num_sent_to); TEST_ASSERT_EQUAL_INT(0, message_handle->num_observers); message_free(message_handle); } void test_message_add_sent_to(void) { const char message[] = "testing"; message_handle_t message_handle = message_init(message, strlen(message), 0); const uint16_t expected[] = {4, 345, 26463, 35, 43663, 347, 3262}; for (int i = 0; i < 7; ++i) { message_add_sent_to(message_handle, expected[i]); } TEST_ASSERT_EQUAL_UINT16_ARRAY(expected, message_handle->sent_to, 7); message_free(message_handle); } void test_message_get(void) { const char expected[] = "testing"; message_handle_t message_handle = message_init(expected, strlen(expected), 0); int message_length = -1; char *actual = message_get(message_handle, &message_length); TEST_ASSERT_EQUAL_UINT16(strlen(expected), message_length); TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, message_length); free(actual); message_free(message_handle); } void test_message_sent_to(void) { const char message[] = "testing"; message_handle_t message_handle = message_init(message, strlen(message), 0); const uint16_t expected[] = {4, 345, 26463, 35, 43663, 347, 3262}; for (int i = 0; i < 7; ++i) { message_add_sent_to(message_handle, expected[i]); } TEST_ASSERT_EQUAL_INT(7, message_handle->num_sent_to); TEST_ASSERT_TRUE(message_sent_to(message_handle, 345)); TEST_ASSERT_TRUE(message_sent_to(message_handle, 43663)); TEST_ASSERT_TRUE(message_sent_to(message_handle, 347)); TEST_ASSERT_FALSE(message_sent_to(message_handle, 0)); TEST_ASSERT_FALSE(message_sent_to(message_handle, -1)); TEST_ASSERT_FALSE(message_sent_to(message_handle, 348)); message_free(message_handle); } bool globalTestVariable = false; void test_observer(void) { globalTestVariable = true; } void test_message_attach_observer(void) { const char message[] = "testing"; message_handle_t message_handle = message_init(message, strlen(message), 0); message_attach_observer(message_handle, test_observer); TEST_ASSERT_EQUAL_INT(1, message_handle->num_observers); TEST_ASSERT_NOT_NULL(message_handle->observers); TEST_ASSERT_EQUAL_PTR(test_observer, message_handle->observers[0]); message_free(message_handle); } void test_message_state_changed(void) { const char message[] = "testing"; message_handle_t message_handle = message_init(message, strlen(message), 0); message_attach_observer(message_handle, &test_observer); globalTestVariable = false; message_state_changed(message_handle); TEST_ASSERT_TRUE(globalTestVariable); message_free(message_handle); }