hamnet70/impl/test/test_packet_queue.c

77 lines
1.8 KiB
C
Raw Normal View History

/*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Copyright (C) 2024 Thomas Kolb
* Copyright (C) 2024 Simon Ruderich
*/
2024-07-11 23:23:18 +02:00
#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);
2024-07-11 23:23:18 +02:00
// add more packets until the queue is / should be full
for(size_t i = 1; i < PACKET_QUEUE_SIZE; i++) {
2024-07-11 23:23:18 +02:00
char s[2];
s[0] = 'A' + i;
2024-07-11 23:23:18 +02:00
s[1] = 0;
header.tx_seq_nr = i;
2024-07-11 23:23:18 +02:00
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);
2024-07-11 23:23:18 +02:00
}
// 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
2024-07-11 23:23:18 +02:00
// the first nonexisting packets
assert(packet_queue_get(&queue, 10) != NULL);
assert(packet_queue_get(&queue, 11) == NULL);
2024-07-11 23:23:18 +02:00
packet_queue_destroy(&queue);
printf("Test completed successfully!\n");
}