1
+/*
2
+ * Copyright (c) Vicent Marti. All rights reserved.
3
+ *
4
+ * This file is part of clar, distributed under the ISC license.
5
+ * For full terms see the included COPYING file.
6
+ */
7
+#include <assert.h>
8
+#include <setjmp.h>
9
+#include <stdlib.h>
10
+#include <stdio.h>
11
+#include <string.h>
12
+#include <math.h>
13
+#include <stdarg.h>
14
+#include <wchar.h>
15
+#include <time.h>
16
+
17
+/* required for sandboxing */
18
+#include <sys/types.h>
19
+#include <sys/stat.h>
20
+
21
+#ifdef _WIN32
22
+# include <windows.h>
23
+# include <io.h>
24
+# include <shellapi.h>
25
+# include <direct.h>
26
+
27
+# define _MAIN_CC __cdecl
28
+
29
+# ifndef stat
30
+# define stat(path, st) _stat(path, st)
31
+# endif
32
+# ifndef mkdir
33
+# define mkdir(path, mode) _mkdir(path)
34
+# endif
35
+# ifndef chdir
36
+# define chdir(path) _chdir(path)
37
+# endif
38
+# ifndef access
39
+# define access(path, mode) _access(path, mode)
40
+# endif
41
+# ifndef strdup
42
+# define strdup(str) _strdup(str)
43
+# endif
44
+# ifndef strcasecmp
45
+# define strcasecmp(a,b) _stricmp(a,b)
46
+# endif
47
+
48
+# ifndef __MINGW32__
49
+# pragma comment(lib, "shell32")
50
+# ifndef strncpy
51
+# define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE)
52
+# endif
53
+# ifndef W_OK
54
+# define W_OK 02
55
+# endif
56
+# ifndef S_ISDIR
57
+# define S_ISDIR(x) ((x & _S_IFDIR) != 0)
58
+# endif
59
+# define p_snprintf(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
60
+# else
61
+# define p_snprintf snprintf
62
+# endif
63
+
64
+# ifndef PRIuZ
65
+# define PRIuZ "Iu"
66
+# endif
67
+# ifndef PRIxZ
68
+# define PRIxZ "Ix"
69
+# endif
70
+
71
+# if defined(_MSC_VER) || defined(__MINGW32__)
72
+ typedef struct stat STAT_T;
73
+# else
74
+ typedef struct _stat STAT_T;
75
+# endif
76
+#else
77
+# include <sys/wait.h> /* waitpid(2) */
78
+# include <unistd.h>
79
+# define _MAIN_CC
80
+# define p_snprintf snprintf
81
+# ifndef PRIuZ
82
+# define PRIuZ "zu"
83
+# endif
84
+# ifndef PRIxZ
85
+# define PRIxZ "zx"
86
+# endif
87
+ typedef struct stat STAT_T;
88
+#endif
89
+
90
+#define MAX(x, y) (((x) > (y)) ? (x) : (y))
91
+
92
+#include "clar.h"
93
+
94
+static void fs_rm(const char *_source);
95
+static void fs_copy(const char *_source, const char *dest);
96
+
97
+#ifdef CLAR_FIXTURE_PATH
98
+static const char *
99
+fixture_path(const char *base, const char *fixture_name);
100
+#endif
101
+
102
+struct clar_error {
103
+ const char *file;
104
+ const char *function;
105
+ size_t line_number;
106
+ const char *error_msg;
107
+ char *description;
108
+
109
+ struct clar_error *next;
110
+};
111
+
112
+struct clar_explicit {
113
+ size_t suite_idx;
114
+ const char *filter;
115
+
116
+ struct clar_explicit *next;
117
+};
118
+
119
+struct clar_report {
120
+ const char *test;
121
+ int test_number;
122
+ const char *suite;
123
+
124
+ enum cl_test_status status;
125
+ time_t start;
126
+ double elapsed;
127
+
128
+ struct clar_error *errors;
129
+ struct clar_error *last_error;
130
+
131
+ struct clar_report *next;
132
+};
133
+
134
+struct clar_summary {
135
+ const char *filename;
136
+ FILE *fp;
137
+};
138
+
139
+static struct {
140
+ enum cl_test_status test_status;
141
+
142
+ const char *active_test;
143
+ const char *active_suite;
144
+
145
+ int total_skipped;
146
+ int total_errors;
147
+
148
+ int tests_ran;
149
+ int suites_ran;
150
+
151
+ enum cl_output_format output_format;
152
+
153
+ int report_errors_only;
154
+ int exit_on_error;
155
+ int verbosity;
156
+
157
+ int write_summary;
158
+ char *summary_filename;
159
+ struct clar_summary *summary;
160
+
161
+ struct clar_explicit *explicit;
162
+ struct clar_explicit *last_explicit;
163
+
164
+ struct clar_report *reports;
165
+ struct clar_report *last_report;
166
+
167
+ void (*local_cleanup)(void *);
168
+ void *local_cleanup_payload;
169
+
170
+ jmp_buf trampoline;
171
+ int trampoline_enabled;
172
+
173
+ cl_trace_cb *pfn_trace_cb;
174
+ void *trace_payload;
175
+
176
+} _clar;
177
+
178
+struct clar_func {
179
+ const char *name;
180
+ void (*ptr)(void);
181
+};
182
+
183
+struct clar_suite {
184
+ const char *name;
185
+ struct clar_func initialize;
186
+ struct clar_func cleanup;
187
+ const struct clar_func *tests;
188
+ size_t test_count;
189
+ int enabled;
190
+};
191
+
192
+/* From clar_print_*.c */
193
+static void clar_print_init(int test_count, int suite_count, const char *suite_names);
194
+static void clar_print_shutdown(int test_count, int suite_count, int error_count);
195
+static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error);
196
+static void clar_print_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status failed);
197
+static void clar_print_onsuite(const char *suite_name, int suite_index);
198
+static void clar_print_onabort(const char *msg, ...);
199
+
200
+/* From clar_sandbox.c */
201
+static void clar_unsandbox(void);
202
+static int clar_sandbox(void);
203
+
204
+/* From summary.h */
205
+static struct clar_summary *clar_summary_init(const char *filename);
206
+static int clar_summary_shutdown(struct clar_summary *fp);
207
+
208
+/* Load the declarations for the test suite */
209
+#include "clar.suite"
210
+
211
+
212
+#define CL_TRACE(ev) \
213
+ do { \
214
+ if (_clar.pfn_trace_cb) \
215
+ _clar.pfn_trace_cb(ev, \
216
+ _clar.active_suite, \
217
+ _clar.active_test, \
218
+ _clar.trace_payload); \
219
+ } while (0)
220
+
221
+void cl_trace_register(cl_trace_cb *cb, void *payload)
222
+{
223
+ _clar.pfn_trace_cb = cb;
224
+ _clar.trace_payload = payload;
225
+}
226
+
227
+
228
+/* Core test functions */
229
+static void
230
+clar_report_errors(struct clar_report *report)
231
+{
232
+ struct clar_error *error;
233
+ int i = 1;
234
+
235
+ for (error = report->errors; error; error = error->next)
236
+ clar_print_error(i++, _clar.last_report, error);
237
+}
238
+
239
+static void
240
+clar_report_all(void)
241
+{
242
+ struct clar_report *report;
243
+ struct clar_error *error;
244
+ int i = 1;
245
+
246
+ for (report = _clar.reports; report; report = report->next) {
247
+ if (report->status != CL_TEST_FAILURE)
248
+ continue;
249
+
250
+ for (error = report->errors; error; error = error->next)
251
+ clar_print_error(i++, report, error);
252
+ }
253
+}
254
+
255
+#ifdef WIN32
256
+# define clar_time DWORD
257
+
258
+static void clar_time_now(clar_time *out)
259
+{
260
+ *out = GetTickCount();
261
+}
262
+
263
+static double clar_time_diff(clar_time *start, clar_time *end)
264
+{
265
+ return ((double)*end - (double)*start) / 1000;
266
+}
267
+#else
268
+# include <sys/time.h>
269
+
270
+# define clar_time struct timeval
271
+
272
+static void clar_time_now(clar_time *out)
273
+{
274
+ struct timezone tz;
275
+
276
+ gettimeofday(out, &tz);
277
+}
278
+
279
+static double clar_time_diff(clar_time *start, clar_time *end)
280
+{
281
+ return ((double)end->tv_sec + (double)end->tv_usec / 1.0E6) -
282
+ ((double)start->tv_sec + (double)start->tv_usec / 1.0E6);
283
+}
284
+#endif
285
+
286
+static void
287
+clar_run_test(
288
+ const struct clar_suite *suite,
289
+ const struct clar_func *test,
290
+ const struct clar_func *initialize,
291
+ const struct clar_func *cleanup)
292
+{
293
+ clar_time start, end;
294
+
295
+ _clar.trampoline_enabled = 1;
296
+
297
+ CL_TRACE(CL_TRACE__TEST__BEGIN);
298
+
299
+ _clar.last_report->start = time(NULL);
300
+ clar_time_now(&start);
301
+
302
+ if (setjmp(_clar.trampoline) == 0) {
303
+ if (initialize->ptr != NULL)
304
+ initialize->ptr();
305
+
306
+ CL_TRACE(CL_TRACE__TEST__RUN_BEGIN);
307
+ test->ptr();
308
+ CL_TRACE(CL_TRACE__TEST__RUN_END);
309
+ }
310
+
311
+ clar_time_now(&end);
312
+
313
+ _clar.trampoline_enabled = 0;
314
+
315
+ if (_clar.last_report->status == CL_TEST_NOTRUN)
316
+ _clar.last_report->status = CL_TEST_OK;
317
+
318
+ _clar.last_report->elapsed = clar_time_diff(&start, &end);
319
+
320
+ if (_clar.local_cleanup != NULL)
321
+ _clar.local_cleanup(_clar.local_cleanup_payload);
322
+
323
+ if (cleanup->ptr != NULL)
324
+ cleanup->ptr();
325
+
326
+ CL_TRACE(CL_TRACE__TEST__END);
327
+
328
+ _clar.tests_ran++;
329
+
330
+ /* remove any local-set cleanup methods */
331
+ _clar.local_cleanup = NULL;
332
+ _clar.local_cleanup_payload = NULL;
333
+
334
+ if (_clar.report_errors_only) {
335
+ clar_report_errors(_clar.last_report);
336
+ } else {
337
+ clar_print_ontest(suite->name, test->name, _clar.tests_ran, _clar.last_report->status);
338
+ }
339
+}
340
+
341
+static void
342
+clar_run_suite(const struct clar_suite *suite, const char *filter)
343
+{
344
+ const struct clar_func *test = suite->tests;
345
+ size_t i, matchlen;
346
+ struct clar_report *report;
347
+ int exact = 0;
348
+
349
+ if (!suite->enabled)
350
+ return;
351
+
352
+ if (_clar.exit_on_error && _clar.total_errors)
353
+ return;
354
+
355
+ if (!_clar.report_errors_only)
356
+ clar_print_onsuite(suite->name, ++_clar.suites_ran);
357
+
358
+ _clar.active_suite = suite->name;
359
+ _clar.active_test = NULL;
360
+ CL_TRACE(CL_TRACE__SUITE_BEGIN);
361
+
362
+ if (filter) {
363
+ size_t suitelen = strlen(suite->name);
364
+ matchlen = strlen(filter);
365
+ if (matchlen <= suitelen) {
366
+ filter = NULL;
367
+ } else {
368
+ filter += suitelen;
369
+ while (*filter == ':')
370
+ ++filter;
371
+ matchlen = strlen(filter);
372
+
373
+ if (matchlen && filter[matchlen - 1] == '$') {
374
+ exact = 1;
375
+ matchlen--;
376
+ }
377
+ }
378
+ }
379
+
380
+ for (i = 0; i < suite->test_count; ++i) {
381
+ if (filter && strncmp(test[i].name, filter, matchlen))
382
+ continue;
383
+
384
+ if (exact && strlen(test[i].name) != matchlen)
385
+ continue;
386
+
387
+ _clar.active_test = test[i].name;
388
+
389
+ report = calloc(1, sizeof(struct clar_report));
390
+ report->suite = _clar.active_suite;
391
+ report->test = _clar.active_test;
392
+ report->test_number = _clar.tests_ran;
393
+ report->status = CL_TEST_NOTRUN;
394
+
395
+ if (_clar.reports == NULL)
396
+ _clar.reports = report;
397
+
398
+ if (_clar.last_report != NULL)
399
+ _clar.last_report->next = report;
400
+
401
+ _clar.last_report = report;
402
+
403
+ clar_run_test(suite, &test[i], &suite->initialize, &suite->cleanup);
404
+
405
+ if (_clar.exit_on_error && _clar.total_errors)
406
+ return;
407
+ }
408
+
409
+ _clar.active_test = NULL;
410
+ CL_TRACE(CL_TRACE__SUITE_END);
411
+}
412
+
413
+static void
414
+clar_usage(const char *arg)
415
+{
416
+ printf("Usage: %s [options]\n\n", arg);
417
+ printf("Options:\n");
418
+ printf(" -sname Run only the suite with `name` (can go to individual test name)\n");
419
+ printf(" -iname Include the suite with `name`\n");
420
+ printf(" -xname Exclude the suite with `name`\n");
421
+ printf(" -v Increase verbosity (show suite names)\n");
422
+ printf(" -q Only report tests that had an error\n");
423
+ printf(" -Q Quit as soon as a test fails\n");
424
+ printf(" -t Display results in tap format\n");
425
+ printf(" -l Print suite names\n");
426
+ printf(" -r[filename] Write summary file (to the optional filename)\n");
427
+ exit(-1);
428
+}
429
+
430
+static void
431
+clar_parse_args(int argc, char **argv)
432
+{
433
+ int i;
434
+
435
+ /* Verify options before execute */
436
+ for (i = 1; i < argc; ++i) {
437
+ char *argument = argv[i];
438
+
439
+ if (argument[0] != '-' || argument[1] == '\0'
440
+ || strchr("sixvqQtlr", argument[1]) == NULL) {
441
+ clar_usage(argv[0]);
442
+ }
443
+ }
444
+
445
+ for (i = 1; i < argc; ++i) {
446
+ char *argument = argv[i];
447
+
448
+ switch (argument[1]) {
449
+ case 's':
450
+ case 'i':
451
+ case 'x': { /* given suite name */
452
+ int offset = (argument[2] == '=') ? 3 : 2, found = 0;
453
+ char action = argument[1];
454
+ size_t j, arglen, suitelen, cmplen;
455
+
456
+ argument += offset;
457
+ arglen = strlen(argument);
458
+
459
+ if (arglen == 0)
460
+ clar_usage(argv[0]);
461
+
462
+ for (j = 0; j < _clar_suite_count; ++j) {
463
+ suitelen = strlen(_clar_suites[j].name);
464
+ cmplen = (arglen < suitelen) ? arglen : suitelen;
465
+
466
+ if (strncmp(argument, _clar_suites[j].name, cmplen) == 0) {
467
+ int exact = (arglen >= suitelen);
468
+
469
+ /* Do we have a real suite prefix separated by a
470
+ * trailing '::' or just a matching substring? */
471
+ if (arglen > suitelen && (argument[suitelen] != ':'
472
+ || argument[suitelen + 1] != ':'))
473
+ continue;
474
+
475
+ ++found;
476
+
477
+ if (!exact)
478
+ _clar.verbosity = MAX(_clar.verbosity, 1);
479
+
480
+ switch (action) {
481
+ case 's': {
482
+ struct clar_explicit *explicit =
483
+ calloc(1, sizeof(struct clar_explicit));
484
+ assert(explicit);
485
+
486
+ explicit->suite_idx = j;
487
+ explicit->filter = argument;
488
+
489
+ if (_clar.explicit == NULL)
490
+ _clar.explicit = explicit;
491
+
492
+ if (_clar.last_explicit != NULL)
493
+ _clar.last_explicit->next = explicit;
494
+
495
+ _clar_suites[j].enabled = 1;
496
+ _clar.last_explicit = explicit;
497
+ break;
498
+ }
499
+ case 'i': _clar_suites[j].enabled = 1; break;
500
+ case 'x': _clar_suites[j].enabled = 0; break;
501
+ }
502
+
503
+ if (exact)
504
+ break;
505
+ }
506
+ }
507
+
508
+ if (!found) {
509
+ clar_print_onabort("No suite matching '%s' found.\n", argument);
510
+ exit(-1);
511
+ }
512
+ break;
513
+ }
514
+
515
+ case 'q':
516
+ _clar.report_errors_only = 1;
517
+ break;
518
+
519
+ case 'Q':
520
+ _clar.exit_on_error = 1;
521
+ break;
522
+
523
+ case 't':
524
+ _clar.output_format = CL_OUTPUT_TAP;
525
+ break;
526
+
527
+ case 'l': {
528
+ size_t j;
529
+ printf("Test suites (use -s<name> to run just one):\n");
530
+ for (j = 0; j < _clar_suite_count; ++j)
531
+ printf(" %3d: %s\n", (int)j, _clar_suites[j].name);
532
+
533
+ exit(0);
534
+ }
535
+
536
+ case 'v':
537
+ _clar.verbosity++;
538
+ break;
539
+
540
+ case 'r':
541
+ _clar.write_summary = 1;
542
+ free(_clar.summary_filename);
543
+ _clar.summary_filename = *(argument + 2) ? strdup(argument + 2) : NULL;
544
+ break;
545
+
546
+ default:
547
+ assert(!"Unexpected commandline argument!");
548
+ }
549
+ }
550
+}
551
+
552
+void
553
+clar_test_init(int argc, char **argv)
554
+{
555
+ const char *summary_env;
556
+
557
+ if (argc > 1)
558
+ clar_parse_args(argc, argv);
559
+
560
+ clar_print_init(
561
+ (int)_clar_callback_count,
562
+ (int)_clar_suite_count,
563
+ ""
564
+ );
565
+
566
+ if (!_clar.summary_filename &&
567
+ (summary_env = getenv("CLAR_SUMMARY")) != NULL) {
568
+ _clar.write_summary = 1;
569
+ _clar.summary_filename = strdup(summary_env);
570
+ }
571
+
572
+ if (_clar.write_summary && !_clar.summary_filename)
573
+ _clar.summary_filename = strdup("summary.xml");
574
+
575
+ if (_clar.write_summary &&
576
+ !(_clar.summary = clar_summary_init(_clar.summary_filename))) {
577
+ clar_print_onabort("Failed to open the summary file\n");
578
+ exit(-1);
579
+ }
580
+
581
+ if (clar_sandbox() < 0) {
582
+ clar_print_onabort("Failed to sandbox the test runner.\n");
583
+ exit(-1);
584
+ }
585
+}
586
+
587
+int
588
+clar_test_run(void)
589
+{
590
+ size_t i;
591
+ struct clar_explicit *explicit;
592
+
593
+ if (_clar.explicit) {
594
+ for (explicit = _clar.explicit; explicit; explicit = explicit->next)
595
+ clar_run_suite(&_clar_suites[explicit->suite_idx], explicit->filter);
596
+ } else {
597
+ for (i = 0; i < _clar_suite_count; ++i)
598
+ clar_run_suite(&_clar_suites[i], NULL);
599
+ }
600
+
601
+ return _clar.total_errors;
602
+}
603
+
604
+void
605
+clar_test_shutdown(void)
606
+{
607
+ struct clar_explicit *explicit, *explicit_next;
608
+ struct clar_report *report, *report_next;
609
+
610
+ clar_print_shutdown(
611
+ _clar.tests_ran,
612
+ (int)_clar_suite_count,
613
+ _clar.total_errors
614
+ );
615
+
616
+ clar_unsandbox();
617
+
618
+ if (_clar.write_summary && clar_summary_shutdown(_clar.summary) < 0) {
619
+ clar_print_onabort("Failed to write the summary file\n");
620
+ exit(-1);
621
+ }
622
+
623
+ for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
624
+ explicit_next = explicit->next;
625
+ free(explicit);
626
+ }
627
+
628
+ for (report = _clar.reports; report; report = report_next) {
629
+ report_next = report->next;
630
+ free(report);
631
+ }
632
+
633
+ free(_clar.summary_filename);
634
+}
635
+
636
+int
637
+clar_test(int argc, char **argv)
638
+{
639
+ int errors;
640
+
641
+ clar_test_init(argc, argv);
642
+ errors = clar_test_run();
643
+ clar_test_shutdown();
644
+
645
+ return errors;
646
+}
647
+
648
+static void abort_test(void)
649
+{
650
+ if (!_clar.trampoline_enabled) {
651
+ clar_print_onabort(
652
+ "Fatal error: a cleanup method raised an exception.");
653
+ clar_report_errors(_clar.last_report);
654
+ exit(-1);
655
+ }
656
+
657
+ CL_TRACE(CL_TRACE__TEST__LONGJMP);
658
+ longjmp(_clar.trampoline, -1);
659
+}
660
+
661
+void clar__skip(void)
662
+{
663
+ _clar.last_report->status = CL_TEST_SKIP;
664
+ _clar.total_skipped++;
665
+ abort_test();
666
+}
667
+
668
+void clar__fail(
669
+ const char *file,
670
+ const char *function,
671
+ size_t line,
672
+ const char *error_msg,
673
+ const char *description,
674
+ int should_abort)
675
+{
676
+ struct clar_error *error = calloc(1, sizeof(struct clar_error));
677
+
678
+ if (_clar.last_report->errors == NULL)
679
+ _clar.last_report->errors = error;
680
+
681
+ if (_clar.last_report->last_error != NULL)
682
+ _clar.last_report->last_error->next = error;
683
+
684
+ _clar.last_report->last_error = error;
685
+
686
+ error->file = file;
687
+ error->function = function;
688
+ error->line_number = line;
689
+ error->error_msg = error_msg;
690
+
691
+ if (description != NULL)
692
+ error->description = strdup(description);
693
+
694
+ _clar.total_errors++;
695
+ _clar.last_report->status = CL_TEST_FAILURE;
696
+
697
+ if (should_abort)
698
+ abort_test();
699
+}
700
+
701
+void clar__assert(
702
+ int condition,
703
+ const char *file,
704
+ const char *function,
705
+ size_t line,
706
+ const char *error_msg,
707
+ const char *description,
708
+ int should_abort)
709
+{
710
+ if (condition)
711
+ return;
712
+
713
+ clar__fail(file, function, line, error_msg, description, should_abort);
714
+}
715
+
716
+void clar__assert_equal(
717
+ const char *file,
718
+ const char *function,
719
+ size_t line,
720
+ const char *err,
721
+ int should_abort,
722
+ const char *fmt,
723
+ ...)
724
+{
725
+ va_list args;
726
+ char buf[4096];
727
+ int is_equal = 1;
728
+
729
+ va_start(args, fmt);
730
+
731
+ if (!strcmp("%s", fmt)) {
732
+ const char *s1 = va_arg(args, const char *);
733
+ const char *s2 = va_arg(args, const char *);
734
+ is_equal = (!s1 || !s2) ? (s1 == s2) : !strcmp(s1, s2);
735
+
736
+ if (!is_equal) {
737
+ if (s1 && s2) {
738
+ int pos;
739
+ for (pos = 0; s1[pos] == s2[pos] && s1[pos] && s2[pos]; ++pos)
740
+ /* find differing byte offset */;
741
+ p_snprintf(buf, sizeof(buf), "'%s' != '%s' (at byte %d)",
742
+ s1, s2, pos);
743
+ } else {
744
+ p_snprintf(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
745
+ }
746
+ }
747
+ }
748
+ else if(!strcmp("%.*s", fmt)) {
749
+ const char *s1 = va_arg(args, const char *);
750
+ const char *s2 = va_arg(args, const char *);
751
+ int len = va_arg(args, int);
752
+ is_equal = (!s1 || !s2) ? (s1 == s2) : !strncmp(s1, s2, len);
753
+
754
+ if (!is_equal) {
755
+ if (s1 && s2) {
756
+ int pos;
757
+ for (pos = 0; s1[pos] == s2[pos] && pos < len; ++pos)
758
+ /* find differing byte offset */;
759
+ p_snprintf(buf, sizeof(buf), "'%.*s' != '%.*s' (at byte %d)",
760
+ len, s1, len, s2, pos);
761
+ } else {
762
+ p_snprintf(buf, sizeof(buf), "'%.*s' != '%.*s'", len, s1, len, s2);
763
+ }
764
+ }
765
+ }
766
+ else if (!strcmp("%ls", fmt)) {
767
+ const wchar_t *wcs1 = va_arg(args, const wchar_t *);
768
+ const wchar_t *wcs2 = va_arg(args, const wchar_t *);
769
+ is_equal = (!wcs1 || !wcs2) ? (wcs1 == wcs2) : !wcscmp(wcs1, wcs2);
770
+
771
+ if (!is_equal) {
772
+ if (wcs1 && wcs2) {
773
+ int pos;
774
+ for (pos = 0; wcs1[pos] == wcs2[pos] && wcs1[pos] && wcs2[pos]; ++pos)
775
+ /* find differing byte offset */;
776
+ p_snprintf(buf, sizeof(buf), "'%ls' != '%ls' (at byte %d)",
777
+ wcs1, wcs2, pos);
778
+ } else {
779
+ p_snprintf(buf, sizeof(buf), "'%ls' != '%ls'", wcs1, wcs2);
780
+ }
781
+ }
782
+ }
783
+ else if(!strcmp("%.*ls", fmt)) {
784
+ const wchar_t *wcs1 = va_arg(args, const wchar_t *);
785
+ const wchar_t *wcs2 = va_arg(args, const wchar_t *);
786
+ int len = va_arg(args, int);
787
+ is_equal = (!wcs1 || !wcs2) ? (wcs1 == wcs2) : !wcsncmp(wcs1, wcs2, len);
788
+
789
+ if (!is_equal) {
790
+ if (wcs1 && wcs2) {
791
+ int pos;
792
+ for (pos = 0; wcs1[pos] == wcs2[pos] && pos < len; ++pos)
793
+ /* find differing byte offset */;
794
+ p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls' (at byte %d)",
795
+ len, wcs1, len, wcs2, pos);
796
+ } else {
797
+ p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls'", len, wcs1, len, wcs2);
798
+ }
799
+ }
800
+ }
801
+ else if (!strcmp("%"PRIuZ, fmt) || !strcmp("%"PRIxZ, fmt)) {
802
+ size_t sz1 = va_arg(args, size_t), sz2 = va_arg(args, size_t);
803
+ is_equal = (sz1 == sz2);
804
+ if (!is_equal) {
805
+ int offset = p_snprintf(buf, sizeof(buf), fmt, sz1);
806
+ strncat(buf, " != ", sizeof(buf) - offset);
807
+ p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, sz2);
808
+ }
809
+ }
810
+ else if (!strcmp("%p", fmt)) {
811
+ void *p1 = va_arg(args, void *), *p2 = va_arg(args, void *);
812
+ is_equal = (p1 == p2);
813
+ if (!is_equal)
814
+ p_snprintf(buf, sizeof(buf), "%p != %p", p1, p2);
815
+ }
816
+ else {
817
+ int i1 = va_arg(args, int), i2 = va_arg(args, int);
818
+ is_equal = (i1 == i2);
819
+ if (!is_equal) {
820
+ int offset = p_snprintf(buf, sizeof(buf), fmt, i1);
821
+ strncat(buf, " != ", sizeof(buf) - offset);
822
+ p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, i2);
823
+ }
824
+ }
825
+
826
+ va_end(args);
827
+
828
+ if (!is_equal)
829
+ clar__fail(file, function, line, err, buf, should_abort);
830
+}
831
+
832
+void cl_set_cleanup(void (*cleanup)(void *), void *opaque)
833
+{
834
+ _clar.local_cleanup = cleanup;
835
+ _clar.local_cleanup_payload = opaque;
836
+}
837
+
838
+#include "clar/sandbox.h"
839
+#include "clar/fixtures.h"
840
+#include "clar/fs.h"
841
+#include "clar/print.h"
842
+#include "clar/summary.h"