test.c (1289B)
1 #include <u.h> 2 #include <libc.h> 3 #include "../util/util.h" 4 #include "tablist.h" 5 6 void 7 printnodes(Slice *nodes, int indent) 8 { 9 int i, j; 10 for (i = 0; i < nodes->len; i++) { 11 TLnode **v = slicegetp(nodes, i); 12 TLnode *n = *v; 13 print ("%d:", i); 14 for (j = 0; j < indent; j++) print(" "); 15 if (n == nil) {print("<nil>\n"); continue;} 16 if (n->name != nil) print("[n %.*s]", n->name->len, n->name->p); 17 else if (n->value != nil) print("[v %.*s]", n->value->len, n->value->p); 18 if (n->children != nil) { 19 print("[children %d]\n", n->children->len); 20 printnodes(n->children, indent + 1); 21 } 22 else print("\n"); 23 } 24 } 25 26 27 void 28 test(char *path) 29 { 30 enum { BufSize = 256 }; 31 fprint(2, "%s...\n", path); 32 char *buf = malloc(BufSize); 33 int fd = open(path, OREAD); 34 if (fd < 0) sysfatal("%r"); 35 36 usize n = read(fd, buf, BufSize); 37 print("READ %d bytes\n", n); 38 write(1, buf, n); 39 40 print("DECODE\n"); 41 TLdecoder *tld = initTLdecoder(path); 42 TLdecode(tld, buf, n); 43 44 print("DEBUGPRINT\n"); 45 printnodes(tld->nodes, 0); 46 47 print("ENCODE\n"); 48 TLencoder *tle = initTLencoder(tld->nodes); 49 n = TLencode(tle, buf, BufSize); 50 51 print("WRITE %d bytes\n", n); 52 write(1, buf, n); 53 } 54 55 void 56 main(int argc, char **argv) 57 { 58 int i; 59 for (i = 1; i < argc; i++) test(argv[i]); 60 fprint(2, "OVER\n"); 61 exits(nil); 62 }