diff --git a/ctest.h b/ctest.h index fab67fc..b39f569 100644 --- a/ctest.h +++ b/ctest.h @@ -79,14 +79,19 @@ CTEST_IMPL_DIAG_POP() #define CTEST_IMPL_TEARDOWN_FPNAME(sname) CTEST_IMPL_NAME(sname##_teardown_ptr) #define CTEST_IMPL_MAGIC (0xdeadbeef) -#ifdef __APPLE__ +#if defined(_MSC_VER) +#pragma data_seg(push) +#pragma data_seg(".ctest$u") +#pragma data_seg(pop) +#define CTEST_IMPL_SECTION __declspec(allocate(".ctest$u")) __declspec(align(1)) +#elif defined(__APPLE__) #define CTEST_IMPL_SECTION __attribute__ ((used, section ("__DATA, .ctest"), aligned(1))) #else #define CTEST_IMPL_SECTION __attribute__ ((used, section (".ctest"), aligned(1))) #endif #define CTEST_IMPL_STRUCT(sname, tname, tskip, tdata, tsetup, tteardown) \ - static struct ctest CTEST_IMPL_TNAME(sname, tname) CTEST_IMPL_SECTION = { \ + static struct ctest CTEST_IMPL_SECTION CTEST_IMPL_TNAME(sname, tname) = { \ .ssname=#sname, \ .ttname=#tname, \ .run = CTEST_IMPL_FNAME(sname, tname), \ @@ -187,11 +192,19 @@ void assert_dbl_far(double exp, double real, double tol, const char* caller, int #include #include #include -#include -#include #include #include +#ifndef _WIN32 +#include +#include +#define CTEST_IMPL_ISATTY isatty +#else +#include +#include +#define CTEST_IMPL_ISATTY _isatty +#endif + static size_t ctest_errorsize; static char* ctest_errormsg; #define MSG_SIZE 4096 @@ -405,11 +418,22 @@ static int suite_filter(struct ctest* t) { } static uint64_t getCurrentTime(void) { + uint64_t now64; +#ifndef _WIN32 struct timeval now; gettimeofday(&now, NULL); - uint64_t now64 = (uint64_t) now.tv_sec; + now64 = (uint64_t) now.tv_sec; now64 *= 1000000; now64 += ((uint64_t) now.tv_usec); +#else + LARGE_INTEGER frequency; + LARGE_INTEGER counter; + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&counter); + now64 = counter.QuadPart; + now64 *= 1000000; + now64 /= frequency.QuadPart; +#endif return now64; } @@ -425,7 +449,7 @@ static void color_print(const char* color, const char* text) { static void sighandler(int signum) { char msg[128]; - sprintf(msg, "[SIGNAL %d: %s]", signum, sys_siglist[signum]); + snprintf(msg, sizeof(msg), "[SIGNAL %d: %s]", signum, sys_siglist[signum]); color_print(ANSI_BRED, msg); fflush(stdout); @@ -458,7 +482,7 @@ int ctest_main(int argc, const char *argv[]) #ifdef CTEST_NO_COLORS color_output = 0; #else - color_output = isatty(1); + color_output = CTEST_IMPL_ISATTY(1); #endif uint64_t t1 = getCurrentTime(); @@ -523,7 +547,7 @@ int ctest_main(int argc, const char *argv[]) const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN; char results[80]; - sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %" PRIu64 " ms", total, num_ok, num_fail, num_skip, (t2 - t1)/1000); + snprintf(results, sizeof(results), "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %" PRIu64 " ms", total, num_ok, num_fail, num_skip, (t2 - t1)/1000); color_print(color, results); return num_fail; } diff --git a/main.c b/main.c index 3b047d9..5c3dbd1 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,7 @@ #define CTEST_MAIN // uncomment lines below to enable/disable features. See README.md for details -#define CTEST_SEGFAULT +//#define CTEST_SEGFAULT //#define CTEST_NO_COLORS //#define CTEST_COLOR_OK diff --git a/mytests.c b/mytests.c index ff0ece3..418fe13 100644 --- a/mytests.c +++ b/mytests.c @@ -1,10 +1,18 @@ -#include #include + +#ifndef _WIN32 +#include +#define MSLEEP(ms) usleep((ms) * 1000) +#else +#include +#define MSLEEP(ms) Sleep(ms) +#endif + #include "ctest.h" // basic test without setup/teardown CTEST(suite1, test1) { - usleep(2000); + MSLEEP(2); } // there are many different ASSERT macro's (see ctest.h) @@ -54,7 +62,9 @@ CTEST2(memtest, test2) { } -CTEST_DATA(fail) {}; +CTEST_DATA(fail) { + int unused; +}; // Asserts can also be used in setup/teardown functions CTEST_SETUP(fail) { @@ -122,7 +132,7 @@ CTEST(ctest, test_assert_interval) { CTEST(ctest, test_assert_null) { ASSERT_NULL(NULL); - ASSERT_NULL((void*)0xdeadbeef); + ASSERT_NULL((void*)(intptr_t)0xdeadbeef); } CTEST(ctest, test_assert_not_null_const) { @@ -130,7 +140,7 @@ CTEST(ctest, test_assert_not_null_const) { } CTEST(ctest, test_assert_not_null) { - ASSERT_NOT_NULL((void*)0xdeadbeef); + ASSERT_NOT_NULL((void*)(intptr_t)0xdeadbeef); ASSERT_NOT_NULL(NULL); }