/** Initializes a CMUnitTest structure. */
-#define cmocka_unit_test(f) { #f, f, NULL, NULL }
+#define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
/** Initializes a CMUnitTest structure with a setup function. */
-#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL }
+#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
/** Initializes a CMUnitTest structure with a teardown function. */
-#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown }
+#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
/**
* Initialize an array of CMUnitTest structures with a setup function for a test
* and a teardown function. Either setup or teardown can be NULL.
*/
-#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown }
+#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
+
+/**
+ * Initialize a CMUnitTest structure with given initial state. It will be passed to test
+ * function as an argument later. It can be used when test state does not need special initialization
+ * or was initialized already.
+ * If group state was initialized for this unit test, it won't be overrided by initial state defined here.
+ */
+#define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
+
+/**
+ * Initialize a CMUnitTest structure with given initial state, setup and teardown function. Any of
+ * these values can be NULL. Initial state is passed later to setup function, or directly to test if
+ * none was given.
+ * If group state was initialized for this unit test, it won't be overrided by initial state defined here.
+ */
+#define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
#define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
#define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
CMUnitTestFunction test_func;
CMFixtureFunction setup_func;
CMFixtureFunction teardown_func;
+ void *initial_state;
};
/* Location within some source code. */
assert_non_null(*state);
}
+static int prestate_setup(void **state)
+{
+ int *val = (int *)*state, *a;
+
+ a = malloc(sizeof(int));
+ *a = *val + 1;
+ *state = a;
+
+ return 0;
+}
+
+static int prestate_teardown(void **state)
+{
+ free(*state);
+
+ return 0;
+}
+
+static void prestate_setup_test(void **state)
+{
+ int *a = (int *)*state;
+
+ assert_non_null(a);
+ assert_int_equal(*a, 43);
+}
+
+static void prestate_test(void **state)
+{
+ int *a = (int *)*state;
+
+ assert_non_null(a);
+ assert_int_equal(*a, 42);
+}
+
int main(void) {
+ int prestate = 42;
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(malloc_setup_test, setup_only),
cmocka_unit_test_setup(malloc_setup_test, setup_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
cmocka_unit_test_teardown(malloc_teardown_test, teardown_only),
+ cmocka_unit_test_prestate(prestate_test, &prestate),
+ cmocka_unit_test_prestate_setup_teardown(prestate_setup_test, prestate_setup, prestate_teardown, &prestate),
};
return cmocka_run_group_tests(tests, NULL, NULL);