I have a FIFO queue of packets, which maintains:
- Push index
- Pop index
- Count
I would like to assert my dequeuing algorithm:
if (push_index >= (count-i))
pop_index = push_index-(count-i);
else
pop_index = MAX_NUM_OF_PACKETS+push_index-(count-i);
Here is the code that I have used in order to test it:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM_OF_PACKETS 8
int main()
{
unsigned char push_index;
unsigned char pop_index;
unsigned char count;
unsigned char i;
int n;
srand((unsigned int)time(NULL));
for (n=0; n<100; n++)
{
push_index = (unsigned char)(rand()%MAX_NUM_OF_PACKETS);
count = (unsigned char)(rand()%MAX_NUM_OF_PACKETS);
printf("index = %u, count = %u:",push_index,count);
for (i=0; i<count; i++)
{
if (push_index >= (count-i))
pop_index = push_index-(count-i);
else
pop_index = MAX_NUM_OF_PACKETS+push_index-(count-i);
printf(" %u",pop_index);
}
printf("\n");
}
return 0;
}
For the actual FIFO, you may assume that:
- The maximum number of packets fits in 8 bits
- The attributes (indexes and count) are thread-safe
For the actual FIFO
Is this example code or actual code? – Mast Jul 3 at 8:36