2022-09-19 20:28:07 +08:00
|
|
|
#define MG_ENABLE_SOCKET 0
|
2022-09-23 04:05:30 +08:00
|
|
|
#define MG_ENABLE_LINES 1
|
2022-09-19 20:28:07 +08:00
|
|
|
#define MG_ENABLE_MIP 1
|
|
|
|
#define MG_ENABLE_PACKED_FS 0
|
|
|
|
|
2022-09-03 20:36:08 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include "mongoose.c"
|
2022-12-03 00:40:46 +08:00
|
|
|
#include "driver_mock.c"
|
2022-11-17 01:26:49 +08:00
|
|
|
|
|
|
|
static int s_num_tests = 0;
|
|
|
|
|
|
|
|
#define ASSERT(expr) \
|
|
|
|
do { \
|
|
|
|
s_num_tests++; \
|
|
|
|
if (!(expr)) { \
|
|
|
|
printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2022-09-03 20:36:08 +08:00
|
|
|
static void test_queue(void) {
|
2022-09-19 20:28:07 +08:00
|
|
|
static uint8_t
|
|
|
|
buf[sizeof(size_t) + sizeof(uint16_t) + 3]; // fit 1 element but not 2
|
2022-09-03 20:36:08 +08:00
|
|
|
uint16_t val = 1234;
|
2022-09-08 01:31:08 +08:00
|
|
|
static struct queue q = {buf, sizeof(buf), 0, 0};
|
2022-09-03 20:36:08 +08:00
|
|
|
|
|
|
|
// Write to an empty queue, and read back
|
2022-12-03 00:40:46 +08:00
|
|
|
ASSERT(q_avail(&q) == 0);
|
|
|
|
ASSERT(q_write(&q, &val, sizeof(val)) == true);
|
|
|
|
ASSERT(q_avail(&q) == sizeof(val));
|
|
|
|
ASSERT(q.head > q.tail);
|
2022-09-08 01:31:08 +08:00
|
|
|
// Only one element may fit
|
2022-12-03 00:40:46 +08:00
|
|
|
ASSERT(q_write(&q, &val, sizeof(val)) == false);
|
2022-09-03 20:36:08 +08:00
|
|
|
val = 0;
|
2022-12-03 00:40:46 +08:00
|
|
|
ASSERT(q_read(&q, &val) == sizeof(val));
|
|
|
|
ASSERT(val == 1234);
|
|
|
|
ASSERT(q_avail(&q) == 0);
|
2022-09-03 20:36:08 +08:00
|
|
|
|
|
|
|
// Second write - wrap over the buffer boundary
|
2022-12-03 00:40:46 +08:00
|
|
|
ASSERT(q_write(&q, &val, sizeof(val)) == true);
|
|
|
|
ASSERT(q_avail(&q) == sizeof(val));
|
|
|
|
ASSERT(q.head < q.tail);
|
2022-09-08 01:31:08 +08:00
|
|
|
// Only one element may fit
|
2022-12-03 00:40:46 +08:00
|
|
|
ASSERT(q_write(&q, &val, sizeof(val)) == false);
|
2022-09-03 20:36:08 +08:00
|
|
|
val = 0;
|
2022-12-03 00:40:46 +08:00
|
|
|
ASSERT(q_read(&q, &val) == sizeof(val));
|
|
|
|
ASSERT(val == 1234);
|
|
|
|
ASSERT(q_avail(&q) == 0);
|
2022-09-03 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2022-09-19 20:28:07 +08:00
|
|
|
static void test_statechange(void) {
|
2022-11-09 21:11:22 +08:00
|
|
|
char tx[1540];
|
2022-09-19 20:28:07 +08:00
|
|
|
struct mip_if iface;
|
|
|
|
memset(&iface, 0, sizeof(iface));
|
|
|
|
iface.ip = mg_htonl(0x01020304);
|
|
|
|
iface.state = MIP_STATE_READY;
|
2022-11-09 21:11:22 +08:00
|
|
|
iface.tx.ptr = tx, iface.tx.len = sizeof(tx);
|
2022-09-19 20:28:07 +08:00
|
|
|
iface.driver = &mip_driver_mock;
|
|
|
|
onstatechange(&iface);
|
|
|
|
}
|
|
|
|
|
2022-11-30 22:47:37 +08:00
|
|
|
static void ph(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
|
if (ev == MG_EV_POLL) ++(*(int *) fn_data);
|
|
|
|
(void) c, (void) ev_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_poll(void) {
|
2022-12-01 04:37:32 +08:00
|
|
|
int count = 0, i;
|
2022-11-30 22:47:37 +08:00
|
|
|
struct mg_mgr mgr;
|
|
|
|
mg_mgr_init(&mgr);
|
2022-12-01 04:37:32 +08:00
|
|
|
struct mip_if mif;
|
|
|
|
memset(&mif, 0, sizeof(mif));
|
|
|
|
mif.driver = &mip_driver_mock;
|
|
|
|
mip_init(&mgr, &mif);
|
2022-11-30 22:47:37 +08:00
|
|
|
mg_http_listen(&mgr, "http://127.0.0.1:12346", ph, &count);
|
2022-12-01 04:37:32 +08:00
|
|
|
for (i = 0; i < 10; i++) mg_mgr_poll(&mgr, 0);
|
2022-11-30 22:47:37 +08:00
|
|
|
ASSERT(count == 10);
|
2022-12-01 04:37:32 +08:00
|
|
|
mip_free(&mif);
|
2022-11-30 22:47:37 +08:00
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
}
|
|
|
|
|
2022-09-03 20:36:08 +08:00
|
|
|
int main(void) {
|
|
|
|
test_queue();
|
2022-09-19 20:28:07 +08:00
|
|
|
test_statechange();
|
2022-11-30 22:47:37 +08:00
|
|
|
test_poll();
|
2022-11-29 18:20:22 +08:00
|
|
|
printf("SUCCESS. Total tests: %d\n", s_num_tests);
|
2022-09-03 20:36:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|