From 7a43775661e75fb67c7308bbafa61df362908db9 Mon Sep 17 00:00:00 2001 From: Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> Date: Thu, 4 Oct 2018 02:04:14 +0700 Subject: [PATCH] Assert tests on the first failure. (#4952) * Assert tests on the first failure. Disable pop-up message box on assertion. * Fix format and code style * Move MSVC dependent code to ifdef --- tests/test.cpp | 3 ++- tests/test_assert.cpp | 37 ++++++++++++++++++++++++++++++++++++- tests/test_assert.h | 3 +++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/tests/test.cpp b/tests/test.cpp index 4f89dd3de..030c5d9cf 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -2083,6 +2083,7 @@ int FlatBufferTests() { } int main(int /*argc*/, const char * /*argv*/ []) { + InitTestEngine(); FlatBufferTests(); FlatBufferBuilderTest(); diff --git a/tests/test_assert.cpp b/tests/test_assert.cpp index 30ab77620..dd028664d 100644 --- a/tests/test_assert.cpp +++ b/tests/test_assert.cpp @@ -1,3 +1,8 @@ +#ifdef _MSC_VER +# include +# include +#endif + #include "test_assert.h" int testing_fails = 0; @@ -6,8 +11,10 @@ void TestFail(const char *expval, const char *val, const char *exp, const char *file, int line, const char *func) { TEST_OUTPUT_LINE("VALUE: \"%s\"", expval); TEST_OUTPUT_LINE("EXPECTED: \"%s\"", val); - TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s in %s", file, line, exp, func? func : ""); + TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s in %s", file, line, exp, + func ? func : ""); testing_fails++; + assert(0); // assert on first failure under debug } void TestEqStr(const char *expval, const char *val, const char *exp, @@ -15,3 +22,31 @@ void TestEqStr(const char *expval, const char *val, const char *exp, if (strcmp(expval, val) != 0) { TestFail(expval, val, exp, file, line); } } +#ifdef _MSC_VER +// Without this hook function the message box not suppressed. +int msvc_no_dialog_box_on_assert(int rpt_type, char *msg, int *ret_val) { + (void)ret_val; + TEST_OUTPUT_LINE("TEST ASSERTED: %d: %s", rpt_type, msg); + return 1; +} +#endif + +void InitTestEngine() { + testing_fails = 0; + // Disable stdout buffering to prevent information lost on assertion or core + // dump. + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + // clang-format off + + #ifdef _MSC_VER + // Suppress pop-up message box on assertion (MSVC2010, MSVC2012). + // This message box hangs CI-test on the hour until timeout expired. + // Default mode is file, file is stderr. + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); + _CrtSetReportHook(msvc_no_dialog_box_on_assert); + #endif + // clang-format on +} diff --git a/tests/test_assert.h b/tests/test_assert.h index 5a1446684..768c8ff3d 100644 --- a/tests/test_assert.h +++ b/tests/test_assert.h @@ -17,6 +17,9 @@ extern int testing_fails; +// Prepare test engine (MSVC assertion setup, etc) +void InitTestEngine(); + void TestFail(const char *expval, const char *val, const char *exp, const char *file, int line, const char *func = 0);