skip to content
==> tests/buf.c <==
1/* Appending, printf, little-endian words, take and truncate. */
2#include "mesh/buf.h"
3#include "mesh/check.h"
4
5#include <stdlib.h>
6#include <string.h>
7
8int main(void) {
9    mesh_buf b = MESH_BUF_INIT;
10    CHECK_S(mesh_buf_str(&b), "");
11    mesh_buf_puts(&b, "ok");
12    mesh_buf_putc(&b, ' ');
13    mesh_buf_printf(&b, "%u us %s", 42u, "done");
14    CHECK_S(b.data, "ok 42 us done");
15    CHECK_U(b.len, 13);
16    for (int i = 0; i < 1000; i++) mesh_buf_putc(&b, 'x');
17    CHECK_U(b.len, 1013);
18    CHECK_U(b.data[1013], 0);
19    mesh_buf_truncate(&b, 2);
20    CHECK_S(b.data, "ok");
21    mesh_buf_clear(&b);
22    mesh_buf_u32le(&b, 0x04030201u);
23    mesh_buf_u64le(&b, 0x0c0b0a0908070605ull);
24    mesh_buf_zeros(&b, 2);
25    const unsigned char want[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0};
26    CHECK_U(b.len, sizeof want);
27    CHECK(memcmp(b.data, want, sizeof want) == 0);
28    char *s = mesh_buf_take(&b);
29    CHECK(b.data == NULL && b.len == 0);
30    free(s);
31    char *f = mesh_buf_fmt("%s=%d", "a", 7);
32    CHECK_S(f, "a=7");
33    free(f);
34    char *d = mesh_xstrndup("hello", 3);
35    CHECK_S(d, "hel");
36    free(d);
37    mesh_buf_free(&b);
38    return mesh_check_done();
39}