Browse Source

a few more test suites

escapecharc now supports non-ascii
master
Felix von Leitner 3 years ago
parent
commit
84d3184cbc
10 changed files with 126 additions and 2 deletions
  1. +1
    -1
      fmt.h
  2. +10
    -0
      fmt/fmt_8long.c
  3. +10
    -0
      fmt/fmt_8longlong.c
  4. +17
    -0
      fmt/fmt_asn1derlength.c
  5. +16
    -0
      fmt/fmt_asn1dertag.c
  6. +50
    -1
      fmt/fmt_escapecharc.c
  7. +10
    -0
      fmt/fmt_ulong.c
  8. +10
    -0
      fmt/fmt_xlong.c
  9. +1
    -0
      textcode/scan_base64.c
  10. +1
    -0
      textcode/scan_jsonescape.c

+ 1
- 1
fmt.h View File

@@ -198,7 +198,7 @@ size_t fmt_escapecharquotedprintable(char* dest,uint32_t ch) __pure__;
/* MIME quoted-printable escaping with UTF-8: 'ö' -> '=c3=b6', characters > 0x7fffffff not supported */
size_t fmt_escapecharquotedprintableutf8(char* dest,uint32_t ch) __pure__;

/* C escaping: '\' -> '\\', newline -> '\n', 0xc2 -> '\302' */
/* C99 style escaping: '\' -> '\\', newline -> '\n', 0xc2 -> '\302' */
size_t fmt_escapecharc(char* dest,uint32_t ch) __pure__;

/* internal functions, may be independently useful */


+ 10
- 0
fmt/fmt_8long.c View File

@@ -11,3 +11,13 @@ size_t fmt_8long(char *dest,unsigned long i) {
}
return len;
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

int main() {
char buf[100];
assert(fmt_8long(buf,012345)==5 && !memcmp(buf,"12345",5));
}
#endif

+ 10
- 0
fmt/fmt_8longlong.c View File

@@ -12,3 +12,13 @@ size_t fmt_8longlong(char *dest,unsigned long long i) {
}
return len;
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

int main() {
char buf[100];
assert(fmt_8longlong(buf,0123456701234567)==15 && !memcmp(buf,"123456701234567",15));
}
#endif

+ 17
- 0
fmt/fmt_asn1derlength.c View File

@@ -23,3 +23,20 @@ size_t fmt_asn1derlength(char* dest,unsigned long long l) {
}
return i+1;
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

int main() {
char buf[100];
#define zap() memset(buf,'_',sizeof buf)
assert(fmt_asn1derlength(NULL,0)==1);
zap(); assert(fmt_asn1derlength(buf,0)==1 && !memcmp(buf,"\x00_",2));
assert(fmt_asn1derlength(NULL,0xc2)==2);
zap(); assert(fmt_asn1derlength(buf,0xc2)==2 && !memcmp(buf,"\x81\xc2_",3));
assert(fmt_asn1derlength(NULL,0x1234)==3);
zap(); assert(fmt_asn1derlength(buf,0x1234)==3 && !memcmp(buf,"\x82\x12\x34_",3));
return 0;
}
#endif

+ 16
- 0
fmt/fmt_asn1dertag.c View File

@@ -13,3 +13,19 @@ size_t fmt_asn1dertag(char* dest,unsigned long long l) {
}
return i;
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

char buf[100];
void zap() { size_t i; for (i=0; i<sizeof(buf); ++i) buf[i]='_'; }

int main() {
assert(fmt_asn1dertag(NULL,0)==1);
zap(); assert(fmt_asn1dertag(buf,0)==1 && !memcmp(buf,"\x00_",2));
assert(fmt_asn1dertag(NULL,0xc2)==2);
zap(); assert(fmt_asn1dertag(buf,0xc2)==2 && !memcmp(buf,"\x81\x42_",3));
return 0;
}
#endif

+ 50
- 1
fmt/fmt_escapecharc.c View File

@@ -1,5 +1,9 @@
#include "fmt.h"

#ifdef UNITTEST
#include "fmt_tohex.c"
#endif

