grapes / som / Tests / cb_test.c @ ec807410
History | View | Annotate | Download (1.67 KB)
1 |
#include <stdint.h> |
---|---|
2 |
#include <stdlib.h> |
3 |
#include <stdio.h> |
4 |
#include <string.h> |
5 |
#include "chunk.h" |
6 |
#include "chunkbuffer.h" |
7 |
|
8 |
static struct chunk *chunk_forge(int id) |
9 |
{ |
10 |
struct chunk *c;
|
11 |
char buff[64]; |
12 |
|
13 |
c = malloc(sizeof(struct chunk)); |
14 |
if (c == NULL) { |
15 |
return c;
|
16 |
} |
17 |
|
18 |
sprintf(buff, "Chunk %d", id);
|
19 |
c->id = id; |
20 |
c->timestamp = 40 * id;
|
21 |
c->data = strdup(buff); |
22 |
c->size = strlen(c->data) + 1;
|
23 |
c->attributes_size = 0;
|
24 |
c->attributes = NULL;
|
25 |
return c;
|
26 |
} |
27 |
|
28 |
static void chunk_add(struct chunk_buffer *cb, int id) |
29 |
{ |
30 |
struct chunk *c;
|
31 |
int res;
|
32 |
|
33 |
printf("Inserting %d... ", id);
|
34 |
c = chunk_forge(id); |
35 |
if (c) {
|
36 |
res = cb_add_chunk(cb, c); |
37 |
if (res < 0) { |
38 |
printf("not inserted (out of window)");
|
39 |
} |
40 |
} else {
|
41 |
printf("Failed to create the chunk");
|
42 |
} |
43 |
printf("\n");
|
44 |
free(c); |
45 |
} |
46 |
|
47 |
static void cb_print(const struct chunk_buffer *cb) |
48 |
{ |
49 |
struct chunk *buff;
|
50 |
int i, size;
|
51 |
|
52 |
buff = cb_get_chunks(cb, &size); |
53 |
for (i = 0; i < size; i++) { |
54 |
printf("C[%d]: %s %d\n", i, buff[i].data, buff[i].id);
|
55 |
} |
56 |
} |
57 |
|
58 |
int main(int argc, char *argv[]) |
59 |
{ |
60 |
struct chunk_buffer *b;
|
61 |
|
62 |
b = cb_init("size=8,time=now");
|
63 |
if (b == NULL) { |
64 |
printf("Error initialising the Chunk Buffer\n");
|
65 |
|
66 |
return -1; |
67 |
} |
68 |
chunk_add(b, 10);
|
69 |
chunk_add(b, 5);
|
70 |
chunk_add(b, 12);
|
71 |
chunk_add(b, 40);
|
72 |
cb_print(b); |
73 |
|
74 |
chunk_add(b, 51);
|
75 |
chunk_add(b, 2);
|
76 |
chunk_add(b, 13);
|
77 |
chunk_add(b, 11);
|
78 |
cb_print(b); |
79 |
|
80 |
chunk_add(b, 26);
|
81 |
cb_print(b); |
82 |
chunk_add(b, 30);
|
83 |
cb_print(b); |
84 |
chunk_add(b, 110);
|
85 |
cb_print(b); |
86 |
chunk_add(b, 64);
|
87 |
chunk_add(b, 4);
|
88 |
cb_print(b); |
89 |
chunk_add(b, 7);
|
90 |
chunk_add(b, 34);
|
91 |
chunk_add(b, 2);
|
92 |
chunk_add(b, 33);
|
93 |
cb_print(b); |
94 |
|
95 |
cb_destroy(b); |
96 |
|
97 |
return 0; |
98 |
} |