Add test for gbuf
This commit is contained in:
@@ -26,7 +26,7 @@ struct GBuf {
|
||||
void *buf;
|
||||
};
|
||||
|
||||
/** Allocatea new GBuf.
|
||||
/** Allocate a new GBuf.
|
||||
*
|
||||
* @param initsz Intial capacity of the GBuf
|
||||
* @return New GBuf, or NULL if allocation fails,
|
||||
@@ -56,7 +56,18 @@ void uc_gbuf_unref(GBuf *buf);
|
||||
* @return 0 on success, -1 on failure when allocation fails if he buffer
|
||||
* needs to grow
|
||||
*/
|
||||
int uc_gbuf_append(GBuf *buf,void *data,size_t len);
|
||||
int uc_gbuf_append(GBuf *buf,const void *data,size_t len);
|
||||
|
||||
/** Do printf formatting on a GBuf.
|
||||
* The buffer will grow as needed.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
int uc_gbuf_printf(GBuf *buf, const char *fmt, ...);
|
||||
|
||||
/** Expand the capacity of the GBuf.
|
||||
*
|
||||
@@ -69,7 +80,7 @@ int uc_gbuf_grow(GBuf *buf, size_t addlen);
|
||||
* @param buff buffer
|
||||
* @return length of unused space
|
||||
*/
|
||||
size_t uc_gbuf_remaining(const GBuf *buf)
|
||||
static inline size_t uc_gbuf_remaining(const GBuf *buf)
|
||||
{
|
||||
return buf->len - buf->used;
|
||||
}
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ uc_gbuf_unref(GBuf *buf)
|
||||
}
|
||||
|
||||
int
|
||||
uc_gbuf_append(GBuf *buf,void *data,size_t len)
|
||||
uc_gbuf_append(GBuf *buf,const void *data,size_t len)
|
||||
{
|
||||
unsigned char *gbuf;
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <check.h>
|
||||
#include <string.h>
|
||||
#include <ucore/ucore_buffer.h>
|
||||
|
||||
|
||||
START_TEST (test_new_gbuf)
|
||||
GBuf *buf = uc_new_gbuf(11);
|
||||
|
||||
fail_if(buf == NULL);
|
||||
fail_if(buf->len != 11);
|
||||
fail_if(buf->ref_cnt != 1);
|
||||
fail_if(buf->buf == NULL);
|
||||
fail_if(buf->used != 0);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_gbuf_append1)
|
||||
const char *data = "lorum_ ipson 1234567890";
|
||||
int rc;
|
||||
GBuf *buf = uc_new_gbuf(11);
|
||||
|
||||
fail_if(buf == NULL);
|
||||
|
||||
rc = uc_gbuf_append(buf, data, strlen(data) + 1);
|
||||
fail_if(rc != 0);
|
||||
fail_if(buf->used != strlen(data) + 1);
|
||||
fail_if(strcmp(data, buf->buf) != 0);
|
||||
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite *buffer_suite(void)
|
||||
{
|
||||
Suite *s = suite_create("buffer");
|
||||
TCase *tc = tcase_create("buffer (GBuf) tests");
|
||||
tcase_add_test(tc, test_new_gbuf);
|
||||
tcase_add_test(tc, test_gbuf_append1);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ extern Suite *read_file_suite(void);
|
||||
extern Suite *pack_suite(void);
|
||||
extern Suite *logging_suite(void);
|
||||
extern Suite *threadqueue_suite(void);
|
||||
extern Suite *buffer_suite(void);
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
@@ -31,6 +32,7 @@ main (int argc, char *argv[])
|
||||
srunner_add_suite(sr, pack_suite());
|
||||
srunner_add_suite(sr, logging_suite());
|
||||
srunner_add_suite(sr, threadqueue_suite());
|
||||
srunner_add_suite(sr, buffer_suite());
|
||||
|
||||
|
||||
srunner_run_all (sr, CK_VERBOSE);
|
||||
|
||||
Reference in New Issue
Block a user