mongoose/test/mip_test.c

58 lines
1.5 KiB
C
Raw Normal View History

2022-09-19 13:28:07 +01:00
#define MG_ENABLE_SOCKET 0
2022-09-22 21:05:30 +01:00
#define MG_ENABLE_LINES 1
2022-09-19 13:28:07 +01:00
#define MG_ENABLE_MIP 1
#define MG_ENABLE_PACKED_FS 0
2022-09-03 13:36:08 +01:00
#include <assert.h>
#include "mongoose.c"
2022-09-19 13:28:07 +01:00
#include "driver_mock.c"
2022-09-03 13:36:08 +01:00
static void test_queue(void) {
2022-09-19 13:28:07 +01:00
static uint8_t
buf[sizeof(size_t) + sizeof(uint16_t) + 3]; // fit 1 element but not 2
2022-09-03 13:36:08 +01:00
uint16_t val = 1234;
static struct queue q = {buf, sizeof(buf), 0, 0};
2022-09-03 13:36:08 +01:00
// Write to an empty queue, and read back
assert(q_avail(&q) == 0);
assert(q_write(&q, &val, sizeof(val)) == true);
assert(q_avail(&q) == sizeof(val));
assert(q.head > q.tail);
// Only one element may fit
assert(q_write(&q, &val, sizeof(val)) == false);
2022-09-03 13:36:08 +01:00
val = 0;
assert(q_read(&q, &val) == sizeof(val));
assert(val == 1234);
assert(q_avail(&q) == 0);
// Second write - wrap over the buffer boundary
assert(q_write(&q, &val, sizeof(val)) == true);
assert(q_avail(&q) == sizeof(val));
assert(q.head < q.tail);
// Only one element may fit
assert(q_write(&q, &val, sizeof(val)) == false);
2022-09-03 13:36:08 +01:00
val = 0;
assert(q_read(&q, &val) == sizeof(val));
assert(val == 1234);
assert(q_avail(&q) == 0);
}
2022-09-19 13:28:07 +01:00
static void test_statechange(void) {
uint8_t tx[1540];
struct mip_if iface;
memset(&iface, 0, sizeof(iface));
iface.ip = mg_htonl(0x01020304);
iface.state = MIP_STATE_READY;
iface.tx.buf = tx, iface.tx.len = sizeof(tx);
iface.driver = &mip_driver_mock;
onstatechange(&iface);
}
2022-09-03 13:36:08 +01:00
int main(void) {
test_queue();
2022-09-19 13:28:07 +01:00
test_statechange();
2022-09-03 13:36:08 +01:00
printf("SUCCESS\n");
return 0;
}