hamnet70/impl/test/test_packet_queue.c
Thomas Kolb 2e91fd7c42 Apply free software+documentation licenses
All code is now licensed under GPLv3+. The documentation is licensed under
CC BY-SA 4.0.

This is now officially free software! \o/
2024-08-23 11:53:40 +02:00

77 lines
1.8 KiB
C

/*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Copyright (C) 2024 Thomas Kolb
* Copyright (C) 2024 Simon Ruderich
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <layer2/packet_queue.h>
int main(void)
{
packet_queue_t queue;
layer2_packet_header_t header = {0};
uint8_t *data;
size_t data_len;
// initialize
packet_queue_init(&queue);
// add a first packet
header.tx_seq_nr = 0;
data_len = 2;
data = malloc(data_len);
strncpy((char*)data, "A", data_len);
assert(packet_queue_add(&queue, &header, data, data_len));
assert(packet_queue_get_used_space(&queue) == 1);
assert(packet_queue_get_free_space(&queue) == PACKET_QUEUE_SIZE-1);
// add more packets until the queue is / should be full
for(size_t i = 1; i < PACKET_QUEUE_SIZE; i++) {
char s[2];
s[0] = 'A' + i;
s[1] = 0;
header.tx_seq_nr = i;
data_len = 2;
data = malloc(data_len);
strncpy((char*)data, s, data_len);
assert(packet_queue_add(&queue, &header, data, data_len));
assert(packet_queue_get_used_space(&queue) == i+1);
assert(packet_queue_get_free_space(&queue) == PACKET_QUEUE_SIZE-1-i);
}
// adding another packet should fail now
assert(!packet_queue_add(&queue, &header, data, data_len));
// delete the first five packets
packet_queue_delete(&queue, 5);
assert(packet_queue_get_free_space(&queue) == 5);
// now the sequence number of the first packet should be 5
const packet_queue_entry_t *entry = packet_queue_get(&queue, 0);
assert(entry->header.tx_seq_nr == 5);
// try to access the 11th and 12th packet, which are the last existing and
// the first nonexisting packets
assert(packet_queue_get(&queue, 10) != NULL);
assert(packet_queue_get(&queue, 11) == NULL);
packet_queue_destroy(&queue);
printf("Test completed successfully!\n");
}