summaryrefslogtreecommitdiff
path: root/test/test_key_value_match.c
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-06-04 00:17:22 +0100
committerFreeArtMan <dos21h@gmail.com>2017-06-04 00:17:22 +0100
commit18b2ebe3df26009ef6b2aeb0670a396e876fca25 (patch)
tree3b851d392330877513c460b03e87b4d0d52ef0fb /test/test_key_value_match.c
parenta3c2eb86925cf35ef6b0fbb12249cee6a15060cb (diff)
downloadnetbytes-18b2ebe3df26009ef6b2aeb0670a396e876fca25.tar.gz
netbytes-18b2ebe3df26009ef6b2aeb0670a396e876fca25.zip
Added nb_match functionality
Diffstat (limited to 'test/test_key_value_match.c')
-rw-r--r--test/test_key_value_match.c223
1 files changed, 223 insertions, 0 deletions
diff --git a/test/test_key_value_match.c b/test/test_key_value_match.c
new file mode 100644
index 0000000..4384a07
--- /dev/null
+++ b/test/test_key_value_match.c
@@ -0,0 +1,223 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+ char *name;
+ char *desc;
+} 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;
+}
+
+kv_user_name* kv_empty()
+{
+ kv_user_name *ret = NULL;
+
+ ret = calloc(1, sizeof(kv_user_name));
+ if (!ret)
+ {
+ return NULL;
+ }
+
+
+ return ret;
+}
+
+netbyte_store* kv_marsh(kv_user_name *kv)
+{
+ 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;
+}
+
+
+int kv_unmarsh(netbyte_store *store, kv_user_name *kv)
+{
+ __nb_type *type=NULL;
+
+ if (!store)
+ {
+ return -1;
+ }
+
+ if (!kv)
+ {
+ return -1;
+ }
+
+ if (0 == nb_val(store, 0, &type))
+ if (type->type == NBT_U8)
+ {
+ nb_u8 *u8 = (nb_u8 *)type->nb_val;
+ kv->id = u8->val;
+ } else
+ {
+ printf("ERR\n");
+ }
+
+ if (0 == nb_val(store, 1, &type))
+ if (type->type == NBT_U8ARRAY)
+ {
+ nb_u8arr *u8arr = (nb_u8arr *)type->nb_val;
+ kv->name = alloc_new_str_s(u8arr->val, u8arr->len);
+ } else
+ {
+ printf("ERR\n");
+ }
+
+ if (0 == nb_val(store, 2, &type))
+ if (type->type == NBT_U8ARRAY)
+ {
+ nb_u8arr *u8arr = (nb_u8arr *)type->nb_val;
+ kv->desc = alloc_new_str_s(u8arr->val, u8arr->len);
+ } else
+ {
+ printf("ERR\n");
+ }
+
+ return 0;
+}
+
+
+void kv_free(kv_user_name *kv)
+{
+ if (kv!=NULL)
+ {
+ free(kv->name);
+ free(kv->desc);
+ free(kv);
+ }
+}
+
+int main()
+{
+ int er=-1;
+ uint8_t *res=NULL;
+ FILE *f=NULL;
+ int i=0;
+
+ kv_user_name *kv=NULL;
+
+ netbyte_store *nb=malloc(sizeof(netbyte_store));
+ nb_tok_arr *pat = NULL;
+
+ printf("Start test\n");
+
+ f = fopen("test_kv_save.md","r");
+ if (nb_fread( nb, fileno(f) ) == 0)
+ {
+ nb_print( nb );
+ }
+ fclose( f );
+
+
+ pat = nb_tok_create(32);
+ if (pat == NULL)
+ {
+ printf("ERR nb_tok_create\n");
+ }
+ if (-1 == nb_parse("u8,u8[],u8[] ",pat))
+ {
+ printf("ERR nb_parse\n");
+ }
+
+
+ for (i=0;i<pat->len;i++)
+ {
+ nb_tok tok = pat->tok[i];
+ printf("%d S:%d TS:%d L:%d A:%d\n",
+ i, tok.sign, tok.type_size, tok.len, tok.arr
+ );
+ }
+
+ if (nb_match(nb, pat)==0)
+ {
+ printf("Match\n");
+ } else
+ {
+ printf("No match\n");
+ }
+
+
+
+ kv = kv_empty();
+ kv_unmarsh(nb, kv);
+
+
+ printf("ID:%d\n", kv->id);
+ printf("NAME:%s\n",kv->name);
+ printf("DESC:%s\n", kv->desc);
+
+ kv_free(kv);
+
+ nb_free(nb);
+
+
+ printf("End test\n");
+ return 0;
+} \ No newline at end of file