avr/packet-radio/tests/packet_buffer.c

181 lines
6.4 KiB
C
Raw Normal View History

/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
This AVR library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This AVR library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this AVR library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <criterion/criterion.h>
#include <stdio.h>
#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);
printf("packet-type: %d\n", packet->type);
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));
}