File: fntests.c

package info (click to toggle)
argtable2 6-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,520 kB
  • ctags: 380
  • sloc: sh: 8,918; ansic: 4,305; makefile: 144
file content (156 lines) | stat: -rw-r--r-- 6,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "../src/argtable2.h"
#include <assert.h>

/* for memory leak debugging */
#ifdef DMALLOC
#include "dmalloc.h"
#endif


 /*
Here we verify that arg_freetable() works proprely, at least by nulling the argtable
array entries when finished.
*/
void test_freetable(void)
    {
    struct arg_lit  *list    = arg_lit0("lL",NULL,                      "list files");
    struct arg_lit  *recurse = arg_lit0("R",NULL,                       "recurse through subdirectories");
    struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              "define scalar value k (default is 3)");
    struct arg_str  *defines = arg_strn("D","define","MACRO",0,20,      "macro definitions");
    struct arg_file *outfile = arg_file0("o",NULL,"<output>",           "output file (default is \"-\")");
    struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            "verbose messages");
    struct arg_lit  *help    = arg_lit0(NULL,"help",                    "print this help and exit");
    struct arg_lit  *version = arg_lit0(NULL,"version",                 "print version information and exit");
    struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,20,           "input file(s)");
    struct arg_end  *end     = arg_end(20);
    void* argtable[]  = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};

    printf("testing arg_freetable()...\n");
    
    assert( argtable[0]!=NULL );
    assert( argtable[1]!=NULL );
    assert( argtable[2]!=NULL );
    assert( argtable[3]!=NULL );
    assert( argtable[4]!=NULL );
    assert( argtable[5]!=NULL );
    assert( argtable[6]!=NULL );
    assert( argtable[7]!=NULL );
    assert( argtable[8]!=NULL );
    assert( argtable[9]!=NULL );
    
    arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
    
    assert( argtable[0]==NULL );
    assert( argtable[1]==NULL );
    assert( argtable[2]==NULL );
    assert( argtable[3]==NULL );
    assert( argtable[4]==NULL );
    assert( argtable[5]==NULL );
    assert( argtable[6]==NULL );
    assert( argtable[7]==NULL );
    assert( argtable[8]==NULL );
    assert( argtable[9]==NULL );
    
    printf("arg_freetable() OK\n");
    }

       
/*
Here we verify that arg_nullcheck() works properly at detecting NULL
entries in the argtable array.
In this test, we first build a normal argtable (taken from myprog.c)
and then a bunch of copycat argtable arrays that have NULL entries
dispersed within them. We test arg_nullcheck on each of these
argtable arrays to verify iots behaviour.
*/
void test_nullcheck(void)
    {
    struct arg_lit  *list    = arg_lit0("lL",NULL,                      "list files");
    struct arg_lit  *recurse = arg_lit0("R",NULL,                       "recurse through subdirectories");
    struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              "define scalar value k (default is 3)");
    struct arg_str  *defines = arg_strn("D","define","MACRO",0,20,      "macro definitions");
    struct arg_file *outfile = arg_file0("o",NULL,"<output>",           "output file (default is \"-\")");
    struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            "verbose messages");
    struct arg_lit  *help    = arg_lit0(NULL,"help",                    "print this help and exit");
    struct arg_lit  *version = arg_lit0(NULL,"version",                 "print version information and exit");
    struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,20,           "input file(s)");
    struct arg_end  *end     = arg_end(20);
    void* argtable[]  = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};
    void* argtable1[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,NULL};
    void* argtable2[] = {list,recurse,repeat,defines,outfile,verbose,help,version,NULL,   NULL};
    void* argtable3[] = {list,recurse,repeat,defines,outfile,verbose,help,NULL,   infiles,end};
    void* argtable4[] = {NULL,recurse,NULL,  NULL,   outfile,NULL,   help,version,infiles,NULL};

    printf("testing arg_nullcheck()...\n");
    assert(arg_nullcheck(argtable)  == 0);
    assert(arg_nullcheck(argtable1) == 1);
    assert(arg_nullcheck(argtable2) == 1);
    assert(arg_nullcheck(argtable3) == 1);
    assert(arg_nullcheck(argtable4) == 1);
    printf("arg_nullcheck() OK\n");
    
    arg_free(argtable);
    }


void test_arg_print_glossary(void)
    {
    struct arg_lit  *list    = arg_lit0("lL",NULL,                      "list files");
    struct arg_lit  *recurse = arg_lit0("R",NULL,                       "recurse through subdirectories");
    struct arg_int  *repeat  = arg_int0("k","scalar",NULL,              "define scalar value k (default is 3)");
    struct arg_str  *defines = arg_strn("D","define","MACRO",0,20,      "macro definitions");
    struct arg_file *outfile = arg_file0("o",NULL,"<output>",           "output file (default is \"-\")");
    struct arg_lit  *verbose = arg_lit0("v","verbose,debug",            "verbose messages");
    struct arg_lit  *help    = arg_lit0(NULL,"help",                    "print this help and exit");
    struct arg_lit  *version = arg_lit0(NULL,"version",                 "print version information and exit");
    struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,20,           "input file(s)");
    struct arg_end  *end     = arg_end(20);
    void* argtable[]  = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end};

    printf("exercising arg_print_glossary()...\n");
    arg_print_glossary(stdout,argtable,NULL);    

    printf("exercising arg_print_glossary_gnu()...\n");
    arg_print_glossary_gnu(stdout,argtable);    

    arg_free(argtable);
    }


void test_printsyntax(void)
    {  
    struct arg_lit *help1 = arg_lit1("h",    NULL, "Syntax help");
    struct arg_end *end1  = arg_end(20);
    void *argtable1[] = { help1, end1 };

    struct arg_lit *help2 = arg_lit1(NULL, "help", "Syntax help");
    struct arg_end *end2  = arg_end(20);
    void *argtable2[] = { help2, end2 };

    printf("testing arg_printsyntax()...\n");
    assert(arg_nullcheck(argtable1)  == 0);
    assert(arg_nullcheck(argtable2)  == 0);

    printf("foo");
    arg_print_syntax(stdout,argtable1,"\n");

    printf("foo");
    arg_print_syntax(stdout,argtable2,"\n");
    
    arg_free(argtable1);
    arg_free(argtable2);
    }

int main(int argc, char **argv)
    {
    test_freetable();
    test_nullcheck();
    test_arg_print_glossary();
    test_printsyntax();

    /* close stdin and stdout to stop memcheck whining about their memory not being freed */
    fclose(stdin);
    fclose(stdout);
    
    return 0;
    }