Skip to content

Commit 27f444b

Browse files
rickrick
rick
authored and
rick
committed
functional struct introspection test case
1 parent 15b02d1 commit 27f444b

File tree

11 files changed

+177
-0
lines changed

11 files changed

+177
-0
lines changed

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,9 @@
493493
[submodule "subprojects/parson"]
494494
path = subprojects/parson
495495
url = https://github.com/kgabis/parson
496+
[submodule "submodules/sqldbal"]
497+
path = submodules/sqldbal
498+
url = https://github.com/somnisoft/sqldbal
499+
[submodule "submodules/mkcreflect"]
500+
path = submodules/mkcreflect
501+
url = https://github.com/loganek/mkcreflect

deps-test/deps-test.c

+88
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#define MKCREFLECT_IMPL
12
#include "../submodules/generic-print/print.h"
23
#include "deps-test.h"
34
#include <assert.h>
45
static int do_get_google();
56
static inline int file_exists(const char *path);
67

8+
79
enum ExampleEvents {
810
EVENT_START = 100,
911
EVENT_MIDDLE = 200,
@@ -12,6 +14,80 @@ enum ExampleEvents {
1214
struct FnArgs {
1315
int counter;
1416
};
17+
typedef struct {
18+
int p1;
19+
int p2;
20+
} BaseStruct;
21+
22+
MKCREFLECT_DEFINE_STRUCT(T,
23+
(STRING, char, s, 20),
24+
(INTEGER, unsigned int, i))
25+
26+
MKCREFLECT_DEFINE_STRUCT(Address,
27+
(STRING, char, street, 20),
28+
(STRUCT, T, t),
29+
(INTEGER, unsigned int, house_number))
30+
31+
32+
MKCREFLECT_DEFINE_STRUCT(TestStruct,
33+
(INTEGER, int, int_field),
34+
(STRING, char, array_field, 20),
35+
(INTEGER, size_t, size_field),
36+
(INTEGER, unsigned int, field1),
37+
(INTEGER, int64_t, field2, 20),
38+
(STRING, char, field3, 10),
39+
(STRUCT, BaseStruct, field4),
40+
(FLOAT, float, field5),
41+
(DOUBLE, double, field6),
42+
(POINTER, void *, field7, 3))
43+
44+
45+
Address address;
46+
TestStruct test;
47+
T t;
48+
MKCREFLECT_TypeInfo *test_info, *address_info, *t_info;
49+
50+
51+
void do_mkcreflect(){
52+
test_info = mkcreflect_get_TestStruct_type_info();
53+
54+
for (size_t i = 0; i < test_info->fields_count; i++) {
55+
MKCREFLECT_FieldInfo *field = &test_info->fields[i];
56+
printf(" * %s\n", field->field_name);
57+
printf(" Type: %s\n", field->field_type);
58+
printf(" Total size: %lu\n", field->size);
59+
printf(" Offset: %lu\n", field->offset);
60+
if (field->array_size != -1) {
61+
printf(" It is an array! Number of elements: %d, size of single element: %lu\n",
62+
field->array_size, field->size / field->array_size);
63+
}
64+
}
65+
address_info = mkcreflect_get_Address_type_info();
66+
for (size_t i = 0; i < address_info->fields_count; i++) {
67+
MKCREFLECT_FieldInfo *field = &address_info->fields[i];
68+
printf(" * %s\n", field->field_name);
69+
printf(" Type: %s\n", field->field_type);
70+
printf(" Total size: %lu\n", field->size);
71+
printf(" Offset: %lu\n", field->offset);
72+
if (field->array_size != -1) {
73+
printf(" It is an array! Number of elements: %d, size of single element: %lu\n",
74+
field->array_size, field->size / field->array_size);
75+
}
76+
}
77+
t_info = mkcreflect_get_T_type_info();
78+
for (size_t i = 0; i < t_info->fields_count; i++) {
79+
MKCREFLECT_FieldInfo *field = &t_info->fields[i];
80+
printf(" * %s\n", field->field_name);
81+
printf(" Type: %s\n", field->field_type);
82+
printf(" Total size: %lu\n", field->size);
83+
printf(" Offset: %lu\n", field->offset);
84+
if (field->array_size != -1) {
85+
printf(" It is an array! Number of elements: %d, size of single element: %lu\n",
86+
field->array_size, field->size / field->array_size);
87+
}
88+
}
89+
}
90+
1591
char JSON_TESTS_FILE[] = ".tests.json",
1692
*JSON_TESTS_CONTENT;
1793

@@ -836,6 +912,13 @@ TEST t_vtparse_simple(void){
836912
}
837913

838914

915+
TEST t_mkcreflect(){
916+
do_mkcreflect();
917+
918+
PASS();
919+
}
920+
921+
839922
TEST t_generic_print(){
840923
int x[] = { 1, 2, 3 };
841924
char *args[] = { "gcc", "hello.c", "-o", "hello" };
@@ -1020,6 +1103,10 @@ SUITE(s_generic_print) {
10201103
PASS();
10211104
}
10221105

1106+
SUITE(s_mkcreflect) {
1107+
RUN_TEST(t_mkcreflect);
1108+
PASS();
1109+
}
10231110
SUITE(s_json) {
10241111
RUN_TEST(t_read_json_file);
10251112
RUN_TEST(t_process_json_lines);
@@ -1059,6 +1146,7 @@ int main(int argc, char **argv) {
10591146
RUN_SUITE(s_generic_print);
10601147
RUN_SUITE(s_murmurhash);
10611148
RUN_SUITE(s_libbeaufort);
1149+
RUN_SUITE(s_mkcreflect);
10621150
GREATEST_MAIN_END();
10631151
size_t used = do_dmt_summary();
10641152
ASSERT_EQ(used, 0);

deps-test/deps-test.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
//////////////////////////////////////
33
#include <ctype.h>
4+
#include <inttypes.h>
45
#include <stdarg.h>
56
#include <stdint.h>
67
#include <stdio.h>
@@ -23,12 +24,15 @@
2324
#include "submodules/libbeaufort/include/beaufort.h"
2425
#include "submodules/microtar/src/microtar.h"
2526
#include "submodules/microui/src/microui.h"
27+
#include "submodules/mkcreflect/lib/include/mkcreflect.h"
2628
#include "submodules/murmurhash.c/murmurhash.h"
2729
#include "submodules/occurrences/occurrences.h"
2830
#include "submodules/rhash_md5.c/md5.h"
2931
#include "submodules/siphash/siphash.h"
32+
#include "submodules/sqldbal/src/sqldbal.h"
3033
#include "submodules/str-replace.c/src/str-replace.h"
3134
#include "submodules/str-truncate.c/src/str-truncate.h"
3235
#include "submodules/timestamp/timestamp.h"
3336
#include "submodules/tiny-regex-c/re.h"
3437
#include "submodules/vtparse/vtparse/vtparse.h"
38+

deps-test/meson.build

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ deps_test_deps = [
2525
throw_h_dep,
2626
c_timestamp_dep,
2727
siphash_dep,
28+
sqldbal_dep,
2829
c_forever_dep,
2930
dbg_dep,
3031
c_eventemitter_dep,
@@ -38,6 +39,9 @@ deps_test_deps = [
3839
cansid_dep,
3940
ansi_utils_dep,
4041
http_get_dep,
42+
mkcreflect_dep,
43+
]
44+
deps_c_args = [
4145
]
4246
deps_link_args = [
4347
]
@@ -47,6 +51,7 @@ if get_option('enable-binaries')
4751
dependencies: deps_test_deps,
4852
include_directories: deps_include_dirs,
4953
link_args: deps_link_args,
54+
c_args: deps_c_args,
5055
install: true,
5156
pie: true,
5257
)

meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ else
1414
endif
1515

1616

17+
subdir('meson/sqlite3')
1718

1819
version_info = configuration_data()
1920
version_info.set('version', meson.project_version())

meson/deps/mkcreflect/meson.build

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
mkcreflect_srcs = [files(
2+
'../../../submodules/mkcreflect/lib/include/mkcreflect.h',
3+
)]
4+
5+
mkcreflect_dirs = [
6+
'../../../submodules/mkcreflect',
7+
'../../../submodules/mkcreflect/lib',
8+
'../../../submodules/mkcreflect/lib/include',
9+
inc,
10+
]
11+
12+
mkcreflect_deps = [
13+
14+
]
15+
16+
mkcreflect_lib = library('mkcreflect',
17+
mkcreflect_srcs,
18+
install: false,
19+
dependencies: mkcreflect_deps,
20+
c_args: ['-DMKCREFLECT_IMPL'],
21+
include_directories: mkcreflect_dirs,
22+
)
23+
24+
mkcreflect_dep = declare_dependency(
25+
include_directories: mkcreflect_dirs,
26+
link_with: mkcreflect_lib,
27+
)

meson/deps/sqldbal/meson.build

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
sqldbal_srcs = [files(
2+
'../../../submodules/sqldbal/src/sqldbal.c',
3+
'../../../submodules/sqldbal/src/sqldbal.h',
4+
)]
5+
6+
sqldbal_dirs = [
7+
'../../../submodules/sqldbal',
8+
'../../../submodules/sqldbal/src',
9+
inc,
10+
]
11+
12+
sqldbal_deps = [
13+
14+
]
15+
sqldbal_link_args = [
16+
'-lsqlite3',
17+
]
18+
sqldbal_c_args = [
19+
'-DSQLDBAL_SQLITE',
20+
]
21+
22+
sqldbal_lib = static_library('sqldbal',
23+
sqldbal_srcs,
24+
install: false,
25+
dependencies: sqldbal_deps,
26+
link_args: sqldbal_link_args,
27+
c_args: sqldbal_c_args,
28+
include_directories: sqldbal_dirs,
29+
)
30+
31+
sqldbal_dep = declare_dependency(
32+
include_directories: sqldbal_dirs,
33+
link_with: sqldbal_lib,
34+
)

meson/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ subdir('deps/env.h')
2828
subdir('deps/throw.h')
2929
subdir('deps/c-timestamp')
3030
subdir('deps/siphash')
31+
subdir('deps/sqldbal')
3132
subdir('deps/libspng')
33+
subdir('deps/mkcreflect')
3234
subdir('deps/asprintf')
3335
subdir('deps/debug_print_h')
3436
subdir('deps/assertf')

meson/sqlite3/meson.build

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sqldbal_link_args = [
2+
'-lsqlite3',
3+
]
4+
sqldbal_c_args = [
5+
'-DSQLDBAL_SQLITE',
6+
]
7+
add_project_link_arguments(sqldbal_link_args,language : ['c'])
8+
add_project_arguments(sqldbal_c_args, language : 'c')

submodules/mkcreflect

Submodule mkcreflect added at f1fdf2a

submodules/sqldbal

Submodule sqldbal added at df58a97

0 commit comments

Comments
 (0)