diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 8 | ||||
-rw-r--r-- | test/test_add_elem.c | 16 | ||||
-rw-r--r-- | test/test_alltypes.c | 48 | ||||
-rw-r--r-- | test/test_key_value_store.c | 101 | ||||
-rw-r--r-- | test/test_multiple_write.c | 25 | ||||
-rw-r--r-- | test/test_save.c | 44 |
6 files changed, 157 insertions, 85 deletions
diff --git a/test/Makefile b/test/Makefile index c4f4b28..c70c7f0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,11 +1,15 @@ CC=gcc -CFLAGS=-I../ -lnetbytes -Wl,-rpath=../ -L../ +CFLAGS=-g3 -I../ -lnetbytes -Wl,-rpath=../ -L../ LDFLAGS= -SOURCE=test_save test_multiple_write test_multiple_read test_add_elem test_alltypes +SOURCE=test_save test_multiple_write test_multiple_read test_add_elem test_alltypes \ +test_key_value_store test_key_value_load SOURCES=$(SOURCE:=.c) make: $(SOURCE) +leak: + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test_key_value_store + clean: rm -f $(SOURCE)
\ No newline at end of file diff --git a/test/test_add_elem.c b/test/test_add_elem.c index 5244880..7a789d1 100644 --- a/test/test_add_elem.c +++ b/test/test_add_elem.c @@ -66,10 +66,10 @@ int main() nb_u8 id; nb_u8arr name; - netbyte_store nb; + netbyte_store *nb=malloc(sizeof(netbyte_store)); printf("Start test\n"); - nb_init( &nb ); + nb_init( nb ); printf("Your name : "); fflush( stdin ); n = fgets( str, STR_SIZE, stdin ); @@ -82,21 +82,23 @@ int main() printf("er create u8arr: %d\n",er); - er = nb_add_u8( &nb, &id ); + er = nb_add_u8( nb, &id ); if (er) printf("er add u8: %d\n",er); - er = nb_add_u8arr( &nb, &name ); + er = nb_add_u8arr( nb, &name ); if (er) printf("er add u8arr: %d\n",er); - pr_store( &nb ); - res = nb_create( &nb ); + pr_store( nb ); + res = nb_create( nb ); f = fopen("test_many.nb","a"); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); + free(res); + nb_free(nb); printf("End test\n"); return 0; diff --git a/test/test_alltypes.c b/test/test_alltypes.c index 2975d2a..b6eb801 100644 --- a/test/test_alltypes.c +++ b/test/test_alltypes.c @@ -104,8 +104,7 @@ int main() { FILE *f=NULL; int er; - netbyte_store nb; - netbyte_store nb2; + netbyte_store *nb = malloc(sizeof(netbyte_store)); nb_u8 u8; nb_u8arr u8arr; nb_u16 u16; @@ -120,7 +119,7 @@ int main() printf("Start test\n"); - nb_init( &nb ); + nb_init( nb ); er = nb_u8_create( &u8, 0x10 ); if (er) printf("er create u8: %d\n",er); @@ -142,8 +141,8 @@ int main() pr_u8arr( &u8arr ); #if 1 - pr_store( &nb ); - er = nb_add_u8( &nb, &u8 ); + pr_store( nb ); + er = nb_add_u8( nb, &u8 ); if (er) printf("er add u8: %d\n",er); #endif @@ -156,54 +155,39 @@ int main() #endif #if 1 - pr_store( &nb ); - er = nb_add_u16( &nb, &u16 ); + pr_store( nb ); + er = nb_add_u16( nb, &u16 ); if (er) printf("er add u16: %d\n",er); #endif #if 1 - pr_store( &nb ); - er = nb_add_u32( &nb, &u32 ); + pr_store( nb ); + er = nb_add_u32( nb, &u32 ); if (er) printf("er add u32: %d\n",er); #endif #if 1 - pr_store( &nb ); - er = nb_add_u64( &nb, &u64 ); + pr_store( nb ); + er = nb_add_u64( nb, &u64 ); if (er) printf("er add u64: %d\n",er); #endif - pr_store( &nb ); - res = nb_create( &nb ); - pr_store( &nb ); + pr_store( nb ); + res = nb_create( nb ); + pr_store( nb ); printf("res 0x%x\n", res); f = fopen("test_alltypes.nb","w+"); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); - /* - printf("LOAD:-----\n"); - nb_init( &nb2 ); - pr_store( &nb2 ); - nb_load( &nb2, res ); - pr_store( &nb2 ); - - printf( "->1 [%s]\n", ((nb_u8arr *)nb2.types[1].nb_val)->val ); - - __NBT_TYPED *t=0xf8; - nb_u8arr *v; - nb_type( &nb2, 1, &t ); - printf("get type: %02x\n", t ); - nb_val( &nb2, 1, (uint8_t **)&v ); - printf("get value: %02x\n", v->val ); - */ - free( res ); + nb_free(nb); + printf("End test\n"); return 0; diff --git a/test/test_key_value_store.c b/test/test_key_value_store.c index 983b3b8..b499c9a 100644 --- a/test/test_key_value_store.c +++ b/test/test_key_value_store.c @@ -3,6 +3,38 @@ #include <netbytes.h> +char *alloc_new_str_s(char *str, size_t size) +{ + char *ret = NULL; + + if (str == NULL) + { + return NULL; + } + + //1MB is enought + if (size > (1024*1024)) + { + return NULL; + } + + ret = malloc(size+1); //extra for 1 zero at then end + if (ret == NULL) + { + return NULL; + } + + memcpy(ret, str, size); + ret[size] = 0; //add zero at the end + + return ret; +} + +char *alloc_new_str(char *str) +{ + return alloc_new_str_s(str, strlen(str)); +} + typedef struct kv_user_name { int id; @@ -12,17 +44,51 @@ typedef struct kv_user_name kv_user_name* kv_new(int id, char *name, char *desc) { - + kv_user_name *ret = NULL; + + ret = malloc(sizeof(kv_user_name)); + if (!ret) + { + return NULL; + } + + ret->id = id; + ret->name = alloc_new_str(name); + ret->desc = alloc_new_str(desc); + + return ret; } netbyte_store* kv_marsh(kv_user_name *kv) { - return NULL; + netbyte_store *nb=NULL; + nb_u8 nb_id; + nb_u8arr nb_name, nb_desc; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + nb_u8_create(&nb_id, kv->id); + nb_add_u8(nb, &nb_id); + nb_u8arr_create(&nb_name, strlen(kv->name), kv->name); + nb_add_u8arr(nb, &nb_name); + nb_u8arr_create(&nb_desc, strlen(kv->desc), kv->desc); + nb_add_u8arr(nb, &nb_desc); + + nb_print(nb); + nb_print(nb); + + return nb; } -void kv_free() +void kv_free(kv_user_name *kv) { - + if (kv!=NULL) + { + free(kv->name); + free(kv->desc); + free(kv); + } } @@ -34,19 +100,26 @@ int main() FILE *f; int i=0; - netbyte_store nb; + kv_user_name *kv=NULL; + + netbyte_store *nb; printf("Start test\n"); - nb_init( &nb ); - f = fopen("test_many.nb","r"); - while (nb_fread( &nb, fileno(f) ) == 0) - { - printf("ITER %d: ", i ); - pr_store( &nb ); - i += 1; - } - fclose( f ); + kv = kv_new(1,"Name","Name of the human"); + nb = kv_marsh(kv); + nb_print(nb); + kv_free(kv); + + //nb_print(nb); + res = nb_create(nb); + + f = fopen("test_kv_save.nd","w+"); + fwrite(res, 1, nb->size, f); + fclose(f); + + free(res); + nb_free(nb); printf("End test\n"); return 0; diff --git a/test/test_multiple_write.c b/test/test_multiple_write.c index 53d7cae..9e23185 100644 --- a/test/test_multiple_write.c +++ b/test/test_multiple_write.c @@ -64,10 +64,10 @@ int main() uint8_t *res; FILE *f; - netbyte_store nb; + netbyte_store *nb=malloc(sizeof(netbyte_store)); nb_u8arr u8arr; - nb_init( &nb ); + nb_init( nb ); er = nb_u8arr_create( &u8arr, len, &str[0] ); if (er) @@ -75,22 +75,25 @@ int main() pr_u8arr( &u8arr ); - er = nb_add_u8arr( &nb, &u8arr ); + er = nb_add_u8arr( nb, &u8arr ); if (er) printf("er add u8arr: %d\n",er); - pr_store( &nb ); - res = nb_create( &nb ); - pr_store( &nb ); + pr_store( nb ); + res = nb_create( nb ); + pr_store( nb ); f = fopen("test_many.nb","w+"); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); + free(res); + nb_free(nb); + printf("End test\n"); return 0; }
\ No newline at end of file diff --git a/test/test_save.c b/test/test_save.c index 6e3be63..6c1f0ae 100644 --- a/test/test_save.c +++ b/test/test_save.c @@ -59,8 +59,8 @@ int main() { FILE *f=NULL; int er; - netbyte_store nb; - netbyte_store nb2; + netbyte_store *nb=malloc(sizeof(netbyte_store)); + netbyte_store *nb2=malloc(sizeof(netbyte_store)); nb_u8 u8; nb_u8arr u8arr; const int len = 11; @@ -69,7 +69,7 @@ int main() printf("Start test\n"); - nb_init( &nb ); + nb_init( nb ); er = nb_u8_create( &u8, 0x10 ); if (er) printf("er create u8: %d\n",er); @@ -82,45 +82,51 @@ int main() pr_u8arr( &u8arr ); #if 1 - pr_store( &nb ); - er = nb_add_u8( &nb, &u8 ); + pr_store( nb ); + er = nb_add_u8( nb, &u8 ); if (er) printf("er add u8: %d\n",er); #endif #if 1 - pr_store( &nb ); - er = nb_add_u8arr( &nb, &u8arr ); + pr_store( nb ); + er = nb_add_u8arr( nb, &u8arr ); if (er) printf("er add u8arr: %d\n",er); #endif - pr_store( &nb ); - res = nb_create( &nb ); - pr_store( &nb ); + pr_store( nb ); + res = nb_create( nb ); + pr_store( nb ); printf("res 0x%x\n", res); f = fopen("test.nb","w+"); - fwrite( res, 1, nb.size , f ); + fwrite( res, 1, nb->size , f ); fclose( f ); printf("LOAD:-----\n"); - nb_init( &nb2 ); - pr_store( &nb2 ); - nb_load( &nb2, res ); - pr_store( &nb2 ); + nb_init( nb2 ); + pr_store( nb2 ); + - printf( "->1 [%s]\n", ((nb_u8arr *)nb2.types[1].nb_val)->val ); + nb_load( nb2, res ); + pr_store( nb2 ); + + + printf( "->1 [%s]\n", ((nb_u8arr *)nb2->types[1].nb_val)->val ); __NBT_TYPED *t=0xf8; nb_u8arr *v; - nb_type( &nb2, 1, &t ); + nb_type( nb2, 1, &t ); printf("get type: %02x\n", t ); - nb_val( &nb2, 1, (uint8_t **)&v ); + nb_val( nb2, 1, (uint8_t **)&v ); printf("get value: %02x\n", v->val ); - free( res ); + + nb_free(nb); + nb_free(nb2); + free( res ); res = NULL; printf("End test\n"); return 0; |