structinit.c 6.54 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include <git2/sys/commit_graph.h>
3
#include <git2/sys/config.h>
4
#include <git2/sys/filter.h>
5 6
#include <git2/sys/odb_backend.h>
#include <git2/sys/refdb_backend.h>
7
#include <git2/sys/transport.h>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#define STRINGIFY(s) #s

/* Checks two conditions for the specified structure:
 *     1. That the initializers for the latest version produces the same
 *        in-memory representation.
 *     2. That the function-based initializer supports all versions from 1...n,
 *        where n is the latest version (often represented by GIT_*_VERSION).
 *
 * Parameters:
 *     structname: The name of the structure to test, e.g. git_blame_options.
 *     structver: The latest version of the specified structure.
 *     macroinit: The macro that initializes the latest version of the structure.
 *     funcinitname: The function that initializes the structure. Must have the
 *                   signature "int (structname* instance, int version)".
 */
#define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
25
do { \
26 27
	structname structname##_macro_latest = macroinit; \
	structname structname##_func_latest; \
28
	int structname##_curr_ver = structver - 1; \
29
	memset(&structname##_func_latest, 0, sizeof(structname##_func_latest)); \
30
	cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
31 32
	options_cmp(&structname##_macro_latest, &structname##_func_latest, \
		sizeof(structname), STRINGIFY(structname)); \
33 34 35 36 37 38
	\
	while (structname##_curr_ver > 0) \
	{ \
		structname macro; \
		cl_git_pass(funcinitname(&macro, structname##_curr_ver)); \
		structname##_curr_ver--; \
39 40
	}\
} while(0)
41

42 43 44 45 46 47 48 49
static void options_cmp(void *one, void *two, size_t size, const char *name)
{
	size_t i;

	for (i = 0; i < size; i++) {
		if (((char *)one)[i] != ((char *)two)[i]) {
			char desc[1024];

50
			p_snprintf(desc, 1024, "Difference in %s at byte %" PRIuZ ": macro=%u / func=%u",
51
				name, i, ((char *)one)[i], ((char *)two)[i]);
52
			clar__fail(__FILE__, __func__, __LINE__,
53 54 55 56 57 58 59
				"Difference between macro and function options initializer",
				desc, 0);
			return;
		}
	}
}

60
void test_core_structinit__compare(void)
61
{
62 63 64 65 66 67 68 69
	/* These tests assume that they can memcmp() two structures that were
	 * initialized with the same static initializer.  Eg,
	 * git_blame_options = GIT_BLAME_OPTIONS_INIT;
	 *
	 * This assumption fails when there is padding between structure members,
	 * which is not guaranteed to be initialized to anything sane at all.
	 *
	 * Assume most compilers, in a debug build, will clear that memory for
Dimitris Apostolou committed
70
	 * us or set it to sentinel markers.  Etc.
71 72 73 74 75
	 */
#if !defined(DEBUG) && !defined(_DEBUG)
	clar__skip();
#endif

76 77 78 79 80
	/* apply */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_apply_options, GIT_APPLY_OPTIONS_VERSION, \
		GIT_APPLY_OPTIONS_INIT, git_apply_options_init);

81 82 83
	/* blame */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_blame_options, GIT_BLAME_OPTIONS_VERSION, \
84
		GIT_BLAME_OPTIONS_INIT, git_blame_options_init);
85

86 87 88 89 90
	/* blob_filter_options */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_blob_filter_options, GIT_BLOB_FILTER_OPTIONS_VERSION, \
		GIT_BLOB_FILTER_OPTIONS_INIT, git_blob_filter_options_init);

91 92
	/* checkout */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
93
		git_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, \
94
		GIT_CHECKOUT_OPTIONS_INIT, git_checkout_options_init);
95 96 97 98

	/* clone */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_clone_options, GIT_CLONE_OPTIONS_VERSION, \
99
		GIT_CLONE_OPTIONS_INIT, git_clone_options_init);
100

101 102 103 104 105 106 107
	/* commit_graph_writer */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_commit_graph_writer_options, \
		GIT_COMMIT_GRAPH_WRITER_OPTIONS_VERSION, \
		GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT, \
		git_commit_graph_writer_options_init);

108 109 110
	/* diff */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_diff_options, GIT_DIFF_OPTIONS_VERSION, \
111
		GIT_DIFF_OPTIONS_INIT, git_diff_options_init);
112 113 114 115

	/* diff_find */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
116
		GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_options_init);
117

118 119 120 121 122
	/* filter */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_filter, GIT_FILTER_VERSION, \
		GIT_FILTER_INIT, git_filter_init);

123 124 125
	/* merge_file_input */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \
126
		GIT_MERGE_FILE_INPUT_INIT, git_merge_file_input_init);
127 128 129 130

	/* merge_file */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_merge_file_options, GIT_MERGE_FILE_OPTIONS_VERSION, \
131
		GIT_MERGE_FILE_OPTIONS_INIT, git_merge_file_options_init);
132

133 134
	/* merge_tree */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
135
		git_merge_options, GIT_MERGE_OPTIONS_VERSION, \
136
		GIT_MERGE_OPTIONS_INIT, git_merge_options_init);
137 138 139 140

	/* push */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_push_options, GIT_PUSH_OPTIONS_VERSION, \
141
		GIT_PUSH_OPTIONS_INIT, git_push_options_init);
142 143 144 145 146 147 148 149 150

	/* remote */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_remote_callbacks, GIT_REMOTE_CALLBACKS_VERSION, \
		GIT_REMOTE_CALLBACKS_INIT, git_remote_init_callbacks);

	/* repository_init */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_repository_init_options, GIT_REPOSITORY_INIT_OPTIONS_VERSION, \
151
		GIT_REPOSITORY_INIT_OPTIONS_INIT, git_repository_init_options_init);
152 153 154

	/* revert */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
155
		git_revert_options, GIT_REVERT_OPTIONS_VERSION, \
156
		GIT_REVERT_OPTIONS_INIT, git_revert_options_init);
157

158 159 160
	/* stash apply */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_VERSION, \
161
		GIT_STASH_APPLY_OPTIONS_INIT, git_stash_apply_options_init);
162

163 164 165
	/* status */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_status_options, GIT_STATUS_OPTIONS_VERSION, \
166
		GIT_STATUS_OPTIONS_INIT, git_status_options_init);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	/* transport */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_transport, GIT_TRANSPORT_VERSION, \
		GIT_TRANSPORT_INIT, git_transport_init);

	/* config_backend */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_config_backend, GIT_CONFIG_BACKEND_VERSION, \
		GIT_CONFIG_BACKEND_INIT, git_config_init_backend);

	/* odb_backend */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_odb_backend, GIT_ODB_BACKEND_VERSION, \
		GIT_ODB_BACKEND_INIT, git_odb_init_backend);

	/* refdb_backend */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_refdb_backend, GIT_REFDB_BACKEND_VERSION, \
		GIT_REFDB_BACKEND_INIT, git_refdb_init_backend);
187 188 189 190

	/* submodule update */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
191
		GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_options_init);
192 193 194 195

	/* submodule update */
	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
196
		GIT_PROXY_OPTIONS_INIT, git_proxy_options_init);
197 198 199

	CHECK_MACRO_FUNC_INIT_EQUAL( \
		git_diff_patchid_options, GIT_DIFF_PATCHID_OPTIONS_VERSION, \
200
		GIT_DIFF_PATCHID_OPTIONS_INIT, git_diff_patchid_options_init);
201
}