(c) queue implementation testing
This commit is contained in:
@@ -37,6 +37,7 @@ C_FLAGS := $(C_DEF) \
|
|||||||
-Wfloat-equal \
|
-Wfloat-equal \
|
||||||
-Wlogical-op \
|
-Wlogical-op \
|
||||||
-Wno-maybe-uninitialized \
|
-Wno-maybe-uninitialized \
|
||||||
|
-Wno-unused \
|
||||||
-Werror
|
-Werror
|
||||||
|
|
||||||
BINARY := $(BIN_DIR)/$(NAME)
|
BINARY := $(BIN_DIR)/$(NAME)
|
||||||
|
|||||||
+62
-11
@@ -3,22 +3,73 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define ARRAY_SIZE ( ( int ) 16 )
|
#define QUEUE_SIZE ( ( uint32_t ) 4u )
|
||||||
|
#define QUEUE_EMPTY_VALUE ( ( uint32_t ) 0u )
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t head;
|
||||||
|
uint32_t tail;
|
||||||
|
uint32_t queue_data[ QUEUE_SIZE ];
|
||||||
|
} queue_t;
|
||||||
|
|
||||||
|
static void queue_initialise( queue_t *queue );
|
||||||
|
static int queue_enqueue( queue_t *queue, uint32_t message );
|
||||||
|
static int queue_dequeue( queue_t *queue, uint32_t* message );
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char string[256];
|
queue_t queue;
|
||||||
char blargh[] = "hello.world";
|
queue_initialise( &queue );
|
||||||
char* temp;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
temp = strrchr( blargh, '.' );
|
for ( int n = 0; n < ( int )QUEUE_SIZE + 1; n++ )
|
||||||
len = strlen( temp );
|
{
|
||||||
|
if ( queue_enqueue( &queue, ( uint32_t )n ) )
|
||||||
|
{
|
||||||
|
printf( "failed to queue message: 0x%4x\n", n );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf( "successfully queued message: 0x%4x\n", n );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
printf( "temp[ %ld ] = %s\n", len, &temp[ 1 ] );
|
void queue_initialise( queue_t *queue )
|
||||||
|
{
|
||||||
|
queue->head = 0;
|
||||||
|
queue->tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
strncpy( &string[ 0 ], &temp[ 1 ], len - 1 );
|
int queue_enqueue( queue_t *queue, uint32_t message )
|
||||||
strncpy( &string[ len - 1 ], blargh, ( size_t )( temp - blargh ) );
|
{
|
||||||
|
if ( !queue ) return -1;
|
||||||
|
|
||||||
printf( "string = %s\n", string );
|
if ( ( queue->head + 1 ) % QUEUE_SIZE == queue->tail )
|
||||||
|
{
|
||||||
|
// queue full
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue->queue_data[ queue->head ] = message;
|
||||||
|
queue->head = ( queue->head + 1 ) % QUEUE_SIZE;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int queue_dequeue( queue_t *queue, uint32_t* message )
|
||||||
|
{
|
||||||
|
if ( !queue ) return -1;
|
||||||
|
if ( !message ) return -1;
|
||||||
|
|
||||||
|
if ( queue->head == queue->tail )
|
||||||
|
{
|
||||||
|
// queue empty
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*message = queue->queue_data[ queue->tail ];
|
||||||
|
queue->tail = ( queue->tail + 1 ) % QUEUE_SIZE;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user