-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "generic_queue.h" | ||
|
||
void GENQ_init(generic_queue_t *q, size_t elem_size) | ||
{ | ||
q->queue_elem_size = elem_size; | ||
q->tail = q->head = q->cnt_elems = 0; | ||
q->max_elems = GENQ_SIZE/q->queue_elem_size; | ||
} | ||
bool GENQ_is_empty(generic_queue_t *q) | ||
{ | ||
return q->cnt_elems == 0; | ||
} | ||
bool GENQ_is_full(generic_queue_t *q) | ||
{ | ||
return q->cnt_elems >= q->max_elems; | ||
} | ||
bool GENQ_pop(generic_queue_t *q, void *e) | ||
{ | ||
if(GENQ_is_empty(q)) return 0; | ||
q->cnt_elems--; | ||
|
||
memcpy(e, q->queue+q->head*q->queue_elem_size, q->queue_elem_size); | ||
q->head++; | ||
q->head %= q->max_elems; | ||
return 1; | ||
} | ||
bool GENQ_push(generic_queue_t *q, void *e) | ||
{ | ||
if(GENQ_is_full(q)) return 0; | ||
q->cnt_elems++; | ||
|
||
memcpy(q->queue+q->tail*q->queue_elem_size, e, q->queue_elem_size); | ||
q->tail++; | ||
q->tail %= q->max_elems; | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef _GENERIC_QUEUE | ||
#define _GENERIC_QUEUE | ||
|
||
#include "inttypes.h" | ||
#include "stdbool.h" | ||
#include "stdlib.h" | ||
#include "string.h" | ||
|
||
#define GENQ_SIZE 1000 | ||
|
||
typedef struct{ | ||
size_t head, tail; | ||
uint8_t queue[GENQ_SIZE]; | ||
size_t queue_elem_size; | ||
size_t max_elems; | ||
int cnt_elems; | ||
} generic_queue_t; | ||
|
||
void GENQ_init(generic_queue_t *, size_t); | ||
bool GENQ_is_empty(generic_queue_t*); | ||
bool GENQ_is_full(generic_queue_t*); | ||
bool GENQ_pop(generic_queue_t*, void *); | ||
bool GENQ_push(generic_queue_t*, void *); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "stdio.h" | ||
#include "../generic_queue.h" | ||
|
||
generic_queue_t q; | ||
|
||
#define n 10000 | ||
|
||
typedef struct{ | ||
long int v; | ||
char prova[5]; | ||
} test; | ||
|
||
int main() | ||
{ | ||
GENQ_init(&q, sizeof(test)); | ||
|
||
test v[n]; | ||
char prova[] = "asdaa"; | ||
for(int i=0;i<n;i++) | ||
{ | ||
v[i].v = i+1; | ||
memcpy(v[i].prova, prova, sizeof(char)*5); | ||
} | ||
int tot = GENQ_SIZE / sizeof(test); | ||
int cnt = 0; | ||
for(int i=0;i<n;i++) | ||
{ | ||
if(!GENQ_push(&q, v+i)) | ||
{ | ||
//printf("errore push\n"); | ||
break; | ||
} | ||
else | ||
cnt++; | ||
} | ||
int check = (cnt == tot); | ||
//printf("pushed %d elements\n", cnt); | ||
for(int i=0;i<10;i++) | ||
{ | ||
test tmp; | ||
if(!GENQ_pop(&q, &tmp)) | ||
{ | ||
printf("error\n"); | ||
return 11; | ||
} | ||
cnt--; | ||
} | ||
for(int i=0;i<n;i++) | ||
{ | ||
if(!GENQ_push(&q, v+i)) | ||
{ | ||
//printf("errore push\n"); | ||
break; | ||
} | ||
else | ||
cnt++; | ||
} | ||
check = check || (cnt == tot); | ||
cnt = 0; | ||
for(int i=0;i<n;i++) | ||
{ | ||
test tmp; | ||
if(GENQ_pop(&q, &tmp)) | ||
{ | ||
//printf("%d %s\n", tmp.v, tmp.prova); | ||
cnt++; | ||
} | ||
else | ||
{ | ||
//printf("errore pop\n"); | ||
break; | ||
} | ||
} | ||
check = check || (cnt == 0); | ||
printf("maximum elements: %d\n", tot); | ||
if(check) printf("test passed\n"); | ||
else printf("test not passed\n"); | ||
//printf("popped %d elements\n", cnt); | ||
} |