clar.c 18.4 KB
Newer Older
Russell Belfer committed
1 2 3 4 5 6
/*
 * Copyright (c) Vicent Marti. All rights reserved.
 *
 * This file is part of clar, distributed under the ISC license.
 * For full terms see the included COPYING file.
 */
Vicent Marti committed
7 8 9 10 11 12 13
#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>
14
#include <wchar.h>
15
#include <time.h>
Vicent Marti committed
16 17 18 19 20 21 22 23 24 25 26 27 28

/* required for sandboxing */
#include <sys/types.h>
#include <sys/stat.h>

#ifdef _WIN32
#	include <windows.h>
#	include <io.h>
#	include <shellapi.h>
#	include <direct.h>

#	define _MAIN_CC __cdecl

Russell Belfer committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#	ifndef stat
#		define stat(path, st) _stat(path, st)
#	endif
#	ifndef mkdir
#		define mkdir(path, mode) _mkdir(path)
#	endif
#	ifndef chdir
#		define chdir(path) _chdir(path)
#	endif
#	ifndef access
#		define access(path, mode) _access(path, mode)
#	endif
#	ifndef strdup
#		define strdup(str) _strdup(str)
#	endif
#	ifndef strcasecmp
#		define strcasecmp(a,b) _stricmp(a,b)
#	endif
Vicent Marti committed
47 48 49

#	ifndef __MINGW32__
#		pragma comment(lib, "shell32")
Russell Belfer committed
50 51 52 53 54 55 56 57 58 59
#		ifndef strncpy
#			define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE)
#		endif
#		ifndef W_OK
#			define W_OK 02
#		endif
#		ifndef S_ISDIR
#			define S_ISDIR(x) ((x & _S_IFDIR) != 0)
#		endif
#		define p_snprintf(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
Vicent Marti committed
60
#	else
Russell Belfer committed
61 62 63 64 65 66 67 68
#		define p_snprintf snprintf
#	endif

#	ifndef PRIuZ
#		define PRIuZ "Iu"
#	endif
#	ifndef PRIxZ
#		define PRIxZ "Ix"
Vicent Marti committed
69
#	endif
Edward Thomson committed
70

71
#	if defined(_MSC_VER) || defined(__MINGW32__)
Edward Thomson committed
72 73
	typedef struct stat STAT_T;
#	else
Vicent Marti committed
74
	typedef struct _stat STAT_T;
Edward Thomson committed
75
#	endif
Vicent Marti committed
76 77 78 79
#else
#	include <sys/wait.h> /* waitpid(2) */
#	include <unistd.h>
#	define _MAIN_CC
Russell Belfer committed
80 81 82 83 84 85 86
#	define p_snprintf snprintf
#	ifndef PRIuZ
#		define PRIuZ "zu"
#	endif
#	ifndef PRIxZ
#		define PRIxZ "zx"
#	endif
Vicent Marti committed
87 88 89
	typedef struct stat STAT_T;
#endif

90 91
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

Vicent Marti committed
92 93 94 95 96 97 98 99 100 101
#include "clar.h"

static void fs_rm(const char *_source);
static void fs_copy(const char *_source, const char *dest);

static const char *
fixture_path(const char *base, const char *fixture_name);

struct clar_error {
	const char *file;
102
	const char *function;
103
	size_t line_number;
Vicent Marti committed
104 105 106 107 108 109
	const char *error_msg;
	char *description;

	struct clar_error *next;
};

110 111 112 113 114 115
struct clar_explicit {
	size_t suite_idx;
	const char *filter;

	struct clar_explicit *next;
};
Edward Thomson committed
116

Etienne Samson committed
117 118 119 120 121 122
struct clar_report {
	const char *test;
	int test_number;
	const char *suite;

	enum cl_test_status status;
123 124
	time_t start;
	double elapsed;
Etienne Samson committed
125 126 127 128 129 130 131

	struct clar_error *errors;
	struct clar_error *last_error;

	struct clar_report *next;
};

132 133 134 135 136
struct clar_summary {
	const char *filename;
	FILE *fp;
};

