-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cc
85 lines (70 loc) · 2.42 KB
/
test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "setting.h"
#include <cstring>
#include <iostream>
template<typename T>
bool compare_array(T* array1,size_t sizeofarray1,T* array2,size_t sizeofarray2){
if (sizeofarray1!=sizeofarray2)return false;
for (unsigned i=0;i<sizeofarray1;i++){
if (array1[i]!=array2[i])return false;
}
return true;
}
FILE* secure_open(const char* filename,const char* permission){
FILE* file=NULL;
#if defined(_MSC_VER)
fopen_s(&file,filename,permission);
#else
file=fopen(filename,permission);
#endif
return file;
}
bool compare_bytes(const char* array1,const char* array2){
return strcmp(array1,array2)==0;
}
int main(const int argc,const char** argv){
pktvdp test_ktvdp;
pktvdp string_ktvdp;
pktvdp int_ktvdp;
pktvdp short_ktvdp;
pktvdp float_ktvdp;
pktvdp double_ktvdp;
pktvdp int64_ktvdp;
if (!create_ktvdp(&test_ktvdp)||!(create_ktvdp(&string_ktvdp)||!create_ktvdp(&int_ktvdp)||!create_ktvdp(&short_ktvdp))){
std::cerr<<"Failed to create ktvdp!"<<std::endl;
return 0x1;
}
int value=0x41424344;
const char* testKey="hi everyone how are you peoples";
const size_t testKeySize=strlen(testKey);
const char* description="This is a long description about the field and";
const short type=FT_STRING;
const char* string_val="charles";
if (!fill_ktvdp(test_ktvdp,testKey,type,(const void*)string_val,description)){
std::cerr<<"Failed to fill ktvdp!"<<std::endl;
return 0x2;
}
if (test_ktvdp->key_size!=testKeySize||test_ktvdp->field_type!=type||strcmp(description,test_ktvdp->description)!=0||strcmp(string_val,test_ktvdp->value.string_val)!=0){
std::cout<<(test_ktvdp->key_size!=testKeySize)<<std::endl;
std::cout<<(test_ktvdp->field_type!=type)<<std::endl;
std::cout<<(strcmp(description,test_ktvdp->description)!=0)<<std::endl;
std::cout<<(strcmp(test_ktvdp->value.string_val,string_val)!=0)<<std::endl;
std::cerr<<"ktvdp is not created correctly!"<<std::endl;
return 0x3;
}
char* serialized_ktvdp_bytes;
unsigned serialized_ktvdp_bytes_size;
if (!serialize_ktvdp(test_ktvdp,&serialized_ktvdp_bytes,&serialized_ktvdp_bytes_size)){
std::cerr<<"Failed to serialize ktvdp!"<<std::endl;
return 0x4;
}
FILE* file=NULL;
const char* filename="serialized_ktvdp.ktvdp";
file=secure_open(filename,"w");
if (file==NULL){
std::cerr<<"Failed to open file "<<filename<<" for write"<<std::endl;
return 0x5;
}
fwrite(serialized_ktvdp_bytes,sizeof(char),serialized_ktvdp_bytes_size,file);
fclose(file);
return 0;
}