You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

83 lines
1.7 KiB

  1. #include <stdarg.h>
  2. #include <stddef.h>
  3. #include <setjmp.h>
  4. #include <cmocka.h>
  5. #include <stdlib.h>
  6. static int setup_only(void **state)
  7. {
  8. *state = malloc(1);
  9. return 0;
  10. }
  11. static int teardown_only(void **state)
  12. {
  13. free(*state);
  14. return 0;
  15. }
  16. static void malloc_setup_test(void **state)
  17. {
  18. assert_non_null(*state);
  19. free(*state);
  20. }
  21. static void malloc_teardown_test(void **state)
  22. {
  23. *state = malloc(1);
  24. assert_non_null(*state);
  25. }
  26. static int prestate_setup(void **state)
  27. {
  28. int *val = (int *)*state, *a;
  29. a = malloc(sizeof(int));
  30. *a = *val + 1;
  31. *state = a;
  32. return 0;
  33. }
  34. static int prestate_teardown(void **state)
  35. {
  36. free(*state);
  37. return 0;
  38. }
  39. static void prestate_setup_test(void **state)
  40. {
  41. int *a = (int *)*state;
  42. assert_non_null(a);
  43. assert_int_equal(*a, 43);
  44. }
  45. static void prestate_test(void **state)
  46. {
  47. int *a = (int *)*state;
  48. assert_non_null(a);
  49. assert_int_equal(*a, 42);
  50. }
  51. int main(void) {
  52. int prestate = 42;
  53. const struct CMUnitTest tests[] = {
  54. cmocka_unit_test_setup(malloc_setup_test, setup_only),
  55. cmocka_unit_test_setup(malloc_setup_test, setup_only),
  56. cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
  57. cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
  58. cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
  59. cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
  60. cmocka_unit_test_prestate(prestate_test, &prestate),
  61. cmocka_unit_test_prestate_setup_teardown(prestate_setup_test, prestate_setup, prestate_teardown, &prestate),
  62. };
  63. return cmocka_run_group_tests(tests, NULL, NULL);
  64. }