1 #include "clar_libgit2.h"
2 #include <git2/sys/commit_graph.h>
3 #include <git2/sys/config.h>
4 #include <git2/sys/filter.h>
5 #include <git2/sys/odb_backend.h>
6 #include <git2/sys/refdb_backend.h>
7 #include <git2/sys/transport.h>
9 #define STRINGIFY(s) #s
11 /* Checks two conditions for the specified structure:
12 * 1. That the initializers for the latest version produces the same
13 * in-memory representation.
14 * 2. That the function-based initializer supports all versions from 1...n,
15 * where n is the latest version (often represented by GIT_*_VERSION).
18 * structname: The name of the structure to test, e.g. git_blame_options.
19 * structver: The latest version of the specified structure.
20 * macroinit: The macro that initializes the latest version of the structure.
21 * funcinitname: The function that initializes the structure. Must have the
22 * signature "int (structname* instance, int version)".
24 #define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
26 structname structname##_macro_latest = macroinit; \
27 structname structname##_func_latest; \
28 int structname##_curr_ver = structver - 1; \
29 memset(&structname##_func_latest, 0, sizeof(structname##_func_latest)); \
30 cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
31 options_cmp(&structname##_macro_latest, &structname##_func_latest, \
32 sizeof(structname), STRINGIFY(structname)); \
34 while (structname##_curr_ver > 0) \
37 cl_git_pass(funcinitname(¯o, structname##_curr_ver)); \
38 structname##_curr_ver--; \
42 static void options_cmp(void *one
, void *two
, size_t size
, const char *name
)
46 for (i
= 0; i
< size
; i
++) {
47 if (((char *)one
)[i
] != ((char *)two
)[i
]) {
50 p_snprintf(desc
, 1024, "Difference in %s at byte %" PRIuZ
": macro=%u / func=%u",
51 name
, i
, ((char *)one
)[i
], ((char *)two
)[i
]);
52 clar__fail(__FILE__
, __func__
, __LINE__
,
53 "Difference between macro and function options initializer",
60 void test_core_structinit__compare(void)
62 /* These tests assume that they can memcmp() two structures that were
63 * initialized with the same static initializer. Eg,
64 * git_blame_options = GIT_BLAME_OPTIONS_INIT;
66 * This assumption fails when there is padding between structure members,
67 * which is not guaranteed to be initialized to anything sane at all.
69 * Assume most compilers, in a debug build, will clear that memory for
70 * us or set it to sentinal markers. Etc.
72 #if !defined(DEBUG) && !defined(_DEBUG)
77 CHECK_MACRO_FUNC_INIT_EQUAL( \
78 git_apply_options
, GIT_APPLY_OPTIONS_VERSION
, \
79 GIT_APPLY_OPTIONS_INIT
, git_apply_options_init
);
82 CHECK_MACRO_FUNC_INIT_EQUAL( \
83 git_blame_options
, GIT_BLAME_OPTIONS_VERSION
, \
84 GIT_BLAME_OPTIONS_INIT
, git_blame_options_init
);
86 /* blob_filter_options */
87 CHECK_MACRO_FUNC_INIT_EQUAL( \
88 git_blob_filter_options
, GIT_BLOB_FILTER_OPTIONS_VERSION
, \
89 GIT_BLOB_FILTER_OPTIONS_INIT
, git_blob_filter_options_init
);
92 CHECK_MACRO_FUNC_INIT_EQUAL( \
93 git_checkout_options
, GIT_CHECKOUT_OPTIONS_VERSION
, \
94 GIT_CHECKOUT_OPTIONS_INIT
, git_checkout_options_init
);
97 CHECK_MACRO_FUNC_INIT_EQUAL( \
98 git_clone_options
, GIT_CLONE_OPTIONS_VERSION
, \
99 GIT_CLONE_OPTIONS_INIT
, git_clone_options_init
);
101 /* commit_graph_writer */
102 CHECK_MACRO_FUNC_INIT_EQUAL( \
103 git_commit_graph_writer_options
, \
104 GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION
, \
105 GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT
, \
106 git_commit_graph_writer_options_init
);
109 CHECK_MACRO_FUNC_INIT_EQUAL( \
110 git_diff_options
, GIT_DIFF_OPTIONS_VERSION
, \
111 GIT_DIFF_OPTIONS_INIT
, git_diff_options_init
);
114 CHECK_MACRO_FUNC_INIT_EQUAL( \
115 git_diff_find_options
, GIT_DIFF_FIND_OPTIONS_VERSION
, \
116 GIT_DIFF_FIND_OPTIONS_INIT
, git_diff_find_options_init
);
119 CHECK_MACRO_FUNC_INIT_EQUAL( \
120 git_filter
, GIT_FILTER_VERSION
, \
121 GIT_FILTER_INIT
, git_filter_init
);
123 /* merge_file_input */
124 CHECK_MACRO_FUNC_INIT_EQUAL( \
125 git_merge_file_input
, GIT_MERGE_FILE_INPUT_VERSION
, \
126 GIT_MERGE_FILE_INPUT_INIT
, git_merge_file_input_init
);
129 CHECK_MACRO_FUNC_INIT_EQUAL( \
130 git_merge_file_options
, GIT_MERGE_FILE_OPTIONS_VERSION
, \
131 GIT_MERGE_FILE_OPTIONS_INIT
, git_merge_file_options_init
);
134 CHECK_MACRO_FUNC_INIT_EQUAL( \
135 git_merge_options
, GIT_MERGE_OPTIONS_VERSION
, \
136 GIT_MERGE_OPTIONS_INIT
, git_merge_options_init
);
139 CHECK_MACRO_FUNC_INIT_EQUAL( \
140 git_push_options
, GIT_PUSH_OPTIONS_VERSION
, \
141 GIT_PUSH_OPTIONS_INIT
, git_push_options_init
);
144 CHECK_MACRO_FUNC_INIT_EQUAL( \
145 git_remote_callbacks
, GIT_REMOTE_CALLBACKS_VERSION
, \
146 GIT_REMOTE_CALLBACKS_INIT
, git_remote_init_callbacks
);
148 /* repository_init */
149 CHECK_MACRO_FUNC_INIT_EQUAL( \
150 git_repository_init_options
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, \
151 GIT_REPOSITORY_INIT_OPTIONS_INIT
, git_repository_init_options_init
);
154 CHECK_MACRO_FUNC_INIT_EQUAL( \
155 git_revert_options
, GIT_REVERT_OPTIONS_VERSION
, \
156 GIT_REVERT_OPTIONS_INIT
, git_revert_options_init
);
159 CHECK_MACRO_FUNC_INIT_EQUAL( \
160 git_stash_apply_options
, GIT_STASH_APPLY_OPTIONS_VERSION
, \
161 GIT_STASH_APPLY_OPTIONS_INIT
, git_stash_apply_options_init
);
164 CHECK_MACRO_FUNC_INIT_EQUAL( \
165 git_status_options
, GIT_STATUS_OPTIONS_VERSION
, \
166 GIT_STATUS_OPTIONS_INIT
, git_status_options_init
);
169 CHECK_MACRO_FUNC_INIT_EQUAL( \
170 git_transport
, GIT_TRANSPORT_VERSION
, \
171 GIT_TRANSPORT_INIT
, git_transport_init
);
174 CHECK_MACRO_FUNC_INIT_EQUAL( \
175 git_config_backend
, GIT_CONFIG_BACKEND_VERSION
, \
176 GIT_CONFIG_BACKEND_INIT
, git_config_init_backend
);
179 CHECK_MACRO_FUNC_INIT_EQUAL( \
180 git_odb_backend
, GIT_ODB_BACKEND_VERSION
, \
181 GIT_ODB_BACKEND_INIT
, git_odb_init_backend
);
184 CHECK_MACRO_FUNC_INIT_EQUAL( \
185 git_refdb_backend
, GIT_REFDB_BACKEND_VERSION
, \
186 GIT_REFDB_BACKEND_INIT
, git_refdb_init_backend
);
188 /* submodule update */
189 CHECK_MACRO_FUNC_INIT_EQUAL( \
190 git_submodule_update_options
, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION
, \
191 GIT_SUBMODULE_UPDATE_OPTIONS_INIT
, git_submodule_update_options_init
);
193 /* submodule update */
194 CHECK_MACRO_FUNC_INIT_EQUAL( \
195 git_proxy_options
, GIT_PROXY_OPTIONS_VERSION
, \
196 GIT_PROXY_OPTIONS_INIT
, git_proxy_options_init
);
198 CHECK_MACRO_FUNC_INIT_EQUAL( \
199 git_diff_patchid_options
, GIT_DIFF_PATCHID_OPTIONS_VERSION
, \
200 GIT_DIFF_PATCHID_OPTIONS_INIT
, git_diff_patchid_options_init
);