Fix and clarify gnuf_printf
This commit is contained in:
@@ -59,13 +59,16 @@ void uc_gbuf_unref(GBuf *buf);
|
|||||||
int uc_gbuf_append(GBuf *buf,const void *data,size_t len);
|
int uc_gbuf_append(GBuf *buf,const void *data,size_t len);
|
||||||
|
|
||||||
/** Do printf formatting on a GBuf.
|
/** Do printf formatting on a GBuf.
|
||||||
* The buffer will grow as needed.
|
* The buffer will grow as needed,
|
||||||
|
* The buffer ensures that the added string will be nul terminated,
|
||||||
|
* however the nul terminator is not counted in the ->used member.
|
||||||
*
|
*
|
||||||
* @param buf The GBuf
|
* @param buf The GBuf
|
||||||
* @param fmt printf stype formatting string
|
* @param fmt printf stype formatting string
|
||||||
* @param ... format arguments
|
* @param ... format arguments
|
||||||
*
|
*
|
||||||
* @return The number of chars written, or negative if an error occurs
|
* @return The number of chars written excluding the terminating nul
|
||||||
|
* character , or negative if an error occurs
|
||||||
*/
|
*/
|
||||||
int uc_gbuf_printf(GBuf *buf, const char *fmt, ...);
|
int uc_gbuf_printf(GBuf *buf, const char *fmt, ...);
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -90,8 +90,9 @@ int uc_gbuf_printf(GBuf *buf, const char *fmt, ...)
|
|||||||
|
|
||||||
} while(rc >= remaining);
|
} while(rc >= remaining);
|
||||||
|
|
||||||
if(rc > 0)
|
if(rc > 0) {
|
||||||
buf->used += rc;
|
buf->used += rc;
|
||||||
|
}
|
||||||
|
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,22 @@ START_TEST (test_new_gbuf)
|
|||||||
fail_if(buf->ref_cnt != 1);
|
fail_if(buf->ref_cnt != 1);
|
||||||
fail_if(buf->buf == NULL);
|
fail_if(buf->buf == NULL);
|
||||||
fail_if(buf->used != 0);
|
fail_if(buf->used != 0);
|
||||||
|
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_gbuf_grow)
|
||||||
|
GBuf *buf = uc_new_gbuf(33);
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
fail_if(buf == NULL);
|
||||||
|
fail_if(buf->len != 33);
|
||||||
|
|
||||||
|
rc = uc_gbuf_grow(buf, 128);
|
||||||
|
|
||||||
|
fail_if(rc != 0);
|
||||||
|
fail_if(buf->len < 128 + 33);
|
||||||
|
fail_if(buf->used != 0);
|
||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_gbuf_append1)
|
START_TEST (test_gbuf_append1)
|
||||||
@@ -27,6 +43,32 @@ START_TEST (test_gbuf_append1)
|
|||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_gbuf_printf1)
|
||||||
|
int rc;
|
||||||
|
GBuf *buf = uc_new_gbuf(10);
|
||||||
|
|
||||||
|
fail_if(buf == NULL);
|
||||||
|
|
||||||
|
rc = uc_gbuf_printf(buf, "test %d", 1);
|
||||||
|
fail_if(rc != 6, "rc was %d", rc);
|
||||||
|
fail_if(buf->used != 6, "buf->used was %d", buf->used);
|
||||||
|
fail_if(strcmp("test 1", buf->buf) != 0);
|
||||||
|
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_gbuf_printf2)
|
||||||
|
int rc;
|
||||||
|
GBuf *buf = uc_new_gbuf(8);
|
||||||
|
|
||||||
|
fail_if(buf == NULL);
|
||||||
|
|
||||||
|
rc = uc_gbuf_printf(buf, "test %d test %d test %d", 1, 2, 3);
|
||||||
|
fail_if(rc != 20, "rc was %d", rc);
|
||||||
|
fail_if(buf->used != 20, "buf->used was %d", buf->used);
|
||||||
|
fail_if(strcmp("test 1 test 2 test 3", buf->buf) != 0);
|
||||||
|
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
|
||||||
Suite *buffer_suite(void)
|
Suite *buffer_suite(void)
|
||||||
{
|
{
|
||||||
@@ -34,6 +76,9 @@ Suite *buffer_suite(void)
|
|||||||
TCase *tc = tcase_create("buffer (GBuf) tests");
|
TCase *tc = tcase_create("buffer (GBuf) tests");
|
||||||
tcase_add_test(tc, test_new_gbuf);
|
tcase_add_test(tc, test_new_gbuf);
|
||||||
tcase_add_test(tc, test_gbuf_append1);
|
tcase_add_test(tc, test_gbuf_append1);
|
||||||
|
tcase_add_test(tc, test_gbuf_grow);
|
||||||
|
tcase_add_test(tc, test_gbuf_printf1);
|
||||||
|
tcase_add_test(tc, test_gbuf_printf2);
|
||||||
|
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user