Fix and clarify gnuf_printf

This commit is contained in:
Nils O. Selåsdal
2012-11-18 14:04:57 +01:00
parent 65a6c67277
commit 9bf56c1756
3 changed files with 54 additions and 5 deletions
+5 -2
View File
@@ -59,13 +59,16 @@ void uc_gbuf_unref(GBuf *buf);
int uc_gbuf_append(GBuf *buf,const void *data,size_t len);
/** 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 fmt printf stype formatting string
* @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, ...);
+2 -1
View File
@@ -90,8 +90,9 @@ int uc_gbuf_printf(GBuf *buf, const char *fmt, ...)
} while(rc >= remaining);
if(rc > 0)
if(rc > 0) {
buf->used += rc;
}
va_end(ap);
+45
View File
@@ -11,6 +11,22 @@ START_TEST (test_new_gbuf)
fail_if(buf->ref_cnt != 1);
fail_if(buf->buf == NULL);
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
START_TEST (test_gbuf_append1)
@@ -27,6 +43,32 @@ START_TEST (test_gbuf_append1)
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)
{
@@ -34,6 +76,9 @@ Suite *buffer_suite(void)
TCase *tc = tcase_create("buffer (GBuf) tests");
tcase_add_test(tc, test_new_gbuf);
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);