Skip to content

Commit 73f637e

Browse files
authored
Support compilation with pre-C99 versions of MSVC
1 parent 8b90e5f commit 73f637e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

utest/ctest.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
#define WEAK
2929
#endif
3030

31+
#ifndef __MSC_VER
3132
#include <inttypes.h> /* intmax_t, uintmax_t, PRI* */
33+
#endif
34+
3235
#include <stddef.h> /* size_t */
3336

3437
typedef void (*SetupFunc)(void*);
@@ -72,6 +75,13 @@ struct ctest {
7275
#define __CTEST_NO_TIME
7376
#define CTEST_NO_COLORS
7477

78+
#if __MSC_VER >= 1500
79+
#include <inttypes.h>
80+
#else
81+
#include <stdint.h>
82+
#define CTEST_NO_INTTYPES
83+
#endif
84+
7585
#ifndef CTEST_ADD_TESTS_MANUALLY
7686
#pragma section(".ctest$a")
7787
#pragma section(".ctest$u")
@@ -480,43 +490,71 @@ void assert_data(const unsigned char* exp, size_t expsize,
480490
const char* caller, int line) {
481491
size_t i;
482492
if (expsize != realsize) {
493+
#ifndef CTEST_NO_INTTYPES
483494
CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize);
495+
#else
496+
CTEST_ERR("%s:%d expected %u bytes, got %u", caller, line, (uintmax_t) expsize, (uintmax_t) realsize);
497+
#endif
484498
}
485499
for (i=0; i<expsize; i++) {
486500
if (exp[i] != real[i]) {
501+
#ifndef CTEST_NO_INTTYPES
487502
CTEST_ERR("%s:%d expected 0x%02x at offset %" PRIuMAX " got 0x%02x",
503+
#else
504+
CTEST_ERR("%s:%d expected 0x%02x at offset %u got 0x%02x",
505+
#endif
488506
caller, line, exp[i], (uintmax_t) i, real[i]);
489507
}
490508
}
491509
}
492510

493511
void assert_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
494512
if (exp != real) {
513+
#ifndef CTEST_NO_INTTYPES
495514
CTEST_ERR("%s:%d expected %" PRIdMAX ", got %" PRIdMAX, caller, line, exp, real);
515+
#else
516+
CTEST_ERR("%s:%d expected %d, got %d", caller, line, exp, real);
517+
#endif
496518
}
497519
}
498520

499521
void assert_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
500522
if (exp != real) {
523+
#ifndef CTEST_NO_INTTYPES
501524
CTEST_ERR("%s:%d expected %" PRIuMAX ", got %" PRIuMAX, caller, line, exp, real);
525+
#else
526+
CTEST_ERR("%s:%d expected %u, got %u", caller, line, exp, real);
527+
#endif
502528
}
503529
}
504530

505531
void assert_not_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
506532
if ((exp) == (real)) {
533+
#ifndef CTEST_NO_INTTYPES
507534
CTEST_ERR("%s:%d should not be %" PRIdMAX, caller, line, real);
535+
#else
536+
CTEST_ERR("%s:%d should not be %d", caller, line, real);
537+
#endif
508538
}
509539
}
510540

511541
void assert_not_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
512542
if ((exp) == (real)) {
543+
#ifndef CTEST_NO_INTTYPES
513544
CTEST_ERR("%s:%d should not be %" PRIuMAX, caller, line, real);
545+
#else
546+
CTEST_ERR("%s:%d should not be %u", caller, line, real);
547+
#endif
514548
}
515549
}
516550

517551
void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line) {
518552
if (real < exp1 || real > exp2) {
553+
#ifndef CTEST_NO_INTTYPES
519554
CTEST_ERR("%s:%d expected %" PRIdMAX "-%" PRIdMAX ", got %" PRIdMAX, caller, line, exp1, exp2, real);
555+
#else
556+
CTEST_ERR("%s:%d expected %d-%d, got %d", caller, line, exp1, exp2, real);
557+
#endif
520558
}
521559
}
522560

0 commit comments

Comments
 (0)