]> git.proxmox.com Git - libgit2.git/blob - tests/core/structinit.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / core / structinit.c
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>
7
8 #define STRINGIFY(s) #s
9
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).
15 *
16 * Parameters:
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)".
22 */
23 #define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
24 do { \
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)); \
32 \
33 while (structname##_curr_ver > 0) \
34 { \
35 structname macro; \
36 cl_git_pass(funcinitname(&macro, structname##_curr_ver)); \
37 structname##_curr_ver--; \
38 }\
39 } while(0)
40
41 static void options_cmp(void *one, void *two, size_t size, const char *name)
42 {
43 size_t i;
44
45 for (i = 0; i < size; i++) {
46 if (((char *)one)[i] != ((char *)two)[i]) {
47 char desc[1024];
48
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",
53 desc, 0);
54 return;
55 }
56 }
57 }
58
59 void test_core_structinit__compare(void)
60 {
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;
64 *
65 * This assumption fails when there is padding between structure members,
66 * which is not guaranteed to be initialized to anything sane at all.
67 *
68 * Assume most compilers, in a debug build, will clear that memory for
69 * us or set it to sentinal markers. Etc.
70 */
71 #if !defined(DEBUG) && !defined(_DEBUG)
72 clar__skip();
73 #endif
74
75 /* apply */
76 CHECK_MACRO_FUNC_INIT_EQUAL( \
77 git_apply_options, GIT_APPLY_OPTIONS_VERSION, \
78 GIT_APPLY_OPTIONS_INIT, git_apply_options_init);
79
80 /* blame */
81 CHECK_MACRO_FUNC_INIT_EQUAL( \
82 git_blame_options, GIT_BLAME_OPTIONS_VERSION, \
83 GIT_BLAME_OPTIONS_INIT, git_blame_options_init);
84
85 /* checkout */
86 CHECK_MACRO_FUNC_INIT_EQUAL( \
87 git_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, \
88 GIT_CHECKOUT_OPTIONS_INIT, git_checkout_options_init);
89
90 /* clone */
91 CHECK_MACRO_FUNC_INIT_EQUAL( \
92 git_clone_options, GIT_CLONE_OPTIONS_VERSION, \
93 GIT_CLONE_OPTIONS_INIT, git_clone_options_init);
94
95 /* diff */
96 CHECK_MACRO_FUNC_INIT_EQUAL( \
97 git_diff_options, GIT_DIFF_OPTIONS_VERSION, \
98 GIT_DIFF_OPTIONS_INIT, git_diff_options_init);
99
100 /* diff_find */
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);
104
105 /* filter */
106 CHECK_MACRO_FUNC_INIT_EQUAL( \
107 git_filter, GIT_FILTER_VERSION, \
108 GIT_FILTER_INIT, git_filter_init);
109
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);
114
115 /* merge_file */
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);
119
120 /* merge_tree */
121 CHECK_MACRO_FUNC_INIT_EQUAL( \
122 git_merge_options, GIT_MERGE_OPTIONS_VERSION, \
123 GIT_MERGE_OPTIONS_INIT, git_merge_options_init);
124
125 /* push */
126 CHECK_MACRO_FUNC_INIT_EQUAL( \
127 git_push_options, GIT_PUSH_OPTIONS_VERSION, \
128 GIT_PUSH_OPTIONS_INIT, git_push_options_init);
129
130 /* remote */
131 CHECK_MACRO_FUNC_INIT_EQUAL( \
132 git_remote_callbacks, GIT_REMOTE_CALLBACKS_VERSION, \
133 GIT_REMOTE_CALLBACKS_INIT, git_remote_init_callbacks);
134
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);
139
140 /* revert */
141 CHECK_MACRO_FUNC_INIT_EQUAL( \
142 git_revert_options, GIT_REVERT_OPTIONS_VERSION, \
143 GIT_REVERT_OPTIONS_INIT, git_revert_options_init);
144
145 /* stash apply */
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);
149
150 /* status */
151 CHECK_MACRO_FUNC_INIT_EQUAL( \
152 git_status_options, GIT_STATUS_OPTIONS_VERSION, \
153 GIT_STATUS_OPTIONS_INIT, git_status_options_init);
154
155 /* transport */
156 CHECK_MACRO_FUNC_INIT_EQUAL( \
157 git_transport, GIT_TRANSPORT_VERSION, \
158 GIT_TRANSPORT_INIT, git_transport_init);
159
160 /* config_backend */
161 CHECK_MACRO_FUNC_INIT_EQUAL( \
162 git_config_backend, GIT_CONFIG_BACKEND_VERSION, \
163 GIT_CONFIG_BACKEND_INIT, git_config_init_backend);
164
165 /* odb_backend */
166 CHECK_MACRO_FUNC_INIT_EQUAL( \
167 git_odb_backend, GIT_ODB_BACKEND_VERSION, \
168 GIT_ODB_BACKEND_INIT, git_odb_init_backend);
169
170 /* refdb_backend */
171 CHECK_MACRO_FUNC_INIT_EQUAL( \
172 git_refdb_backend, GIT_REFDB_BACKEND_VERSION, \
173 GIT_REFDB_BACKEND_INIT, git_refdb_init_backend);
174
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);
179
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);
184
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);
188 }