Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.51 KB)

1 a9979f3b Luca Abeni
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4 8ab58ec7 Luca Abeni
 *  This is free software; see gpl-3.0.txt
5 a9979f3b Luca Abeni
 */
6
7
#include <stdint.h>
8 c81e8439 Luca Abeni
#include <stdlib.h>
9 a9979f3b Luca Abeni
#include <stdio.h>
10
#include <string.h>
11 45085f9a Alessandro Russo
#include <inttypes.h>
12 a9979f3b Luca Abeni
#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 54bcaba9 Luca Abeni
  fprintf(f, "\tTS: %"PRIu64"\n", c->timestamp);
21 a9979f3b Luca Abeni
  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 c81e8439 Luca Abeni
  src_c.data = strdup("ciao");
44 a9979f3b Luca Abeni
  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 d4a41f14 CsabaKiraly
  fprintf(stdout, "Encoding in 23 bytes: %d\n", res);
52 a9979f3b Luca Abeni
  res = encodeChunk(&src_c, buff, sizeof(buff));
53 94e5889b Alessandro Russo
  fprintf(stdout, "Encoding in %zu bytes: %d\n", sizeof(buff), res);
54 c81e8439 Luca Abeni
  free(src_c.data);
55 a9979f3b Luca Abeni
56
  res = decodeChunk(&dst_c, buff, res);
57
  fprintf(stdout, "Decoding it: %d\n", res);
58
  chunk_print(stdout, &dst_c);
59 6efeb8d3 Luca Abeni
  free(dst_c.data);
60 a9979f3b Luca Abeni
61
  return 0;
62
}