70 lines
1.7 KiB
C
70 lines
1.7 KiB
C
|
#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-2);
|
||
|
|
||
|
// add more packets until the queue is / should be full
|
||
|
for(size_t i = 0; i < PACKET_QUEUE_SIZE-2; i++) {
|
||
|
char s[2];
|
||
|
s[0] = 'A' + i + 1;
|
||
|
s[1] = 0;
|
||
|
|
||
|
header.tx_seq_nr = i+1;
|
||
|
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+2);
|
||
|
assert(packet_queue_get_free_space(&queue) == PACKET_QUEUE_SIZE-3-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 10th and 11th packet, which are the last existing and
|
||
|
// the first nonexisting packets
|
||
|
assert(packet_queue_get(&queue, 9) != NULL);
|
||
|
assert(packet_queue_get(&queue, 10) == NULL);
|
||
|
|
||
|
packet_queue_destroy(&queue);
|
||
|
|
||
|
printf("Test completed successfully!\n");
|
||
|
}
|