add parse test and remove code that no longer makes sense to have in the library

- improve coverage
This commit is contained in:
Martin Schröder 2016-08-30 22:34:27 +02:00
parent 47ab829334
commit 7a997fc419
8 changed files with 137 additions and 28 deletions

View file

@ -141,7 +141,7 @@ static struct blob_field *blob_new_attr(struct blob *buf, int id, int payload){
return attr;
}
/*
struct blob_field *blob_put_binary(struct blob *buf, const void *ptr, unsigned int len){
assert(ptr);
struct blob_field *attr;
@ -156,7 +156,7 @@ struct blob_field *blob_put_binary(struct blob *buf, const void *ptr, unsigned i
memcpy(attr->data, ptr, len);
return attr;
}
*/
static struct blob_field *blob_put(struct blob *buf, int id, const void *ptr, unsigned int len){
struct blob_field *attr;
@ -310,6 +310,6 @@ void blob_dump(struct blob *self){
printf("size: %d, memory: %d\n", (int)blob_size(self), (int)self->memlen);
for(int c = 0; c < 8; c++) printf("%02x ", ((char*)self->buf)[c] & 0xff);
printf("\n");
_blob_field_dump(blob_head(self), 0);
blob_field_dump(blob_head(self));
printf("============================\n");
}

View file

@ -34,7 +34,7 @@ void blob_field_set_raw_len(struct blob_field *attr, unsigned int len){
attr->id_len |= cpu_to_be32(len);
}
/*
bool blob_field_check_type(const void *ptr, unsigned int len, int type){
const char *data = ptr;
@ -54,6 +54,7 @@ bool blob_field_check_type(const void *ptr, unsigned int len, int type){
return true;
}
*/
bool
blob_field_equal(const struct blob_field *a1, const struct blob_field *a2){
@ -78,7 +79,7 @@ void *blob_field_data(const struct blob_field *attr){
return (void *) attr->data;
}
/*
struct blob_field *blob_field_copy(struct blob_field *attr){
if(!attr) return NULL;
struct blob_field *ret;
@ -91,46 +92,47 @@ struct blob_field *blob_field_copy(struct blob_field *attr){
memcpy(ret, attr, size);
return ret;
}
*/
uint8_t
blob_field_get_u8(const struct blob_field *attr){
assert(attr);
return *((uint8_t *) attr->data);
}
/*
void blob_field_set_u8(const struct blob_field *attr, uint8_t val){
if(!attr) return;
*((uint8_t *) attr->data) = val;
}
*/
uint16_t
blob_field_get_u16(const struct blob_field *attr){
assert(attr);
uint16_t *tmp = (uint16_t*)attr->data;
return be16_to_cpu(*tmp);
}
/*
void
blob_field_set_u16(const struct blob_field *attr, uint16_t val){
if(!attr) return;
uint16_t *tmp = (uint16_t*)attr->data;
*tmp = cpu_to_be16(val);
}
*/
uint32_t
blob_field_get_u32(const struct blob_field *attr){
assert(attr);
uint32_t *tmp = (uint32_t*)attr->data;
return be32_to_cpu(*tmp);
}
/*
void
blob_field_set_u32(const struct blob_field *attr, uint32_t val){
if(!attr) return;
uint32_t *tmp = (uint32_t*)attr->data;
*tmp = cpu_to_be32(val);
}
*/
uint64_t
blob_field_get_u64(const struct blob_field *attr){
assert(attr);
@ -145,37 +147,37 @@ blob_field_get_i8(const struct blob_field *attr){
assert(attr);
return blob_field_get_u8(attr);
}
/*
void
blob_field_set_i8(const struct blob_field *attr, int8_t val){
if(!attr) return;
blob_field_set_u8(attr, val);
}
*/
int16_t
blob_field_get_i16(const struct blob_field *attr){
assert(attr);
return blob_field_get_u16(attr);
}
/*
void
blob_field_set_i16(const struct blob_field *attr, int16_t val){
if(!attr) return;
blob_field_set_u16(attr, val);
}
*/
int32_t
blob_field_get_i32(const struct blob_field *attr){
assert(attr);
return blob_field_get_u32(attr);
}
/*
void
blob_field_set_i32(const struct blob_field *attr, int32_t val){
if(!attr) return;
blob_field_set_u32(attr, val);
}
*/
int64_t
blob_field_get_i64(const struct blob_field *attr){
assert(attr);
@ -200,6 +202,7 @@ long long blob_field_get_int(const struct blob_field *self){
case BLOB_FIELD_INT8: return blob_field_get_i8(self);
case BLOB_FIELD_INT16: return blob_field_get_i16(self);
case BLOB_FIELD_INT32: return blob_field_get_i32(self);
case BLOB_FIELD_INT64: return blob_field_get_i64(self);
case BLOB_FIELD_FLOAT32: return blob_field_get_f32(self);
case BLOB_FIELD_FLOAT64: return blob_field_get_f64(self);
case BLOB_FIELD_STRING: {
@ -218,6 +221,7 @@ double blob_field_get_real(const struct blob_field *self){
case BLOB_FIELD_INT8: return blob_field_get_i8(self);
case BLOB_FIELD_INT16: return blob_field_get_i16(self);
case BLOB_FIELD_INT32: return blob_field_get_i32(self);
case BLOB_FIELD_INT64: return blob_field_get_i64(self);
case BLOB_FIELD_FLOAT32: return blob_field_get_f32(self);
case BLOB_FIELD_FLOAT64: return blob_field_get_f64(self);
case BLOB_FIELD_STRING: {
@ -256,14 +260,14 @@ unsigned int blob_field_type(const struct blob_field *attr){
int id = (be32_to_cpu(attr->id_len) & BLOB_FIELD_ID_MASK) >> BLOB_FIELD_ID_SHIFT;
return id;
}
/*
void blob_field_set_type(struct blob_field *self, int type){
assert(self);
int id_len = be32_to_cpu(self->id_len);
id_len = (id_len & ~BLOB_FIELD_ID_MASK) | (type << BLOB_FIELD_ID_SHIFT);
self->id_len = cpu_to_be32(id_len);
}
*/
//! returns full length of attribute
unsigned int
blob_field_raw_len(const struct blob_field *attr){

View file

@ -78,7 +78,7 @@ long double unpack754(uint64_t i, unsigned bits, unsigned expbits){
return result;
}
/*
void *__calloc_a(size_t len, ...)
{
va_list ap, ap1;
@ -105,7 +105,8 @@ void *__calloc_a(size_t len, ...)
return ret;
}
*/
#if 0
#ifdef __APPLE__
#include <mach/mach_host.h> /* host_get_clock_service() */
#include <mach/mach_port.h> /* mach_port_deallocate() */
@ -152,5 +153,5 @@ int clock_gettime(int type, struct timespec *tv)
out:
return retval;
}
#endif
#endif

View file

@ -1,5 +1,5 @@
@CODE_COVERAGE_RULES@
check_PROGRAMS=random read-write json
check_PROGRAMS=random read-write json parse
random_SOURCES=random.c
random_CFLAGS=$(CODE_COVERAGE_CFLAGS) -I../src/ -std=c99
random_LDFLAGS=$(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
@ -9,4 +9,7 @@ read_write_LDFLAGS=$(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
json_SOURCES=json.c
json_CFLAGS=$(CODE_COVERAGE_CFLAGS) -I../src/ -D_GNU_SOURCE -std=c99
json_LDFLAGS=$(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
parse_SOURCES=parse.c
parse_CFLAGS=$(CODE_COVERAGE_CFLAGS) -I../src/ -D_GNU_SOURCE -std=c99
parse_LDFLAGS=$(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
TESTS=$(check_PROGRAMS)

View file

@ -77,7 +77,8 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
check_PROGRAMS = random$(EXEEXT) read-write$(EXEEXT) json$(EXEEXT)
check_PROGRAMS = random$(EXEEXT) read-write$(EXEEXT) json$(EXEEXT) \
parse$(EXEEXT)
subdir = test
DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \
$(top_srcdir)/config/depcomp $(top_srcdir)/config/test-driver
@ -101,6 +102,12 @@ am__v_lt_1 =
json_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(json_CFLAGS) $(CFLAGS) \
$(json_LDFLAGS) $(LDFLAGS) -o $@
am_parse_OBJECTS = parse-parse.$(OBJEXT)
parse_OBJECTS = $(am_parse_OBJECTS)
parse_LDADD = $(LDADD)
parse_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(parse_CFLAGS) $(CFLAGS) \
$(parse_LDFLAGS) $(LDFLAGS) -o $@
am_random_OBJECTS = random-random.$(OBJEXT)
random_OBJECTS = $(am_random_OBJECTS)
random_LDADD = $(LDADD)
@ -147,8 +154,10 @@ 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 = $(json_SOURCES) $(random_SOURCES) $(read_write_SOURCES)
DIST_SOURCES = $(json_SOURCES) $(random_SOURCES) $(read_write_SOURCES)
SOURCES = $(json_SOURCES) $(parse_SOURCES) $(random_SOURCES) \
$(read_write_SOURCES)
DIST_SOURCES = $(json_SOURCES) $(parse_SOURCES) $(random_SOURCES) \
$(read_write_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -510,6 +519,9 @@ read_write_LDFLAGS = $(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
json_SOURCES = json.c
json_CFLAGS = $(CODE_COVERAGE_CFLAGS) -I../src/ -D_GNU_SOURCE -std=c99
json_LDFLAGS = $(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
parse_SOURCES = parse.c
parse_CFLAGS = $(CODE_COVERAGE_CFLAGS) -I../src/ -D_GNU_SOURCE -std=c99
parse_LDFLAGS = $(CODE_COVERAGE_LDFLAGS) -L../src/.libs/ -lblobpack -lm
TESTS = $(check_PROGRAMS)
all: all-am
@ -559,6 +571,10 @@ json$(EXEEXT): $(json_OBJECTS) $(json_DEPENDENCIES) $(EXTRA_json_DEPENDENCIES)
@rm -f json$(EXEEXT)
$(AM_V_CCLD)$(json_LINK) $(json_OBJECTS) $(json_LDADD) $(LIBS)
parse$(EXEEXT): $(parse_OBJECTS) $(parse_DEPENDENCIES) $(EXTRA_parse_DEPENDENCIES)
@rm -f parse$(EXEEXT)
$(AM_V_CCLD)$(parse_LINK) $(parse_OBJECTS) $(parse_LDADD) $(LIBS)
random$(EXEEXT): $(random_OBJECTS) $(random_DEPENDENCIES) $(EXTRA_random_DEPENDENCIES)
@rm -f random$(EXEEXT)
$(AM_V_CCLD)$(random_LINK) $(random_OBJECTS) $(random_LDADD) $(LIBS)
@ -574,6 +590,7 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/json-json.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parse-parse.Po@am__quote@
@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@
@ -612,6 +629,20 @@ json-json.obj: json.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) $(json_CFLAGS) $(CFLAGS) -c -o json-json.obj `if test -f 'json.c'; then $(CYGPATH_W) 'json.c'; else $(CYGPATH_W) '$(srcdir)/json.c'; fi`
parse-parse.o: parse.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(parse_CFLAGS) $(CFLAGS) -MT parse-parse.o -MD -MP -MF $(DEPDIR)/parse-parse.Tpo -c -o parse-parse.o `test -f 'parse.c' || echo '$(srcdir)/'`parse.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/parse-parse.Tpo $(DEPDIR)/parse-parse.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='parse.c' object='parse-parse.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) $(parse_CFLAGS) $(CFLAGS) -c -o parse-parse.o `test -f 'parse.c' || echo '$(srcdir)/'`parse.c
parse-parse.obj: parse.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(parse_CFLAGS) $(CFLAGS) -MT parse-parse.obj -MD -MP -MF $(DEPDIR)/parse-parse.Tpo -c -o parse-parse.obj `if test -f 'parse.c'; then $(CYGPATH_W) 'parse.c'; else $(CYGPATH_W) '$(srcdir)/parse.c'; fi`
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/parse-parse.Tpo $(DEPDIR)/parse-parse.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='parse.c' object='parse-parse.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) $(parse_CFLAGS) $(CFLAGS) -c -o parse-parse.obj `if test -f 'parse.c'; then $(CYGPATH_W) 'parse.c'; else $(CYGPATH_W) '$(srcdir)/parse.c'; fi`
random-random.o: random.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(random_CFLAGS) $(CFLAGS) -MT random-random.o -MD -MP -MF $(DEPDIR)/random-random.Tpo -c -o random-random.o `test -f 'random.c' || echo '$(srcdir)/'`random.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/random-random.Tpo $(DEPDIR)/random-random.Po
@ -860,6 +891,13 @@ json.log: json$(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)
parse.log: parse$(EXEEXT)
@p='parse$(EXEEXT)'; \
b='parse'; \
$(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); \

29
test/parse.c Normal file
View file

@ -0,0 +1,29 @@
#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);
blob_put_bool(&blob, true);
blob_put_string(&blob, "foo");
blob_put_int(&blob, -13);
blob_put_real(&blob, M_PI);
struct blob_field *out[4];
blob_dump_json(&blob);
TEST(blob_field_parse(blob_head(&blob), "isif", out, 4));
TEST(blob_field_get_bool(out[0]) == true);
TEST(strcmp(blob_field_get_string(out[1]), "foo") == 0);
TEST(blob_field_get_int(out[2]) == -13);
TEST(is_equal(blob_field_get_real(out[3]), M_PI));
return 0;
}

View file

@ -13,10 +13,23 @@ int main(){
blob_put_bool(&blob, true);
blob_put_string(&blob, "foo");
blob_put_int(&blob, -13);
blob_put_int(&blob, 100);
blob_put_int(&blob, 200);
blob_put_int(&blob, 1000);
blob_put_int(&blob, 70000);
blob_put_string(&blob, "70000");
blob_put_real(&blob, 70000.0f);
blob_put_int(&blob, 100);
blob_put_int(&blob, 200);
blob_put_int(&blob, 1000);
blob_put_string(&blob, "3.14");
blob_put_int(&blob, 5000000000lu);
blob_put_real(&blob, M_PI);
struct blob_field *f = blob_put_string(&blob, "copyme");
blob_put_attr(&blob, f);
struct blob_field *f2 = blob_put_attr(&blob, f);
TEST(blob_field_equal(f, f2));
blob_offset_t o = blob_open_table(&blob);
blob_put_string(&blob, "one");
@ -41,7 +54,18 @@ int main(){
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(blob_field_get_int(child = blob_field_next_child(root, child)) == 100);
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == 200);
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == 1000);
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == 70000);
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == 70000);
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == 70000);
TEST(blob_field_get_real(child = blob_field_next_child(root, child)) == 100);
TEST(blob_field_get_real(child = blob_field_next_child(root, child)) == 200);
TEST(blob_field_get_real(child = blob_field_next_child(root, child)) == 1000);
TEST(is_equal(blob_field_get_real(child = blob_field_next_child(root, child)), 3.14f));
TEST(blob_field_get_int(child = blob_field_next_child(root, child)) == 5000000000lu);
TEST(is_equal(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);

View file

@ -1,4 +1,14 @@
#pragma once
#include <math.h>
#ifndef FLT_EPSILON
#define FLT_EPSILON 1e-5
#endif
static inline unsigned char is_equal(float a, float b){
return fabsf(a - b) < FLT_EPSILON;
}
#define STR(x) #x
#define TEST(x) if(!(x)){ printf("test failed at %d, %s: %s\n", __LINE__, __FILE__, STR(x)); exit(-1); } else { printf("[OK] %s\n", STR(x)); }