static void fmt_oct3(char* dest,uint8_t w) {
dest[2]=(char)((w&7)+'0'); w>>=3;
dest[1]=(char)((w&7)+'0'); w>>=3;
@@ -8,7 +12,28 @@ static void fmt_oct3(char* dest,uint8_t w) {

size_t fmt_escapecharc(char* dest,uint32_t ch) {
char c;
if (ch>0xff) return 0;
if (ch>0xff) {
if (dest) {
unsigned int i;
dest[0]='\\';
if (ch>0xffff) {
dest[1]='U';
dest[2]=fmt_tohex((ch>>28)&0xf);
dest[3]=fmt_tohex((ch>>24)&0xf);
dest[4]=fmt_tohex((ch>>20)&0xf);
dest[5]=fmt_tohex((ch>>16)&0xf);
i=6;
} else {
dest[1]='u';
i=2;
}
dest[i]=fmt_tohex((ch>>12)&0xf);
dest[i+1]=fmt_tohex((ch>>8)&0xf);
dest[i+2]=fmt_tohex((ch>>4)&0xf);
dest[i+3]=fmt_tohex(ch&0xf);
}
return (ch>0xffff ? 10 : 6);
}
switch (ch) {
case '\a': c='a'; goto doescape;
case '\b': c='b'; goto doescape;
@@ -34,3 +59,27 @@ size_t fmt_escapecharc(char* dest,uint32_t ch) {
return 4;
}
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>
#include <stdio.h>

int main() {
char buf[100];
assert(fmt_escapecharc(buf,0)==4 && !memcmp(buf,"\\000",4));
assert(fmt_escapecharc(buf,'\a')==2 && !memcmp(buf,"\\a",2));
assert(fmt_escapecharc(buf,'\b')==2 && !memcmp(buf,"\\b",2));
assert(fmt_escapecharc(buf,'\f')==2 && !memcmp(buf,"\\f",2));
assert(fmt_escapecharc(buf,'\n')==2 && !memcmp(buf,"\\n",2));
assert(fmt_escapecharc(buf,'\r')==2 && !memcmp(buf,"\\r",2));
assert(fmt_escapecharc(buf,0x1b)==2 && !memcmp(buf,"\\e",2));
assert(fmt_escapecharc(buf,'\t')==2 && !memcmp(buf,"\\t",2));
assert(fmt_escapecharc(buf,'\v')==2 && !memcmp(buf,"\\v",2));
assert(fmt_escapecharc(buf,'\\')==2 && !memcmp(buf,"\\\\",2));
assert(fmt_escapecharc(buf,'1')==4 && !memcmp(buf,"\\001",2));
assert(fmt_escapecharc(buf,0xfefe)==6 && !memcmp(buf,"\\ufefe",6));
assert(fmt_escapecharc(buf,0xfefec0de)==10 && !memcmp(buf,"\\Ufefec0de",10));
}

#endif

+ 10
- 0
fmt/fmt_ulong.c View File

@@ -9,3 +9,13 @@ size_t fmt_ulong(char *dest,unsigned long i) {
*--dest = (char)((tmp%10)+'0');
return len;
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

int main() {
char buf[100];
assert(fmt_ulong(buf,12345)==5 && !memcmp(buf,"12345",5));
}
#endif

+ 10
- 0
fmt/fmt_xlong.c View File

@@ -16,3 +16,13 @@ size_t fmt_xlong(char *dest,unsigned long i) {
}
return len;
}

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

int main() {
char buf[100];
assert(fmt_xlong(buf,0x12345)==5 && !memcmp(buf,"12345",5));
}
#endif

+ 1
- 0
textcode/scan_base64.c View File

@@ -49,5 +49,6 @@ int main() {
size_t i,l;
memset(buf,0,10); assert(scan_base64("Zm5vcmQ=",buf,&l)==8 && l==5 && !memcmp(buf,"fnord",6));
memset(buf,0,10); assert(scan_base64("Zm5vcmQ",buf,&l)==7 && l==5 && !memcmp(buf,"fnord",6));
memset(buf,0,10); assert(scan_base64("//8=",buf,&l)==4 && l==2 && !memcmp(buf,"\xff\xff",3));
}
#endif

+ 1
- 0
textcode/scan_jsonescape.c View File

@@ -72,6 +72,7 @@ abort:

#ifdef UNITTEST
#include <assert.h>
#include <string.h>

int main() {
char buf[100];


Loading…
Cancel
Save