Backstory
I'm porting the QuickCheck unit test framework to C (see the working code at GitHub). The syntax will be:
for_all(property, gen1, gen2, gen3 ...);
Where property
is a function to test, for example bool is_odd(int)
. gen1
, gen2
, etc. are functions that generate input values for property
. Some generate integers, some generate chars, some generate strings, and so on.
for_all
will accept a function with arbitrary inputs (any number of arguments, any types of arguments). for_all
will run the generators, creating test values to pass to the property function. For example, the property is_odd
is a function with type bool f(int)
. for_all
will use the generates to create 100 test cases. If the property returns false for any of them, for_all
will print the offending test case values. Otherwise, for_all
will print "SUCCESS"
.
Thus for_all
should use a va_list
to access the generators. Once we call the generator functions, how do we pass them to the property function?
Example
If is_odd
has the type bool f(int)
, how would we implement a function apply()
that has this syntax:
apply(is_odd, generated_values);
Secondary Issue
See SO.
How can we intelligently print the arbitrary values of a failing test case? A test case may be a single integer, or two characters, or a string, or some combination of the above? We won't know ahead of time whether to use:
printf("%d %d %d
", some_int, some_int, some_int);
printf("%c
" a_character);
printf("%s%s
", a_string, a_struct_requiring_its_own_printf_function);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…