add new read-write test

This commit is contained in:
Martin Schröder 2016-08-30 20:37:21 +02:00
parent 3ddcade7c0
commit feca41ac8c
8 changed files with 133 additions and 23 deletions

View file

@ -320,7 +320,7 @@ top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
lib_LTLIBRARIES = libblobpack.la
libblobpack_la_SOURCES = blob.c blob_field.c blob_json.c ujsondec.c ujsonenc.c utils.c
libblobpack_la_CFLAGS = -std=gnu99 -Wall -Werror
libblobpack_la_CFLAGS = $(CODE_COVERAGE_CFLAGS) -std=gnu99 -Wall -Werror
all: all-am
.SUFFIXES:
@ -682,6 +682,7 @@ uninstall-am: uninstall-libLTLIBRARIES
mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \
tags tags-am uninstall uninstall-am uninstall-libLTLIBRARIES
@CODE_COVERAGE_RULES@
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.

View file

@ -248,21 +248,11 @@ struct blob_field *blob_put_real(struct blob *buf, double value){
struct blob_field *blob_put_attr(struct blob *buf, struct blob_field *attr){
if(!attr) return NULL;
struct blob_field *head = blob_head(buf);
int cur_len = blob_field_raw_pad_len(head);
int attr_pad_len = blob_field_raw_pad_len(attr);
int req_len = cur_len + attr_pad_len;
if (!blob_resize(buf, req_len))
return NULL;
struct blob_field *ret = (struct blob_field*)(buf->buf + cur_len);
memcpy(ret, attr, attr_pad_len);
blob_field_set_raw_len(blob_head(buf), req_len);
return ret;
size_t s = blob_field_data_len(attr);
struct blob_field *f = blob_new_attr(buf, blob_field_type(attr), s);
memcpy(f, attr, blob_field_raw_pad_len(attr));
return f;
}
static void __attribute__((unused)) _blob_field_dump(struct blob_field *node, int indent){

View file

@ -120,7 +120,9 @@ struct blob_field *blob_put_bool(struct blob *buf, bool val);
struct blob_field *blob_put_string(struct blob *buf, const char *str);
//! write binary data into the buffer
struct blob_field *blob_put_binary(struct blob *buf, const void *data, unsigned int size);
// NOTE: binary no longer supported because it is not representable in json and also because for binary data we actually need a whole new field type since we need to use size and our blob size is always padded.
// for binary use an array with integers instead
//struct blob_field *blob_put_binary(struct blob *buf, const void *data, unsigned int size);
//! write a number into the buffer
struct blob_field *blob_put_int(struct blob *buf, long long val);

View file

@ -240,11 +240,15 @@ blob_field_get_string(const struct blob_field *attr){
return attr->data;
}
void
blob_field_get_raw(const struct blob_field *attr, uint8_t *data, size_t data_size){
/*
size_t blob_field_get_raw(const struct blob_field *attr, uint8_t *data, size_t data_size){
assert(attr);
size_t s = blob_field_size(attr);
if(data_size > s) data_size = s;
memcpy(data, attr->data, data_size);
return data_size;
}
*/
//! returns the type of the attribute
unsigned int blob_field_type(const struct blob_field *attr){

View file

@ -42,7 +42,7 @@ bool blob_field_get_bool(const struct blob_field *self);
long long int blob_field_get_int(const struct blob_field *self);
double blob_field_get_real(const struct blob_field *self);
const char *blob_field_get_string(const struct blob_field *self);
//size_t blob_field_get_binary(const struct blob_field *attr, uint8_t *data, size_t data_size);
/*
uint8_t blob_field_get_u8(const struct blob_field *attr);
uint8_t blob_field_set_u8(const struct blob_field *attr, uint8_t val);

View file

@ -1,6 +1,9 @@
@CODE_COVERAGE_RULES@
check_PROGRAMS=random
check_PROGRAMS=random read-write
random_SOURCES=random.c
random_CFLAGS=$(CODE_COVERAGE_CFLAGS) -I../src/ -std=c99
random_LDFLAGS=$(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
read_write_SOURCES=read-write.c
read_write_CFLAGS=$(CODE_COVERAGE_CFLAGS) -I../src/ -D_GNU_SOURCE -std=c99
read_write_LDFLAGS=$(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
TESTS=$(check_PROGRAMS)

View file

@ -77,7 +77,7 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
check_PROGRAMS = random$(EXEEXT)
check_PROGRAMS = random$(EXEEXT) read-write$(EXEEXT)
subdir = test
DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \
$(top_srcdir)/config/depcomp $(top_srcdir)/config/test-driver
@ -101,6 +101,12 @@ am__v_lt_1 =
random_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(random_CFLAGS) $(CFLAGS) \
$(random_LDFLAGS) $(LDFLAGS) -o $@
am_read_write_OBJECTS = read_write-read-write.$(OBJEXT)
read_write_OBJECTS = $(am_read_write_OBJECTS)
read_write_LDADD = $(LDADD)
read_write_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(read_write_CFLAGS) \
$(CFLAGS) $(read_write_LDFLAGS) $(LDFLAGS) -o $@
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
am__v_P_0 = false
@ -135,8 +141,8 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
am__v_CCLD_0 = @echo " CCLD " $@;
am__v_CCLD_1 =
SOURCES = $(random_SOURCES)
DIST_SOURCES = $(random_SOURCES)
SOURCES = $(random_SOURCES) $(read_write_SOURCES)
DIST_SOURCES = $(random_SOURCES) $(read_write_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -492,6 +498,9 @@ top_srcdir = @top_srcdir@
random_SOURCES = random.c
random_CFLAGS = $(CODE_COVERAGE_CFLAGS) -I../src/ -std=c99
random_LDFLAGS = $(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
read_write_SOURCES = read-write.c
read_write_CFLAGS = $(CODE_COVERAGE_CFLAGS) -I../src/ -D_GNU_SOURCE -std=c99
read_write_LDFLAGS = $(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
TESTS = $(check_PROGRAMS)
all: all-am
@ -541,6 +550,10 @@ random$(EXEEXT): $(random_OBJECTS) $(random_DEPENDENCIES) $(EXTRA_random_DEPENDE
@rm -f random$(EXEEXT)
$(AM_V_CCLD)$(random_LINK) $(random_OBJECTS) $(random_LDADD) $(LIBS)
read-write$(EXEEXT): $(read_write_OBJECTS) $(read_write_DEPENDENCIES) $(EXTRA_read_write_DEPENDENCIES)
@rm -f read-write$(EXEEXT)
$(AM_V_CCLD)$(read_write_LINK) $(read_write_OBJECTS) $(read_write_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
@ -548,6 +561,7 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/random-random.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/read_write-read-write.Po@am__quote@
.c.o:
@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
@ -584,6 +598,20 @@ random-random.obj: random.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(random_CFLAGS) $(CFLAGS) -c -o random-random.obj `if test -f 'random.c'; then $(CYGPATH_W) 'random.c'; else $(CYGPATH_W) '$(srcdir)/random.c'; fi`
read_write-read-write.o: read-write.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(read_write_CFLAGS) $(CFLAGS) -MT read_write-read-write.o -MD -MP -MF $(DEPDIR)/read_write-read-write.Tpo -c -o read_write-read-write.o `test -f 'read-write.c' || echo '$(srcdir)/'`read-write.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/read_write-read-write.Tpo $(DEPDIR)/read_write-read-write.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='read-write.c' object='read_write-read-write.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(read_write_CFLAGS) $(CFLAGS) -c -o read_write-read-write.o `test -f 'read-write.c' || echo '$(srcdir)/'`read-write.c
read_write-read-write.obj: read-write.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(read_write_CFLAGS) $(CFLAGS) -MT read_write-read-write.obj -MD -MP -MF $(DEPDIR)/read_write-read-write.Tpo -c -o read_write-read-write.obj `if test -f 'read-write.c'; then $(CYGPATH_W) 'read-write.c'; else $(CYGPATH_W) '$(srcdir)/read-write.c'; fi`
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/read_write-read-write.Tpo $(DEPDIR)/read_write-read-write.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='read-write.c' object='read_write-read-write.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(read_write_CFLAGS) $(CFLAGS) -c -o read_write-read-write.obj `if test -f 'read-write.c'; then $(CYGPATH_W) 'read-write.c'; else $(CYGPATH_W) '$(srcdir)/read-write.c'; fi`
mostlyclean-libtool:
-rm -f *.lo
@ -790,6 +818,13 @@ random.log: random$(EXEEXT)
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
read-write.log: read-write$(EXEEXT)
@p='read-write$(EXEEXT)'; \
b='read-write'; \
$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
.test.log:
@p='$<'; \
$(am__set_b); \

75
test/read-write.c Normal file
View file

@ -0,0 +1,75 @@
#include "test-funcs.h"
#include <blobpack.h>
#include <stdbool.h>
#include <math.h>
#include <memory.h>
int main(){
struct blob blob;
blob_init(&blob, 0, 0);
char data_in[] = {1, 2, 3, 4, 5, 6, 7};
char data_out[sizeof(data_in)];
blob_put_bool(&blob, true);
blob_put_string(&blob, "foo");
blob_put_int(&blob, -13);
blob_put_real(&blob, M_PI);
struct blob_field *f = blob_put_string(&blob, "copyme");
blob_put_attr(&blob, f);
blob_offset_t o = blob_open_table(&blob);
blob_put_string(&blob, "one");
blob_put_int(&blob, 1);
blob_put_string(&blob, "two");
blob_put_int(&blob, 2);
blob_put_string(&blob, "three");
blob_put_int(&blob, 3);
blob_close_table(&blob, o);
o = blob_open_array(&blob);
blob_put_int(&blob, 1);
blob_put_int(&blob, 2);
blob_put_int(&blob, 3);
blob_close_array(&blob, o);
blob_dump(&blob);
struct blob_field *root = blob_head(&blob);
struct blob_field *child = blob_field_first_child(root);
TEST(blob_field_get_bool(child) == true);
TEST(strcmp(blob_field_get_string(child = blob_field_next_child(root, child)), "foo") == 0);
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == -13);
TEST(blob_field_get_real(child = blob_field_next_child(root, child)) == M_PI);
TEST(strcmp(blob_field_get_string(child = blob_field_next_child(root, child)), "copyme") == 0);
TEST(strcmp(blob_field_get_string(child = blob_field_next_child(root, child)), "copyme") == 0);
// iterate over the list
child = blob_field_next_child(root, child);
struct blob_field *list = child;
child = blob_field_first_child(child);
TEST(strcmp(blob_field_get_string(child), "one") == 0);
TEST(blob_field_get_int(child = blob_field_next_child(list, child)) == 1);
TEST(strcmp(blob_field_get_string(child = blob_field_next_child(list, child)), "two") == 0);
TEST(blob_field_get_int(child = blob_field_next_child(list, child)) == 2);
TEST(strcmp(blob_field_get_string(child = blob_field_next_child(list, child)), "three") == 0);
TEST(blob_field_get_int(child = blob_field_next_child(list, child)) == 3);
// make sure we correctly return null when no more children available
TEST(blob_field_next_child(list, child) == 0);
// test the array element
list = blob_field_next_child(root, list);
child = blob_field_first_child(list);
TEST(blob_field_get_int(child) == 1);
TEST(blob_field_get_int(child = blob_field_next_child(list, child)) == 2);
TEST(blob_field_get_int(child = blob_field_next_child(list, child)) == 3);
return 0;
}