#include #include #include "packet_buffer.h" Test(packet_buffer, empty_buffer) { packet_buffer_t *buffer = packet_buffer_new(); cr_assert(packet_buffer_is_empty(buffer)); cr_assert(!packet_buffer_is_full(buffer)); } Test(packet_buffer, full_buffer) { packet_buffer_status_e status = packet_buffer_status_ok; packet_buffer_t *buffer = packet_buffer_new(); packet_t *packet; packet = packet_buffer_enqueue(buffer, &status); cr_assert(packet != NULL); cr_assert(status == packet_buffer_status_ok); cr_assert(!packet_buffer_is_empty(buffer)); cr_assert(!packet_buffer_is_full(buffer)); packet = packet_buffer_enqueue(buffer, &status); cr_assert(packet != NULL); cr_assert(status == packet_buffer_status_ok); packet = packet_buffer_enqueue(buffer, &status); cr_assert(packet != NULL); cr_assert(status == packet_buffer_status_ok); packet = packet_buffer_enqueue(buffer, &status); cr_assert(packet != NULL); cr_assert(status == packet_buffer_status_ok); packet = packet_buffer_enqueue(buffer, &status); cr_assert(packet != NULL); cr_assert(status == packet_buffer_status_ok); cr_assert(packet_buffer_is_full(buffer)); packet = packet_buffer_enqueue(buffer, &status); cr_assert(packet == NULL); cr_assert(status == packet_buffer_status_full); cr_assert(packet_buffer_is_full(buffer)); } Test(packet_buffer, basic_enqueue_and_dequeue) { packet_buffer_status_e status = packet_buffer_status_ok; packet_buffer_t *buffer = packet_buffer_new(); packet_t *packet; packet = packet_buffer_enqueue(buffer, &status); cr_assert(status == packet_buffer_status_ok); packet->type = packet_type_data; packet->count = 0; packet->sender = 15; packet->receiver = 16; packet->length = 2; memcpy(packet->message, (char [2]){ 25, 24 }, 2); packet = packet_buffer_head(buffer, &status); cr_assert(status == packet_buffer_status_ok); cr_assert(packet->type == packet_type_data); cr_assert(packet->count == 0); cr_assert(packet->sender == 15); cr_assert(packet->receiver == 16); cr_assert(packet->length == 2); cr_assert(packet->message[0] == 25); cr_assert(packet->message[1] == 24); packet_buffer_dequeue(buffer, &status); cr_assert(status == packet_buffer_status_ok); } Test(packet_buffer, enqueue_to_full) { packet_buffer_status_e status = packet_buffer_status_ok; packet_buffer_t *buffer = packet_buffer_new(); packet_t *packet; packet = packet_buffer_enqueue(buffer, &status); cr_assert(status == packet_buffer_status_ok); packet->type = packet_type_data; packet->count = 0; packet->sender = 15; packet->receiver = 16; packet->length = 2; memcpy(packet->message, (char [2]){ 25, 24 }, 2); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); cr_assert(status == packet_buffer_status_ok); packet = packet_buffer_head(buffer, &status); cr_assert(status == packet_buffer_status_ok); cr_assert(packet->type == packet_type_data); cr_assert(packet->count == 0); cr_assert(packet->sender == 15); cr_assert(packet->receiver == 16); cr_assert(packet->length == 2); cr_assert(packet->message[0] == 25); cr_assert(packet->message[1] == 24); packet_buffer_dequeue(buffer, &status); cr_assert(status == packet_buffer_status_ok); } Test(packet_buffer, circular_buffer) { packet_buffer_status_e status = packet_buffer_status_ok; packet_buffer_t *buffer = packet_buffer_new(); packet_t *packet; packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet->type = packet_type_data; packet->count = 0; packet->sender = 15; packet->receiver = 16; packet->length = 2; memcpy(packet->message, (char [2]){ 25, 24 }, 2); packet_buffer_dequeue(buffer, &status); packet_buffer_dequeue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); packet = packet_buffer_enqueue(buffer, &status); cr_assert(status == packet_buffer_status_ok); packet->type = packet_type_data; packet->count = 1; packet->sender = 15; packet->receiver = 16; packet->length = 2; memcpy(packet->message, (char [2]){ 25, 24 }, 2); packet = packet_buffer_head(buffer, &status); cr_assert(status == packet_buffer_status_ok); cr_assert(packet->type == packet_type_data); cr_assert(packet->count == 0); cr_assert(packet->sender == 15); cr_assert(packet->receiver == 16); cr_assert(packet->length == 2); cr_assert(packet->message[0] == 25); cr_assert(packet->message[1] == 24); packet_buffer_dequeue(buffer, &status); packet_buffer_dequeue(buffer, &status); packet_buffer_dequeue(buffer, &status); packet_buffer_dequeue(buffer, &status); packet = packet_buffer_head(buffer, &status); cr_assert(status == packet_buffer_status_ok); cr_assert(packet->type == packet_type_data); cr_assert(packet->count == 1); cr_assert(packet->sender == 15); cr_assert(packet->receiver == 16); cr_assert(packet->length == 2); cr_assert(packet->message[0] == 25); cr_assert(packet->message[1] == 24); packet_buffer_dequeue(buffer, &status); cr_assert(status == packet_buffer_status_ok); cr_assert(packet_buffer_is_empty(buffer)); }