137
static struct {
Vicent Marti committed
138
	enum cl_test_status test_status;
Etienne Samson committed
139

Vicent Marti committed
140 141 142
	const char *active_test;
	const char *active_suite;

Vicent Marti committed
143
	int total_skipped;
Vicent Marti committed
144 145 146 147 148
	int total_errors;

	int tests_ran;
	int suites_ran;

149 150
	enum cl_output_format output_format;

Vicent Marti committed
151 152
	int report_errors_only;
	int exit_on_error;
153
	int verbosity;
154

155
	int write_summary;
156
	char *summary_filename;
157
	struct clar_summary *summary;
Vicent Marti committed
158

159 160 161
	struct clar_explicit *explicit;
	struct clar_explicit *last_explicit;

Etienne Samson committed
162 163
	struct clar_report *reports;
	struct clar_report *last_report;
Vicent Marti committed
164 165 166 167 168 169

	void (*local_cleanup)(void *);
	void *local_cleanup_payload;

	jmp_buf trampoline;
	int trampoline_enabled;
170 171 172 173

	cl_trace_cb *pfn_trace_cb;
	void *trace_payload;

Vicent Marti committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
} _clar;

struct clar_func {
	const char *name;
	void (*ptr)(void);
};

struct clar_suite {
	const char *name;
	struct clar_func initialize;
	struct clar_func cleanup;
	const struct clar_func *tests;
	size_t test_count;
	int enabled;
};

/* From clar_print_*.c */
static void clar_print_init(int test_count, int suite_count, const char *suite_names);
static void clar_print_shutdown(int test_count, int suite_count, int error_count);
Etienne Samson committed
193
static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error);
194
static void clar_print_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status failed);
Vicent Marti committed
195 196 197 198 199 200 201
static void clar_print_onsuite(const char *suite_name, int suite_index);
static void clar_print_onabort(const char *msg, ...);

/* From clar_sandbox.c */
static void clar_unsandbox(void);
static int clar_sandbox(void);

202
/* From summary.h */
203 204
static struct clar_summary *clar_summary_init(const char *filename);
static int clar_summary_shutdown(struct clar_summary *fp);
205

Vicent Marti committed
206 207 208
/* Load the declarations for the test suite */
#include "clar.suite"

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

#define CL_TRACE(ev)													\
	do {																\
		if (_clar.pfn_trace_cb)											\
			_clar.pfn_trace_cb(ev,										\
							   _clar.active_suite,						\
							   _clar.active_test,						\
							   _clar.trace_payload);					\
	} while (0)

void cl_trace_register(cl_trace_cb *cb, void *payload)
{
	_clar.pfn_trace_cb = cb;
	_clar.trace_payload = payload;
}


Vicent Marti committed
226 227
/* Core test functions */
static void
228
clar_report_errors(struct clar_report *report)
Etienne Samson committed
229
{
230
	struct clar_error *error;
Etienne Samson committed
231
	int i = 1;
232 233 234

	for (error = report->errors; error; error = error->next)
		clar_print_error(i++, _clar.last_report, error);
Etienne Samson committed
235 236 237 238 239 240
}

