1 /* tinytest_demo.c -- Copyright 2009-2012 Nick Mathewson
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * 3. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Welcome to the example file for tinytest! I'll show you how to set up
28 * some simple and not-so-simple testcases. */
30 /* Make sure you include these headers. */
32 #include "tinytest_macros.h"
45 /* ============================================================ */
47 /* First, let's see if strcmp is working. (All your test cases should be
48 * functions declared to take a single void * as an argument.) */
50 test_strcmp(void *data)
52 (void)data; /* This testcase takes no data. */
54 /* Let's make sure the empty string is equal to itself */
56 /* This macro tells tinytest to stop the current test
57 * and go straight to the "end" label. */
58 tt_abort_msg("The empty string was not equal to itself");
61 /* Pretty often, calling tt_abort_msg to indicate failure is more
62 heavy-weight than you want. Instead, just say: */
63 tt_assert(strcmp("testcase", "testcase") == 0);
65 /* Occasionally, you don't want to stop the current testcase just
66 because a single assertion has failed. In that case, use
68 tt_want(strcmp("tinytest", "testcase") > 0);
70 /* You can use the tt_*_op family of macros to compare values and to
71 fail unless they have the relationship you want. They produce
72 more useful output than tt_assert, since they display the actual
73 values of the failing things.
75 Fail unless strcmp("abc, "abc") == 0 */
76 tt_int_op(strcmp("abc", "abc"), ==, 0);
78 /* Fail unless strcmp("abc, "abcd") is less than 0 */
79 tt_int_op(strcmp("abc", "abcd"), < , 0);
81 /* Incidentally, there's a test_str_op that uses strcmp internally. */
82 tt_str_op("abc", <, "abcd");
85 /* Every test-case function needs to finish with an "end:"
86 label and (optionally) code to clean up local variables. */
91 /* ============================================================ */
93 /* Now let's mess with setup and teardown functions! These are handy if
94 you have a bunch of tests that all need a similar environment, and you
95 want to reconstruct that environment freshly for each one. */
97 /* First you declare a type to hold the environment info, and functions to
98 set it up and tear it down. */
100 /* We're just going to have couple of character buffer. Using
101 setup/teardown functions is probably overkill for this case.
103 You could also do file descriptors, complicated handles, temporary
108 /* The setup function needs to take a const struct testcase_t and return
111 setup_data_buffer(const struct testcase_t *testcase)
113 struct data_buffer *db = malloc(sizeof(struct data_buffer));
115 /* If you had a complicated set of setup rules, you might behave
116 differently here depending on testcase->flags or
117 testcase->setup_data or even or testcase->name. */
119 /* Returning a NULL here would mean that we couldn't set up for this
120 test, so we don't need to test db for null. */
123 /* The clean function deallocates storage carefully and returns true on
126 clean_data_buffer(const struct testcase_t *testcase, void *ptr)
128 struct data_buffer *db = ptr;
136 /* Finally, declare a testcase_setup_t with these functions. */
137 struct testcase_setup_t data_buffer_setup = {
138 setup_data_buffer, clean_data_buffer
142 /* Now let's write our test. */
144 test_memcpy(void *ptr)
146 /* This time, we use the argument. */
147 struct data_buffer *db = ptr;
149 /* We'll also introduce a local variable that might need cleaning up. */
152 /* Let's make sure that memcpy does what we'd like. */
153 strcpy(db->buffer1, "String 0");
154 memcpy(db->buffer2, db->buffer1, sizeof(db->buffer1));
155 tt_str_op(db->buffer1, ==, db->buffer2);
157 /* This one works if there's an internal NUL. */
158 tt_mem_op(db->buffer1, <, db->buffer2, sizeof(db->buffer1));
160 /* Now we've allocated memory that's referenced by a local variable.
161 The end block of the function will clean it up. */
162 mem = strdup("Hello world.");
165 /* Another rather trivial test. */
166 tt_str_op(db->buffer1, !=, mem);
169 /* This time our end block has something to do. */
175 test_timeout(void *ptr)
187 tt_int_op(t2-t1, >=, 4);
189 tt_int_op(t2-t1, <=, 6);
196 test_timeout_retry(void *ptr)
213 tt_int_op(t2-t1, >=, 4);
215 tt_int_op(t2-t1, <=, 6);
221 /* ============================================================ */
223 /* Now we need to make sure that our tests get invoked. First, you take
224 a bunch of related tests and put them into an array of struct testcase_t.
227 struct testcase_t demo_tests[] = {
228 /* Here's a really simple test: it has a name you can refer to it
229 with, and a function to invoke it. */
230 { "strcmp", test_strcmp, },
232 /* The second test has a flag, "TT_FORK", to make it run in a
233 subprocess, and a pointer to the testcase_setup_t that configures
235 { "memcpy", test_memcpy, TT_FORK, &data_buffer_setup },
237 /* This flag is off-by-default, since it takes a while to run. You
238 * can enable it manually by passing +demo/timeout at the command line.*/
239 { "timeout", test_timeout, TT_OFF_BY_DEFAULT },
241 /* This test will be retried. (and it will not pass from the first
243 { "timeout_retry", test_timeout_retry, TT_RETRIABLE },
245 /* The array has to end with END_OF_TESTCASES. */
249 /* Next, we make an array of testgroups. This is mandatory. Unlike more
250 heavy-duty testing frameworks, groups can't nest. */
251 struct testgroup_t groups[] = {
253 /* Every group has a 'prefix', and an array of tests. That's it. */
254 { "demo/", demo_tests },
259 /* We can also define test aliases. These can be used for types of tests that
260 * cut across groups. */
261 const char *alltests[] = { "+..", NULL };
262 const char *slowtests[] = { "+demo/timeout", NULL };
263 struct testlist_alias_t aliases[] = {
266 { "SLOW", slowtests },
273 main(int c, const char **v)
275 /* Finally, just call tinytest_main(). It lets you specify verbose
276 or quiet output with --verbose and --quiet. You can list
279 tinytest-demo demo/memcpy
281 or use a ..-wildcard to select multiple tests with a common
284 tinytest-demo demo/..
286 If you list no tests, you get them all by default, so that
287 "tinytest-demo" and "tinytest-demo .." mean the same thing.
290 tinytest_set_aliases(aliases);
291 return tinytest_main(c, v, groups);