Statistics
| Branch: | Revision:

grapes / som / Tests / cb_test.c @ 410fd7f8

History | View | Annotate | Download (1.33 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

    
32
  printf("Inserting %d\n", id);
33
  c = chunk_forge(id);
34
  if (c) {
35
    cb_add_chunk(cb, c);
36
  } else {
37
    printf("Failed to create the chunk");
38
  }
39
  free(c);
40
}
41

    
42
static void cb_print(const struct chunk_buffer *cb)
43
{
44
  struct chunk *buff;
45
  int i, size;
46

    
47
  buff = cb_get_chunks(cb, &size);
48
  for (i = 0; i < size; i++) {
49
    printf("C[%d]: %s %d\n", i, buff[i].data, buff[i].id);
50
  }
51
}
52

    
53
int main(int argc, char *argv[])
54
{
55
  struct chunk_buffer *b;
56

    
57
  b = cb_init("size=32");
58
  if (b == NULL) {
59
    printf("Error initialising the Chunk Buffer\n");
60

    
61
    return -1;
62
  }
63
  chunk_add(b, 10);
64
  chunk_add(b, 5);
65
  chunk_add(b, 12);
66
  chunk_add(b, 40);
67
  chunk_add(b, 51);
68
  chunk_add(b, 2);
69
  chunk_add(b, 13);
70
  chunk_add(b, 11);
71
  chunk_add(b, 26);
72
  chunk_add(b, 30);
73
  cb_print(b);
74

    
75
  cb_destroy(b);
76

    
77
  return 0;
78
}