Skip to content

Commit 786a8d4

Browse files
committed
Problem: code duplication in test_reqrep_tcp
Solution: extracted make_connect_address
1 parent 8372797 commit 786a8d4

1 file changed

Lines changed: 14 additions & 30 deletions

File tree

tests/test_reqrep_tcp.cpp

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ void test_single_connect (int ipv6)
6666
test_context_socket_close (sb);
6767
}
6868

69+
void make_connect_address (char *connect_address,
70+
const int ipv6,
71+
const int port,
72+
const char *bind_address)
73+
{
74+
sprintf (connect_address, "tcp://%s:%i;%s", ipv6 ? "[::1]" : "127.0.0.1",
75+
port, strrchr (bind_address, '/') + 1);
76+
}
77+
6978
void test_multi_connect (int ipv6)
7079
{
7180
size_t len = MAX_SOCKET_STRING;
@@ -88,12 +97,7 @@ void test_multi_connect (int ipv6)
8897
zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int)));
8998
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_0));
9099
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_1));
91-
if (!ipv6)
92-
sprintf (my_endpoint_3, "tcp://127.0.0.1:5564;%s",
93-
strrchr (my_endpoint_2, '/') + 1);
94-
else
95-
sprintf (my_endpoint_3, "tcp://[::1]:5564;%s",
96-
strrchr (my_endpoint_2, '/') + 1);
100+
make_connect_address (my_endpoint_3, ipv6, 5564, my_endpoint_2);
97101
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_3));
98102

99103
bounce (sb0, sc);
@@ -137,37 +141,17 @@ void test_multi_connect_same_port (int ipv6)
137141
void *sc0 = test_context_socket (ZMQ_REQ);
138142
TEST_ASSERT_SUCCESS_ERRNO (
139143
zmq_setsockopt (sc0, ZMQ_IPV6, &ipv6, sizeof (int)));
140-
if (!ipv6)
141-
sprintf (my_endpoint_2, "tcp://127.0.0.1:5564;%s",
142-
strrchr (my_endpoint_0, '/') + 1);
143-
else
144-
sprintf (my_endpoint_2, "tcp://[::1]:5564;%s",
145-
strrchr (my_endpoint_0, '/') + 1);
144+
make_connect_address (my_endpoint_2, ipv6, 5564, my_endpoint_0);
146145
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_2));
147-
if (!ipv6)
148-
sprintf (my_endpoint_3, "tcp://127.0.0.1:5565;%s",
149-
strrchr (my_endpoint_1, '/') + 1);
150-
else
151-
sprintf (my_endpoint_3, "tcp://[::1]:5565;%s",
152-
strrchr (my_endpoint_1, '/') + 1);
146+
make_connect_address (my_endpoint_3, ipv6, 5565, my_endpoint_1);
153147
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc0, my_endpoint_3));
154148

155149
void *sc1 = test_context_socket (ZMQ_REQ);
156150
TEST_ASSERT_SUCCESS_ERRNO (
157151
zmq_setsockopt (sc1, ZMQ_IPV6, &ipv6, sizeof (int)));
158-
if (!ipv6)
159-
sprintf (my_endpoint_4, "tcp://127.0.0.1:5565;%s",
160-
strrchr (my_endpoint_0, '/') + 1);
161-
else
162-
sprintf (my_endpoint_4, "tcp://[::1]:5565;%s",
163-
strrchr (my_endpoint_0, '/') + 1);
152+
make_connect_address (my_endpoint_4, ipv6, 5565, my_endpoint_0);
164153
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_4));
165-
if (!ipv6)
166-
sprintf (my_endpoint_5, "tcp://127.0.0.1:5564;%s",
167-
strrchr (my_endpoint_1, '/') + 1);
168-
else
169-
sprintf (my_endpoint_5, "tcp://[::1]:5564;%s",
170-
strrchr (my_endpoint_1, '/') + 1);
154+
make_connect_address (my_endpoint_5, ipv6, 5564, my_endpoint_1);
171155
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc1, my_endpoint_5));
172156

173157
bounce (sb0, sc0);

0 commit comments

Comments
 (0)