Blame tests/core/structinit.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include <git2/sys/config.h>
Packit Service 20376f
#include <git2/sys/filter.h>
Packit Service 20376f
#include <git2/sys/odb_backend.h>
Packit Service 20376f
#include <git2/sys/refdb_backend.h>
Packit Service 20376f
#include <git2/sys/transport.h>
Packit Service 20376f
Packit Service 20376f
#define STRINGIFY(s) #s
Packit Service 20376f
Packit Service 20376f
/* Checks two conditions for the specified structure:
Packit Service 20376f
 *     1. That the initializers for the latest version produces the same
Packit Service 20376f
 *        in-memory representation.
Packit Service 20376f
 *     2. That the function-based initializer supports all versions from 1...n,
Packit Service 20376f
 *        where n is the latest version (often represented by GIT_*_VERSION).
Packit Service 20376f
 *
Packit Service 20376f
 * Parameters:
Packit Service 20376f
 *     structname: The name of the structure to test, e.g. git_blame_options.
Packit Service 20376f
 *     structver: The latest version of the specified structure.
Packit Service 20376f
 *     macroinit: The macro that initializes the latest version of the structure.
Packit Service 20376f
 *     funcinitname: The function that initializes the structure. Must have the
Packit Service 20376f
 *                   signature "int (structname* instance, int version)".
Packit Service 20376f
 */
Packit Service 20376f
#define CHECK_MACRO_FUNC_INIT_EQUAL(structname, structver, macroinit, funcinitname) \
Packit Service 20376f
do { \
Packit Service 20376f
	structname structname##_macro_latest = macroinit; \
Packit Service 20376f
	structname structname##_func_latest; \
Packit Service 20376f
	int structname##_curr_ver = structver - 1; \
Packit Service 20376f
	memset(&structname##_func_latest, 0, sizeof(structname##_func_latest)); \
Packit Service 20376f
	cl_git_pass(funcinitname(&structname##_func_latest, structver)); \
Packit Service 20376f
	options_cmp(&structname##_macro_latest, &structname##_func_latest, \
Packit Service 20376f
		sizeof(structname), STRINGIFY(structname)); \
Packit Service 20376f
	\
Packit Service 20376f
	while (structname##_curr_ver > 0) \
Packit Service 20376f
	{ \
Packit Service 20376f
		structname macro; \
Packit Service 20376f
		cl_git_pass(funcinitname(&macro, structname##_curr_ver)); \
Packit Service 20376f
		structname##_curr_ver--; \
Packit Service 20376f
	}\
Packit Service 20376f
} while(0)
Packit Service 20376f
Packit Service 20376f
static void options_cmp(void *one, void *two, size_t size, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < size; i++) {
Packit Service 20376f
		if (((char *)one)[i] != ((char *)two)[i]) {
Packit Service 20376f
			char desc[1024];
Packit Service 20376f
Packit Service 20376f
			p_snprintf(desc, 1024, "Difference in %s at byte %" PRIuZ ": macro=%u / func=%u",
Packit Service 20376f
				name, i, ((char *)one)[i], ((char *)two)[i]);
Packit Service 20376f
			clar__fail(__FILE__, __LINE__,
Packit Service 20376f
				"Difference between macro and function options initializer",
Packit Service 20376f
				desc, 0);
Packit Service 20376f
			return;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_structinit__compare(void)
Packit Service 20376f
{
Packit Service 20376f
	/* These tests assume that they can memcmp() two structures that were
Packit Service 20376f
	 * initialized with the same static initializer.  Eg,
Packit Service 20376f
	 * git_blame_options = GIT_BLAME_OPTIONS_INIT;
Packit Service 20376f
	 *
Packit Service 20376f
	 * This assumption fails when there is padding between structure members,
Packit Service 20376f
	 * which is not guaranteed to be initialized to anything sane at all.
Packit Service 20376f
	 *
Packit Service 20376f
	 * Assume most compilers, in a debug build, will clear that memory for
Packit Service 20376f
	 * us or set it to sentinal markers.  Etc.
Packit Service 20376f
	 */
Packit Service 20376f
#if !defined(DEBUG) && !defined(_DEBUG)
Packit Service 20376f
	clar__skip();
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	/* blame */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_blame_options, GIT_BLAME_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_BLAME_OPTIONS_INIT, git_blame_init_options);
Packit Service 20376f
Packit Service 20376f
	/* checkout */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_CHECKOUT_OPTIONS_INIT, git_checkout_init_options);
Packit Service 20376f
Packit Service 20376f
	/* clone */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_clone_options, GIT_CLONE_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_CLONE_OPTIONS_INIT, git_clone_init_options);
Packit Service 20376f
Packit Service 20376f
	/* diff */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_diff_options, GIT_DIFF_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_DIFF_OPTIONS_INIT, git_diff_init_options);
Packit Service 20376f
Packit Service 20376f
	/* diff_find */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_init_options);
Packit Service 20376f
Packit Service 20376f
	/* filter */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_filter, GIT_FILTER_VERSION, \
Packit Service 20376f
		GIT_FILTER_INIT, git_filter_init);
Packit Service 20376f
Packit Service 20376f
	/* merge_file_input */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \
Packit Service 20376f
		GIT_MERGE_FILE_INPUT_INIT, git_merge_file_init_input);
Packit Service 20376f
Packit Service 20376f
	/* merge_file */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_merge_file_options, GIT_MERGE_FILE_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_MERGE_FILE_OPTIONS_INIT, git_merge_file_init_options);
Packit Service 20376f
Packit Service 20376f
	/* merge_tree */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_merge_options, GIT_MERGE_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_MERGE_OPTIONS_INIT, git_merge_init_options);
