Merge branch 'maint-0.4.8' into release-0.4.8
[tor.git] / src / ext / tinytest_demo.c
blobc07f099791e8c409df51d4e86276beac2299af73
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
5 * are met:
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. */
31 #include "tinytest.h"
32 #include "tinytest_macros.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <time.h>
40 #ifdef _WIN32
41 #include <windows.h>
42 #else
43 #include <unistd.h>
44 #endif
46 /* ============================================================ */
48 /* First, let's see if strcmp is working. (All your test cases should be
49 * functions declared to take a single void * as an argument.) */
50 void
51 test_strcmp(void *data)
53 (void)data; /* This testcase takes no data. */
55 /* Let's make sure the empty string is equal to itself */
56 if (strcmp("","")) {
57 /* This macro tells tinytest to stop the current test
58 * and go straight to the "end" label. */
59 tt_abort_msg("The empty string was not equal to itself");
62 /* Pretty often, calling tt_abort_msg to indicate failure is more
63 heavy-weight than you want. Instead, just say: */
64 tt_assert(strcmp("testcase", "testcase") == 0);
66 /* Occasionally, you don't want to stop the current testcase just
67 because a single assertion has failed. In that case, use
68 tt_want: */
69 tt_want(strcmp("tinytest", "testcase") > 0);
71 /* You can use the tt_*_op family of macros to compare values and to
72 fail unless they have the relationship you want. They produce
73 more useful output than tt_assert, since they display the actual
74 values of the failing things.
76 Fail unless strcmp("abc, "abc") == 0 */
77 tt_int_op(strcmp("abc", "abc"), OP_EQ, 0);
79 /* Fail unless strcmp("abc, "abcd") is less than 0 */
80 tt_int_op(strcmp("abc", "abcd"), OP_LT, 0);
82 /* Incidentally, there's a test_str_op that uses strcmp internally. */
83 tt_str_op("abc", OP_LT, "abcd");
86 /* Every test-case function needs to finish with an "end:"
87 label and (optionally) code to clean up local variables. */
88 end:
92 /* ============================================================ */
94 /* Now let's mess with setup and teardown functions! These are handy if
95 you have a bunch of tests that all need a similar environment, and you
96 want to reconstruct that environment freshly for each one. */
98 /* First you declare a type to hold the environment info, and functions to
99 set it up and tear it down. */
100 struct data_buffer {
101 /* We're just going to have couple of character buffer. Using
102 setup/teardown functions is probably overkill for this case.
104 You could also do file descriptors, complicated handles, temporary
105 files, etc. */
106 char buffer1[512];
107 char buffer2[512];
109 /* The setup function needs to take a const struct testcase_t and return
110 void* */
111 void *
112 setup_data_buffer(const struct testcase_t *testcase)
114 struct data_buffer *db = malloc(sizeof(struct data_buffer));
116 /* If you had a complicated set of setup rules, you might behave
117 differently here depending on testcase->flags or
118 testcase->setup_data or even or testcase->name. */
120 /* Returning a NULL here would mean that we couldn't set up for this
121 test, so we don't need to test db for null. */
122 return db;
124 /* The clean function deallocates storage carefully and returns true on
125 success. */
127 clean_data_buffer(const struct testcase_t *testcase, void *ptr)
129 struct data_buffer *db = ptr;
131 if (db) {
132 free(db);
133 return 1;
135 return 0;
137 /* Finally, declare a testcase_setup_t with these functions. */
138 struct testcase_setup_t data_buffer_setup = {
139 setup_data_buffer, clean_data_buffer
143 /* Now let's write our test. */
144 void
145 test_memcpy(void *ptr)
147 /* This time, we use the argument. */
148 struct data_buffer *db = ptr;
150 /* We'll also introduce a local variable that might need cleaning up. */
151 char *mem = NULL;
153 /* Let's make sure that memcpy does what we'd like. */
154 strcpy(db->buffer1, "String 0");
155 memcpy(db->buffer2, db->buffer1, sizeof(db->buffer1));
156 tt_str_op(db->buffer1, OP_EQ, db->buffer2);
158 /* tt_mem_op() does a memcmp, as opposed to the strcmp in tt_str_op() */
159 db->buffer2[100] = 3; /* Make the buffers unequal */
160 tt_mem_op(db->buffer1, OP_LT, db->buffer2, sizeof(db->buffer1));
162 /* Now we've allocated memory that's referenced by a local variable.
163 The end block of the function will clean it up. */
164 mem = strdup("Hello world.");
165 tt_assert(mem);
167 /* Another rather trivial test. */
168 tt_str_op(db->buffer1, OP_NE, mem);
170 end:
171 /* This time our end block has something to do. */
172 if (mem)
173 free(mem);
176 void
177 test_timeout(void *ptr)
179 time_t t1, t2;
180 (void)ptr;
181 t1 = time(NULL);
182 #ifdef _WIN32
183 Sleep(5000);
184 #else
185 sleep(5);
186 #endif
187 t2 = time(NULL);
189 tt_int_op(t2-t1, OP_GE, 4);
191 tt_int_op(t2-t1, OP_LE, 6);
193 end:
197 /* ============================================================ */
199 /* Now we need to make sure that our tests get invoked. First, you take
200 a bunch of related tests and put them into an array of struct testcase_t.
203 struct testcase_t demo_tests[] = {
204 /* Here's a really simple test: it has a name you can refer to it
205 with, and a function to invoke it. */
206 { "strcmp", test_strcmp, },
208 /* The second test has a flag, "TT_FORK", to make it run in a
209 subprocess, and a pointer to the testcase_setup_t that configures
210 its environment. */
211 { "memcpy", test_memcpy, TT_FORK, &data_buffer_setup },
213 /* This flag is off-by-default, since it takes a while to run. You
214 * can enable it manually by passing +demo/timeout at the command line.*/
215 { "timeout", test_timeout, TT_OFF_BY_DEFAULT },
217 /* The array has to end with END_OF_TESTCASES. */
218 END_OF_TESTCASES
221 /* Next, we make an array of testgroups. This is mandatory. Unlike more
222 heavy-duty testing frameworks, groups can't nest. */
223 struct testgroup_t groups[] = {
225 /* Every group has a 'prefix', and an array of tests. That's it. */
226 { "demo/", demo_tests },
228 END_OF_GROUPS
231 /* We can also define test aliases. These can be used for types of tests that
232 * cut across groups. */
233 const char *alltests[] = { "+..", NULL };
234 const char *slowtests[] = { "+demo/timeout", NULL };
235 struct testlist_alias_t aliases[] = {
237 { "ALL", alltests },
238 { "SLOW", slowtests },
240 END_OF_ALIASES
245 main(int c, const char **v)
247 /* Finally, just call tinytest_main(). It lets you specify verbose
248 or quiet output with --verbose and --quiet. You can list
249 specific tests:
251 tinytest-demo demo/memcpy
253 or use a ..-wildcard to select multiple tests with a common
254 prefix:
256 tinytest-demo demo/..
258 If you list no tests, you get them all by default, so that
259 "tinytest-demo" and "tinytest-demo .." mean the same thing.
262 tinytest_set_aliases(aliases);
263 return tinytest_main(c, v, groups);