Statistics
| Branch: | Revision:

grapes / som / Tests / chunkidset_test.c @ 747990ee

History | View | Annotate | Download (2.53 KB)

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

    
7
#include <stdint.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include "chunkidset.h"
12
#include "trade_sig_la.h"
13

    
14
static void check_chunk(struct chunkID_set *c, int n)
15
{
16
  printf("Is %d in the Chunk ID Set?\t", n);
17
  if (chunkID_set_check(c, n) >= 0) {
18
    printf("Yes\n");
19
  } else {
20
    printf("No\n");
21
  }
22
}
23

    
24
static void add_chunk(struct chunkID_set *c, int n)
25
{
26
  int res;
27

    
28
  res = chunkID_set_add_chunk(c, n);
29
  printf("Add %d ---> Result=%d\tSize=%d\n", n, res, chunkID_set_size(c));
30
}
31

    
32
static void print_set(struct chunkID_set *c)
33
{
34
  int i, size = chunkID_set_size(c);
35

    
36
  printf("Chunk ID Set size: %d\n", size);
37
  printf("Chunks (in priority order): ");
38
  for (i = 0; i < size; i++) {
39
    printf(" %d", chunkID_set_get_chunk(c, i));
40
  }
41

    
42
  printf("\n");
43
}
44

    
45
static void populate(struct chunkID_set *cset)
46
{
47
  int i;
48

    
49
  for(i = 1; i < 400; i+=3){
50
    add_chunk(cset, (i%140) );
51
  }  
52
}
53

    
54
static void simple_test(void)
55
{
56
  struct chunkID_set *cset;
57
  char config[32];
58
  sprintf(config,"size=%d",10);
59
  cset = chunkID_set_init(config);
60

    
61
  printf("Chunk ID Set initialised: size is %d\n", chunkID_set_size(cset));
62
  populate(cset);
63
  print_set(cset);
64
  check_chunk(cset, 4);
65
  check_chunk(cset, 2);
66
  check_chunk(cset, 3);
67
  check_chunk(cset, 9);
68
  chunkID_set_clear(cset, 0);
69
  free(cset);
70
}
71

    
72
static void encoding_test(const char *mode)
73
{
74
  struct chunkID_set *cset, *cset1;
75
  static uint8_t buff[2048];
76
  int res, meta_len;
77
  void *meta;
78
  char config[32];
79

    
80
  sprintf(config,"type=%s", mode);
81
  cset = chunkID_set_init(config);
82
  populate(cset);
83
  res = encodeChunkSignaling(cset, NULL, 0, buff, sizeof(buff));
84
  printf("Encoding Result: %d\n", res);
85
  chunkID_set_clear(cset, 0);
86
  free(cset);
87
  
88
  cset1 = decodeChunkSignaling(&meta, &meta_len, buff, res);
89
  print_set(cset1);
90
  chunkID_set_clear(cset1, 0);
91
  free(cset1);
92
}
93

    
94
static void metadata_test(void)
95
{
96
  struct chunkID_set *cset;
97
  static uint8_t buff[2048];
98
  int res, meta_len;
99
  void *meta;
100

    
101
  meta = strdup("This is a test");
102
  res = encodeChunkSignaling(NULL, meta, strlen(meta) + 1, buff, sizeof(buff));
103
  printf("Encoding Result: %d\n", res);
104
  free(meta);
105
  
106
  cset = decodeChunkSignaling(&meta, &meta_len, buff, res);
107
  printf("Decoded MetaData: %s (%d)\n", (char *)meta, meta_len);
108

    
109
  if (cset) {
110
    chunkID_set_clear(cset, 0);
111
    free(cset);
112
  }
113

    
114
  free(meta);
115
}
116

    
117
int main(int argc, char *argv[])
118
{
119
  simple_test();
120
  encoding_test("priority");
121
  encoding_test("bitmap");
122
  metadata_test();
123

    
124
  return 0;
125
}