1 #include "../src/argtable2.h"
2 #include <assert.h>
3 
4 /* for memory leak debugging */
5 #ifdef DMALLOC
6 #include "dmalloc.h"
7 #endif
8 
9 
10  /*
11 Here we verify that arg_freetable() works proprely, at least by nulling the argtable
12 array entries when finished.
13 */
test_freetable(void)14 void test_freetable(void)
15     {
16     struct arg_lit  *list    = arg_lit0("lL",NULL,                      "list files");
17     struct arg_lit  *recurse = arg_lit0("R",NULL,                       "recurse through subdirectories");
18     struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              "define scalar value k (default is 3)");
19     struct arg_str  *defines = arg_strn("D","define","MACRO",0,20,      "macro definitions");
20     struct arg_file *outfile = arg_file0("o",NULL,"<output>",           "output file (default is \"-\")");
21     struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            "verbose messages");
22     struct arg_lit  *help    = arg_lit0(NULL,"help",                    "print this help and exit");
23     struct arg_lit  *version = arg_lit0(NULL,"version",                 "print version information and exit");
24     struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,20,           "input file(s)");
25     struct arg_end  *end     = arg_end(20);
26     void* argtable[]  = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};
27 
28     printf("testing arg_freetable()...\n");
29 
30     assert( argtable[0]!=NULL );
31     assert( argtable[1]!=NULL );
32     assert( argtable[2]!=NULL );
33     assert( argtable[3]!=NULL );
34     assert( argtable[4]!=NULL );
35     assert( argtable[5]!=NULL );
36     assert( argtable[6]!=NULL );
37     assert( argtable[7]!=NULL );
38     assert( argtable[8]!=NULL );
39     assert( argtable[9]!=NULL );
40 
41     arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
42 
43     assert( argtable[0]==NULL );
44     assert( argtable[1]==NULL );
45     assert( argtable[2]==NULL );
46     assert( argtable[3]==NULL );
47     assert( argtable[4]==NULL );
48     assert( argtable[5]==NULL );
49     assert( argtable[6]==NULL );
50     assert( argtable[7]==NULL );
51     assert( argtable[8]==NULL );
52     assert( argtable[9]==NULL );
53 
54     printf("arg_freetable() OK\n");
55     }
56 
57 
58 /*
59 Here we verify that arg_nullcheck() works properly at detecting NULL
60 entries in the argtable array.
61 In this test, we first build a normal argtable (taken from myprog.c)
62 and then a bunch of copycat argtable arrays that have NULL entries
63 dispersed within them. We test arg_nullcheck on each of these
64 argtable arrays to verify iots behaviour.
65 */
test_nullcheck(void)66 void test_nullcheck(void)
67     {
68     struct arg_lit  *list    = arg_lit0("lL",NULL,                      "list files");
69     struct arg_lit  *recurse = arg_lit0("R",NULL,                       "recurse through subdirectories");
70     struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              "define scalar value k (default is 3)");
71     struct arg_str  *defines = arg_strn("D","define","MACRO",0,20,      "macro definitions");
72     struct arg_file *outfile = arg_file0("o",NULL,"<output>",           "output file (default is \"-\")");
73     struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            "verbose messages");
74     struct arg_lit  *help    = arg_lit0(NULL,"help",                    "print this help and exit");
75     struct arg_lit  *version = arg_lit0(NULL,"version",                 "print version information and exit");
76     struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,20,           "input file(s)");
77     struct arg_end  *end     = arg_end(20);
78     void* argtable[]  = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};
79     void* argtable1[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,NULL};
80     void* argtable2[] = {list,recurse,repeat,defines,outfile,verbose,help,version,NULL,   NULL};
81     void* argtable3[] = {list,recurse,repeat,defines,outfile,verbose,help,NULL,   infiles,end};
82     void* argtable4[] = {NULL,recurse,NULL,  NULL,   outfile,NULL,   help,version,infiles,NULL};
83 
84     printf("testing arg_nullcheck()...\n");
85     assert(arg_nullcheck(argtable)  == 0);
86     assert(arg_nullcheck(argtable1) == 1);
87     assert(arg_nullcheck(argtable2) == 1);
88     assert(arg_nullcheck(argtable3) == 1);
89     assert(arg_nullcheck(argtable4) == 1);
90     printf("arg_nullcheck() OK\n");
91 
92     arg_free(argtable);
93     }
94 
95 
test_arg_print_glossary(void)96 void test_arg_print_glossary(void)
97     {
98     struct arg_lit  *list    = arg_lit0("lL",NULL,                      "list files");
99     struct arg_lit  *recurse = arg_lit0("R",NULL,                       "recurse through subdirectories");
100     struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              "define scalar value k (default is 3)");
101     struct arg_str  *defines = arg_strn("D","define","MACRO",0,20,      "macro definitions");
102     struct arg_file *outfile = arg_file0("o",NULL,"<output>",           "output file (default is \"-\")");
103     struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            "verbose messages");
104     struct arg_lit  *help    = arg_lit0(NULL,"help",                    "print this help and exit");
105     struct arg_lit  *version = arg_lit0(NULL,"version",                 "print version information and exit");
106     struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,20,           "input file(s)");
107     struct arg_end  *end     = arg_end(20);
108     void* argtable[]  = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};
109 
110     printf("exercising arg_print_glossary()...\n");
111     arg_print_glossary(stdout,argtable,NULL);
112 
113     printf("exercising arg_print_glossary_gnu()...\n");
114     arg_print_glossary_gnu(stdout,argtable);
115 
116     arg_free(argtable);
117     }
118 
119 
test_printsyntax(void)120 void test_printsyntax(void)
121     {
122     struct arg_lit *help1 = arg_lit1("h",    NULL, "Syntax help");
123     struct arg_end *end1  = arg_end(20);
124     void *argtable1[] = { help1, end1 };
125 
126     struct arg_lit *help2 = arg_lit1(NULL, "help", "Syntax help");
127     struct arg_end *end2  = arg_end(20);
128     void *argtable2[] = { help2, end2 };
129 
130     printf("testing arg_printsyntax()...\n");
131     assert(arg_nullcheck(argtable1)  == 0);
132     assert(arg_nullcheck(argtable2)  == 0);
133 
134     printf("foo");
135     arg_print_syntax(stdout,argtable1,"\n");
136 
137     printf("foo");
138     arg_print_syntax(stdout,argtable2,"\n");
139 
140     arg_free(argtable1);
141     arg_free(argtable2);
142     }
143 
144 
test_argc_zero(void)145 void test_argc_zero(void)
146     {
147     /* simulate an empty argv[] list as occurs on TI DSP */
148     int argc = 0;
149     char **argv = NULL;
150 
151     /* argument table */
152     struct arg_int *a    = arg_int0(NULL,NULL,"a","a is <int>");
153     struct arg_int *b    = arg_int0(NULL,NULL,"b","b is <int>");
154     struct arg_int *c    = arg_int0(NULL,NULL,"c","c is <int>");
155     struct arg_end *end  = arg_end(20);
156     void* argtable[] = {a,b,c,end};
157 
158     int nerrors;
159 
160     printf("testing arg_parse() with argc==0...\n");
161 
162     /* verify the argtable[] entries were allocated sucessfully */
163     assert(arg_nullcheck(argtable) == 0);
164 
165     printf("foo");
166     arg_print_syntax(stdout,argtable,"\n");
167 
168     /* Try parsing the empty command line */
169     nerrors = arg_parse(argc,argv,argtable);
170     assert(nerrors==0);
171 
172     /* deallocate each non-null entry in argtable[] */
173     arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
174     }
175 
176 
test_initial_strings(void)177 void test_initial_strings(void)
178     {
179     struct arg_str  *defines = arg_strn("D","define","MACRO",0,3, "macro definitions");
180     struct arg_file *outfile = arg_filen("o",NULL,"<output>",0,5, "output file (default is \"-\")");
181     struct arg_end  *end     = arg_end(20);
182     void* argtable[]  = {defines,outfile,end};
183     int i;
184 
185     printf("testing initial strings...\n");
186 
187     /* arg_str: assert initial string values are "" */
188     for (i=0; i<defines->hdr.maxcount; i++)
189         {
190         assert( strcmp(defines->sval[i],"") == 0 );
191         printf("defines->sval[%d] == \"\" OK\n",i);
192         }
193 
194     /* arg_file: assert initial string values are "" */
195     for (i=0; i<outfile->hdr.maxcount; i++)
196         {
197         assert( strcmp(outfile->filename[i],"") == 0 );
198         printf("outfile->filename[%d]  == \"\" OK\n",i);
199         assert( strcmp(outfile->basename[i],"") == 0 );
200         printf("outfile->basename[%d]  == \"\" OK\n",i);
201         assert( strcmp(outfile->extension[i],"") == 0 );
202         printf("outfile->extension[%d] == \"\" OK\n",i);
203         }
204 
205     /* deallocate each non-null entry in argtable[] */
206     arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
207     }
208 
209 
main(int argc,char ** argv)210 int main(int argc, char **argv)
211     {
212     test_freetable();
213     test_nullcheck();
214     test_arg_print_glossary();
215     test_printsyntax();
216     test_argc_zero();
217     test_initial_strings();
218 
219     /* close stdin and stdout to stop memcheck whining about their memory not being freed */
220     fclose(stdin);
221     fclose(stdout);
222 
223     return 0;
224     }
225