diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 2 | ||||
-rw-r--r-- | test/test_alltypes.c | 210 | ||||
-rw-r--r-- | test/test_multiple_read.c | 10 |
3 files changed, 216 insertions, 6 deletions
diff --git a/test/Makefile b/test/Makefile index 3fec11a..c4f4b28 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-I../ -lnetbytes -Wl,-rpath=../ -L../ LDFLAGS= -SOURCE=test_save test_multiple_write test_multiple_read test_add_elem +SOURCE=test_save test_multiple_write test_multiple_read test_add_elem test_alltypes SOURCES=$(SOURCE:=.c) make: $(SOURCE) diff --git a/test/test_alltypes.c b/test/test_alltypes.c new file mode 100644 index 0000000..2975d2a --- /dev/null +++ b/test/test_alltypes.c @@ -0,0 +1,210 @@ +#include <stdio.h> +#include <stdlib.h> + +#include <netbytes.h> + +void pr_u8( nb_u8 *u8 ) +{ + if ( !u8 ) + { + printf("u8 : NULL\n"); + return; + } else + { + printf("u8 : t=0x%x v=0x%x\n", u8->type, u8->val ); + } +} + +void pr_u8arr( nb_u8arr *u8arr ) +{ + if (!u8arr) + { + printf("u8arr: NULL\n"); + return ; + } else + { + printf("u8arr: t=0x%x l=0x%x v=0x%x\n", + u8arr->type, u8arr->len, u8arr->val ); + } +} + +void pr_u16( nb_u16 *u16 ) +{ + if ( !u16 ) + { + printf("u16: NULL\n"); + return; + } else + { + printf("u16: t=0x%x v=0x%x\n", u16->type, u16->val ); + } +} + +void pr_u32( nb_u32 *u32 ) +{ + if ( !u32 ) + { + printf("u32: NULL\n"); + return; + } else + { + printf("u32: t=0x%x v=0x%x\n", u32->type, u32->val ); + } +} + +void pr_u64( nb_u64 *u64 ) +{ + if ( !u64 ) + { + printf("u64: NULL\n"); + return; + } else + { + printf("u64: t=0x%x v=0x%016llx\n", u64->type, u64->val ); + } +} + +void pr_store( netbyte_store *nb ) +{ + int i; + + if (!nb) + { + printf("nb: NULL\n"); + return; + } + + printf("nb: s=0x%x c=0x%x\n", nb->size, nb->count ); + for (i=0;i<nb->count;i++) + { + printf("\t[%x] -> ",i ); + if ( nb->types[i].type == NBT_U8 ) + { + pr_u8( (nb_u8 *)nb->types[i].nb_val ); + } else if ( nb->types[i].type == NBT_U8ARRAY ) + { + pr_u8arr( (nb_u8arr *)nb->types[i].nb_val ); + } else if ( nb->types[i].type == NBT_U16 ) + { + pr_u16( (nb_u16 *)nb->types[i].nb_val ); + } else if ( nb->types[i].type == NBT_U32 ) + { + pr_u32( (nb_u32 *)nb->types[i].nb_val ); + } else if ( nb->types[i].type == NBT_U64 ) + { + pr_u64( (nb_u64 *)nb->types[i].nb_val ); + } else + { + printf("Unknown\n"); + } + } +} + +int main() +{ + FILE *f=NULL; + int er; + netbyte_store nb; + netbyte_store nb2; + nb_u8 u8; + nb_u8arr u8arr; + nb_u16 u16; + nb_u16arr u16arr; + nb_u32 u32; + nb_u32arr u32arr; + nb_u64 u64; + nb_u64arr u64arr; + const int len = 11; + char *str = "0123456789"; + uint8_t *res=NULL; + + printf("Start test\n"); + + nb_init( &nb ); + er = nb_u8_create( &u8, 0x10 ); + if (er) + printf("er create u8: %d\n",er); + er = nb_u8arr_create( &u8arr, len, &str[0] ); + if (er) + printf("er create u8arr: %d\n",er); + er = nb_u16_create( &u16, 0x2111 ); + if (er) + printf("er create u16: %d\n",er); + er = nb_u32_create( &u32, 0x42322212 ); + if (er) + printf("er create u32: %d\n",er); + er = nb_u64_create( &u64, 0x8373635343332313 ); + if (er) + printf("er create u64: %d\n",er); + + + pr_u8( &u8 ); + pr_u8arr( &u8arr ); + + #if 1 + pr_store( &nb ); + er = nb_add_u8( &nb, &u8 ); + if (er) + printf("er add u8: %d\n",er); + #endif + + #if 0 + pr_store( &nb ); + er = nb_add_u8arr( &nb, &u8arr ); + if (er) + printf("er add u8arr: %d\n",er); + #endif + + #if 1 + 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 ); + if (er) + printf("er add u32: %d\n",er); + #endif + + #if 1 + 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 ); + + printf("res 0x%x\n", res); + + f = fopen("test_alltypes.nb","w+"); + 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 ); + + printf("End test\n"); + return 0; +}
\ No newline at end of file diff --git a/test/test_multiple_read.c b/test/test_multiple_read.c index 3bec7dc..f020e34 100644 --- a/test/test_multiple_read.c +++ b/test/test_multiple_read.c @@ -7,11 +7,11 @@ void pr_u8( nb_u8 *u8 ) { if ( !u8 ) { - printf("\tu8: NULL\n"); + printf("u8: NULL\n"); return; } else { - printf("\tu8: t=0x%x v=0x%x\n", u8->type, u8->val ); + printf("u8: t=0x%x v=0x%x\n", u8->type, u8->val ); } } @@ -19,11 +19,11 @@ void pr_u8arr( nb_u8arr *u8arr ) { if (!u8arr) { - printf("\tu8arr: NULL\n"); + printf("u8arr: NULL\n"); return ; } else { - printf("\tu8arr: t=0x%x l=0x%x v=0x%x\n", + printf("u8arr: t=0x%x l=0x%x v=0x%x\n", u8arr->type, u8arr->len, u8arr->val ); } } @@ -41,7 +41,7 @@ void pr_store( netbyte_store *nb ) printf("nb: s=0x%x c=0x%x\n", nb->size, nb->count ); for (i=0;i<nb->count;i++) { - //printf("\t[%x] -> ",i ); + printf("\t[%x] -> ",i ); if ( nb->types[i].type == NBT_U8 ) { pr_u8( (nb_u8 *)nb->types[i].nb_val ); |