Statistics
| Branch: | Revision:

grapes / src / Tests / cb_test.c @ 88f5c397

History | View | Annotate | Download (1.86 KB)

1 8ab58ec7 Luca Abeni
/*
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 410fd7f8 Luca Abeni
#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 ec807410 Luca Abeni
  int res;
39 410fd7f8 Luca Abeni
40 ec807410 Luca Abeni
  printf("Inserting %d... ", id);
41 410fd7f8 Luca Abeni
  c = chunk_forge(id);
42
  if (c) {
43 ec807410 Luca Abeni
    res = cb_add_chunk(cb, c);
44
    if (res < 0) {
45
      printf("not inserted (out of window)");
46 63f580ea Luca Abeni
      free(c->data);
47
      free(c->attributes);
48 ec807410 Luca Abeni
    }
49 410fd7f8 Luca Abeni
  } else {
50
    printf("Failed to create the chunk");
51
  }
52 ec807410 Luca Abeni
  printf("\n");
53 410fd7f8 Luca Abeni
  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 ec807410 Luca Abeni
  b = cb_init("size=8,time=now");
72 410fd7f8 Luca Abeni
  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 4893c9e0 Luca Abeni
  chunk_add(b, 12);
81 410fd7f8 Luca Abeni
  chunk_add(b, 40);
82 ec807410 Luca Abeni
  cb_print(b);
83
84 410fd7f8 Luca Abeni
  chunk_add(b, 51);
85
  chunk_add(b, 2);
86
  chunk_add(b, 13);
87
  chunk_add(b, 11);
88 ec807410 Luca Abeni
  cb_print(b);
89
90 410fd7f8 Luca Abeni
  chunk_add(b, 26);
91 ec807410 Luca Abeni
  cb_print(b);
92 410fd7f8 Luca Abeni
  chunk_add(b, 30);
93
  cb_print(b);
94 ec807410 Luca Abeni
  chunk_add(b, 110);
95
  cb_print(b);
96
  chunk_add(b, 64);
97
  chunk_add(b, 4);
98
  cb_print(b);
99
  chunk_add(b, 7);
100
  chunk_add(b, 34);
101
  chunk_add(b, 2);
102
  chunk_add(b, 33);
103
  cb_print(b);
104 410fd7f8 Luca Abeni
105
  cb_destroy(b);
106
107
  return 0;
108
}