int main(int argc, char **argv) { sexp_t *sx_in, *sx_out; int fd; char *b; size_t l = 0; CSTRING *s = NULL; pcont_t *pc; /* read data */ fd = open("testdata",O_RDONLY); if (fd <= 0) { printf("Error opening test data file ``testdata''\n"); exit(1); } b = readfile(fd,&l,1024,256); close(fd); /* report */ printf("Read %lu bytes of data.\n",(unsigned long)l); sx_in = (sexp_t *)malloc(sizeof(sexp_t)); assert(sx_in != NULL); sx_in->ty = SEXP_VALUE; sx_in->next = sx_in->list = NULL; sx_in->aty = SEXP_BINARY; sx_in->bindata = b; sx_in->binlength = l; sx_in = new_sexp_list(sx_in); printf("Created expression.\n"); print_sexp_cstr(&s,sx_in,l+1024); destroy_sexp(sx_in); b = NULL; sx_in = NULL; printf("Destroyed AST and buffer.\n"); pc = init_continuation(NULL); pc->mode = PARSER_INLINE_BINARY; pc = cparse_sexp(s->base,s->len,pc); sx_out = pc->last_sexp; printf("Parsed unparsed version back to AST.\n"); assert(sx_out != NULL); b = sx_out->list->bindata; l = sx_out->list->binlength; fd = open("testdata_out",O_RDWR|O_CREAT); if (fd <= 0) { printf("Error opening ``testdata_out'': Create empty file to write to.\n"); exit(1); } write(fd,b,l); close(fd); printf("Extracted and wrote bindata from AST.\n"); exit(1); }
int main(int argc, char **argv) { int fd; sexp_t *sx; sexp_iowrap_t *iow; int diff; unsigned int i; int ch; char fname[BUFSIZ]; CSTRING *s1,*s2; int passes, failures; char outbuf1[8192], outbuf2[8192]; s1 = s2 = NULL; passes = failures = 0; strcpy(fname,TESTFILE); while ((ch = getopt(argc,argv,"f:")) != -1) { switch ((char)ch) { case 'f': strcpy(fname,optarg); break; default: break; } } fd = open(fname,O_RDONLY); iow = init_iowrap(fd); printf("TESTING CSTRING BASED UNPARSE:\n"); sx = read_one_sexp(iow); while (sx != NULL) { print_sexp_cstr(&s1,sx,8); destroy_sexp(sx); sx = parse_sexp(s1->base,s1->curlen); if (sx == NULL) { fprintf(stderr,"ERROR: parser error state of %d\n", sexp_errno); exit(1); } print_sexp_cstr(&s2,sx,8); destroy_sexp(sx); sexp_cleanup(); diff = 0; for (i=0;i<s1->curlen;i++) { diff += abs((s1->base[i] - s2->base[i])); if (s1->base[i] == '\0') break; } /** * diff is the lexical difference between the first unparsing * of the original buffer and the unparsed version of the parsed * version of the first unparsed string. In other words, does: * * orig->parse->unparse == orig->parse->unparse->parse->unparse * * This catches issues with print and parse to make sure the meaning * of the original is kept (or at least, "bugs" in the parser have * matching "bugs" in the printer.) */ if (diff != 0) { printf("FIXED POINT MISSED (diff=%d): \nS1: %s\nS2: %s\n",diff, s1->base,s2->base); failures++; } else { passes++; } /** clean up strings **/ sdestroy(s1); sdestroy(s2); s1 = s2 = NULL; sx = read_one_sexp(iow); } destroy_iowrap(iow); close(fd); printf("TOTAL TESTS: %d PASS=%d FAIL=%d\n\n", passes+failures,passes,failures); passes = failures = 0; /*** *** now do normal fixed length buffer unparse testing ***/ fd = open(fname,O_RDONLY); iow = init_iowrap(fd); printf("TESTING FIXED SIZE BUFFER BASED UNPARSE:\n"); sx = read_one_sexp(iow); while (sx != NULL) { print_sexp(outbuf1,8192,sx); destroy_sexp(sx); sx = parse_sexp(outbuf1,8192); if (sx == NULL) { fprintf(stderr,"ERROR: parser error state of %d\n", sexp_errno); exit(1); } print_sexp(outbuf2,8192,sx); destroy_sexp(sx); sexp_cleanup(); diff = 0; for (i=0;i<8192;i++) { diff += abs((outbuf1[i] - outbuf2[i])); if (outbuf1[i] == '\0' || outbuf2[i] == '\0') break; } /** * diff is the lexical difference between the first unparsing * of the original buffer and the unparsed version of the parsed * version of the first unparsed string. In other words, does: * * orig->parse->unparse == orig->parse->unparse->parse->unparse * * This catches issues with print and parse to make sure the meaning * of the original is kept (or at least, "bugs" in the parser have * matching "bugs" in the printer.) */ if (diff != 0) { printf("FIXED POINT MISSED (diff=%d): \nS1: %s\nS2: %s\n",diff, outbuf1,outbuf2); failures++; } else { passes++; } sx = read_one_sexp(iow); } destroy_iowrap(iow); close(fd); printf("TOTAL TESTS: %d PASS=%d FAIL=%d\n", passes+failures,passes,failures); exit(0); }