Statistics
| Branch: | Revision:

grapes / som / Tests / chunk_encoding_test.c @ c81e8439

History | View | Annotate | Download (1.46 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 "chunk.h"
12
#include "trade_msg_la.h"
13

    
14
static void chunk_print(FILE *f, const struct chunk *c)
15
{
16
  const uint8_t *p;
17

    
18
  fprintf(f, "Chunk %d:\n", c->id);
19
  fprintf(f, "\tTS: %llu\n", c->timestamp);
20
  fprintf(f, "\tPayload size: %d\n", c->size);
21
  fprintf(f, "\tAttributes size: %d\n", c->attributes_size);
22
  p = c->data;
23
  fprintf(f, "\tPayload:\n");
24
  fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
25
  if (c->attributes_size > 0) {
26
    p = c->attributes;
27
    fprintf(f, "\tAttributes:\n");
28
    fprintf(f, "\t\t%c %c %c %c ...:\n", p[0], p[1], p[2], p[3]);
29
  }
30
}
31

    
32
int main(int argc, char *argv[])
33
{
34
  struct chunk src_c;
35
  struct chunk dst_c;
36
  uint8_t buff[100];
37
  int res;
38

    
39
  src_c.id = 666;
40
  src_c.timestamp = 1000000000ULL;
41
  src_c.size = strlen("ciao") + 1;
42
  src_c.data = strdup("ciao");
43
  src_c.attributes_size = 0;
44

    
45
  chunk_print(stdout, &src_c);
46

    
47
  res = encodeChunk(&src_c, buff, 15);
48
  fprintf(stdout, "Encoding in 15 bytes: %d\n", res);
49
  res = encodeChunk(&src_c, buff, 23);
50
  fprintf(stdout, "Encoding in 23 bytes: %d\n", res);
51
  res = encodeChunk(&src_c, buff, sizeof(buff));
52
  fprintf(stdout, "Encoding in %d bytes: %d\n", sizeof(buff), res);
53
  free(src_c.data);
54

    
55
  res = decodeChunk(&dst_c, buff, res);
56
  fprintf(stdout, "Decoding it: %d\n", res);
57
  chunk_print(stdout, &dst_c);
58

    
59
  return 0;
60
}