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>
7 #define STRINGIFY(s) #s
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).
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)".
22 #define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
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)); \
32 while (structname##_curr_ver > 0) \
35 cl_git_pass(funcinitname(¯o, structname##_curr_ver)); \
36 structname##_curr_ver--; \
40 static void options_cmp(void *one
, void *two
, size_t size
, const char *name
)
44 for (i
= 0; i
< size
; i
++) {
45 if (((char *)one
)[i
] != ((char *)two
)[i
]) {
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",
58 void test_core_structinit__compare(void)
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;
64 * This assumption fails when there is padding between structure members,
65 * which is not guaranteed to be initialized to anything sane at all.
67 * Assume most compilers, in a debug build, will clear that memory for
68 * us or set it to sentinal markers. Etc.
70 #if !defined(DEBUG) && !defined(_DEBUG)
75 CHECK_MACRO_FUNC_INIT_EQUAL( \
76 git_blame_options
, GIT_BLAME_OPTIONS_VERSION
, \
77 GIT_BLAME_OPTIONS_INIT
, git_blame_init_options
);
80 CHECK_MACRO_FUNC_INIT_EQUAL( \
81 git_checkout_options
, GIT_CHECKOUT_OPTIONS_VERSION
, \
82 GIT_CHECKOUT_OPTIONS_INIT
, git_checkout_init_options
);
85 CHECK_MACRO_FUNC_INIT_EQUAL( \
86 git_clone_options
, GIT_CLONE_OPTIONS_VERSION
, \
87 GIT_CLONE_OPTIONS_INIT
, git_clone_init_options
);
90 CHECK_MACRO_FUNC_INIT_EQUAL( \
91 git_diff_options
, GIT_DIFF_OPTIONS_VERSION
, \
92 GIT_DIFF_OPTIONS_INIT
, git_diff_init_options
);
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
);
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
);
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
);
110 CHECK_MACRO_FUNC_INIT_EQUAL( \
111 git_merge_options
, GIT_MERGE_OPTIONS_VERSION
, \
112 GIT_MERGE_OPTIONS_INIT
, git_merge_init_options
);
115 CHECK_MACRO_FUNC_INIT_EQUAL( \
116 git_push_options
, GIT_PUSH_OPTIONS_VERSION
, \
117 GIT_PUSH_OPTIONS_INIT
, git_push_init_options
);
120 CHECK_MACRO_FUNC_INIT_EQUAL( \
121 git_remote_callbacks
, GIT_REMOTE_CALLBACKS_VERSION
, \
122 GIT_REMOTE_CALLBACKS_INIT
, git_remote_init_callbacks
);
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
);
130 CHECK_MACRO_FUNC_INIT_EQUAL( \
131 git_revert_options
, GIT_REVERT_OPTIONS_VERSION
, \
132 GIT_REVERT_OPTIONS_INIT
, git_revert_init_options
);
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
);
140 CHECK_MACRO_FUNC_INIT_EQUAL( \
141 git_status_options
, GIT_STATUS_OPTIONS_VERSION
, \
142 GIT_STATUS_OPTIONS_INIT
, git_status_init_options
);
145 CHECK_MACRO_FUNC_INIT_EQUAL( \
146 git_transport
, GIT_TRANSPORT_VERSION
, \
147 GIT_TRANSPORT_INIT
, git_transport_init
);
150 CHECK_MACRO_FUNC_INIT_EQUAL( \
151 git_config_backend
, GIT_CONFIG_BACKEND_VERSION
, \
152 GIT_CONFIG_BACKEND_INIT
, git_config_init_backend
);
155 CHECK_MACRO_FUNC_INIT_EQUAL( \
156 git_odb_backend
, GIT_ODB_BACKEND_VERSION
, \
157 GIT_ODB_BACKEND_INIT
, git_odb_init_backend
);
160 CHECK_MACRO_FUNC_INIT_EQUAL( \
161 git_refdb_backend
, GIT_REFDB_BACKEND_VERSION
, \
162 GIT_REFDB_BACKEND_INIT
, git_refdb_init_backend
);
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
);
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
);