static void
clar_report_all(void)
{
	struct clar_report *report;
241 242 243 244 245 246
	struct clar_error *error;
	int i = 1;

	for (report = _clar.reports; report; report = report->next) {
		if (report->status != CL_TEST_FAILURE)
			continue;
Etienne Samson committed
247

248 249
		for (error = report->errors; error; error = error->next)
			clar_print_error(i++, report, error);
Etienne Samson committed
250
	}
Vicent Marti committed
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#ifdef WIN32
# define clar_time DWORD

static void clar_time_now(clar_time *out)
{
	*out = GetTickCount();
}

static double clar_time_diff(clar_time *start, clar_time *end)
{
	return ((double)*end - (double)*start) / 1000;
}
#else
# include <sys/time.h>

# define clar_time struct timeval

static void clar_time_now(clar_time *out)
{
	struct timezone tz;

	gettimeofday(out, &tz);
}

static double clar_time_diff(clar_time *start, clar_time *end)
{
	return ((double)end->tv_sec + (double)end->tv_usec / 1.0E6) -
	       ((double)start->tv_sec + (double)start->tv_usec / 1.0E6);
}
#endif

Vicent Marti committed
284 285
static void
clar_run_test(
286
	const struct clar_suite *suite,
Vicent Marti committed
287 288 289 290
	const struct clar_func *test,
	const struct clar_func *initialize,
	const struct clar_func *cleanup)
{
291
	clar_time start, end;
292

Vicent Marti committed
293 294
	_clar.trampoline_enabled = 1;

295 296
	CL_TRACE(CL_TRACE__TEST__BEGIN);

297 298 299
	_clar.last_report->start = time(NULL);
	clar_time_now(&start);

Vicent Marti committed
300 301 302 303
	if (setjmp(_clar.trampoline) == 0) {
		if (initialize->ptr != NULL)
			initialize->ptr();

304
		CL_TRACE(CL_TRACE__TEST__RUN_BEGIN);
Vicent Marti committed
305
		test->ptr();
306
		CL_TRACE(CL_TRACE__TEST__RUN_END);
Vicent Marti committed
307 308
	}

309
	clar_time_now(&end);
310

Vicent Marti committed
311 312
	_clar.trampoline_enabled = 0;

Etienne Samson committed
313 314 315
	if (_clar.last_report->status == CL_TEST_NOTRUN)
		_clar.last_report->status = CL_TEST_OK;

316
	_clar.last_report->elapsed = clar_time_diff(&start, &end);
317

Vicent Marti committed
318 319 320 321 322 323
	if (_clar.local_cleanup != NULL)
		_clar.local_cleanup(_clar.local_cleanup_payload);

	if (cleanup->ptr != NULL)
		cleanup->ptr();

324 325
	CL_TRACE(CL_TRACE__TEST__END);

Vicent Marti committed
326 327 328 329 330 331
	_clar.tests_ran++;

	/* remove any local-set cleanup methods */
	_clar.local_cleanup = NULL;
	_clar.local_cleanup_payload = NULL;

Vicent Marti committed
332
	if (_clar.report_errors_only) {
333
		clar_report_errors(_clar.last_report);
Vicent Marti committed
334
	} else {
335
		clar_print_ontest(suite->name, test->name, _clar.tests_ran, _clar.last_report->status);
Vicent Marti committed
336
	}
Vicent Marti committed
337 338 339
}

static void
340
clar_run_suite(const struct clar_suite *suite, const char *filter)
Vicent Marti committed
341 342
{
	const struct clar_func *test = suite->tests;
343
	size_t i, matchlen;
Etienne Samson committed
344
	struct clar_report *report;
345
	int exact = 0;
Vicent Marti committed
346 347 348 349 350 351 352 353 354 355 356

	if (!suite->enabled)
		return;

	if (_clar.exit_on_error && _clar.total_errors)
		return;

	if (!_clar.report_errors_only)
		clar_print_onsuite(suite->name, ++_clar.suites_ran);

	_clar.active_suite = suite->name;
357 358
	_clar.active_test = NULL;
	CL_TRACE(CL_TRACE__SUITE_BEGIN);
Vicent Marti committed
359

360
	if (filter) {
361
		size_t suitelen = strlen(suite->name);
362 363 364
		matchlen = strlen(filter);
		if (matchlen <= suitelen) {
			filter = NULL;
365
		} else {
366 367 368 369
			filter += suitelen;
			while (*filter == ':')
				++filter;
			matchlen = strlen(filter);
370 371 372 373 374

			if (matchlen && filter[matchlen - 1] == '$') {
				exact = 1;
				matchlen--;
			}
375 376 377
		}
	}

Vicent Marti committed
378
	for (i = 0; i < suite->test_count; ++i) {
379
		if (filter && strncmp(test[i].name, filter, matchlen))
380 381
			continue;

382 383 384
		if (exact && strlen(test[i].name) != matchlen)
			continue;

Vicent Marti committed
385
		_clar.active_test = test[i].name;
Etienne Samson committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

		report = calloc(1, sizeof(struct clar_report));
		report->suite = _clar.active_suite;
		report->test = _clar.active_test;
		report->test_number = _clar.tests_ran;
		report->status = CL_TEST_NOTRUN;

		if (_clar.reports == NULL)
			_clar.reports = report;

		if (_clar.last_report != NULL)
			_clar.last_report->next = report;

		_clar.last_report = report;

401
		clar_run_test(suite, &test[i], &suite->initialize, &suite->cleanup);
Vicent Marti committed
402 403 404 405

		if (_clar.exit_on_error && _clar.total_errors)
			return;
	}
406 407 408

	_clar.active_test = NULL;
	CL_TRACE(CL_TRACE__SUITE_END);
Vicent Marti committed
409 410 411 412 413 414 415
}

static void
clar_usage(const char *arg)
{
	printf("Usage: %s [options]\n\n", arg);
	printf("Options:\n");
416 417 418 419 420 421
	printf("  -sname        Run only the suite with `name` (can go to individual test name)\n");
	printf("  -iname        Include the suite with `name`\n");
	printf("  -xname        Exclude the suite with `name`\n");
	printf("  -v            Increase verbosity (show suite names)\n");
	printf("  -q            Only report tests that had an error\n");
	printf("  -Q            Quit as soon as a test fails\n");
Edward Thomson committed
422
	printf("  -t            Display results in tap format\n");
423 424
	printf("  -l            Print suite names\n");
	printf("  -r[filename]  Write summary file (to the optional filename)\n");
Vicent Marti committed
425 426 427 428 429 430 431 432
	exit(-1);
}

static void
clar_parse_args(int argc, char **argv)
{
	int i;

433
	/* Verify options before execute */
Vicent Marti committed
434 435 436
	for (i = 1; i < argc; ++i) {
		char *argument = argv[i];

437
		if (argument[0] != '-' || argument[1] == '\0'
Edward Thomson committed
438
		    || strchr("sixvqQtlr", argument[1]) == NULL) {
Vicent Marti committed
439
			clar_usage(argv[0]);
440 441 442 443 444
		}
	}

	for (i = 1; i < argc; ++i) {
		char *argument = argv[i];
Vicent Marti committed
445 446 447 448 449

		switch (argument[1]) {
		case 's':
		case 'i':
		case 'x': { /* given suite name */
Russell Belfer committed
450
			int offset = (argument[2] == '=') ? 3 : 2, found = 0;
Vicent Marti committed
451
			char action = argument[1];
452
			size_t j, arglen, suitelen, cmplen;
Vicent Marti committed
453 454

			argument += offset;
455
			arglen = strlen(argument);
Vicent Marti committed
456

457
			if (arglen == 0)
Vicent Marti committed
458 459 460
				clar_usage(argv[0]);

			for (j = 0; j < _clar_suite_count; ++j) {
461 462
				suitelen = strlen(_clar_suites[j].name);
				cmplen = (arglen < suitelen) ? arglen : suitelen;
463 464

				if (strncmp(argument, _clar_suites[j].name, cmplen) == 0) {
465
					int exact = (arglen >= suitelen);
Russell Belfer committed
466

467 468 469 470 471 472
					/* Do we have a real suite prefix separated by a
					 * trailing '::' or just a matching substring? */
					if (arglen > suitelen && (argument[suitelen] != ':'
						    || argument[suitelen + 1] != ':'))
					    continue;

Russell Belfer committed
473 474 475
					++found;

					if (!exact)
476
						_clar.verbosity = MAX(_clar.verbosity, 1);
Russell Belfer committed
477

Vicent Marti committed
478
					switch (action) {
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
					case 's': {
						struct clar_explicit *explicit =
							calloc(1, sizeof(struct clar_explicit));
						assert(explicit);

						explicit->suite_idx = j;
						explicit->filter = argument;

						if (_clar.explicit == NULL)
							_clar.explicit = explicit;

						if (_clar.last_explicit != NULL)
							_clar.last_explicit->next = explicit;

						_clar_suites[j].enabled = 1;
						_clar.last_explicit = explicit;
						break;
					}
497 498
					case 'i': _clar_suites[j].enabled = 1; break;
					case 'x': _clar_suites[j].enabled = 0; break;
Vicent Marti committed
499
					}
Russell Belfer committed
500 501 502

					if (exact)
						break;
Vicent Marti committed
503 504 505
				}
			}

Russell Belfer committed
506
			if (!found) {
Vicent Marti committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520
				clar_print_onabort("No suite matching '%s' found.\n", argument);
				exit(-1);
			}
			break;
		}

		case 'q':
			_clar.report_errors_only = 1;
			break;

		case 'Q':
			_clar.exit_on_error = 1;
			break;

Edward Thomson committed
521 522 523
		case 't':
			_clar.output_format = CL_OUTPUT_TAP;
			break;
Vicent Marti committed
524 525 526 527 528 529 530 531 532 533

		case 'l': {
			size_t j;
			printf("Test suites (use -s<name> to run just one):\n");
			for (j = 0; j < _clar_suite_count; ++j)
				printf(" %3d: %s\n", (int)j, _clar_suites[j].name);

			exit(0);
		}

534
		case 'v':
535
			_clar.verbosity++;
536 537
			break;

538 539
		case 'r':
			_clar.write_summary = 1;
540
			free(_clar.summary_filename);
541
			_clar.summary_filename = *(argument + 2) ? strdup(argument + 2) : NULL;
542 543
			break;

Vicent Marti committed
544
		default:
545
			assert(!"Unexpected commandline argument!");
Vicent Marti committed
546 547 548 549
		}
	}
}

Edward Thomson committed
550 551
void
clar_test_init(int argc, char **argv)
Vicent Marti committed
552
{
553 554
	const char *summary_env;

555 556 557
	if (argc > 1)
		clar_parse_args(argc, argv);

Vicent Marti committed
558 559 560 561 562 563
	clar_print_init(
		(int)_clar_callback_count,
		(int)_clar_suite_count,
		""
	);

564 565
	if (!_clar.summary_filename &&
	    (summary_env = getenv("CLAR_SUMMARY")) != NULL) {
566
		_clar.write_summary = 1;
567
		_clar.summary_filename = strdup(summary_env);
568 569
	}

570 571 572
	if (_clar.write_summary && !_clar.summary_filename)
		_clar.summary_filename = strdup("summary.xml");

573 574 575
	if (_clar.write_summary &&
	    !(_clar.summary = clar_summary_init(_clar.summary_filename))) {
		clar_print_onabort("Failed to open the summary file\n");
576 577 578
		exit(-1);
	}

Vicent Marti committed
579 580 581 582
	if (clar_sandbox() < 0) {
		clar_print_onabort("Failed to sandbox the test runner.\n");
		exit(-1);
	}
Edward Thomson committed
583
}
Vicent Marti committed
584

Edward Thomson committed
585
int
586
clar_test_run(void)
Edward Thomson committed
587
{
588 589
	size_t i;
	struct clar_explicit *explicit;
Edward Thomson committed
590

591 592 593 594
	if (_clar.explicit) {
		for (explicit = _clar.explicit; explicit; explicit = explicit->next)
			clar_run_suite(&_clar_suites[explicit->suite_idx], explicit->filter);
	} else {
Vicent Marti committed
595
		for (i = 0; i < _clar_suite_count; ++i)
596
			clar_run_suite(&_clar_suites[i], NULL);
Vicent Marti committed
597 598
	}

Edward Thomson committed
599 600 601 602
	return _clar.total_errors;
}

void
603
clar_test_shutdown(void)
Edward Thomson committed
604
{
605
	struct clar_explicit *explicit, *explicit_next;
Etienne Samson committed
606
	struct clar_report *report, *report_next;
607

Vicent Marti committed
608 609 610 611 612 613 614
	clar_print_shutdown(
		_clar.tests_ran,
		(int)_clar_suite_count,
		_clar.total_errors
	);

	clar_unsandbox();
615

616 617 618 619
	if (_clar.write_summary && clar_summary_shutdown(_clar.summary) < 0) {
		clar_print_onabort("Failed to write the summary file\n");
		exit(-1);
	}
620

621 622 623 624
	for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
		explicit_next = explicit->next;
		free(explicit);
	}
Etienne Samson committed
625 626 627 628 629

	for (report = _clar.reports; report; report = report_next) {
		report_next = report->next;
		free(report);
	}
630 631

	free(_clar.summary_filename);
Edward Thomson committed
632 633 634 635 636 637 638 639 640 641 642 643
}

