1 #include "clar_libgit2.h"
2 #include <git2/sys/config.h>
3 #include <git2/sys/filter.h>
4 #include <git2/sys/odb_backend.h>
5 #include <git2/sys/refdb_backend.h>
6 #include <git2/sys/transport.h>
8 #define STRINGIFY(s) #s
10 /* Checks two conditions for the specified structure:
11 * 1. That the initializers for the latest version produces the same
12 * in-memory representation.
13 * 2. That the function-based initializer supports all versions from 1...n,
14 * where n is the latest version (often represented by GIT_*_VERSION).
17 * structname: The name of the structure to test, e.g. git_blame_options.
18 * structver: The latest version of the specified structure.
19 * macroinit: The macro that initializes the latest version of the structure.
20 * funcinitname: The function that initializes the structure. Must have the
21 * signature "int (structname* instance, int version)".
23 #define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
25 structname structname##_macro_latest = macroinit; \
26 structname structname##_func_latest; \
27 int structname##_curr_ver = structver - 1; \
28 memset(&structname##_func_latest, 0, sizeof(structname##_func_latest)); \
29 cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
30 options_cmp(&structname##_macro_latest, &structname##_func_latest, \
31 sizeof(structname), STRINGIFY(structname)); \
33 while (structname##_curr_ver > 0) \
36 cl_git_pass(funcinitname(¯o, structname##_curr_ver)); \
37 structname##_curr_ver--; \
41 static void options_cmp(void *one
, void *two
, size_t size
, const char *name
)
45 for (i
= 0; i
< size
; i
++) {
46 if (((char *)one
)[i
] != ((char *)two
)[i
]) {
49 p_snprintf(desc
, 1024, "Difference in %s at byte %" PRIuZ
": macro=%u / func=%u",
50 name
, i
, ((char *)one
)[i
], ((char *)two
)[i
]);
51 clar__fail(__FILE__
, __func__
, __LINE__
,
52 "Difference between macro and function options initializer",
59 void test_core_structinit__compare(void)
61 /* These tests assume that they can memcmp() two structures that were
62 * initialized with the same static initializer. Eg,
63 * git_blame_options = GIT_BLAME_OPTIONS_INIT;
65 * This assumption fails when there is padding between structure members,
66 * which is not guaranteed to be initialized to anything sane at all.
68 * Assume most compilers, in a debug build, will clear that memory for
69 * us or set it to sentinal markers. Etc.
71 #if !defined(DEBUG) && !defined(_DEBUG)
76 CHECK_MACRO_FUNC_INIT_EQUAL( \
77 git_apply_options
, GIT_APPLY_OPTIONS_VERSION
, \
78 GIT_APPLY_OPTIONS_INIT
, git_apply_options_init
);
81 CHECK_MACRO_FUNC_INIT_EQUAL( \
82 git_blame_options
, GIT_BLAME_OPTIONS_VERSION
, \
83 GIT_BLAME_OPTIONS_INIT
, git_blame_options_init
);
86 CHECK_MACRO_FUNC_INIT_EQUAL( \
87 git_checkout_options
, GIT_CHECKOUT_OPTIONS_VERSION
, \
88 GIT_CHECKOUT_OPTIONS_INIT
, git_checkout_options_init
);
91 CHECK_MACRO_FUNC_INIT_EQUAL( \
92 git_clone_options
, GIT_CLONE_OPTIONS_VERSION
, \
93 GIT_CLONE_OPTIONS_INIT
, git_clone_options_init
);
96 CHECK_MACRO_FUNC_INIT_EQUAL( \
97 git_diff_options
, GIT_DIFF_OPTIONS_VERSION
, \
98 GIT_DIFF_OPTIONS_INIT
, git_diff_options_init
);
101 CHECK_MACRO_FUNC_INIT_EQUAL( \
102 git_diff_find_options
, GIT_DIFF_FIND_OPTIONS_VERSION
, \
103 GIT_DIFF_FIND_OPTIONS_INIT
, git_diff_find_options_init
);
106 CHECK_MACRO_FUNC_INIT_EQUAL( \
107 git_filter
, GIT_FILTER_VERSION
, \
108 GIT_FILTER_INIT
, git_filter_init
);
110 /* merge_file_input */
111 CHECK_MACRO_FUNC_INIT_EQUAL( \
112 git_merge_file_input
, GIT_MERGE_FILE_INPUT_VERSION
, \
113 GIT_MERGE_FILE_INPUT_INIT
, git_merge_file_input_init
);
116 CHECK_MACRO_FUNC_INIT_EQUAL( \
117 git_merge_file_options
, GIT_MERGE_FILE_OPTIONS_VERSION
, \
118 GIT_MERGE_FILE_OPTIONS_INIT
, git_merge_file_options_init
);
121 CHECK_MACRO_FUNC_INIT_EQUAL( \
122 git_merge_options
, GIT_MERGE_OPTIONS_VERSION
, \
123 GIT_MERGE_OPTIONS_INIT
, git_merge_options_init
);
126 CHECK_MACRO_FUNC_INIT_EQUAL( \
127 git_push_options
, GIT_PUSH_OPTIONS_VERSION
, \
128 GIT_PUSH_OPTIONS_INIT
, git_push_options_init
);
131 CHECK_MACRO_FUNC_INIT_EQUAL( \
132 git_remote_callbacks
, GIT_REMOTE_CALLBACKS_VERSION
, \
133 GIT_REMOTE_CALLBACKS_INIT
, git_remote_init_callbacks
);
135 /* repository_init */
136 CHECK_MACRO_FUNC_INIT_EQUAL( \
137 git_repository_init_options
, GIT_REPOSITORY_INIT_OPTIONS_VERSION
, \
138 GIT_REPOSITORY_INIT_OPTIONS_INIT
, git_repository_init_options_init
);
141 CHECK_MACRO_FUNC_INIT_EQUAL( \
142 git_revert_options
, GIT_REVERT_OPTIONS_VERSION
, \
143 GIT_REVERT_OPTIONS_INIT
, git_revert_options_init
);
146 CHECK_MACRO_FUNC_INIT_EQUAL( \
147 git_stash_apply_options
, GIT_STASH_APPLY_OPTIONS_VERSION
, \
148 GIT_STASH_APPLY_OPTIONS_INIT
, git_stash_apply_options_init
);
151 CHECK_MACRO_FUNC_INIT_EQUAL( \
152 git_status_options
, GIT_STATUS_OPTIONS_VERSION
, \
153 GIT_STATUS_OPTIONS_INIT
, git_status_options_init
);
156 CHECK_MACRO_FUNC_INIT_EQUAL( \
157 git_transport
, GIT_TRANSPORT_VERSION
, \
158 GIT_TRANSPORT_INIT
, git_transport_init
);
161 CHECK_MACRO_FUNC_INIT_EQUAL( \
162 git_config_backend
, GIT_CONFIG_BACKEND_VERSION
, \
163 GIT_CONFIG_BACKEND_INIT
, git_config_init_backend
);
166 CHECK_MACRO_FUNC_INIT_EQUAL( \
167 git_odb_backend
, GIT_ODB_BACKEND_VERSION
, \
168 GIT_ODB_BACKEND_INIT
, git_odb_init_backend
);
171 CHECK_MACRO_FUNC_INIT_EQUAL( \
172 git_refdb_backend
, GIT_REFDB_BACKEND_VERSION
, \
173 GIT_REFDB_BACKEND_INIT
, git_refdb_init_backend
);
175 /* submodule update */
176 CHECK_MACRO_FUNC_INIT_EQUAL( \
177 git_submodule_update_options
, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION
, \
178 GIT_SUBMODULE_UPDATE_OPTIONS_INIT
, git_submodule_update_options_init
);
180 /* submodule update */
181 CHECK_MACRO_FUNC_INIT_EQUAL( \
182 git_proxy_options
, GIT_PROXY_OPTIONS_VERSION
, \
183 GIT_PROXY_OPTIONS_INIT
, git_proxy_options_init
);
185 CHECK_MACRO_FUNC_INIT_EQUAL( \
186 git_diff_patchid_options
, GIT_DIFF_PATCHID_OPTIONS_VERSION
, \
187 GIT_DIFF_PATCHID_OPTIONS_INIT
, git_diff_patchid_options_init
);