Packit Service 20376f
Packit Service 20376f
	/* push */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_push_options, GIT_PUSH_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_PUSH_OPTIONS_INIT, git_push_init_options);
Packit Service 20376f
Packit Service 20376f
	/* remote */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_remote_callbacks, GIT_REMOTE_CALLBACKS_VERSION, \
Packit Service 20376f
		GIT_REMOTE_CALLBACKS_INIT, git_remote_init_callbacks);
Packit Service 20376f
Packit Service 20376f
	/* repository_init */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_repository_init_options, GIT_REPOSITORY_INIT_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_REPOSITORY_INIT_OPTIONS_INIT, git_repository_init_init_options);
Packit Service 20376f
Packit Service 20376f
	/* revert */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_revert_options, GIT_REVERT_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_REVERT_OPTIONS_INIT, git_revert_init_options);
Packit Service 20376f
Packit Service 20376f
	/* stash apply */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_STASH_APPLY_OPTIONS_INIT, git_stash_apply_init_options);
Packit Service 20376f
Packit Service 20376f
	/* status */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_status_options, GIT_STATUS_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_STATUS_OPTIONS_INIT, git_status_init_options);
Packit Service 20376f
Packit Service 20376f
	/* transport */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_transport, GIT_TRANSPORT_VERSION, \
Packit Service 20376f
		GIT_TRANSPORT_INIT, git_transport_init);
Packit Service 20376f
Packit Service 20376f
	/* config_backend */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_config_backend, GIT_CONFIG_BACKEND_VERSION, \
Packit Service 20376f
		GIT_CONFIG_BACKEND_INIT, git_config_init_backend);
Packit Service 20376f
Packit Service 20376f
	/* odb_backend */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_odb_backend, GIT_ODB_BACKEND_VERSION, \
Packit Service 20376f
		GIT_ODB_BACKEND_INIT, git_odb_init_backend);
Packit Service 20376f
Packit Service 20376f
	/* refdb_backend */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_refdb_backend, GIT_REFDB_BACKEND_VERSION, \
Packit Service 20376f
		GIT_REFDB_BACKEND_INIT, git_refdb_init_backend);
Packit Service 20376f
Packit Service 20376f
	/* submodule update */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_init_options);
Packit Service 20376f
Packit Service 20376f
	/* submodule update */
Packit Service 20376f
	CHECK_MACRO_FUNC_INIT_EQUAL( \
Packit Service 20376f
		git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
Packit Service 20376f
		GIT_PROXY_OPTIONS_INIT, git_proxy_init_options);
Packit Service 20376f
}