From e036a3406f7e7025fd2f6e28e506cf78052796af Mon Sep 17 00:00:00 2001 From: Pawel Szewczyk Date: Tue, 26 May 2015 10:46:49 +0200 Subject: [PATCH] libusbgx: tests: Move initialization from for_each functions to tests Instead of passing void **state as argument for for_each_* functions and initializing state there, we make initialization first and pass initialized state as argument for them. It looks more natural this way. This also allows to run these functions multiple times in single test function. Change-Id: If84bbedf374f1f0ebf70315cea950dcd2e527d18 Signed-off-by: Pawel Szewczyk [Update description] Signed-off-by: Krzysztof Opasiak --- tests/test.c | 120 +++++++++++++++++++++++++++++++++++++++++++++--------- tests/usbg-test.c | 24 ++--------- tests/usbg-test.h | 20 +++++---- 3 files changed, 116 insertions(+), 48 deletions(-) diff --git a/tests/test.c b/tests/test.c index 9846853..ad7a5ef 100644 --- a/tests/test.c +++ b/tests/test.c @@ -422,7 +422,11 @@ static int setup_random_len_gadget_strs_data(void **state) */ static void test_get_gadget(void **state) { - for_each_test_gadget(state, assert_gadget_equal); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_gadget(ts, s, assert_gadget_equal); } /** @@ -484,7 +488,11 @@ static void try_get_gadget_name(usbg_gadget *g, struct test_gadget *tg) */ static void test_get_gadget_name(void **state) { - for_each_test_gadget(state, try_get_gadget_name); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_gadget(ts, s, try_get_gadget_name); } static void try_get_gadget_name_len(usbg_gadget *g, struct test_gadget *tg) @@ -503,7 +511,11 @@ static void try_get_gadget_name_len(usbg_gadget *g, struct test_gadget *tg) */ static void test_get_gadget_name_len(void **state) { - for_each_test_gadget(state, try_get_gadget_name_len); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_gadget(ts, s, try_get_gadget_name_len); } /** @@ -535,7 +547,11 @@ static void try_cpy_gadget_name(usbg_gadget *g, struct test_gadget *tg) */ static void test_cpy_gadget_name(void **state) { - for_each_test_gadget(state, try_cpy_gadget_name); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_gadget(ts, s, try_cpy_gadget_name); } /** @@ -591,7 +607,11 @@ static void test_init(void **state) */ static void test_get_function(void **state) { - for_each_test_function(state, assert_func_equal); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_function(ts, s, assert_func_equal); } /** @@ -687,7 +707,11 @@ static void try_get_function_instance(usbg_function *f, struct test_function *tf */ static void test_get_function_instance(void **state) { - for_each_test_function(state, try_get_function_instance); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_function(ts, s, try_get_function_instance); } /** @@ -718,7 +742,11 @@ static void try_cpy_function_instance(usbg_function *f, struct test_function *tf */ static void test_cpy_function_instance(void **state) { - for_each_test_function(state, try_cpy_function_instance); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_function(ts, s, try_cpy_function_instance); } /** @@ -742,7 +770,11 @@ static void try_get_function_type(usbg_function *f, struct test_function *tf) */ static void test_get_function_type(void **state) { - for_each_test_function(state, try_get_function_type); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_function(ts, s, try_get_function_type); } /** @@ -765,7 +797,11 @@ static void try_get_function_instance_len(usbg_function *f, struct test_function */ static void test_get_function_instance_len(void **state) { - for_each_test_function(state, try_get_function_instance_len); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_function(ts, s, try_get_function_instance_len); } /** @@ -829,7 +865,11 @@ static void test_cpy_configfs_path(void **state) */ static void test_get_config(void **state) { - for_each_test_config(state, assert_config_equal); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, assert_config_equal); } static void test_get_config_without_label(void **state) @@ -903,7 +943,11 @@ static void try_get_config_label(usbg_config *c, struct test_config *tc) */ static void test_get_config_label(void **state) { - for_each_test_config(state, try_get_config_label); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_get_config_label); } /** @@ -925,7 +969,11 @@ static void try_get_config_id(usbg_config *c, struct test_config *tc) */ static void test_get_config_id(void **state) { - for_each_test_config(state, try_get_config_id); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_get_config_id); } /** @@ -1225,7 +1273,11 @@ static void try_get_binding_target(struct test_binding *tb, usbg_binding *b) */ static void test_get_binding_target(void **state) { - for_each_binding(state, try_get_binding_target); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_binding(ts, s, try_get_binding_target); } /** @@ -1250,7 +1302,11 @@ static void try_get_binding_name(struct test_binding *tb, usbg_binding *b) */ static void test_get_binding_name(void **state) { - for_each_binding(state, try_get_binding_name); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_binding(ts, s, try_get_binding_name); } /** @@ -1273,7 +1329,11 @@ static void try_get_binding_name_len(struct test_binding *tb, usbg_binding *b) */ static void test_get_binding_name_len(void **state) { - for_each_binding(state, try_get_binding_name_len); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_binding(ts, s, try_get_binding_name_len); } /** @@ -1295,7 +1355,11 @@ static void try_set_config_strs(usbg_config *c, struct test_config *tc) */ static void test_set_config_strs(void **state) { - for_each_test_config(state, try_set_config_strs); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_set_config_strs); } /** @@ -1317,7 +1381,11 @@ static void try_set_config_string(usbg_config *c, struct test_config *tc) */ static void test_set_config_string(void **state) { - for_each_test_config(state, try_set_config_string); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_set_config_string); } /** @@ -1342,7 +1410,11 @@ static void try_get_config_strs(usbg_config *c, struct test_config *tc) */ static void test_get_config_strs(void **state) { - for_each_test_config(state, try_get_config_strs); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_get_config_strs); } /** @@ -1369,7 +1441,11 @@ static void try_get_config_attrs(usbg_config *c, struct test_config *tc) */ static void test_get_config_attrs(void **state) { - for_each_test_config(state, try_get_config_attrs); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_get_config_attrs); } /** @@ -1391,7 +1467,11 @@ static void try_set_config_attrs(usbg_config *c, struct test_config *tc) */ static void test_set_config_attrs(void **state) { - for_each_test_config(state, try_set_config_attrs); + usbg_state *s = NULL; + struct test_state *ts; + + safe_init_with_state(state, &ts, &s); + for_each_test_config(ts, s, try_set_config_attrs); } /** diff --git a/tests/usbg-test.c b/tests/usbg-test.c index 3aeb2d1..8e70025 100644 --- a/tests/usbg-test.c +++ b/tests/usbg-test.c @@ -1100,17 +1100,13 @@ void assert_gadget_strs_equal(usbg_gadget_strs *actual, usbg_gadget_strs *expect assert_string_equal(get_gadget_str(actual, i), get_gadget_str(expected, i)); } -void for_each_test_function(void **state, FunctionTest fun) +void for_each_test_function(struct test_state *ts, usbg_state *s, FunctionTest fun) { - usbg_state *s = NULL; - struct test_state *ts; struct test_gadget *tg; struct test_function *tf; usbg_gadget *g = NULL; usbg_function *f = NULL; - safe_init_with_state(state, &ts, &s); - for (tg = ts->gadgets; tg->name; ++tg) { g = usbg_get_gadget(s, tg->name); assert_non_null(g); @@ -1121,17 +1117,13 @@ void for_each_test_function(void **state, FunctionTest fun) } } -void for_each_test_config(void **state, ConfigTest fun) +void for_each_test_config(struct test_state *ts, usbg_state *s, ConfigTest fun) { - usbg_state *s = NULL; usbg_gadget *g = NULL; usbg_config *c = NULL; - struct test_state *ts; struct test_gadget *tg; struct test_config *tc; - safe_init_with_state(state, &ts, &s); - for (tg = ts->gadgets; tg->name; tg++) { g = usbg_get_gadget(s, tg->name); assert_non_null(g); @@ -1142,19 +1134,15 @@ void for_each_test_config(void **state, ConfigTest fun) } } -void for_each_binding(void **state, BindingTestFunc fun) +void for_each_binding(struct test_state *ts, usbg_state *s, BindingTestFunc fun) { - struct test_state *ts; struct test_gadget *tg; struct test_config *tc; struct test_binding *tb; - usbg_state *s = NULL; usbg_gadget *g = NULL; usbg_config *c = NULL; usbg_binding *b = NULL; - safe_init_with_state(state, &ts, &s); - for (tg = ts->gadgets; tg->name; tg++) { g = usbg_get_gadget(s, tg->name); assert_non_null(g); @@ -1172,15 +1160,11 @@ void for_each_binding(void **state, BindingTestFunc fun) } } -void for_each_test_gadget(void **state, GadgetTestFunc fun) +void for_each_test_gadget(struct test_state *ts, usbg_state *s, GadgetTestFunc fun) { - struct test_state *ts; struct test_gadget *tg; - usbg_state *s = NULL; usbg_gadget *g = NULL; - safe_init_with_state(state, &ts, &s); - for (tg = ts->gadgets; tg->name; ++tg) { g = usbg_get_gadget(s, tg->name); assert_non_null(g); diff --git a/tests/usbg-test.h b/tests/usbg-test.h index 9bb6bfd..5fad226 100644 --- a/tests/usbg-test.h +++ b/tests/usbg-test.h @@ -414,10 +414,11 @@ typedef void (*FunctionTest)(usbg_function *f, struct test_function *tf); /** * @brief Call given function for all usb functions present in given state - * @param[in] state Properly prepared state to be tested + * @param[in] ts Test state to be tested + * @param[in] s Properly prepared usbg state to be tested * @param[in] fun Function to be called on each usb function in state */ -void for_each_test_function(void **state, FunctionTest fun); +void for_each_test_function(struct test_state *ts, usbg_state *s, FunctionTest fun); /** * @brief Function that performs some test on given usbg config @@ -426,10 +427,11 @@ typedef void (*ConfigTest)(usbg_config *c, struct test_config *tc); /** * @brief Call given function for all usb configs present in given state - * @param[in] state Properly prepared state to be tested + * @param[in] ts Test state to be tested + * @param[in] s Properly prepared usbg state to be tested * @param[in] fun Function to be called on each usb function in state */ -void for_each_test_config(void **state, ConfigTest fun); +void for_each_test_config(struct test_state *ts, usbg_state *s, ConfigTest fun); /** * @brief Function that performs test on given usbg binding @@ -438,10 +440,11 @@ typedef void (*BindingTestFunc)(struct test_binding *tb, usbg_binding *b); /** * @brief Call given function for all usb bindings present in given state - * @param[in] state Properly prepared state to be tested + * @param[in] ts Test state to be tested + * @param[in] s Properly prepared usbg state to be tested * @param[in] fun Function to be called on each usb binding in state */ -void for_each_binding(void **state, BindingTestFunc fun); +void for_each_binding(struct test_state *ts, usbg_state *s, BindingTestFunc fun); /** * @brief Function that performs test on given usbg gadget @@ -450,10 +453,11 @@ typedef void (*GadgetTestFunc)(usbg_gadget *g, struct test_gadget *tg); /** * @brief Call given function for all usb gadgets present in given state - * @param[in] state Properly prepared state to be tested + * @param[in] ts Test state to be tested + * @param[in] s Properly prepared usbg state to be tested * @param[in] fun Function to be called on each usb gadget in state */ -void for_each_test_gadget(void **state, GadgetTestFunc fun); +void for_each_test_gadget(struct test_state *ts, usbg_state *s, GadgetTestFunc fun); static inline void *safe_calloc(int count, size_t size) { -- 2.7.4