/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following functions.
- print_title
- usage
- get_options
1 /***************************************
2 $Revision: 1.1.1.1 $
3
4 Example code: Unit test driver template.
5
6 ******************/ /******************
7 Modification History:
8 ottrey (07/04/1999) Created.
9 ******************/ /******************
10 Copyright (c) 1999 RIPE NCC
11
12 All Rights Reserved
13
14 Permission to use, copy, modify, and distribute this software and its
15 documentation for any purpose and without fee is hereby granted,
16 provided that the above copyright notice appear in all copies and that
17 both that copyright notice and this permission notice appear in
18 supporting documentation, and that the name of the author not be
19 used in advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
23 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
24 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
25 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
26 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 ***************************************/
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <strings.h>
32
33 /*+ String sizes +*/
34 #define STR_S 63
35 #define STR_M 255
36 #define STR_L 1023
37 #define STR_XL 4095
38 #define STR_XXL 16383
39
40 #define MAX_TESTS 100
41
42 /*
43 * Global Variables
44 */
45 static char Unit_name[STR_S]; /*+ Name of test. +*/
46 int Verbose; /*+ Use verbose output. +*/
47 int Short; /*+ Use short output. +*/
48 char *Infile_name; /*+ Input file to read from. +*/
49 int Test[MAX_TESTS]; /*+ Test to execute +*/
50
51
52 /* print_title() */
53 /*++++++++++++++++++++++++++++++++++++++
54 Prints a pretty title
55
56 char *text The text to be displayed in the title.
57
58 More:
59 +html+ <PRE>
60 +latex+ \begin{verbatim}
61 Authors:
62 ottrey
63
64 +latex+ \end{verbatim}
65 +html+ </PRE>
66
67 ++++++++++++++++++++++++++++++++++++++*/
68 static void print_title(char *text) {
/* [<][>][^][v][top][bottom][index][help] */
69 char test_name[STR_M];
70 char border[STR_M];
71 unsigned int len, i;
72
73 sprintf(test_name, "%s: Unit test %s", Unit_name, text);
74
75 if (Infile_name != NULL) {
76 strcat(test_name, " (");
77 strcat(test_name, Infile_name);
78 strcat(test_name, ")");
79 }
80
81 len = strlen(test_name);
82 border[0] = '*';
83 for (i=1; i < len+3; i++) {
84 border[i] = '-';
85 }
86 border[len+3] = '*';
87 border[len+4] = '\0';
88 printf("%s\n| %s |\n%s\n", border, test_name, border);
89
90 } /* print_title() */
91
92 /* usage() */
93 /*++++++++++++++++++++++++++++++++++++++
94 Displays the usage pattern
95
96 More:
97 +html+ <PRE>
98 +latex+ \begin{verbatim}
99 Authors:
100 ottrey
101
102 Post-Conditions:
103 The user is returned the usage pattern.
104
105 +latex+ \end{verbatim}
106 +html+ </PRE>
107
108 ++++++++++++++++++++++++++++++++++++++*/
109 static void usage(void)
/* [<][>][^][v][top][bottom][index][help] */
110 {
111 fprintf(stderr, "Usage: %s\n", Unit_name);
112 fprintf(stderr, " -v \t\tVerbose\n");
113 fprintf(stderr, " -s \t\tShort\n");
114 fprintf(stderr, " --tests=1,3,4 \tONLY do tests 1, 3 and 4\n");
115 fprintf(stderr, " --file=infile \tGet input from `infile'\n");
116
117 exit(1);
118 } /* usage() */
119
120 /* get_options() */
121 /*++++++++++++++++++++++++++++++++++++++
122 Gets the options from the command line.
123
124 More:
125 +html+ <PRE>
126 +latex+ \begin{verbatim}
127 Authors:
128 ottrey
129 +latex+ \end{verbatim}
130 +html+ </PRE>
131
132 ++++++++++++++++++++++++++++++++++++++*/
133 static void get_options(int argc, char **argv) {
/* [<][>][^][v][top][bottom][index][help] */
134 int i;
135 int test_no;
136 char *test_no_str;
137
138 Verbose = 0;
139 Short = 0;
140 for (i=0; i < MAX_TESTS; i++) {
141 Test[i] = 1;
142 }
143
144 strcpy(Unit_name, argv[0]);
145
146 for (i=1; i < argc; i++) {
147 if (strncmp(argv[i], "-v", 2) == 0) {
148 Short = 0;
149 Verbose = 1;
150 }
151 else if (strncmp(argv[i], "-s", 2) == 0) {
152 Short = 1;
153 Verbose = 0;
154 }
155 else if (strncmp(argv[i], "--file=", 7) == 0) {
156 strtok(argv[i], "=");
157 Infile_name = (char *)strtok(NULL, "=");
158 }
159 else if (strncmp(argv[i], "--tests=", 8) == 0) {
160 /* Clear the tests */
161 for (test_no=0; test_no < MAX_TESTS; test_no++) {
162 Test[test_no] = 0;
163 }
164 /* Now set these tests */
165 strtok(argv[i], "=");
166 while ( (test_no_str = (char *)strtok(NULL, ", ")) != NULL) {
167 test_no = atoi(test_no_str);
168 Test[test_no] = 1;
169 }
170 }
171 else {
172 fprintf(stderr, "%s: Invalid option `%s'\n", Unit_name, argv[i]);
173 usage();
174 }
175 }
176 } /* get_options() */
177