Support compilation with pre-C99 versions of MSVC

This commit is contained in:
Martin Kroeker 2021-05-14 15:08:12 +02:00 committed by GitHub
parent 8b90e5f202
commit 73f637e584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 0 deletions

View File

@ -28,7 +28,10 @@
#define WEAK
#endif
#ifndef __MSC_VER
#include <inttypes.h> /* intmax_t, uintmax_t, PRI* */
#endif
#include <stddef.h> /* size_t */
typedef void (*SetupFunc)(void*);
@ -72,6 +75,13 @@ struct ctest {
#define __CTEST_NO_TIME
#define CTEST_NO_COLORS
#if __MSC_VER >= 1500
#include <inttypes.h>
#else
#include <stdint.h>
#define CTEST_NO_INTTYPES
#endif
#ifndef CTEST_ADD_TESTS_MANUALLY
#pragma section(".ctest$a")
#pragma section(".ctest$u")
@ -480,11 +490,19 @@ void assert_data(const unsigned char* exp, size_t expsize,
const char* caller, int line) {
size_t i;
if (expsize != realsize) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize);
#else
CTEST_ERR("%s:%d expected %u bytes, got %u", caller, line, (uintmax_t) expsize, (uintmax_t) realsize);
#endif
}
for (i=0; i<expsize; i++) {
if (exp[i] != real[i]) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d expected 0x%02x at offset %" PRIuMAX " got 0x%02x",
#else
CTEST_ERR("%s:%d expected 0x%02x at offset %u got 0x%02x",
#endif
caller, line, exp[i], (uintmax_t) i, real[i]);
}
}
@ -492,31 +510,51 @@ void assert_data(const unsigned char* exp, size_t expsize,
void assert_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
if (exp != real) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d expected %" PRIdMAX ", got %" PRIdMAX, caller, line, exp, real);
#else
CTEST_ERR("%s:%d expected %d, got %d", caller, line, exp, real);
#endif
}
}
void assert_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
if (exp != real) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d expected %" PRIuMAX ", got %" PRIuMAX, caller, line, exp, real);
#else
CTEST_ERR("%s:%d expected %u, got %u", caller, line, exp, real);
#endif
}
}
void assert_not_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
if ((exp) == (real)) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d should not be %" PRIdMAX, caller, line, real);
#else
CTEST_ERR("%s:%d should not be %d", caller, line, real);
#endif
}
}
void assert_not_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
if ((exp) == (real)) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d should not be %" PRIuMAX, caller, line, real);
#else
CTEST_ERR("%s:%d should not be %u", caller, line, real);
#endif
}
}
void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line) {
if (real < exp1 || real > exp2) {
#ifndef CTEST_NO_INTTYPES
CTEST_ERR("%s:%d expected %" PRIdMAX "-%" PRIdMAX ", got %" PRIdMAX, caller, line, exp1, exp2, real);
#else
CTEST_ERR("%s:%d expected %d-%d, got %d", caller, line, exp1, exp2, real);
#endif
}
}