tinytest_demo.c 8.0 KB

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