diff options
author | Pawel Szewczyk <p.szewczyk@samsung.com> | 2015-05-05 11:49:01 +0200 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2015-07-30 15:56:05 +0200 |
commit | bddf612196f8a3c9661d3dfe17938b1e840d0423 (patch) | |
tree | 90e0a8ed05f31434593a8ecd28bacb857ef0d267 | |
parent | 5c0113bb4ab065d9689bf8d26fa9593432498f01 (diff) | |
download | libusbg-bddf612196f8a3c9661d3dfe17938b1e840d0423.tar.gz libusbg-bddf612196f8a3c9661d3dfe17938b1e840d0423.tar.bz2 libusbg-bddf612196f8a3c9661d3dfe17938b1e840d0423.zip |
libusbg: tests: Separate common code of gadget testing
Most gadget tests run the same initialization and loop through all
gadgets, performing single operation on them and checking results.
Common code is now separated to for_each_test_gadget function.
Change-Id: I0a1870487aa0c25153c96e57da5501e1bccb8132
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Reviewed-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | tests/test.c | 104 | ||||
-rw-r--r-- | tests/usbg-test.c | 20 | ||||
-rw-r--r-- | tests/usbg-test.h | 12 |
3 files changed, 62 insertions, 74 deletions
diff --git a/tests/test.c b/tests/test.c index 828822d..6f56126 100644 --- a/tests/test.c +++ b/tests/test.c @@ -474,22 +474,7 @@ static int setup_random_len_gadget_strs_data(void **state) */ static void test_get_gadget(void **state) { - usbg_gadget *g = NULL; - usbg_state *s = NULL; - struct test_state *st; - int i = 0; - - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; - - for (i = 0; st->gadgets[i].name; i++) { - g = usbg_get_gadget(s, st->gadgets[i].name); - assert_non_null(g); - assert_gadget_equal(g, &st->gadgets[i]); - } + for_each_test_gadget(state, assert_gadget_equal); } /** @@ -545,30 +530,31 @@ static void test_get_first_gadget_fail(void **state) assert_null(g); } +static void try_get_gadget_name(usbg_gadget *g, struct test_gadget *tg) +{ + const char *name; + + name = usbg_get_gadget_name(g); + assert_string_equal(name, tg->name); +} + /** * @brief Tests getting name of gadget * @details Check if gadget name is returned correctly */ static void test_get_gadget_name(void **state) { - usbg_gadget *g = NULL; - usbg_state *s = NULL; - struct test_state *st; - int i = 0; - const char *name; - - st = (struct test_state *)(*state); - *state = NULL; + for_each_test_gadget(state, try_get_gadget_name); +} - init_with_state(st, &s); - *state = s; +static void try_get_gadget_name_len(usbg_gadget *g, struct test_gadget *tg) +{ + int len; + int expected; - for (i = 0; st->gadgets[i].name; i++) { - g = usbg_get_gadget(s, st->gadgets[i].name); - assert_non_null(g); - name = usbg_get_gadget_name(g); - assert_string_equal(name, st->gadgets[i].name); - } + expected = strlen(tg->name); + len = usbg_get_gadget_name_len(g); + assert_int_equal(len, expected); } /** @@ -577,27 +563,7 @@ static void test_get_gadget_name(void **state) */ static void test_get_gadget_name_len(void **state) { - usbg_gadget *g = NULL; - usbg_state *s = NULL; - struct test_state *st; - int i = 0; - int len; - int expected; - - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; - - for (i = 0; st->gadgets[i].name; i++) { - g = usbg_get_gadget(s, st->gadgets[i].name); - assert_non_null(g); - - expected = strlen(st->gadgets[i].name); - len = usbg_get_gadget_name_len(g); - assert_int_equal(len, expected); - } + for_each_test_gadget(state, try_get_gadget_name_len); } /** @@ -613,33 +579,23 @@ static void test_get_gadget_name_fail(void **state) assert_null(name); } +static void try_cpy_gadget_name(usbg_gadget *g, struct test_gadget *tg) +{ + char name[USBG_MAX_NAME_LENGTH]; + int ret; + + ret = usbg_cpy_gadget_name(g, name, USBG_MAX_NAME_LENGTH); + assert_int_equal(ret, USBG_SUCCESS); + assert_string_equal(name, tg->name); +} + /** * @brief Tests copying gadget's name * @details Check if copying gadget name copy actual name correctly */ static void test_cpy_gadget_name(void **state) { - usbg_gadget *g = NULL; - usbg_state *s = NULL; - struct test_state *st; - int i = 0; - char name[USBG_MAX_NAME_LENGTH]; - int ret; - - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; - - for (i = 0; st->gadgets[i].name; i++) { - g = usbg_get_gadget(s, st->gadgets[i].name); - assert_non_null(g); - - ret = usbg_cpy_gadget_name(g, name, USBG_MAX_NAME_LENGTH); - assert_int_equal(ret, USBG_SUCCESS); - assert_string_equal(name, st->gadgets[i].name); - } + for_each_test_gadget(state, try_cpy_gadget_name); } /** diff --git a/tests/usbg-test.c b/tests/usbg-test.c index 5aacea9..3e00006 100644 --- a/tests/usbg-test.c +++ b/tests/usbg-test.c @@ -1174,3 +1174,23 @@ void for_each_binding(void **state, BindingTestFunc fun) } } } + +void for_each_test_gadget(void **state, GadgetTestFunc fun) +{ + struct test_state *ts; + struct test_gadget *tg; + usbg_state *s = NULL; + usbg_gadget *g = NULL; + + ts = (struct test_state *)(*state); + *state = NULL; + + init_with_state(ts, &s); + *state = s; + + for (tg = ts->gadgets; tg->name; ++tg) { + g = usbg_get_gadget(s, tg->name); + assert_non_null(g); + fun(g, tg); + } +} diff --git a/tests/usbg-test.h b/tests/usbg-test.h index 9c41dee..6abfa7f 100644 --- a/tests/usbg-test.h +++ b/tests/usbg-test.h @@ -435,6 +435,18 @@ typedef void (*BindingTestFunc)(struct test_binding *tb, usbg_binding *b); */ void for_each_binding(void **state, BindingTestFunc fun); +/** + * @brief Function that performs test on given usbg gadget + */ +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] fun Function to be called on each usb gadget in state + */ +void for_each_test_gadget(void **state, GadgetTestFunc fun); + static inline void *safe_calloc(int count, size_t size) { void *ptr; |