int
clar_test(int argc, char **argv)
{
	int errors;

	clar_test_init(argc, argv);
	errors = clar_test_run();
	clar_test_shutdown();

	return errors;
Vicent Marti committed
644 645
}

Vicent Marti committed
646 647 648 649 650
static void abort_test(void)
{
	if (!_clar.trampoline_enabled) {
		clar_print_onabort(
				"Fatal error: a cleanup method raised an exception.");
651
		clar_report_errors(_clar.last_report);
Vicent Marti committed
652 653 654
		exit(-1);
	}

655
	CL_TRACE(CL_TRACE__TEST__LONGJMP);
Vicent Marti committed
656 657 658 659 660
	longjmp(_clar.trampoline, -1);
}

void clar__skip(void)
{
Etienne Samson committed
661
	_clar.last_report->status = CL_TEST_SKIP;
Vicent Marti committed
662 663 664 665
	_clar.total_skipped++;
	abort_test();
}

Russell Belfer committed
666
void clar__fail(
Vicent Marti committed
667
	const char *file,
668
	const char *function,
669
	size_t line,
Vicent Marti committed
670 671 672 673
	const char *error_msg,
	const char *description,
	int should_abort)
{
Russell Belfer committed
674
	struct clar_error *error = calloc(1, sizeof(struct clar_error));
Vicent Marti committed
675

Etienne Samson committed
676 677
	if (_clar.last_report->errors == NULL)
		_clar.last_report->errors = error;
Vicent Marti committed
678

Etienne Samson committed
679 680
	if (_clar.last_report->last_error != NULL)
		_clar.last_report->last_error->next = error;
Vicent Marti committed
681

Etienne Samson committed
682
	_clar.last_report->last_error = error;
Vicent Marti committed
683 684

	error->file = file;
685
	error->function = function;
Vicent Marti committed
686 687 688 689 690 691 692
	error->line_number = line;
	error->error_msg = error_msg;

	if (description != NULL)
		error->description = strdup(description);

	_clar.total_errors++;
Etienne Samson committed
693
	_clar.last_report->status = CL_TEST_FAILURE;
Vicent Marti committed
694

Vicent Marti committed
695 696
	if (should_abort)
		abort_test();
Vicent Marti committed
697 698
}

