2023-02-25 13:32:02 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util/bytebuf.h"
|
|
|
|
|
2023-10-16 03:38:35 +00:00
|
|
|
static void test_bytebuf_simple(void) {
|
2023-02-25 13:32:02 +00:00
|
|
|
struct sc_bytebuf buf;
|
|
|
|
uint8_t data[20];
|
|
|
|
|
|
|
|
bool ok = sc_bytebuf_init(&buf, 20);
|
|
|
|
assert(ok);
|
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "hello", sizeof("hello") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 5);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_read(&buf, data, 4);
|
|
|
|
assert(!strncmp((char *) data, "hell", 4));
|
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) " world", sizeof(" world") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 7);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "!", 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 8);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_read(&buf, &data[4], 8);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 0);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
data[12] = '\0';
|
|
|
|
assert(!strcmp((char *) data, "hello world!"));
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 0);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_destroy(&buf);
|
|
|
|
}
|
|
|
|
|
2023-10-16 03:38:35 +00:00
|
|
|
static void test_bytebuf_boundaries(void) {
|
2023-02-25 13:32:02 +00:00
|
|
|
struct sc_bytebuf buf;
|
|
|
|
uint8_t data[20];
|
|
|
|
|
|
|
|
bool ok = sc_bytebuf_init(&buf, 20);
|
|
|
|
assert(ok);
|
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 6);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 12);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 18);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_read(&buf, data, 9);
|
|
|
|
assert(!strncmp((char *) data, "hello hel", 9));
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 9);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "world", sizeof("world") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 14);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "!", 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 15);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_skip(&buf, 3);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 12);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_read(&buf, data, 12);
|
|
|
|
data[12] = '\0';
|
|
|
|
assert(!strcmp((char *) data, "hello world!"));
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 0);
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
sc_bytebuf_destroy(&buf);
|
|
|
|
}
|
|
|
|
|
2023-10-16 03:38:35 +00:00
|
|
|
static void test_bytebuf_two_steps_write(void) {
|
2023-02-25 17:45:05 +00:00
|
|
|
struct sc_bytebuf buf;
|
|
|
|
uint8_t data[20];
|
|
|
|
|
|
|
|
bool ok = sc_bytebuf_init(&buf, 20);
|
|
|
|
assert(ok);
|
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 6);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 12);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_prepare_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 12); // write not committed yet
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_read(&buf, data, 9);
|
|
|
|
assert(!strncmp((char *) data, "hello hel", 3));
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 3);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_commit_write(&buf, sizeof("hello ") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 9);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_prepare_write(&buf, (uint8_t *) "world", sizeof("world") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 9); // write not committed yet
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_commit_write(&buf, sizeof("world") - 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 14);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_write(&buf, (uint8_t *) "!", 1);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 15);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_skip(&buf, 3);
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 12);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_read(&buf, data, 12);
|
|
|
|
data[12] = '\0';
|
|
|
|
assert(!strcmp((char *) data, "hello world!"));
|
2023-03-11 09:16:43 +00:00
|
|
|
assert(sc_bytebuf_can_read(&buf) == 0);
|
2023-02-25 17:45:05 +00:00
|
|
|
|
|
|
|
sc_bytebuf_destroy(&buf);
|
|
|
|
}
|
|
|
|
|
2023-02-25 13:32:02 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
|
|
|
test_bytebuf_simple();
|
|
|
|
test_bytebuf_boundaries();
|
2023-02-25 17:45:05 +00:00
|
|
|
test_bytebuf_two_steps_write();
|
2023-02-25 13:32:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|