File: protobuf.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (50 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "fmt.h"
#include "scan.h"
#include <math.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

int main() {
  char buf[100];
  double pi=M_PI;
  float fpi=pi;

  unsigned long long l;
  size_t len;
  unsigned char c;

  assert(fmt_varint(buf,23)==1 && buf[0]==23);
  assert(scan_varint(buf,1,&l)==1 && l==23);
  assert(fmt_varint(buf,150)==2 && buf[0]==(char)0x96 && buf[1]==1);
  assert(scan_varint(buf,2,&l)==2 && l==150);

  assert(fmt_pb_tag(buf,23,2)==2 && (unsigned char)buf[0]==((23<<3)+2));
  assert(scan_pb_tag(buf,2,&len,&c)==2 && len==23 && c==2);

  assert(fmt_pb_type0_int(buf,150)==2 && scan_pb_type0_int(buf,2,&l)==2 && l==150);
  assert(fmt_pb_type0_sint(buf,150)==2 && scan_pb_type0_sint(buf,2,&l)==2 && l==150);

  {
    double d;
    assert(fmt_pb_type1_double(buf,pi)==8 && scan_pb_type1_double(buf,8,&d)==8 && d==pi);
  }

  {
    float f;
    assert(fmt_pb_type5_float(buf,fpi)==4 && scan_pb_type5_float(buf,4,&f)==4 && f==fpi);
  }

  {
    const char* s;
    size_t l;
    assert(fmt_pb_type2_string(buf,"fnord",5)==6);
    assert(scan_pb_type2_stringlen(buf,6,&s,&l)==1 && l==5);
  }

  assert(fmt_pb_int(buf,1,150)==3 && !memcmp(buf,"\x08\x96\x01",3));
  assert(fmt_pb_sint(buf,2,150)==3 && !memcmp(buf,"\x10\xac\x02",3));
  assert(fmt_pb_double(buf,3,pi)==9 && buf[0]==0x19);
  assert(fmt_pb_float(buf,4,fpi)==5 && buf[0]==0x25);
  assert(fmt_pb_string(buf,5,"fnord",5)==7 && !memcmp(buf,"\x2a\x05""fnord",7));
}