Skip to content

Commit 631540a

Browse files
ECHOTEST internal loop simplification and fix
The loop was wrongly incrementing the index of the array after assiging the value. Thus the first array element was used twice and the last one was never user. The issue is fixed and the loops are refactored and simplified to avoid such confusion in the future.
1 parent 2641fb3 commit 631540a

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ void TCPSOCKET_ECHOTEST()
6767

6868
int recvd;
6969
int sent;
70-
int x = 0;
71-
for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) {
70+
for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); s_idx++) {
71+
int pkt_s = pkt_sizes[s_idx];
7272
fill_tx_buffer_ascii(tcp_global::tx_buffer, BUFF_SIZE);
7373
sent = sock.send(tcp_global::tx_buffer, pkt_s);
7474
if (sent < 0) {
75-
printf("[Round#%02d] network error %d\n", x, sent);
75+
printf("[Round#%02d] network error %d\n", s_idx, sent);
7676
TEST_FAIL();
7777
break;
7878
} else if (sent != pkt_s) {
79-
printf("[%02d] sock.send return size %d does not match the expectation %d\n", x, sent, pkt_s);
79+
printf("[%02d] sock.send return size %d does not match the expectation %d\n", s_idx, sent, pkt_s);
8080
TEST_FAIL();
8181
break;
8282
}
@@ -85,7 +85,7 @@ void TCPSOCKET_ECHOTEST()
8585
while (bytes2recv) {
8686
recvd = sock.recv(&(tcp_global::rx_buffer[sent - bytes2recv]), bytes2recv);
8787
if (recvd < 0) {
88-
printf("[Round#%02d] network error %d\n", x, recvd);
88+
printf("[Round#%02d] network error %d\n", s_idx, recvd);
8989
TEST_FAIL();
9090
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
9191
return;
@@ -143,7 +143,6 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
143143

144144
int bytes2send;
145145
int sent;
146-
int s_idx = 0;
147146
receive_error = false;
148147
unsigned char *stack_mem = (unsigned char *)malloc(tcp_global::TCP_OS_STACK_SIZE);
149148
TEST_ASSERT_NOT_NULL(stack_mem);
@@ -154,8 +153,8 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
154153

155154
TEST_ASSERT_EQUAL(osOK, receiver_thread->start(callback(&queue, &EventQueue::dispatch_forever)));
156155

157-
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
158-
pkt_s = pkt_sizes[s_idx];
156+
for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
157+
int pkt_s = pkt_sizes[s_idx];
159158
bytes2recv = pkt_s;
160159
bytes2recv_total = pkt_s;
161160

TESTS/netsocket/tls/tlssocket_echotest.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ void TLSSOCKET_ECHOTEST()
7070

7171
int recvd;
7272
int sent;
73-
int x = 0;
74-
for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) {
73+
for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); s_idx++) {
74+
int pkt_s = pkt_sizes[s_idx];
7575
fill_tx_buffer_ascii(tls_global::tx_buffer, BUFF_SIZE);
7676

7777
sent = sock->send(tls_global::tx_buffer, pkt_s);
7878
if (sent < 0) {
79-
printf("[Round#%02d] network error %d\n", x, sent);
79+
printf("[Round#%02d] network error %d\n", s_idx, sent);
8080
TEST_FAIL();
8181
break;
8282
} else if (sent != pkt_s) {
83-
printf("[%02d] sock.send return size %d does not match the expectation %d\n", x, sent, pkt_s);
83+
printf("[%02d] sock.send return size %d does not match the expectation %d\n", s_idx, sent, pkt_s);
8484
TEST_FAIL();
8585
break;
8686
}
@@ -89,7 +89,7 @@ void TLSSOCKET_ECHOTEST()
8989
while (bytes2recv) {
9090
recvd = sock->recv(&(tls_global::rx_buffer[sent - bytes2recv]), bytes2recv);
9191
if (recvd < 0) {
92-
printf("[Round#%02d] network error %d\n", x, recvd);
92+
printf("[Round#%02d] network error %d\n", s_idx, recvd);
9393
TEST_FAIL();
9494
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
9595
return;
@@ -146,7 +146,7 @@ void TLSSOCKET_ECHOTEST_NONBLOCK()
146146

147147
int bytes2send;
148148
int sent;
149-
int s_idx = 0;
149+
;
150150
receive_error = false;
151151
unsigned char *stack_mem = (unsigned char *)malloc(tls_global::TLS_OS_STACK_SIZE);
152152
TEST_ASSERT_NOT_NULL(stack_mem);
@@ -158,8 +158,8 @@ void TLSSOCKET_ECHOTEST_NONBLOCK()
158158
event_queue = &queue;
159159
TEST_ASSERT_EQUAL(osOK, receiver_thread->start(callback(&queue, &EventQueue::dispatch_forever)));
160160

161-
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
162-
pkt_s = pkt_sizes[s_idx];
161+
for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
162+
int pkt_s = pkt_sizes[s_idx];
163163
bytes2recv = pkt_s;
164164
bytes2recv_total = pkt_s;
165165

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ void UDPSOCKET_ECHOTEST()
6868

6969
int recvd;
7070
int sent;
71-
int s_idx = 0;
7271
int packets_sent = 0;
7372
int packets_recv = 0;
74-
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; pkt_s = ++s_idx) {
75-
pkt_s = pkt_sizes[s_idx];
73+
for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
74+
int pkt_s = pkt_sizes[s_idx];
7675

7776
fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
7877

@@ -147,15 +146,14 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
147146
sock.sigio(callback(_sigio_handler));
148147

149148
int sent;
150-
int s_idx = 0;
151149
int packets_sent = 0;
152150
int packets_recv = 0;
153151
Thread *thread;
154152
unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
155153
TEST_ASSERT_NOT_NULL(stack_mem);
156154

157-
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
158-
pkt_s = pkt_sizes[s_idx];
155+
for (int s_idx = 0; s_idx < sizeof(pkt_sizes) / sizeof(*pkt_sizes); ++s_idx) {
156+
int pkt_s = pkt_sizes[s_idx];
159157

160158
thread = new Thread(osPriorityNormal,
161159
OS_STACK_SIZE,

0 commit comments

Comments
 (0)