summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorFreeArtMan <=>2015-11-07 18:18:59 +0000
committerFreeArtMan <=>2015-11-07 18:18:59 +0000
commit3c89841f31cd7fc6cb35c129a9135c5e974a7649 (patch)
tree4f0eb8fd7a90c8f194b4f8580e8c4f512dfa13e2 /test
parent9514d70e98fa7f8fa26f991586169b9da09e7ea6 (diff)
downloadnetbytes-3c89841f31cd7fc6cb35c129a9135c5e974a7649.tar.gz
netbytes-3c89841f31cd7fc6cb35c129a9135c5e974a7649.zip
example/test source
Diffstat (limited to 'test')
-rw-r--r--test/Makefile11
-rw-r--r--test/test_add_elem.c103
-rw-r--r--test/test_multiple_read.c82
-rw-r--r--test/test_multiple_write.c96
-rw-r--r--test/test_save.c127
5 files changed, 419 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..3fec11a
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,11 @@
+CC=gcc
+CFLAGS=-I../ -lnetbytes -Wl,-rpath=../ -L../
+LDFLAGS=
+
+SOURCE=test_save test_multiple_write test_multiple_read test_add_elem
+SOURCES=$(SOURCE:=.c)
+
+make: $(SOURCE)
+
+clean:
+ rm -f $(SOURCE) \ No newline at end of file
diff --git a/test/test_add_elem.c b/test/test_add_elem.c
new file mode 100644
index 0000000..5244880
--- /dev/null
+++ b/test/test_add_elem.c
@@ -0,0 +1,103 @@
+#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_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
+ {
+ printf("Unknown\n");
+ }
+ }
+}
+
+int main()
+{
+ int er;
+ uint8_t *res;
+ FILE *f;
+ const int STR_SIZE=128;
+ char str[STR_SIZE];
+ char *n;
+ nb_u8 id;
+ nb_u8arr name;
+
+ netbyte_store nb;
+
+ printf("Start test\n");
+ nb_init( &nb );
+
+ printf("Your name : "); fflush( stdin );
+ n = fgets( str, STR_SIZE, stdin );
+
+ er = nb_u8_create( &id, 0x11 );
+ if (er)
+ printf("er create u8: %d\n",er);
+ er = nb_u8arr_create( &name, strlen(n), &str[0] );
+ if (er)
+ printf("er create u8arr: %d\n",er);
+
+
+ er = nb_add_u8( &nb, &id );
+ if (er)
+ printf("er add u8: %d\n",er);
+ er = nb_add_u8arr( &nb, &name );
+ if (er)
+ printf("er add u8arr: %d\n",er);
+
+
+ pr_store( &nb );
+ res = nb_create( &nb );
+
+ f = fopen("test_many.nb","a");
+ fwrite( res, 1, nb.size , f );
+ fclose( f );
+
+ 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
new file mode 100644
index 0000000..3bec7dc
--- /dev/null
+++ b/test/test_multiple_read.c
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <netbytes.h>
+
+void pr_u8( nb_u8 *u8 )
+{
+ if ( !u8 )
+ {
+ printf("\tu8: NULL\n");
+ return;
+ } else
+ {
+ printf("\tu8: t=0x%x v=0x%x\n", u8->type, u8->val );
+ }
+}
+
+void pr_u8arr( nb_u8arr *u8arr )
+{
+ if (!u8arr)
+ {
+ printf("\tu8arr: NULL\n");
+ return ;
+ } else
+ {
+ printf("\tu8arr: t=0x%x l=0x%x v=0x%x\n",
+ u8arr->type, u8arr->len, u8arr->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
+ {
+ printf("Unknown\n");
+ }
+ }
+}
+
+int main()
+{
+
+ int er;
+ uint8_t *res;
+ FILE *f;
+ int i=0;
+
+ 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 );
+
+ printf("End test\n");
+ return 0;
+} \ No newline at end of file
diff --git a/test/test_multiple_write.c b/test/test_multiple_write.c
new file mode 100644
index 0000000..53d7cae
--- /dev/null
+++ b/test/test_multiple_write.c
@@ -0,0 +1,96 @@
+#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_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
+ {
+ printf("Unknown\n");
+ }
+ }
+}
+
+int main()
+{
+
+ int er;
+ const int len = 11;
+ char *str = "0123456789";
+ uint8_t *res;
+ FILE *f;
+
+ netbyte_store nb;
+ nb_u8arr u8arr;
+
+ nb_init( &nb );
+
+ er = nb_u8arr_create( &u8arr, len, &str[0] );
+ if (er)
+ printf("er create u8arr: %d\n",er);
+
+ pr_u8arr( &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 );
+
+ 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 );
+ fclose( f );
+
+ printf("End test\n");
+ return 0;
+} \ No newline at end of file
diff --git a/test/test_save.c b/test/test_save.c
new file mode 100644
index 0000000..6e3be63
--- /dev/null
+++ b/test/test_save.c
@@ -0,0 +1,127 @@
+#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_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
+ {
+ printf("Unknown\n");
+ }
+ }
+}
+
+int main()
+{
+ FILE *f=NULL;
+ int er;
+ netbyte_store nb;
+ netbyte_store nb2;
+ nb_u8 u8;
+ nb_u8arr u8arr;
+ 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);
+
+
+ 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 1
+ 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 );
+
+ printf("res 0x%x\n", res);
+
+ f = fopen("test.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