Statistics
| Branch: | Revision:

grapes / src / Tests / chunk_encoding_test.c @ 7bcfc98d

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

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

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

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

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

    
46
  chunk_print(stdout, &src_c);
47

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

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

    
61
  return 0;
62
}