tinytest_demo.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /* ============================================================ */
  35. /* First, let's see if strcmp is working. (All your test cases should be
  36. * functions declared to take a single void * as an argument.) */
  37. void
  38. test_strcmp(void *data)
  39. {
  40. (void)data; /* This testcase takes no data. */
  41. /* Let's make sure the empty string is equal to itself */
  42. if (strcmp("","")) {
  43. /* This macro tells tinytest to stop the current test
  44. * and go straight to the "end" label. */
  45. tt_abort_msg("The empty string was not equal to itself");
  46. }
  47. /* Pretty often, calling tt_abort_msg to indicate failure is more
  48. heavy-weight than you want. Instead, just say: */
  49. tt_assert(strcmp("testcase", "testcase") == 0);
  50. /* Occasionally, you don't want to stop the current testcase just
  51. because a single assertion has failed. In that case, use
  52. tt_want: */
  53. tt_want(strcmp("tinytest", "testcase") > 0);
  54. /* You can use the tt_*_op family of macros to compare values and to
  55. fail unless they have the relationship you want. They produce
  56. more useful output than tt_assert, since they display the actual
  57. values of the failing things.
  58. Fail unless strcmp("abc, "abc") == 0 */
  59. tt_int_op(strcmp("abc", "abc"), ==, 0);
  60. /* Fail unless strcmp("abc, "abcd") is less than 0 */
  61. tt_int_op(strcmp("abc", "abcd"), < , 0);
  62. /* Incidentally, there's a test_str_op that uses strcmp internally. */
  63. tt_str_op("abc", <, "abcd");
  64. /* Every test-case function needs to finish with an "end:"
  65. label and (optionally) code to clean up local variables. */
  66. end:
  67. ;
  68. }
  69. /* ============================================================ */
  70. /* Now let's mess with setup and teardown functions! These are handy if
  71. you have a bunch of tests that all need a similar environment, and you
  72. want to reconstruct that environment freshly for each one. */
  73. /* First you declare a type to hold the environment info, and functions to
  74. set it up and tear it down. */
  75. struct data_buffer {
  76. /* We're just going to have couple of character buffer. Using
  77. setup/teardown functions is probably overkill for this case.
  78. You could also do file descriptors, complicated handles, temporary
  79. files, etc. */
  80. char buffer1[512];
  81. char buffer2[512];
  82. };
  83. /* The setup function needs to take a const struct testcase_t and return
  84. void* */
  85. void *
  86. setup_data_buffer(const struct testcase_t *testcase)
  87. {
  88. struct data_buffer *db = malloc(sizeof(struct data_buffer));
  89. /* If you had a complicated set of setup rules, you might behave
  90. differently here depending on testcase->flags or
  91. testcase->setup_data or even or testcase->name. */
  92. /* Returning a NULL here would mean that we couldn't set up for this
  93. test, so we don't need to test db for null. */
  94. return db;
  95. }
  96. /* The clean function deallocates storage carefully and returns true on
  97. success. */
  98. int
  99. clean_data_buffer(const struct testcase_t *testcase, void *ptr)
  100. {
  101. struct data_buffer *db = ptr;
  102. if (db) {
  103. free(db);
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. /* Finally, declare a testcase_setup_t with these functions. */
  109. struct testcase_setup_t data_buffer_setup = {
  110. setup_data_buffer, clean_data_buffer
  111. };
  112. /* Now let's write our test. */
  113. void
  114. test_memcpy(void *ptr)
  115. {
  116. /* This time, we use the argument. */
  117. struct data_buffer *db = ptr;
  118. /* We'll also introduce a local variable that might need cleaning up. */
  119. char *mem = NULL;
  120. /* Let's make sure that memcpy does what we'd like. */
  121. strcpy(db->buffer1, "String 0");
  122. memcpy(db->buffer2, db->buffer1, sizeof(db->buffer1));
  123. tt_str_op(db->buffer1, ==, db->buffer2);
  124. /* Now we've allocated memory that's referenced by a local variable.
  125. The end block of the function will clean it up. */
  126. mem = strdup("Hello world.");
  127. tt_assert(mem);
  128. /* Another rather trivial test. */
  129. tt_str_op(db->buffer1, !=, mem);
  130. end:
  131. /* This time our end block has something to do. */
  132. if (mem)
  133. free(mem);
  134. }
  135. /* ============================================================ */
  136. /* Now we need to make sure that our tests get invoked. First, you take
  137. a bunch of related tests and put them into an array of struct testcase_t.
  138. */
  139. struct testcase_t demo_tests[] = {
  140. /* Here's a really simple test: it has a name you can refer to it
  141. with, and a function to invoke it. */
  142. { "strcmp", test_strcmp, },
  143. /* The second test has a flag, "TT_FORK", to make it run in a
  144. subprocess, and a pointer to the testcase_setup_t that configures
  145. its environment. */
  146. { "memcpy", test_memcpy, TT_FORK, &data_buffer_setup },
  147. /* The array has to end with END_OF_TESTCASES. */
  148. END_OF_TESTCASES
  149. };
  150. /* Next, we make an array of testgroups. This is mandatory. Unlike more
  151. heavy-duty testing frameworks, groups can't nest. */
  152. struct testgroup_t groups[] = {
  153. /* Every group has a 'prefix', and an array of tests. That's it. */
  154. { "demo/", demo_tests },
  155. END_OF_GROUPS
  156. };
  157. int
  158. main(int c, const char **v)
  159. {
  160. /* Finally, just call tinytest_main(). It lets you specify verbose
  161. or quiet output with --verbose and --quiet. You can list
  162. specific tests:
  163. tinytest-demo demo/memcpy
  164. or use a ..-wildcard to select multiple tests with a common
  165. prefix:
  166. tinytest-demo demo/..
  167. If you list no tests, you get them all by default, so that
  168. "tinytest-demo" and "tinytest-demo .." mean the same thing.
  169. */
  170. return tinytest_main(c, v, groups);
  171. }