Statistics
| Branch: | Revision:

grapes / som / Tests / cb_test.c @ 8ab58ec7

History | View | Annotate | Download (1.84 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7

    
8
#include <stdint.h>
9
#include <stdlib.h>
10
#include <stdio.h>
11
#include <string.h>
12
#include "chunk.h"
13
#include "chunkbuffer.h"
14

    
15
static struct chunk *chunk_forge(int id)
16
{
17
  struct chunk *c;
18
  char buff[64];
19

    
20
  c = malloc(sizeof(struct chunk));
21
  if (c == NULL) {
22
    return c;
23
  }
24

    
25
  sprintf(buff, "Chunk %d", id);
26
  c->id = id;
27
  c->timestamp = 40 * id;
28
  c->data = strdup(buff);
29
  c->size = strlen(c->data) + 1;
30
  c->attributes_size = 0;
31
  c->attributes = NULL;
32
  return c;
33
}
34

    
35
static void chunk_add(struct chunk_buffer *cb, int id)
36
{
37
  struct chunk *c;
38
  int res;
39

    
40
  printf("Inserting %d... ", id);
41
  c = chunk_forge(id);
42
  if (c) {
43
    res = cb_add_chunk(cb, c);
44
    if (res < 0) {
45
      printf("not inserted (out of window)");
46
      free(c->data);
47
      free(c->attributes);
48
    }
49
  } else {
50
    printf("Failed to create the chunk");
51
  }
52
  printf("\n");
53
  free(c);
54
}
55

    
56
static void cb_print(const struct chunk_buffer *cb)
57
{
58
  struct chunk *buff;
59
  int i, size;
60

    
61
  buff = cb_get_chunks(cb, &size);
62
  for (i = 0; i < size; i++) {
63
    printf("C[%d]: %s %d\n", i, buff[i].data, buff[i].id);
64
  }
65
}
66

    
67
int main(int argc, char *argv[])
68
{
69
  struct chunk_buffer *b;
70

    
71
  b = cb_init("size=8,time=now");
72
  if (b == NULL) {
73
    printf("Error initialising the Chunk Buffer\n");
74

    
75
    return -1;
76
  }
77
  chunk_add(b, 10);
78
  chunk_add(b, 5);
79
  chunk_add(b, 12);
80
  chunk_add(b, 40);
81
  cb_print(b);
82

    
83
  chunk_add(b, 51);
84
  chunk_add(b, 2);
85
  chunk_add(b, 13);
86
  chunk_add(b, 11);
87
  cb_print(b);
88

    
89
  chunk_add(b, 26);
90
  cb_print(b);
91
  chunk_add(b, 30);
92
  cb_print(b);
93
  chunk_add(b, 110);
94
  cb_print(b);
95
  chunk_add(b, 64);
96
  chunk_add(b, 4);
97
  cb_print(b);
98
  chunk_add(b, 7);
99
  chunk_add(b, 34);
100
  chunk_add(b, 2);
101
  chunk_add(b, 33);
102
  cb_print(b);
103

    
104
  cb_destroy(b);
105

    
106
  return 0;
107
}