2525#include < iostream>
2626#include < regex>
2727
28- #ifdef WIN32
28+ #ifdef _WIN32
2929#include < Windows.h>
3030#include < conio.h>
3131#else
4545
4646static void print_help ()
4747{
48- std::cout << " Usage: testrunner.exe <test_binaries> [/list] [/listproperties] [/noignore] [/breakonerror]" <<std::endl;
48+ std::cout << " Usage: testrunner.exe <test_binaries> [/list] [/listproperties] [/noignore] [/breakonerror] [/detectleaks] " <<std::endl;
4949 std::cout << " [/name:<test_name>] [/select:@key=value] [/loop:<num_times>]" << std::endl;
5050 std::cout << std::endl;
5151 std::cout << " /list List all the names of the test_binaries and their" << std::endl;
@@ -55,6 +55,7 @@ static void print_help()
5555 std::cout << " test properties." << std::endl;
5656 std::cout << std::endl;
5757 std::cout << " /breakonerror Break into the debugger when a failure is encountered." << std::endl;
58+ std::cout << " /detectleaks Turns CRT leak detection and prints any leaks, Windows only." << std::endl;
5859 std::cout << std::endl;
5960 std::cout << " /name:<test_name> Run only test cases with matching name. Can contain the" << std::endl;
6061 std::cout << " wildcard '*' character." << std::endl;
@@ -84,7 +85,7 @@ static std::vector<std::string> get_files_in_directory()
8485{
8586 std::vector<std::string> files;
8687
87- #ifdef WIN32
88+ #ifdef _WIN32
8889
8990 char exe_directory_buffer[MAX_PATH];
9091 GetModuleFileNameA (NULL , exe_directory_buffer, MAX_PATH);
@@ -347,7 +348,7 @@ static void handle_list_option(bool listProperties, const UnitTest::TestList &te
347348
348349static void ChangeConsoleTextColorToRed ()
349350{
350- #ifdef WIN32
351+ #ifdef _WIN32
351352 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 0x0004 | 0x0008 );
352353#else
353354 std::cout << " \033 [1;31m" ;
@@ -356,7 +357,7 @@ static void ChangeConsoleTextColorToRed()
356357
357358static void ChangeConsoleTextColorToGreen ()
358359{
359- #ifdef WIN32
360+ #ifdef _WIN32
360361 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 0x0002 | 0x0008 );
361362#else
362363 std::cout << " \033 [1;32m" ;
@@ -365,7 +366,7 @@ static void ChangeConsoleTextColorToGreen()
365366
366367static void ChangeConsoleTextColorToGrey ()
367368{
368- #ifdef WIN32
369+ #ifdef _WIN32
369370 SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
370371#else
371372 std::cout << " \033 [0m" ;
@@ -375,7 +376,7 @@ static void ChangeConsoleTextColorToGrey()
375376bool IsTestIgnored (UnitTest::Test *pTest)
376377{
377378 if (pTest->m_properties .Has (" Ignore" )) return true ;
378- #ifdef WIN32
379+ #ifdef _WIN32
379380 if (pTest->m_properties .Has (" Ignore:Windows" )) return true ;
380381#elif defined(__APPLE__)
381382 if (pTest->m_properties .Has (" Ignore:Apple" )) return true ;
@@ -387,24 +388,6 @@ bool IsTestIgnored(UnitTest::Test *pTest)
387388 return false ;
388389}
389390
390- //
391- // These are to handle cases where an exception or assert occurs on a thread
392- // that isn't being waited on and the process exits. These shouldn't be happening,
393- // but could happen if we have a bug.
394- //
395- #ifdef WIN32
396-
397- int CrtReportHandler (int reportType, char *message, int *returnValue)
398- {
399- std::cerr << " In CRT Report Handler. ReportType:" << reportType << " , message:" << message << std::endl;
400-
401- // Cause break into debugger.
402- *returnValue = 1 ;
403- return TRUE ;
404- }
405-
406- #endif
407-
408391typedef std::map<std::string, UnitTest::TestList> testlist_t ;
409392
410393void list_test_options (testlist_t & testlists)
@@ -525,7 +508,15 @@ void run_all_tests(UnitTest::TestRunner& testRunner, testlist_t& testlists)
525508
526509int main (int argc, char * argv[])
527510{
528- #ifdef WIN32
511+ #ifdef _WIN32
512+ // Add standard error as output as well.
513+ _CrtSetReportMode (_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW);
514+ _CrtSetReportFile (_CRT_ASSERT, _CRTDBG_FILE_STDERR);
515+ _CrtSetReportMode (_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW);
516+ _CrtSetReportFile (_CRT_ERROR, _CRTDBG_FILE_STDERR);
517+ _CrtSetReportMode (_CRT_WARN, _CRTDBG_MODE_FILE);
518+ _CrtSetReportFile (_CRT_WARN, _CRTDBG_FILE_STDERR);
519+
529520 // The test runner built with WinRT support might be used on a pre Win8 machine.
530521 // Obviously in that case WinRT test cases can't run, but non WinRT ones should be
531522 // fine. So dynamically try to call RoInitialize/RoUninitialize.
@@ -540,7 +531,6 @@ int main(int argc, char* argv[])
540531 }
541532 }
542533
543- _CrtSetReportHook2 (_CRT_RPTHOOK_INSTALL, CrtReportHandler);
544534 struct console_restorer {
545535 CONSOLE_SCREEN_BUFFER_INFO m_originalConsoleInfo;
546536 console_restorer ()
@@ -585,6 +575,12 @@ int main(int argc, char* argv[])
585575 listOption = true ;
586576 listPropertiesOption = true ;
587577 }
578+ #ifdef _WIN32
579+ if (UnitTest::GlobalSettings::Has (" detectleaks" ))
580+ {
581+ _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
582+ }
583+ #endif
588584
589585 // Start timer.
590586 UnitTest::Timer timer;
@@ -639,7 +635,7 @@ int main(int argc, char* argv[])
639635 << " Took " << elapsedTime << " ms" << std::endl;
640636 }
641637
642- #ifdef WIN32
638+ #ifdef _WIN32
643639 if (hComBase != nullptr )
644640 {
645641 typedef void (WINAPI *RoUnInit)();
0 commit comments