kwil-header-tool/test_files/implementation.c

69 lines
1.7 KiB
C
Raw Permalink Normal View History

2023-09-11 16:44:37 +00:00
#include "header.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
KWIL_GEN_IMPL()
int main(int argc, char* argv[]) {
printf("running kwil test\n");
struct struct_A a = {
2023-09-11 16:44:37 +00:00
.b = -10,
.u = 13,
.a = 1.0,
.dyn_str = "WHOOAAAAA"
};
struct struct_B b = {
.f = 123.0,
.i = 3,
2023-09-16 15:27:03 +00:00
.str_static = "",
.other_struct = a,
.other_struct_typedef = {
.b = -20, .u = 13, .a = -3.14, .dyn_str = "AWESOMEE"
},
.other_enum = VALUE_B,
.other_enum_typedef = VALUE_C
};
2023-09-16 15:27:03 +00:00
int allocate_bytes = struct_B_json_length(&b)+1;
char* json = malloc(allocate_bytes);
int json_len = struct_B_to_json(&b, json);
2023-09-16 15:27:03 +00:00
printf("allocated %d bytes for json of struct_B\n", allocate_bytes);
printf("returned length of json string: %d\n", json_len);
printf("actual length of json string: %zu\n\n", strlen(json));
printf("struct_B as json:\n%s\n\n", json);
2023-09-11 16:44:37 +00:00
int real_len = strlen(json);
2023-09-16 15:27:03 +00:00
if (real_len != allocate_bytes) {
printf("Json Length (%d) does not match allocated space (%d)\n", json_len,
allocate_bytes);
free(json);
2023-09-11 16:44:37 +00:00
return 1;
}
2023-09-16 15:27:03 +00:00
struct struct_B deserialized = struct_B_from_json(json);
allocate_bytes = struct_B_json_length(&b);
char* reserialized_json = malloc(allocate_bytes);
struct_B_to_json(&deserialized, reserialized_json);
printf("deserialzed (serialized):\n%s\n\n", json);
if(strcmp(json, reserialized_json) != 0) {
printf("Deserialized struct does not match original\n");
free(json);
return 2;
}
free(json);
2023-09-11 16:44:37 +00:00
return 0;
}