net: tests: Refactor net_buf tests for easier extensibility

Change-Id: I23380b1c78b5a9c8814c037a2d48b3505c99f6ea
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2016-06-06 11:21:29 +03:00
parent 7029d6ed62
commit 6d93cbb627

View file

@ -17,6 +17,7 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <misc/printk.h> #include <misc/printk.h>
@ -53,23 +54,17 @@ static void buf_destroy(struct net_buf *buf)
static NET_BUF_POOL(bufs_pool, 22, 74, &bufs_fifo, buf_destroy, static NET_BUF_POOL(bufs_pool, 22, 74, &bufs_fifo, buf_destroy,
sizeof(struct bt_data)); sizeof(struct bt_data));
void main(void) static bool net_buf_test_1(void)
{ {
struct net_buf *bufs[ARRAY_SIZE(bufs_pool)]; struct net_buf *bufs[ARRAY_SIZE(bufs_pool)];
struct nano_fifo fifo;
struct net_buf *buf; struct net_buf *buf;
int i; int i;
printk("sizeof(struct net_buf) = %u\n", sizeof(struct net_buf));
printk("sizeof(bufs_pool) = %u\n", sizeof(bufs_pool));
net_buf_pool_init(bufs_pool);
for (i = 0; i < ARRAY_SIZE(bufs_pool); i++) { for (i = 0; i < ARRAY_SIZE(bufs_pool); i++) {
buf = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE); buf = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
if (!buf) { if (!buf) {
printk("Failed to get buffer!\n"); printk("Failed to get buffer!\n");
return; return false;
} }
bufs[i] = buf; bufs[i] = buf;
@ -77,49 +72,84 @@ void main(void)
for (i = 0; i < ARRAY_SIZE(bufs_pool); i++) { for (i = 0; i < ARRAY_SIZE(bufs_pool); i++) {
net_buf_unref(bufs[i]); net_buf_unref(bufs[i]);
bufs[i] = NULL;
} }
if (destroy_called != ARRAY_SIZE(bufs_pool)) { if (destroy_called != ARRAY_SIZE(bufs_pool)) {
printk("Incorrect destroy callback count: %d\n", printk("Incorrect destroy callback count: %d\n",
destroy_called); destroy_called);
return; return false;
} }
bufs[0] = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE); return true;
if (!bufs[0]) { }
static bool net_buf_test_2(void)
{
struct net_buf *frag, *head;
struct nano_fifo fifo;
int i;
head = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
if (!head) {
printk("Failed to get fragment list head!\n"); printk("Failed to get fragment list head!\n");
return; return false;
} }
printk("Fragment list head %p\n", bufs[0]); printk("Fragment list head %p\n", head);
buf = bufs[0]; frag = head;
for (i = 0; i < ARRAY_SIZE(bufs_pool) - 1; i++) { for (i = 0; i < ARRAY_SIZE(bufs_pool) - 1; i++) {
buf->frags = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE); frag->frags = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
if (!buf->frags) { if (!frag->frags) {
printk("Failed to get fragment!\n"); printk("Failed to get fragment!\n");
return; return false;
} }
printk("%p -> %p\n", buf, buf->frags); printk("%p -> %p\n", frag, frag->frags);
buf = buf->frags; frag = frag->frags;
} }
printk("%p -> %p\n", buf, buf->frags); printk("%p -> %p\n", frag, frag->frags);
buf = bufs[0];
nano_fifo_init(&fifo); nano_fifo_init(&fifo);
net_buf_put(&fifo, buf); net_buf_put(&fifo, head);
buf = net_buf_get_timeout(&fifo, 0, TICKS_NONE); head = net_buf_get_timeout(&fifo, 0, TICKS_NONE);
destroy_called = 0; destroy_called = 0;
net_buf_unref(buf); net_buf_unref(head);
if (destroy_called != ARRAY_SIZE(bufs_pool)) { if (destroy_called != ARRAY_SIZE(bufs_pool)) {
printk("Incorrect fragment destroy callback count: %d\n", printk("Incorrect fragment destroy callback count: %d\n",
destroy_called); destroy_called);
return; return false;
} }
printk("Buffer tests passed\n"); return true;
}
static const struct {
const char *name;
bool (*func)(void);
} tests[] = {
{ "Test 1", net_buf_test_1, },
{ "Test 2", net_buf_test_2, },
};
void main(void)
{
int count, pass;
printk("sizeof(struct net_buf) = %u\n", sizeof(struct net_buf));
printk("sizeof(bufs_pool) = %u\n", sizeof(bufs_pool));
net_buf_pool_init(bufs_pool);
for (count = 0, pass = 0; count < ARRAY_SIZE(tests); count++) {
if (!tests[count].func()) {
printk("%s failed!\n", tests[count].name);
} else {
pass++;
}
}
printk("%d / %d tests passed\n", pass, count);
} }