crpc/test/test.c

118 lines
2.6 KiB
C
Raw Normal View History

2023-12-02 11:05:25 +08:00
//
// Created by tqcq on 2023/12/2.
//
#include "StatusResponse.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "tpl.h"
struct ms_t {
char c;
int i;
char s[2];
char o;
char *x;
char y;
double d;
};
void TestTPL() {
tpl_node *tn;
struct ms_t ms = {/*.c =*/ 'a', /*.i =*/ 1, /*.s =*/ {'h','i'}, /*.o =*/ 'o', /*.x =*/ "matilda", /*.y =*/ 'y', /*.d =*/ 3.14 };
struct ms_t ms2;
char buffer[1024];
int dump_sz = 0;
tn = tpl_map("S(cic#cscf)", &ms, 2);
tpl_pack(tn,0);
// tpl_dump(tn,TPL_FILE,"/tmp/test70.tpl");
tpl_dump(tn,TPL_MEM,buffer,&dump_sz);
tpl_free(tn);
memset(&ms2,0,sizeof(struct ms_t));
tn = tpl_map("S(cic#cscf)", &ms2, 2);
// tpl_load(tn,TPL_FILE,"/tmp/test70.tpl");
tpl_load(tn,TPL_MEM,buffer,dump_sz);
tpl_unpack(tn,0);
tpl_free(tn);
printf("%c\n%d\n%c%c\n%c\n%s\n%c\n%f\n", ms2.c, ms2.i, ms2.s[0], ms2.s[1], ms2.o, ms2.x, ms2.y, ms2.d);
free(ms2.x);
}
void TestTPL2() {
tpl_node *tn;
char *s,*t;
void *addr;
int sz;
tn = tpl_map("s",&s);
s = "hello, world!";
tpl_pack(tn,0);
tpl_dump(tn,TPL_MEM,&addr,&sz);
tpl_free(tn);
tn = tpl_map("s",&t);
tpl_load(tn,TPL_MEM,addr,sz);
tpl_unpack(tn,0);
printf("t is %s\n", t);
free(t);
tpl_free(tn);
free(addr);
}
struct TestS {
int32_t a;
char* str;
};
void TestTPL3() {
struct TestS s;
s.a = 1;
s.str = "hello";
tpl_node *tn = tpl_map("S(is)", &s);
tpl_pack(tn, 0);
char *buffer;
int dump_sz = 0;
tpl_dump(tn, TPL_MEM, &buffer, &dump_sz);
tpl_free(tn);
printf("dump_sz: %d\n", dump_sz);
memset(&s, 0, sizeof(struct TestS));
tn = tpl_map("S(is)", &s);
tpl_load(tn, TPL_MEM, buffer, dump_sz);
tpl_unpack(tn, 0);
tpl_free(tn);
printf("%d\n%s\n", s.a, s.str);
}
int main(int argc, char*argv[]) {
// TestTPL3();
char *buffer;
int dump_sz = 0;
StatusResponse response;
{
StatusResponse_Init(&response);
StatusResponse_set_code(&response, 200);
StatusResponse_set_msg(&response, "OK");
dump_sz = StatusResponse_Serialize(&response, &buffer, &dump_sz);
StatusResponse_Destroy(&response);
}
printf("dump_sz: %d\n", dump_sz);
{
StatusResponse_Init(&response);
StatusResponse_Deserialize(&response, buffer, dump_sz);
printf("code: %d\n", StatusResponse_get_code(&response));
printf("msg: %s\n", StatusResponse_get_msg(&response));
StatusResponse_Destroy(&response);
}
return 0;
}