From 6f00ec3623124e4901b9389192c086d8b8b4d0d2 Mon Sep 17 00:00:00 2001 From: Will Estes Date: Thu, 5 Jun 2014 09:15:25 -0400 Subject: refactor mem_nr test for new test suite layout --- tests/.gitignore | 2 + tests/Makefile.am | 7 +- tests/mem_nr.l | 179 ++++++++++++++++++++++++++++++++++++++++++ tests/mem_nr.txt | 25 ++++++ tests/test-mem-nr/.gitignore | 9 --- tests/test-mem-nr/Makefile.am | 44 ----------- tests/test-mem-nr/scanner.l | 179 ------------------------------------------ tests/test-mem-nr/test.input | 25 ------ 8 files changed, 211 insertions(+), 259 deletions(-) create mode 100644 tests/mem_nr.l create mode 100644 tests/mem_nr.txt delete mode 100644 tests/test-mem-nr/.gitignore delete mode 100644 tests/test-mem-nr/Makefile.am delete mode 100644 tests/test-mem-nr/scanner.l delete mode 100644 tests/test-mem-nr/test.input (limited to 'tests') diff --git a/tests/.gitignore b/tests/.gitignore index f7dd131..51ea57f 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -32,3 +32,5 @@ header_nr header_nr_scanner.[ch] header_r header_r_scanner.[ch] +mem_nr +mem_nr.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 4595c88..11653dc 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -40,7 +40,8 @@ check_PROGRAMS = \ debug_r \ extended \ header_nr \ - header_r + header_r \ + mem_nr alloc_extra_SOURCES = alloc_extra.l array_nr_SOURCES = array_nr.l @@ -56,6 +57,7 @@ debug_r_SOURCES = debug_r.l extended_SOURCES = extended.l header_nr_SOURCES = header_nr_scanner.l header_nr_main.c header_r_SOURCES = header_r_scanner.l header_r_main.c +mem_nr_SOURCES = mem_nr.l BUILT_SOURCES = \ bison_nr_scanner.h \ @@ -88,7 +90,8 @@ basic_nr.txt \ header_nr_scanner.h \ header_nr.txt \ header_r_scanner.h \ - header_r.txt + header_r.txt \ + mem_nr.txt # specify how to process .l files in order to test the flex built by make all diff --git a/tests/mem_nr.l b/tests/mem_nr.l new file mode 100644 index 0000000..9935763 --- /dev/null +++ b/tests/mem_nr.l @@ -0,0 +1,179 @@ +/* + * This file is part of flex. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. + */ + +%{ +/* A template scanner file to build "scanner.c". + * The whole idea is to cause memory realloc by + * 1. pushing a lot on the condition stack, and + * 2. eating input greater than YY_BUF_SIZE + */ +#include +#include +#include "config.h" + +/* Insanely small read buffer. This pretty much guarantees at least one realloc. */ +#ifdef YY_BUF_SIZE +#undef YY_BUF_SIZE +#endif +#define YY_BUF_SIZE 8 + +%} + +%option 8bit prefix="test" +%option nounput nomain noyywrap +%option warn stack nodefault +%option noyyalloc noyyrealloc noyyfree + +%x parens + +%% + +{ +"(" { printf("yy_push_state(parens)\n"); yy_push_state(parens); } +len=[0-9]+ { printf("About read token where %s\n",yytext); } +0+ { } +.|\n { } +} + +{ +"(" { printf("yy_push_state(parens)\n"); yy_push_state(parens); } +")" { printf("yy_pop_state()\n");yy_pop_state();} +[^()\n]+ { } +.|\n { } +} + +%% +/* total memory allocated */ +static size_t total_mem=0; + +/* track the amount of memory for ptr. */ +struct memsz { + void* p; + size_t sz; +}; + +static struct memsz * ptrs=0; /* Array of pairs. */ +static int nptrs=0; /* Number of pairs in array. */ +static int arrsz=0; /* Capacity of array. */ + +static void dump_mem(FILE* fp){ + int i; + fprintf(fp,"\tptrs[%d] = {", nptrs); + for (i=0; i < arrsz; i++) + fprintf(fp," {%#lx,%ld},", (long)ptrs[i].p, (long)ptrs[i].sz); + + fprintf(fp,"}\n"); +} + +void * yyalloc(yy_size_t n) +{ + void * p; + int i; + + total_mem += n; + p = (void*)malloc(n); + + if( nptrs >= arrsz){ + /* increase array size by 1 */ + arrsz++; + ptrs = (struct memsz*)realloc( ptrs, arrsz * sizeof(struct memsz)); + ptrs[nptrs].p = 0; + ptrs[nptrs].sz = 0; + } + + /* find a null slot */ + for(i=0; i < arrsz ; i++) + if (ptrs[i].p == 0) { + ptrs[i].p = p; + ptrs[i].sz = n; + } + + nptrs++; + printf("yyflex_alloc(%8ld) total=%8ld return=%#10lx\n",(long)n,(long)total_mem,(long)p); + dump_mem(stdout); + return p; +} + +void * yyrealloc(void* p, yy_size_t n) +{ + int i; + for (i=0; i < arrsz; i++) + if ( ptrs[i].p == p){ + total_mem -= ptrs[i].sz; + total_mem += n; + ptrs[i].p = (void*)realloc(p,n); + ptrs[i].sz = n; + + printf("yyflex_realloc(%#10lx,%8ld) total=%8ld return=%8lx\n", + (long)p,(long)n,(long)total_mem,(long)ptrs[i].p); + dump_mem(stdout); + return ptrs[i].p; + } + + fprintf(stderr,"ERROR: yyflex_realloc could not locate pointer %#lx.\n",(long)p); + dump_mem(stdout); + exit(1); +} + +void yyfree(void* p) +{ + int i; + for (i=0; i < arrsz; i++) + if ( ptrs[i].p == p){ + total_mem -= ptrs[i].sz; + free(p); + ptrs[i].p = 0; + ptrs[i].sz = 0; + nptrs--; + printf("yyflex_free(%#10lx) total=%8ld\n",(long)p,(long)total_mem); + dump_mem(stdout); + return; + } + + fprintf(stderr,"ERROR: yyflex_free could not locate pointer %#lx.\n",(long)p); + dump_mem(stdout); + exit(1); +} + +int main(void); + +int +main () +{ + arrsz = 1; + ptrs = (struct memsz*)calloc(1,sizeof(struct memsz)); + nptrs = 0; + + yyin = stdin; + yyout = stdout; + yylex(); + yylex_destroy(); + free(ptrs); + + if ( nptrs > 0 || total_mem > 0){ + fprintf(stderr,"Oops. Looks like a memory leak: nptrs=%d, unfreed memory=%ld\n",nptrs,(long)total_mem); + exit(1); + } + printf("TEST RETURNING OK.\n"); + return 0; +} diff --git a/tests/mem_nr.txt b/tests/mem_nr.txt new file mode 100644 index 0000000..79aa16a --- /dev/null +++ b/tests/mem_nr.txt @@ -0,0 +1,25 @@ +First we push a lot on the stack by nesting parenthesis: + +(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( +((((((((((((((((((((((((((((((((((((((((((( + +(should be 200 states pushed here) + +)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) +))))))))))))))))))))))))))))))))))))))))))) + +Now we match progressively bigger tokens to increase the read buffer: + +len=1 0 +len=2 00 +len=4 0000 +len=8 00000000 +len=16 0000000000000000 +len=32 00000000000000000000000000000000 +len=64 0000000000000000000000000000000000000000000000000000000000000000 +len=128 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +len=256 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +len=512 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +len=1024 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/tests/test-mem-nr/.gitignore b/tests/test-mem-nr/.gitignore deleted file mode 100644 index 7b73f56..0000000 --- a/tests/test-mem-nr/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -Makefile -Makefile.in -parser.c -parser.h -scanner.c -TEMPLATE -OUTPUT -.deps -test-mem-nr diff --git a/tests/test-mem-nr/Makefile.am b/tests/test-mem-nr/Makefile.am deleted file mode 100644 index 4ad450d..0000000 --- a/tests/test-mem-nr/Makefile.am +++ /dev/null @@ -1,44 +0,0 @@ -# This file is part of flex. - -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: - -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. - -# Neither the name of the University nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. - -# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR -# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED -# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE. - - -FLEX = $(top_builddir)/flex - -EXTRA_DIST = scanner.l test.input -CLEANFILES = scanner.c scanner.h $(testname)$(EXEEXT) OUTPUT $(OBJS) -OBJS = scanner.o - -AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir) -I$(top_builddir) -#LDFLAGS = $(top_srcdir)/libfl.a - -testname = test-mem-nr - -scanner.c: $(srcdir)/scanner.l - $(FLEX) $(LFLAGS) $< - -$(testname)$(EXEEXT): $(OBJS) - $(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(OBJS) $(LOADLIBES) - -test: $(testname)$(EXEEXT) - ./$(testname)$(EXEEXT) < $(srcdir)/test.input - -.c.o: - $(CC) -c -o $@ $(AM_CPPFLAGS) $(CPPFLAGS) $(CFLAGS) $< diff --git a/tests/test-mem-nr/scanner.l b/tests/test-mem-nr/scanner.l deleted file mode 100644 index d03996a..0000000 --- a/tests/test-mem-nr/scanner.l +++ /dev/null @@ -1,179 +0,0 @@ -/* - * This file is part of flex. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE. - */ - -%{ -/* A template scanner file to build "scanner.c". - * The whole idea is to cause memory realloc by - * 1. pushing a lot on the condition stack, and - * 2. eating input greater than YY_BUF_SIZE - */ -#include -#include -#include "config.h" - -/* Insanely small read buffer. This pretty much guarantees at least one realloc. */ -#ifdef YY_BUF_SIZE -#undef YY_BUF_SIZE -#endif -#define YY_BUF_SIZE 8 - -%} - -%option 8bit outfile="scanner.c" prefix="test" -%option nounput nomain noyywrap -%option warn stack nodefault -%option noyyalloc noyyrealloc noyyfree - -%x parens - -%% - -{ -"(" { printf("yy_push_state(parens)\n"); yy_push_state(parens); } -len=[0-9]+ { printf("About read token where %s\n",yytext); } -0+ { } -.|\n { } -} - -{ -"(" { printf("yy_push_state(parens)\n"); yy_push_state(parens); } -")" { printf("yy_pop_state()\n");yy_pop_state();} -[^()\n]+ { } -.|\n { } -} - -%% -/* total memory allocated */ -static size_t total_mem=0; - -/* track the amount of memory for ptr. */ -struct memsz { - void* p; - size_t sz; -}; - -static struct memsz * ptrs=0; /* Array of pairs. */ -static int nptrs=0; /* Number of pairs in array. */ -static int arrsz=0; /* Capacity of array. */ - -static void dump_mem(FILE* fp){ - int i; - fprintf(fp,"\tptrs[%d] = {", nptrs); - for (i=0; i < arrsz; i++) - fprintf(fp," {%#lx,%ld},", (long)ptrs[i].p, (long)ptrs[i].sz); - - fprintf(fp,"}\n"); -} - -void * yyalloc(yy_size_t n) -{ - void * p; - int i; - - total_mem += n; - p = (void*)malloc(n); - - if( nptrs >= arrsz){ - /* increase array size by 1 */ - arrsz++; - ptrs = (struct memsz*)realloc( ptrs, arrsz * sizeof(struct memsz)); - ptrs[nptrs].p = 0; - ptrs[nptrs].sz = 0; - } - - /* find a null slot */ - for(i=0; i < arrsz ; i++) - if (ptrs[i].p == 0) { - ptrs[i].p = p; - ptrs[i].sz = n; - } - - nptrs++; - printf("yyflex_alloc(%8ld) total=%8ld return=%#10lx\n",(long)n,(long)total_mem,(long)p); - dump_mem(stdout); - return p; -} - -void * yyrealloc(void* p, yy_size_t n) -{ - int i; - for (i=0; i < arrsz; i++) - if ( ptrs[i].p == p){ - total_mem -= ptrs[i].sz; - total_mem += n; - ptrs[i].p = (void*)realloc(p,n); - ptrs[i].sz = n; - - printf("yyflex_realloc(%#10lx,%8ld) total=%8ld return=%8lx\n", - (long)p,(long)n,(long)total_mem,(long)ptrs[i].p); - dump_mem(stdout); - return ptrs[i].p; - } - - fprintf(stderr,"ERROR: yyflex_realloc could not locate pointer %#lx.\n",(long)p); - dump_mem(stdout); - exit(1); -} - -void yyfree(void* p) -{ - int i; - for (i=0; i < arrsz; i++) - if ( ptrs[i].p == p){ - total_mem -= ptrs[i].sz; - free(p); - ptrs[i].p = 0; - ptrs[i].sz = 0; - nptrs--; - printf("yyflex_free(%#10lx) total=%8ld\n",(long)p,(long)total_mem); - dump_mem(stdout); - return; - } - - fprintf(stderr,"ERROR: yyflex_free could not locate pointer %#lx.\n",(long)p); - dump_mem(stdout); - exit(1); -} - -int main(void); - -int -main () -{ - arrsz = 1; - ptrs = (struct memsz*)calloc(1,sizeof(struct memsz)); - nptrs = 0; - - yyin = stdin; - yyout = stdout; - yylex(); - yylex_destroy(); - free(ptrs); - - if ( nptrs > 0 || total_mem > 0){ - fprintf(stderr,"Oops. Looks like a memory leak: nptrs=%d, unfreed memory=%ld\n",nptrs,(long)total_mem); - exit(1); - } - printf("TEST RETURNING OK.\n"); - return 0; -} diff --git a/tests/test-mem-nr/test.input b/tests/test-mem-nr/test.input deleted file mode 100644 index 79aa16a..0000000 --- a/tests/test-mem-nr/test.input +++ /dev/null @@ -1,25 +0,0 @@ -First we push a lot on the stack by nesting parenthesis: - -(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( -(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( -((((((((((((((((((((((((((((((((((((((((((( - -(should be 200 states pushed here) - -)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -))))))))))))))))))))))))))))))))))))))))))) - -Now we match progressively bigger tokens to increase the read buffer: - -len=1 0 -len=2 00 -len=4 0000 -len=8 00000000 -len=16 0000000000000000 -len=32 00000000000000000000000000000000 -len=64 0000000000000000000000000000000000000000000000000000000000000000 -len=128 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -len=256 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -len=512 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -len=1024 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -- cgit v1.2.3