]> git.proxmox.com Git - libgit2.git/blame - tests/core/structinit.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / structinit.c
CommitLineData
b9f81997 1#include "clar_libgit2.h"
c25aa7cd 2#include <git2/sys/commit_graph.h>
b9f81997 3#include <git2/sys/config.h>
a78441bc 4#include <git2/sys/filter.h>
b9f81997
MB
5#include <git2/sys/odb_backend.h>
6#include <git2/sys/refdb_backend.h>
c180c065 7#include <git2/sys/transport.h>
b9f81997
MB
8
9#define STRINGIFY(s) #s
10
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).
16 *
17 * Parameters:
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)".
23 */
24#define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
8e524720 25do { \
b9f81997
MB
26 structname structname##_macro_latest = macroinit; \
27 structname structname##_func_latest; \
8e524720 28 int structname##_curr_ver = structver - 1; \
7c48508b 29 memset(&structname##_func_latest, 0, sizeof(structname##_func_latest)); \
b9f81997 30 cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
0ad5c845
ET
31 options_cmp(&structname##_macro_latest, &structname##_func_latest, \
32 sizeof(structname), STRINGIFY(structname)); \
b9f81997 33 \
b9f81997
MB
34 while (structname##_curr_ver > 0) \
35 { \
36 structname macro; \
37 cl_git_pass(funcinitname(&macro, structname##_curr_ver)); \
38 structname##_curr_ver--; \
8e524720
CMN
39 }\
40} while(0)
b9f81997 41
0ad5c845
ET
42static void options_cmp(void *one, void *two, size_t size, const char *name)
43{
44 size_t i;
45
46 for (i = 0; i < size; i++) {
47 if (((char *)one)[i] != ((char *)two)[i]) {
48 char desc[1024];
49
f45f9b6d 50 p_snprintf(desc, 1024, "Difference in %s at byte %" PRIuZ ": macro=%u / func=%u",
0ad5c845 51 name, i, ((char *)one)[i], ((char *)two)[i]);
22a2d3d5 52 clar__fail(__FILE__, __func__, __LINE__,
0ad5c845
ET
53 "Difference between macro and function options initializer",
54 desc, 0);
55 return;
56 }
57 }
58}
59
7c48508b 60void test_core_structinit__compare(void)
b9f81997 61{
7c48508b
ET
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;
65 *
66 * This assumption fails when there is padding between structure members,
67 * which is not guaranteed to be initialized to anything sane at all.
68 *
69 * Assume most compilers, in a debug build, will clear that memory for
e579e0f7 70 * us or set it to sentinel markers. Etc.
7c48508b
ET
71 */
72#if !defined(DEBUG) && !defined(_DEBUG)
73 clar__skip();
74#endif
75
22a2d3d5
UG
76 /* apply */
77 CHECK_MACRO_FUNC_INIT_EQUAL( \
78 git_apply_options, GIT_APPLY_OPTIONS_VERSION, \
79 GIT_APPLY_OPTIONS_INIT, git_apply_options_init);
80
b9f81997
MB
81 /* blame */
82 CHECK_MACRO_FUNC_INIT_EQUAL( \
83 git_blame_options, GIT_BLAME_OPTIONS_VERSION, \
22a2d3d5 84 GIT_BLAME_OPTIONS_INIT, git_blame_options_init);
b9f81997 85
c25aa7cd
PP
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);
90
b9f81997
MB
91 /* checkout */
92 CHECK_MACRO_FUNC_INIT_EQUAL( \
6affd71f 93 git_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, \
22a2d3d5 94 GIT_CHECKOUT_OPTIONS_INIT, git_checkout_options_init);
b9f81997
MB
95
96 /* clone */
97 CHECK_MACRO_FUNC_INIT_EQUAL( \
98 git_clone_options, GIT_CLONE_OPTIONS_VERSION, \
22a2d3d5 99 GIT_CLONE_OPTIONS_INIT, git_clone_options_init);
b9f81997 100
c25aa7cd
PP
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);
107
b9f81997
MB
108 /* diff */
109 CHECK_MACRO_FUNC_INIT_EQUAL( \
110 git_diff_options, GIT_DIFF_OPTIONS_VERSION, \
22a2d3d5 111 GIT_DIFF_OPTIONS_INIT, git_diff_options_init);
b9f81997
MB
112
113 /* diff_find */
114 CHECK_MACRO_FUNC_INIT_EQUAL( \
115 git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
22a2d3d5 116 GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_options_init);
b9f81997 117
a78441bc
MM
118 /* filter */
119 CHECK_MACRO_FUNC_INIT_EQUAL( \
120 git_filter, GIT_FILTER_VERSION, \
121 GIT_FILTER_INIT, git_filter_init);
122
05d47768
ET
123 /* merge_file_input */
124 CHECK_MACRO_FUNC_INIT_EQUAL( \
125 git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \
22a2d3d5 126 GIT_MERGE_FILE_INPUT_INIT, git_merge_file_input_init);
05d47768
ET
127
128 /* merge_file */
129 CHECK_MACRO_FUNC_INIT_EQUAL( \
130 git_merge_file_options, GIT_MERGE_FILE_OPTIONS_VERSION, \
22a2d3d5 131 GIT_MERGE_FILE_OPTIONS_INIT, git_merge_file_options_init);
05d47768 132
b9f81997
MB
133 /* merge_tree */
134 CHECK_MACRO_FUNC_INIT_EQUAL( \
5aa2ac6d 135 git_merge_options, GIT_MERGE_OPTIONS_VERSION, \
22a2d3d5 136 GIT_MERGE_OPTIONS_INIT, git_merge_options_init);
b9f81997 137
b9f81997
MB
138 /* push */
139 CHECK_MACRO_FUNC_INIT_EQUAL( \
140 git_push_options, GIT_PUSH_OPTIONS_VERSION, \
22a2d3d5 141 GIT_PUSH_OPTIONS_INIT, git_push_options_init);
b9f81997
MB
142
143 /* remote */
144 CHECK_MACRO_FUNC_INIT_EQUAL( \
145 git_remote_callbacks, GIT_REMOTE_CALLBACKS_VERSION, \
146 GIT_REMOTE_CALLBACKS_INIT, git_remote_init_callbacks);
147
148 /* repository_init */
149 CHECK_MACRO_FUNC_INIT_EQUAL( \
150 git_repository_init_options, GIT_REPOSITORY_INIT_OPTIONS_VERSION, \
22a2d3d5 151 GIT_REPOSITORY_INIT_OPTIONS_INIT, git_repository_init_options_init);
b9f81997
MB
152
153 /* revert */
154 CHECK_MACRO_FUNC_INIT_EQUAL( \
aa17c3c6 155 git_revert_options, GIT_REVERT_OPTIONS_VERSION, \
22a2d3d5 156 GIT_REVERT_OPTIONS_INIT, git_revert_options_init);
b9f81997 157
19c80a6f
ET
158 /* stash apply */
159 CHECK_MACRO_FUNC_INIT_EQUAL( \
160 git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_VERSION, \
22a2d3d5 161 GIT_STASH_APPLY_OPTIONS_INIT, git_stash_apply_options_init);
19c80a6f 162
b9f81997
MB
163 /* status */
164 CHECK_MACRO_FUNC_INIT_EQUAL( \
165 git_status_options, GIT_STATUS_OPTIONS_VERSION, \
22a2d3d5 166 GIT_STATUS_OPTIONS_INIT, git_status_options_init);
b9f81997
MB
167
168 /* transport */
169 CHECK_MACRO_FUNC_INIT_EQUAL( \
170 git_transport, GIT_TRANSPORT_VERSION, \
171 GIT_TRANSPORT_INIT, git_transport_init);
172
173 /* config_backend */
174 CHECK_MACRO_FUNC_INIT_EQUAL( \
175 git_config_backend, GIT_CONFIG_BACKEND_VERSION, \
176 GIT_CONFIG_BACKEND_INIT, git_config_init_backend);
177
178 /* odb_backend */
179 CHECK_MACRO_FUNC_INIT_EQUAL( \
180 git_odb_backend, GIT_ODB_BACKEND_VERSION, \
181 GIT_ODB_BACKEND_INIT, git_odb_init_backend);
182
183 /* refdb_backend */
184 CHECK_MACRO_FUNC_INIT_EQUAL( \
185 git_refdb_backend, GIT_REFDB_BACKEND_VERSION, \
186 GIT_REFDB_BACKEND_INIT, git_refdb_init_backend);
c868981f
DC
187
188 /* submodule update */
189 CHECK_MACRO_FUNC_INIT_EQUAL( \
190 git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
22a2d3d5 191 GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_options_init);
95170294
PS
192
193 /* submodule update */
194 CHECK_MACRO_FUNC_INIT_EQUAL( \
195 git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
22a2d3d5 196 GIT_PROXY_OPTIONS_INIT, git_proxy_options_init);
eae0bfdc
PP
197
198 CHECK_MACRO_FUNC_INIT_EQUAL( \
199 git_diff_patchid_options, GIT_DIFF_PATCHID_OPTIONS_VERSION, \
22a2d3d5 200 GIT_DIFF_PATCHID_OPTIONS_INIT, git_diff_patchid_options_init);
b9f81997 201}