size_t mget_buffer_memset(mget_buffer_t *buf, char c, size_t length) { buf->length = 0; return mget_buffer_memset_append(buf, c, length); }
static void test_buffer(void) { char sbuf[16]; mget_buffer_t buf, *bufp; // testing buffer on stack, using initial stack memory // without resizing mget_buffer_init(&buf, sbuf, sizeof(sbuf)); mget_buffer_deinit(&buf); // testing buffer on stack, using initial stack memory // with resizing mget_buffer_init(&buf, sbuf, sizeof(sbuf)); _test_buffer(&buf, "Test 1"); mget_buffer_deinit(&buf); // testing buffer on stack, using initial heap memory // without resizing mget_buffer_init(&buf, NULL, 16); mget_buffer_deinit(&buf); // testing buffer on stack, using initial heap memory // with resizing mget_buffer_init(&buf, NULL, 16); _test_buffer(&buf, "Test 2"); mget_buffer_deinit(&buf); // testing buffer on heap, using initial stack memory // without resizing bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); mget_buffer_deinit(bufp); bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); mget_buffer_free(&bufp); // testing buffer on heap, using initial stack memory // with resizing bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); _test_buffer(bufp, "Test 3"); mget_buffer_deinit(bufp); bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); _test_buffer(bufp, "Test 4"); mget_buffer_free(&bufp); // testing buffer on heap, using initial heap memory // without resizing bufp = mget_buffer_alloc(16); mget_buffer_free(&bufp); // testing buffer on heap, using initial heap memory // with resizing bufp = mget_buffer_alloc(16); _test_buffer(bufp, "Test 5"); mget_buffer_free(&bufp); // check that appending works mget_buffer_init(&buf, sbuf, sizeof(sbuf)); mget_buffer_strcpy(&buf, "A"); mget_buffer_strcat(&buf, "B"); mget_buffer_memcat(&buf, "C", 1); mget_buffer_memset_append(&buf, 'D', 1); mget_buffer_printf_append2(&buf, "%s", "E"); if (!strcmp(buf.data, "ABCDE")) ok++; else { failed++; info_printf("test_buffer.append: got %s (expected %s)\n", buf.data, "ABCDE"); } mget_buffer_deinit(&buf); // test mget_buffer_trim() mget_buffer_init(&buf, sbuf, sizeof(sbuf)); for (int mid_ws = 0; mid_ws <= 2; mid_ws++) { char expected[16]; snprintf(expected, sizeof(expected), "x%.*sy", mid_ws, " "); for (int lead_ws = 0; lead_ws <= 2; lead_ws++) { for (int trail_ws = 0; trail_ws <= 2; trail_ws++) { mget_buffer_printf2(&buf, "%.*sx%.*sy%.*s", lead_ws, " ", mid_ws, " ", trail_ws, " "); mget_buffer_trim(&buf); if (!strcmp(buf.data, expected)) ok++; else { failed++; info_printf("test_buffer_trim: got '%s' (expected '%s') (%d, %d, %d)\n", buf.data, expected, lead_ws, mid_ws, trail_ws); } } } } mget_buffer_deinit(&buf); }