From abcaa651ac3af9e57896e2751b164509246dc3fd Mon Sep 17 00:00:00 2001 From: Frediano Ziglio Date: Sun, 9 Oct 2016 09:07:19 +0100 Subject: Simplify code check Add and use check_res function Signed-off-by: Frediano Ziglio --- tests/bandwidth.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/bandwidth.c b/tests/bandwidth.c index 279126a..f202a3c 100644 --- a/tests/bandwidth.c +++ b/tests/bandwidth.c @@ -89,14 +89,25 @@ flush_socks(void) } } +static void +check_res(pthread_t th, unsigned expected) +{ + void *thread_res; + assert(pthread_join(th, &thread_res) == 0); + + unsigned bytes_received = (uintptr_t) thread_res; + + printf("received %u/%u\n", bytes_received, expected); + // check into -5% to +5% + assert(bytes_received >= expected * 0.96 && bytes_received <= expected * 1.04); +} + static void test_bandwidth(unsigned bw) { current_bw = bw; pthread_t th[3]; - void *thread_res; - unsigned bytes_received; unsigned expected = 1024 * 1024 * bw / 8 / 10 + 1 * (1024 + MIN_IP_UDP_HEADER); // launch program with given latency @@ -106,21 +117,13 @@ test_bandwidth(unsigned bw) // one direction assert(pthread_create(th, NULL, recv_proc, SOCK2PTR(udp_socks[1])) == 0); send_proc(SOCK2PTR(udp_socks[0])); - assert(pthread_join(th[0], &thread_res) == 0); - bytes_received = (uintptr_t) thread_res; - printf("received %u/%u\n", bytes_received, expected); - // check into -5% to +5% - assert(bytes_received >= expected * 0.96 && bytes_received <= expected * 1.04); + check_res(th[0], expected); // other direction flush_socks(); assert(pthread_create(th, NULL, recv_proc, SOCK2PTR(udp_socks[0])) == 0); send_proc(SOCK2PTR(udp_socks[1])); - assert(pthread_join(th[0], &thread_res) == 0); - bytes_received = (uintptr_t) thread_res; - printf("received %u/%u\n", bytes_received, expected); - // check into -5% to +5% - assert(bytes_received >= expected * 0.96 && bytes_received <= expected * 1.04); + check_res(th[0], expected); close_udp_pair(udp_socks); kill_latency(); -- cgit v1.2.3