Skip to content

Commit 3a369c4

Browse files
nordic-krchnashif
authored andcommitted
tests: lib: spsc_pbuf: Test refactoring and improvement
Refactoring test by adding macro for total packet length calculation and header length. Tuning test to trigger a scenario where free space was miscalculated. Bug was fixed in the previous commit. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 09b4182 commit 3a369c4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

tests/lib/spsc_pbuf/src/main.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <zephyr/sys/spsc_pbuf.h>
1010
#include <zephyr/random/rand32.h>
1111

12+
#define HDR_LEN sizeof(uint32_t)
13+
#define TLEN(len) ROUND_UP(HDR_LEN + len, sizeof(uint32_t))
14+
1215
/* The buffer size itself would be 199 bytes.
1316
* 212 - sizeof(struct spsc_pbuf) - 1 = 199.
1417
* -1 because internal rd/wr_idx is reserved to mean the buffer is empty.
@@ -191,7 +194,7 @@ ZTEST(test_spsc_pbuf, test_0cpy)
191194
PACKET_WRITE(pb, len1, len1, 0, len1);
192195

193196
/* Remaining space. */
194-
len2 = capacity - ROUND_UP(len1, sizeof(uint32_t)) - 2 * sizeof(uint32_t);
197+
len2 = capacity - TLEN(len1) - HDR_LEN;
195198
/* Request exceeding capacity*/
196199
PACKET_WRITE(pb, len2 + 1, 0, 1, len2);
197200

@@ -235,7 +238,7 @@ ZTEST(test_spsc_pbuf, test_0cpy_discard)
235238
len1 = 14;
236239
PACKET_WRITE(pb, len1, len1, 0, len1);
237240

238-
len2 = capacity - len1 - sizeof(uint32_t) - 10;
241+
len2 = capacity - TLEN(len1) - 10;
239242
PACKET_WRITE(pb, len2, len2, 1, len2);
240243

241244
/* Consume first packet */
@@ -276,7 +279,7 @@ ZTEST(test_spsc_pbuf, test_0cpy_corner1)
276279
len2 = capacity;
277280
len2 = spsc_pbuf_alloc(pb, len2, &buf);
278281

279-
uint16_t exp_len2 = capacity - ROUND_UP(len1, sizeof(uint32_t)) - 2 * sizeof(uint32_t);
282+
uint16_t exp_len2 = capacity - TLEN(len1) - HDR_LEN;
280283

281284
zassert_equal(len2, exp_len2, "got %d, exp: %d", len2, exp_len2);
282285

@@ -303,11 +306,11 @@ ZTEST(test_spsc_pbuf, test_0cpy_corner2)
303306
capacity = spsc_pbuf_capacity(pb);
304307

305308
/* Commit 5 byte packet. */
306-
len1 = 10;
309+
len1 = 16;
307310
PACKET_WRITE(pb, len1, len1, 0, len1);
308311

309312
/* Attempt to allocate packet that will leave 5 bytes at the end. */
310-
len2 = capacity - ROUND_UP(len1, sizeof(uint32_t)) - 2 * sizeof(uint16_t) - 5;
313+
len2 = capacity - TLEN(len1) - HDR_LEN - 5;
311314
PACKET_WRITE(pb, len2, len2, 1, len2);
312315

313316
/* Free first packet. */
@@ -326,7 +329,7 @@ ZTEST(test_spsc_pbuf, test_0cpy_corner2)
326329
/* Get longest available. As now there is only one packet at the beginning
327330
* that should be remaining space decremented by length fields.
328331
*/
329-
uint16_t exp_len = capacity - ROUND_UP(len1, sizeof(uint32_t)) - 2 * sizeof(uint32_t);
332+
uint16_t exp_len = capacity - TLEN(len1) - HDR_LEN;
330333

331334
PACKET_WRITE(pb, capacity, 0, 2, exp_len);
332335
}
@@ -389,21 +392,20 @@ ZTEST(test_spsc_pbuf, test_utilization)
389392

390393
PACKET_CONSUME(pb, len1, 0);
391394
u = spsc_pbuf_get_utilization(pb);
392-
zassert_equal(u, ROUND_UP(len1, sizeof(uint32_t)) + sizeof(uint32_t), NULL);
395+
zassert_equal(u, TLEN(len1), NULL);
393396

394397
len2 = 11;
395398
PACKET_WRITE(pb, len2, len2, 1, len2);
396399
PACKET_CONSUME(pb, len2, 1);
397400
u = spsc_pbuf_get_utilization(pb);
398-
zassert_equal(u, ROUND_UP(len2, sizeof(uint32_t)) + sizeof(uint32_t), NULL);
401+
zassert_equal(u, TLEN(len2), NULL);
399402

400-
len3 = capacity - ROUND_UP(len1, sizeof(uint32_t)) - ROUND_UP(len2, sizeof(uint32_t))
401-
- 3 * sizeof(uint32_t) + sizeof(uint32_t);
403+
len3 = capacity - TLEN(len1) - TLEN(len2);
402404
PACKET_WRITE(pb, SPSC_PBUF_MAX_LEN, len3, 2, len3);
403405
PACKET_CONSUME(pb, len3, 2);
404406

405407
u = spsc_pbuf_get_utilization(pb);
406-
int exp_u = ROUND_UP(len3, sizeof(uint32_t)) + sizeof(uint32_t);
408+
int exp_u = TLEN(len3);
407409

408410
zassert_equal(u, exp_u, NULL);
409411
}

0 commit comments

Comments
 (0)