aboutsummaryrefslogtreecommitdiff
path: root/tests/test_fixtures.c
diff options
context:
space:
mode:
authorPawel Szewczyk <p.szewczyk@samsung.com>2015-09-14 12:52:12 +0200
committerAndreas Schneider <asn@cryptomilk.org>2015-09-23 16:42:11 +0200
commitda22fa6c2435c8421817292e5ca1269c7f4e4959 (patch)
tree68620f0e0c6b517212c8942dad4b8abca994d4b8 /tests/test_fixtures.c
parentcc3386b4ce502e532686a6ec0c56af362cb33e86 (diff)
downloadcmocka-da22fa6c2435c8421817292e5ca1269c7f4e4959.tar.gz
cmocka-da22fa6c2435c8421817292e5ca1269c7f4e4959.tar.xz
cmocka-da22fa6c2435c8421817292e5ca1269c7f4e4959.zip
cmocka: Allow to pass initial data to test cases
Sometimes multiple test cases share the same test function, running it on different data. To pass this data to test functions we must define setup function for each data set. It's not very convienient when there are many states to test. This commit introduce more elegant way to pass data to tests. The initial_state field of CMUnitTest structure can be defined by user. It will be either passed to setup function, which makes any preparation needed, or it will be passed directly to test function when setup func is NULL. Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Diffstat (limited to 'tests/test_fixtures.c')
-rw-r--r--tests/test_fixtures.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/test_fixtures.c b/tests/test_fixtures.c
index 4597626..6d39487 100644
--- a/tests/test_fixtures.c
+++ b/tests/test_fixtures.c
@@ -31,7 +31,42 @@ static void malloc_teardown_test(void **state)
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),
@@ -39,6 +74,8 @@ int main(void) {
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);