@@@ man wip
[mLib] / test / testrig.3
CommitLineData
05fbeb03 1.\" -*-nroff-*-
2.de VS
3.sp 1
4.in +5
5.nf
6.ft B
7..
8.de VE
9.ft R
10.fi
11.in -5
12.sp 1
13..
fbf20b5b 14.TH testrig 3 "5 June 1999" "Straylight/Edgeware" "mLib utilities library"
05fbeb03 15.SH NAME
16testrig \- generic test rig
17.\" @test_run
18.SH SYNOPSIS
19.nf
20.B "#include <mLib/testrig.h>"
d056fbdf 21.PP
4729aa69 22.B "#define TEST_FIELDMAX ..."
d056fbdf 23.PP
adec5584 24.ta 2n
4729aa69 25.B "typedef struct {"
adec5584 26.B " unsigned tests, failed;"
4729aa69 27.B "} test_results";
d056fbdf 28.PP
4729aa69 29.B "typedef struct {"
adec5584
MW
30.BI " void (*cvt)(const char *" buf ", dstr *" d );
31.BI " void (*dump)(dstr *" d ", FILE *" fp );
4729aa69 32.B "} test_type";
d056fbdf 33.PP
4729aa69 34.B "typedef struct {"
adec5584
MW
35.B " const char *name;"
36.BI " void (*test)(dstr " dv "[]);"
37.B " const test_type *f[TEST_FIELDMAX];"
4729aa69 38.B "} test_chunk";
d056fbdf 39.PP
4729aa69 40.B "typedef struct {"
adec5584
MW
41.B " const char *name;"
42.B " const test_chunk *chunks;"
4729aa69 43.B "} test_suite";
d056fbdf 44.PP
4729aa69
MW
45.B "const test_type type_hex;"
46.B "const test_type type_string;"
47.B "const test_type type_int;"
48.B "const test_type type_long;"
49.B "const test_type type_ulong;"
50.B "const test_type type_uint32;"
d056fbdf 51.PP
adec5584
MW
52.ta \w'\fBint test_do('u
53.BI "int test_do(const test_suite " suite [],
54.BI " FILE *" fp ", test_results *" results );
55.ta \w'\fBvoid test_run('u
56.BI "void test_run(int " argc ", char *" argv [],
57.BI " const test_chunk " chunk "[], const char *" def );
05fbeb03 58.fi
59.SH DESCRIPTION
9fcce036
MW
60.SS Structure
61Test vectors are gathered together into
62.I chunks
63which should be processed in the same way. Chunks, in turn, are grouped
64into
65.IR suites .
66.SS Functions
67The
68.B test_do
69function runs a collection of tests, as defined by
70.IR suite ,
71given the test vectors in the file
72.IR fp .
73It returns results in the
74.B test_results
75structure
76.IR results ,
77which has two members:
78.TP
79.B "unsigned tests"
80counts the number of tests carried out, and
81.TP
82.B "unsigned failed"
83counts the number of tests which failed.
84.PP
85The function returns negative if there was a system error or the test
86vector file was corrupt in some way, zero if all the tests were
87successful, or positive if some tests failed.
88.PP
05fbeb03 89The
90.B test_run
9fcce036
MW
91provides a simple command-line interface to the test system. It is
92intended to be called from the
05fbeb03 93.B main
94function of a test rig program to check that a particular function or
9fcce036 95suite of functions are running properly. It does not return. The arguments
05fbeb03 96.I argc
97and
98.I argv
99should just be the arguments given to
100.BR main .
101The
102.I def
103argument gives the name of the default file of test vectors to read.
104This can be overridden at run-time by passing the program a
105.B \-f
106command-line option. The
107.I chunk
108argument is (the address of) an array of
109.I "chunk definitions"
110describing the layout of the test vector file.
111.SS "Test vector file syntax"
112Test vector files are mostly free-form. Comments begin with a hash
113.RB (` # ')
114and extend to the end of the line. Apart from that, newline characters
115are just considered to be whitespace.
116.PP
117Test vector files have the following syntax:
118.PP
119.I file
120::=
9fcce036
MW
121.RI [ suite-header | chunk " ...]"
122.br
123.I suite-header
124::=
125.B suite
126.I name
05fbeb03 127.br
128.I chunk
129::=
130.I name
131.B {
9fcce036 132.RI [ test-vector " ...]"
05fbeb03 133.B }
134.br
135.I test-vector
136::=
137.RI [ value ...]
138.B ;
139.PP
140Briefly in English: a test vector file is divided into chunks, each of
141which consists of a textual name and a brace-enclosed list of test
142vectors. Each test vector consists of a number of values terminated by
143a semicolon.
144.PP
145A value is either a sequence of
146.I "word characters"
147(alphanumerics and some other characters)
148or a string enclosed in quote marks (double or single). Quoted strings
149may contain newlines. In either type of value, a backslash escapes the
150following character.
9fcce036
MW
151.SS "Suite definitions"
152A
153.I suite definition
4729aa69
MW
154is described by the
155.B test_suite
156structure. The
9fcce036
MW
157.I suite
158argument to
159.B test_do
160is a pointer to an array of these structures, terminated by one with a
161null
162.BR name .
05fbeb03 163.SS "Chunk definitions"
9fcce036
MW
164A
165.I "chunk definition"
166describes the format of a named chunk: the number and type of the values
167required and the function to call in order to test the system against
168that test vector. The array is terminated by a chunk definition whose
169name field is a null pointer.
05fbeb03 170.PP
4729aa69
MW
171A chunk definition is described by the
172.B test_chunk
173structure. The members of this structure are as follows:
05fbeb03 174.TP
ff76c38f 175.B "const char *name"
05fbeb03 176The name of the chunk described by this chunk definition, or null if
177this is the termination marker.
178.TP
4729aa69 179.BI "int (*test)(dstr " dv "[])"
05fbeb03 180The test function. It is passed an array of dynamic strings, one for
181each field, and must return nonzero if the test succeeded or zero if the
182test failed. On success, the function should not write anything to
183stdout or stderr; on failure, a report of the test arguments should be
184emitted to stderr.
185.TP
ff76c38f 186.B "test_type *f[TEST_FIELDMAX]"
05fbeb03 187Definitions of the fields. This is an array of pointers to
188.I "field types"
189(see below), terminated by a null pointer.
190.PP
191When the test driver encounters a chunk it has a definition for, it
192reads test vectors one by one, translating each value according to the
193designated field type, and then passing the completed array of fields to
194the test function.
195.SS "Field types"
196A field type describes how a field is to be read and written. A field
4729aa69
MW
197type is described by a
198.B test_type
199structure. The
ff76c38f 200.B cvt
05fbeb03 201member is a function called to read an input string stored in
ff76c38f 202.B buf
05fbeb03 203and output internal-format data in the dynamic string
204.IR d .
205The testrig driver has already stripped of quotes and dealt with
206backslash escapes.
207The
ff76c38f 208.B dump
05fbeb03 209member is called to write the internal-format data in dynamic string
210.I d
211to the
212.B stdio
213stream
214.IR fp .
215.PP
216There are three predefined field types:
217.TP
218.B "type_string"
219The simplest type. The string contents is not interpreted at all.
220.TP
221.B "type_hex"
222The string is interpreted as binary data encoded as hexadecimal.
223.TP
224.B "type_int"
225The string is interpreted as a textual representation of an integer.
226The integer is written to the dynamic string, and can be read out again
227with the expression
228.VS
229*(int *)d.buf
230.VE
231which isn't pretty but does the job.
dadc1afb 232.TP
233.B "type_long"
234As for
235.B type_int
236but reads and stores a
237.B long
238instead.
239.TP
240.B "type_ulong"
241As for
242.B type_long
243but reads and stores an
244.B "unsigned long"
245instead.
246.TP
247.B "type_uint32"
248As for
249.B type_int
250but reads and stores a
251.B uint32
252(see
253.BR bits (3))
254instead.
05fbeb03 255.SH "SEE ALSO"
256.BR mLib (3).
257.SH "AUTHOR"
9b5ac6ff 258Mark Wooding, <mdw@distorted.org.uk>