]> git.proxmox.com Git - libgit2.git/blob - tests/core/structinit.c
tests: core: test initialization of `git_proxy_options`
[libgit2.git] / tests / core / structinit.c
1 #include "clar_libgit2.h"
2 #include <git2/sys/config.h>
3 #include <git2/sys/odb_backend.h>
4 #include <git2/sys/refdb_backend.h>
5 #include <git2/sys/transport.h>
6
7 #define STRINGIFY(s) #s
8
9 /* Checks two conditions for the specified structure:
10 * 1. That the initializers for the latest version produces the same
11 * in-memory representation.
12 * 2. That the function-based initializer supports all versions from 1...n,
13 * where n is the latest version (often represented by GIT_*_VERSION).
14 *
15 * Parameters:
16 * structname: The name of the structure to test, e.g. git_blame_options.
17 * structver: The latest version of the specified structure.
18 * macroinit: The macro that initializes the latest version of the structure.
19 * funcinitname: The function that initializes the structure. Must have the
20 * signature "int (structname* instance, int version)".
21 */
22 #define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
23 do { \
24 structname structname##_macro_latest = macroinit; \
25 structname structname##_func_latest; \
26 int structname##_curr_ver = structver - 1; \
27 memset(&structname##_func_latest, 0, sizeof(structname##_func_latest)); \
28 cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
29 options_cmp(&structname##_macro_latest, &structname##_func_latest, \
30 sizeof(structname), STRINGIFY(structname)); \
31 \
32 while (structname##_curr_ver > 0) \
33 { \
34 structname macro; \
35 cl_git_pass(funcinitname(&macro, structname##_curr_ver)); \
36 structname##_curr_ver--; \
37 }\
38 } while(0)
39
40 static void options_cmp(void *one, void *two, size_t size, const char *name)
41 {
42 size_t i;
43
44 for (i = 0; i < size; i++) {
45 if (((char *)one)[i] != ((char *)two)[i]) {
46 char desc[1024];
47
48 p_snprintf(desc, 1024, "Difference in %s at byte %" PRIuZ ": macro=%u / func=%u",
49 name, i, ((char *)one)[i], ((char *)two)[i]);
50 clar__fail(__FILE__, __LINE__,
51 "Difference between macro and function options initializer",
52 desc, 0);
53 return;
54 }
55 }
56 }
57
58 void test_core_structinit__compare(void)
59 {
60 /* These tests assume that they can memcmp() two structures that were
61 * initialized with the same static initializer. Eg,
62 * git_blame_options = GIT_BLAME_OPTIONS_INIT;
63 *
64 * This assumption fails when there is padding between structure members,
65 * which is not guaranteed to be initialized to anything sane at all.
66 *
67 * Assume most compilers, in a debug build, will clear that memory for
68 * us or set it to sentinal markers. Etc.
69 */
70 #if !defined(DEBUG) && !defined(_DEBUG)
71 clar__skip();
72 #endif
73
74 /* blame */
75 CHECK_MACRO_FUNC_INIT_EQUAL( \
76 git_blame_options, GIT_BLAME_OPTIONS_VERSION, \
77 GIT_BLAME_OPTIONS_INIT, git_blame_init_options);
78
79 /* checkout */
80 CHECK_MACRO_FUNC_INIT_EQUAL( \
81 git_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, \
82 GIT_CHECKOUT_OPTIONS_INIT, git_checkout_init_options);
83
84 /* clone */
85 CHECK_MACRO_FUNC_INIT_EQUAL( \
86 git_clone_options, GIT_CLONE_OPTIONS_VERSION, \
87 GIT_CLONE_OPTIONS_INIT, git_clone_init_options);
88
89 /* diff */
90 CHECK_MACRO_FUNC_INIT_EQUAL( \
91 git_diff_options, GIT_DIFF_OPTIONS_VERSION, \
92 GIT_DIFF_OPTIONS_INIT, git_diff_init_options);
93
94 /* diff_find */
95 CHECK_MACRO_FUNC_INIT_EQUAL( \
96 git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
97 GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_init_options);
98
99 /* merge_file_input */
100 CHECK_MACRO_FUNC_INIT_EQUAL( \
101 git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \
102 GIT_MERGE_FILE_INPUT_INIT, git_merge_file_init_input);
103
104 /* merge_file */
105 CHECK_MACRO_FUNC_INIT_EQUAL( \
106 git_merge_file_options, GIT_MERGE_FILE_OPTIONS_VERSION, \
107 GIT_MERGE_FILE_OPTIONS_INIT, git_merge_file_init_options);
108
109 /* merge_tree */
110 CHECK_MACRO_FUNC_INIT_EQUAL( \
111 git_merge_options, GIT_MERGE_OPTIONS_VERSION, \
112 GIT_MERGE_OPTIONS_INIT, git_merge_init_options);
113
114 /* push */
115 CHECK_MACRO_FUNC_INIT_EQUAL( \
116 git_push_options, GIT_PUSH_OPTIONS_VERSION, \
117 GIT_PUSH_OPTIONS_INIT, git_push_init_options);
118
119 /* remote */
120 CHECK_MACRO_FUNC_INIT_EQUAL( \
121 git_remote_callbacks, GIT_REMOTE_CALLBACKS_VERSION, \
122 GIT_REMOTE_CALLBACKS_INIT, git_remote_init_callbacks);
123
124 /* repository_init */
125 CHECK_MACRO_FUNC_INIT_EQUAL( \
126 git_repository_init_options, GIT_REPOSITORY_INIT_OPTIONS_VERSION, \
127 GIT_REPOSITORY_INIT_OPTIONS_INIT, git_repository_init_init_options);
128
129 /* revert */
130 CHECK_MACRO_FUNC_INIT_EQUAL( \
131 git_revert_options, GIT_REVERT_OPTIONS_VERSION, \
132 GIT_REVERT_OPTIONS_INIT, git_revert_init_options);
133
134 /* stash apply */
135 CHECK_MACRO_FUNC_INIT_EQUAL( \
136 git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_VERSION, \
137 GIT_STASH_APPLY_OPTIONS_INIT, git_stash_apply_init_options);
138
139 /* status */
140 CHECK_MACRO_FUNC_INIT_EQUAL( \
141 git_status_options, GIT_STATUS_OPTIONS_VERSION, \
142 GIT_STATUS_OPTIONS_INIT, git_status_init_options);
143
144 /* transport */
145 CHECK_MACRO_FUNC_INIT_EQUAL( \
146 git_transport, GIT_TRANSPORT_VERSION, \
147 GIT_TRANSPORT_INIT, git_transport_init);
148
149 /* config_backend */
150 CHECK_MACRO_FUNC_INIT_EQUAL( \
151 git_config_backend, GIT_CONFIG_BACKEND_VERSION, \
152 GIT_CONFIG_BACKEND_INIT, git_config_init_backend);
153
154 /* odb_backend */
155 CHECK_MACRO_FUNC_INIT_EQUAL( \
156 git_odb_backend, GIT_ODB_BACKEND_VERSION, \
157 GIT_ODB_BACKEND_INIT, git_odb_init_backend);
158
159 /* refdb_backend */
160 CHECK_MACRO_FUNC_INIT_EQUAL( \
161 git_refdb_backend, GIT_REFDB_BACKEND_VERSION, \
162 GIT_REFDB_BACKEND_INIT, git_refdb_init_backend);
163
164 /* submodule update */
165 CHECK_MACRO_FUNC_INIT_EQUAL( \
166 git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
167 GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_init_options);
168
169 /* submodule update */
170 CHECK_MACRO_FUNC_INIT_EQUAL( \
171 git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
172 GIT_PROXY_OPTIONS_INIT, git_proxy_init_options);
173 }