Russell Belfer committed
699 700 701
void clar__assert(
	int condition,
	const char *file,
702
	const char *function,
703
	size_t line,
Russell Belfer committed
704 705 706 707 708 709 710
	const char *error_msg,
	const char *description,
	int should_abort)
{
	if (condition)
		return;

711
	clar__fail(file, function, line, error_msg, description, should_abort);
Russell Belfer committed
712 713
}

Russell Belfer committed
714
void clar__assert_equal(
Vicent Marti committed
715
	const char *file,
716
	const char *function,
717
	size_t line,
Vicent Marti committed
718
	const char *err,
Russell Belfer committed
719 720 721
	int should_abort,
	const char *fmt,
	...)
Vicent Marti committed
722
{
Russell Belfer committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
	va_list args;
	char buf[4096];
	int is_equal = 1;

	va_start(args, fmt);

	if (!strcmp("%s", fmt)) {
		const char *s1 = va_arg(args, const char *);
		const char *s2 = va_arg(args, const char *);
		is_equal = (!s1 || !s2) ? (s1 == s2) : !strcmp(s1, s2);

		if (!is_equal) {
			if (s1 && s2) {
				int pos;
				for (pos = 0; s1[pos] == s2[pos] && s1[pos] && s2[pos]; ++pos)
					/* find differing byte offset */;
				p_snprintf(buf, sizeof(buf), "'%s' != '%s' (at byte %d)",
					s1, s2, pos);
			} else {
				p_snprintf(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
			}
		}
	}
Edward Thomson committed
746 747 748
	else if(!strcmp("%.*s", fmt)) {
		const char *s1 = va_arg(args, const char *);
		const char *s2 = va_arg(args, const char *);
Edward Thomson committed
749
		int len = va_arg(args, int);
Edward Thomson committed
750 751 752 753
		is_equal = (!s1 || !s2) ? (s1 == s2) : !strncmp(s1, s2, len);

		if (!is_equal) {
			if (s1 && s2) {
Edward Thomson committed
754
				int pos;
Edward Thomson committed
755 756 757 758 759 760 761 762 763
				for (pos = 0; s1[pos] == s2[pos] && pos < len; ++pos)
					/* find differing byte offset */;
				p_snprintf(buf, sizeof(buf), "'%.*s' != '%.*s' (at byte %d)",
					len, s1, len, s2, pos);
			} else {
				p_snprintf(buf, sizeof(buf), "'%.*s' != '%.*s'", len, s1, len, s2);
			}
		}
	}
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	else if (!strcmp("%ls", fmt)) {
		const wchar_t *wcs1 = va_arg(args, const wchar_t *);
		const wchar_t *wcs2 = va_arg(args, const wchar_t *);
		is_equal = (!wcs1 || !wcs2) ? (wcs1 == wcs2) : !wcscmp(wcs1, wcs2);

		if (!is_equal) {
			if (wcs1 && wcs2) {
				int pos;
				for (pos = 0; wcs1[pos] == wcs2[pos] && wcs1[pos] && wcs2[pos]; ++pos)
					/* find differing byte offset */;
				p_snprintf(buf, sizeof(buf), "'%ls' != '%ls' (at byte %d)",
					wcs1, wcs2, pos);
			} else {
				p_snprintf(buf, sizeof(buf), "'%ls' != '%ls'", wcs1, wcs2);
			}
		}
	}
	else if(!strcmp("%.*ls", fmt)) {
		const wchar_t *wcs1 = va_arg(args, const wchar_t *);
		const wchar_t *wcs2 = va_arg(args, const wchar_t *);
		int len = va_arg(args, int);
		is_equal = (!wcs1 || !wcs2) ? (wcs1 == wcs2) : !wcsncmp(wcs1, wcs2, len);

		if (!is_equal) {
			if (wcs1 && wcs2) {
				int pos;
				for (pos = 0; wcs1[pos] == wcs2[pos] && pos < len; ++pos)
					/* find differing byte offset */;
				p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls' (at byte %d)",
					len, wcs1, len, wcs2, pos);
			} else {
				p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls'", len, wcs1, len, wcs2);
			}
		}
	}
799
	else if (!strcmp("%"PRIuZ, fmt) || !strcmp("%"PRIxZ, fmt)) {
Russell Belfer committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
		size_t sz1 = va_arg(args, size_t), sz2 = va_arg(args, size_t);
		is_equal = (sz1 == sz2);
		if (!is_equal) {
			int offset = p_snprintf(buf, sizeof(buf), fmt, sz1);
			strncat(buf, " != ", sizeof(buf) - offset);
			p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, sz2);
		}
	}
	else if (!strcmp("%p", fmt)) {
		void *p1 = va_arg(args, void *), *p2 = va_arg(args, void *);
		is_equal = (p1 == p2);
		if (!is_equal)
			p_snprintf(buf, sizeof(buf), "%p != %p", p1, p2);
	}
	else {
		int i1 = va_arg(args, int), i2 = va_arg(args, int);
		is_equal = (i1 == i2);
		if (!is_equal) {
			int offset = p_snprintf(buf, sizeof(buf), fmt, i1);
			strncat(buf, " != ", sizeof(buf) - offset);
			p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, i2);
821
		}
Vicent Marti committed
822 823
	}

Russell Belfer committed
824 825 826
	va_end(args);

	if (!is_equal)
827
		clar__fail(file, function, line, err, buf, should_abort);
Vicent Marti committed
828 829 830 831 832 833 834 835 836 837 838 839
}

void cl_set_cleanup(void (*cleanup)(void *), void *opaque)
{
	_clar.local_cleanup = cleanup;
	_clar.local_cleanup_payload = opaque;
}

#include "clar/sandbox.h"
#include "clar/fixtures.h"
#include "clar/fs.h"
#include "clar/print.h"
840
#include "clar/summary.h"