diff options
author | Pawel Szewczyk <p.szewczyk@samsung.com> | 2015-05-05 15:23:25 +0200 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2015-07-30 15:56:12 +0200 |
commit | 085a2a19f50185f6d10aaceb8bed323336742981 (patch) | |
tree | eac0b36357206fa49477d47e82f8dabbb532521f | |
parent | bddf612196f8a3c9661d3dfe17938b1e840d0423 (diff) | |
download | libusbg-085a2a19f50185f6d10aaceb8bed323336742981.tar.gz libusbg-085a2a19f50185f6d10aaceb8bed323336742981.tar.bz2 libusbg-085a2a19f50185f6d10aaceb8bed323336742981.zip |
libusbg: tests: Add macros for structures definitions
To avoid defining the same or similar values multiple times, we can define
data by proper macros.
Change-Id: I8fa5fd02c76f16a1b9d65a33482a6cd1caa16b70
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Reviewed-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | tests/test.c | 166 |
1 files changed, 57 insertions, 109 deletions
diff --git a/tests/test.c b/tests/test.c index 6f56126..1fba40e 100644 --- a/tests/test.c +++ b/tests/test.c @@ -26,6 +26,23 @@ #define FILLED_STR(len, c) \ { [0 ... len - 2] = c, [len - 1] = '\0' } +/* two levels of macros allow to strigify result of macro expansion */ +#define STR(s) #s +#define XSTR(s) STR(s) +/* unique string */ +#define UNIQUE XSTR(__COUNTER__) + +#define FUNC_FROM_TYPE(t) { \ + .type = t, \ + .instance = "instance"UNIQUE \ +} + +#define CONF_FROM_BOUND(b) { \ + .label = "c", \ + .id = __COUNTER__, \ + .bound_funcs = b \ +} + static usbg_gadget_attrs min_gadget_attrs = { .bcdUSB = 0x0000, .bDeviceClass = 0x0, @@ -90,14 +107,8 @@ static char *long_udcs[] = { * @details Used to go through init when testing other things */ static struct test_function simple_funcs[] = { - { - .type = F_ECM, - .instance = "0" - }, { - .type = F_ACM, - .instance = "0" - }, - + FUNC_FROM_TYPE(F_ECM), + FUNC_FROM_TYPE(F_ACM), TEST_FUNCTION_LIST_END }; @@ -107,53 +118,23 @@ static struct test_function simple_funcs[] = { * processed correctly */ static struct test_function all_funcs[] = { - { - .type = F_SERIAL, - .instance = "serial_instance0" - }, { - .type = F_ACM, - .instance = "acm_instance0" - }, { - .type = F_OBEX, - .instance = "obex_instance0" - }, { - .type = F_ECM, - .instance = "ecm_instance0" - }, { - .type = F_SUBSET, - .instance = "subset_instance0" - }, { - .type = F_NCM, - .instance = "ncm_instance0" - }, { - .type = F_EEM, - .instance = "eem_instance0" - }, { - .type = F_RNDIS, - .instance = "rndis_instance0" - }, { - .type = F_PHONET, - .instance = "phonet_instance0" - }, { - .type = F_FFS, - .instance = "ffs_instance0" - }, - + FUNC_FROM_TYPE(F_SERIAL), + FUNC_FROM_TYPE(F_ACM), + FUNC_FROM_TYPE(F_OBEX), + FUNC_FROM_TYPE(F_ECM), + FUNC_FROM_TYPE(F_SUBSET), + FUNC_FROM_TYPE(F_NCM), + FUNC_FROM_TYPE(F_EEM), + FUNC_FROM_TYPE(F_RNDIS), + FUNC_FROM_TYPE(F_PHONET), + FUNC_FROM_TYPE(F_FFS), TEST_FUNCTION_LIST_END }; static struct test_function same_type_funcs[] = { - { - .type = F_SERIAL, - .instance = "0" - }, { - .type = F_SERIAL, - .instance = "1" - }, { - .type = F_SERIAL, - .instance = "2" - }, - + FUNC_FROM_TYPE(F_SERIAL), + FUNC_FROM_TYPE(F_SERIAL), + FUNC_FROM_TYPE(F_SERIAL), TEST_FUNCTION_LIST_END }; @@ -171,12 +152,7 @@ static struct test_function no_funcs[] = { * @details Used to pass through init when testing other things */ static struct test_config simple_confs[] = { - { - .label = "c", - .id = 1, - .bound_funcs = simple_funcs - }, - + CONF_FROM_BOUND(simple_funcs), TEST_CONFIG_LIST_END }; @@ -184,30 +160,24 @@ static struct test_config simple_confs[] = { * @brief Configs bound to all avaible function types */ static struct test_config all_bindings_confs[] = { - { - .label = "c", - .id = 2, - .bound_funcs = no_funcs - }, { - .label = "c", - .id = 4, - .bound_funcs = all_funcs - }, - + CONF_FROM_BOUND(no_funcs), + CONF_FROM_BOUND(all_funcs), TEST_CONFIG_LIST_END }; +#define GADGET(n, u, c, f) \ + { \ + .name = n, \ + .udc = u, \ + .configs = c, \ + .functions = f \ + } + /** * @brief Simplest gadget */ static struct test_gadget simple_gadgets[] = { - { - .name = "g1", - .udc = "UDC1", - .configs = simple_confs, - .functions = simple_funcs - }, - + GADGET("g1", "UDC1", simple_confs, simple_funcs), TEST_GADGET_LIST_END }; @@ -215,24 +185,12 @@ static struct test_gadget simple_gadgets[] = { * @brief Gadgets with all avaible functions */ static struct test_gadget all_funcs_gadgets[] = { - { - .name = "all_funcs_gadget1", - .udc = "UDC1", - .configs = all_bindings_confs, - .functions = all_funcs - }, - + GADGET("all_funcs_gadget1", "UDC1", all_bindings_confs, all_funcs), TEST_GADGET_LIST_END }; static struct test_gadget long_udc_gadgets[] = { - { - .name = "long_udc_gadgets", - .udc = long_usbg_string, - .configs = simple_confs, - .functions = simple_funcs - }, - + GADGET("long_udc_gadgets", long_usbg_string, simple_confs, simple_funcs), TEST_GADGET_LIST_END }; @@ -246,35 +204,25 @@ struct test_gadget_strs_data { usbg_gadget_strs *strs; }; +#define STATE(p, g, u) { \ + .configfs_path = p, \ + .gadgets = g, \ + .udcs = u \ +} + /** * @brief Simple state */ -static struct test_state simple_state = { - .configfs_path = "config", - .gadgets = simple_gadgets, - .udcs = simple_udcs -}; +static struct test_state simple_state = STATE("config", simple_gadgets, simple_udcs); /** * @brief State with all functions avaible */ -static struct test_state all_funcs_state = { - .configfs_path = "all_funcs_configfs", - .gadgets = all_funcs_gadgets, - .udcs = simple_udcs -}; +static struct test_state all_funcs_state = STATE("all_funcs_configfs", all_funcs_gadgets, simple_udcs); -static struct test_state long_path_state = { - .configfs_path = long_path_str, - .gadgets = simple_gadgets, - .udcs = simple_udcs -}; +static struct test_state long_path_state = STATE(long_path_str, simple_gadgets, simple_udcs); -static struct test_state long_udc_state = { - .configfs_path = "simple_path", - .gadgets = long_udc_gadgets, - .udcs = long_udcs -}; +static struct test_state long_udc_state = STATE("simple_path", long_udc_gadgets, long_udcs); static usbg_config_attrs *get_random_config_attrs() { @@ -712,7 +660,7 @@ static void test_get_function_type_str(void **state) {F_EEM, "eem"}, {F_RNDIS, "rndis"}, {F_PHONET, "phonet"}, - {F_FFS, "ffs"} + {F_FFS, "ffs"}, }; const char *str; |