Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

  1. #include "fmt.h"
  2. #include "scan.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. int main() {
  8. char buf[100];
  9. double pi=M_PI;
  10. float fpi=pi;
  11. unsigned long long l;
  12. size_t len;
  13. unsigned char c;
  14. assert(fmt_varint(buf,23)==1 && buf[0]==23);
  15. assert(scan_varint(buf,1,&l)==1 && l==23);
  16. assert(fmt_varint(buf,150)==2 && buf[0]==(char)0x96 && buf[1]==1);
  17. assert(scan_varint(buf,2,&l)==2 && l==150);
  18. assert(fmt_pb_tag(buf,23,2)==2 && (unsigned char)buf[0]==((23<<3)+2));
  19. assert(scan_pb_tag(buf,2,&len,&c)==2 && len==23 && c==2);
  20. assert(fmt_pb_type0_int(buf,150)==2 && scan_pb_type0_int(buf,2,&l)==2 && l==150);
  21. assert(fmt_pb_type0_sint(buf,150)==2 && scan_pb_type0_sint(buf,2,&l)==2 && l==150);
  22. {
  23. double d;
  24. assert(fmt_pb_type1_double(buf,pi)==8 && scan_pb_type1_double(buf,8,&d)==8 && d==pi);
  25. }
  26. {
  27. float f;
  28. assert(fmt_pb_type5_float(buf,fpi)==4 && scan_pb_type5_float(buf,4,&f)==4 && f==fpi);
  29. }
  30. {
  31. const char* s;
  32. size_t l;
  33. assert(fmt_pb_type2_string(buf,"fnord",5)==6);
  34. assert(scan_pb_type2_stringlen(buf,6,&s,&l)==1 && l==5);
  35. }
  36. assert(fmt_pb_int(buf,1,150)==3 && !memcmp(buf,"\x08\x96\x01",3));
  37. assert(fmt_pb_sint(buf,2,150)==3 && !memcmp(buf,"\x10\xac\x02",3));
  38. assert(fmt_pb_double(buf,3,pi)==9 && buf[0]==0x19);
  39. assert(fmt_pb_float(buf,4,fpi)==5 && buf[0]==0x25);
  40. assert(fmt_pb_string(buf,5,"fnord",5)==7 && !memcmp(buf,"\x2a\x05""fnord",7));
  41. }