static void _copy_string(wget_buffer_t *buf, unsigned int flags, int field_width, int precision, const char *arg) { size_t length; if (!arg) { wget_buffer_strcat(buf, "(null)"); return; } length = strlen(arg); // info_printf("flags=0x%02x field_width=%d precision=%d length=%zd arg='%s'\n", // flags,field_width,precision,length,arg); if (precision >= 0 && length > (size_t)precision) length = precision; if (field_width) { if ((unsigned)field_width > length) { if (flags & FLAG_LEFT_ADJUST) { wget_buffer_memcat(buf, arg, length); wget_buffer_memset_append(buf, ' ', field_width - length); } else { wget_buffer_memset_append(buf, ' ', field_width - length); wget_buffer_memcat(buf, arg, length); } } else { wget_buffer_memcat(buf, arg, length); } } else { wget_buffer_memcat(buf, arg, length); } }
static void _test_buffer(wget_buffer_t *buf, const char *name) { char test[256]; int it; for (it = 0; it < (int)sizeof(test)-1; it++) { test[it] = 'a' + it % 26; test[it + 1] = 0; wget_buffer_strcpy(buf, test); wget_buffer_strcat(buf, test); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.1 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } wget_buffer_memcpy(buf, test, it + 1); wget_buffer_memcat(buf, test, it + 1); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.2 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } wget_buffer_printf(buf, "%s%s", test, test); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.3 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } wget_buffer_printf(buf, "%s", test); wget_buffer_printf_append(buf, "%s", test); if (!strncmp(buf->data, test, it + 1) && !strncmp(buf->data + it + 1, test, it + 1)) { ok++; } else { failed++; info_printf("test_buffer.4 '%s': [%d] got %s (expected %s%s)\n", name, it, buf->data, test, test); } } }
static void test_buffer(void) { char sbuf[16]; wget_buffer_t buf, *bufp; // testing buffer on stack, using initial stack memory // without resizing wget_buffer_init(&buf, sbuf, sizeof(sbuf)); wget_buffer_deinit(&buf); // testing buffer on stack, using initial stack memory // with resizing wget_buffer_init(&buf, sbuf, sizeof(sbuf)); _test_buffer(&buf, "Test 1"); wget_buffer_deinit(&buf); // testing buffer on stack, using initial heap memory // without resizing wget_buffer_init(&buf, NULL, 16); wget_buffer_deinit(&buf); // testing buffer on stack, using initial heap memory // with resizing wget_buffer_init(&buf, NULL, 16); _test_buffer(&buf, "Test 2"); wget_buffer_deinit(&buf); // testing buffer on heap, using initial stack memory // without resizing bufp = wget_buffer_init(NULL, sbuf, sizeof(sbuf)); wget_buffer_deinit(bufp); bufp = wget_buffer_init(NULL, sbuf, sizeof(sbuf)); wget_buffer_free(&bufp); // testing buffer on heap, using initial stack memory // with resizing bufp = wget_buffer_init(NULL, sbuf, sizeof(sbuf)); _test_buffer(bufp, "Test 3"); wget_buffer_deinit(bufp); bufp = wget_buffer_init(NULL, sbuf, sizeof(sbuf)); _test_buffer(bufp, "Test 4"); wget_buffer_free(&bufp); // testing buffer on heap, using initial heap memory // without resizing bufp = wget_buffer_alloc(16); wget_buffer_free(&bufp); // testing buffer on heap, using initial heap memory // with resizing bufp = wget_buffer_alloc(16); _test_buffer(bufp, "Test 5"); wget_buffer_free(&bufp); // check that appending works wget_buffer_init(&buf, sbuf, sizeof(sbuf)); wget_buffer_strcpy(&buf, "A"); wget_buffer_strcat(&buf, "B"); wget_buffer_memcat(&buf, "C", 1); wget_buffer_memset_append(&buf, 'D', 1); wget_buffer_printf_append(&buf, "%s", "E"); if (!strcmp(buf.data, "ABCDE")) ok++; else { failed++; info_printf("test_buffer.append: got %s (expected %s)\n", buf.data, "ABCDE"); } wget_buffer_deinit(&buf); // test wget_buffer_trim() wget_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++) { wget_buffer_printf(&buf, "%.*sx%.*sy%.*s", lead_ws, " ", mid_ws, " ", trail_ws, " "); wget_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); } } } } wget_buffer_deinit(&buf); }