/* splinter -- buffer and escaping tests. */ #include #include #include "splinter.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) int main(void) { struct sp_buf b; CHECK(sp_buf_init(&b) == 0); CHECK(b.len == 0); CHECK(b.p != NULL); CHECK(strcmp(b.p, "") == 0); CHECK(sp_buf_puts(&b, "hello") == 0); CHECK(b.len == 5); CHECK(strcmp(b.p, "hello") == 0); CHECK(sp_buf_add(&b, " world", 6) == 0); CHECK(b.len == 11); CHECK(strcmp(b.p, "hello world") == 0); CHECK(sp_buf_printf(&b, " %d", 42) == 0); CHECK(strcmp(b.p, "hello world 42") == 0); /* growth: 10k bytes must land, and the NUL must always be there */ { size_t i; sp_buf_clear(&b); for (i = 0; i < 10000; i++) CHECK(sp_buf_puts(&b, "x") == 0); CHECK(b.len == 10000); CHECK(b.p[b.len] == '\0'); CHECK(b.cap > b.len); } sp_buf_clear(&b); CHECK(sp_json_escape(&b, "a\"b\\c\nd\te") == 0); CHECK(strcmp(b.p, "a\\\"b\\\\c\\nd\\te") == 0); sp_buf_clear(&b); CHECK(sp_json_escape(&b, "\x01") == 0); CHECK(strcmp(b.p, "\\u0001") == 0); sp_buf_free(&b); CHECK(b.p == NULL); CHECK(b.len == 0); if (failures == 0) printf("test_buf: ok\n"); return failures == 0 ? 0 : 1; }