diff options
author | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2010-08-31 18:21:13 +0000 |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2010-08-31 18:21:13 +0000 |
commit | 35c39756495bea5959de5778aaaf33a94f8c1e2e (patch) | |
tree | fe8cac8d619895feaa69a4e3678f1edd9d4945c4 /test | |
parent | a9f380f5c7ff75cd715c58e11885dddc54baef02 (diff) | |
download | googletest-35c39756495bea5959de5778aaaf33a94f8c1e2e.tar.gz googletest-35c39756495bea5959de5778aaaf33a94f8c1e2e.tar.bz2 googletest-35c39756495bea5959de5778aaaf33a94f8c1e2e.zip |
Casts char to unsigned char before calling isspace() etc to avoid undefined behavior (by Zhanyong Wan); removes conditional #includes keyed on GTEST_HAS_PROTOBUF_ (by Zhanyong Wan); publishes GTEST_HAS_STREAM_REDIRECTION (by Vlad Losev); forward declares some classes properly (by Samuel Benzaquen); honors the --gtest_catch_exceptions flag (by Vlad Losev).
Diffstat (limited to 'test')
-rw-r--r-- | test/gtest-port_test.cc | 102 | ||||
-rw-r--r-- | test/gtest-printers_test.cc | 2 | ||||
-rwxr-xr-x | test/gtest_catch_exceptions_test.py | 22 | ||||
-rw-r--r-- | test/gtest_catch_exceptions_test_.cc | 17 | ||||
-rw-r--r-- | test/gtest_unittest.cc | 25 |
5 files changed, 93 insertions, 75 deletions
diff --git a/test/gtest-port_test.cc b/test/gtest-port_test.cc index 6cdbfab4..e08afe8e 100644 --- a/test/gtest-port_test.cc +++ b/test/gtest-port_test.cc @@ -377,33 +377,33 @@ TEST(IsInSetTest, WorksForNonNulChars) { EXPECT_TRUE(IsInSet('b', "ab")); } -TEST(IsDigitTest, IsFalseForNonDigit) { - EXPECT_FALSE(IsDigit('\0')); - EXPECT_FALSE(IsDigit(' ')); - EXPECT_FALSE(IsDigit('+')); - EXPECT_FALSE(IsDigit('-')); - EXPECT_FALSE(IsDigit('.')); - EXPECT_FALSE(IsDigit('a')); +TEST(IsAsciiDigitTest, IsFalseForNonDigit) { + EXPECT_FALSE(IsAsciiDigit('\0')); + EXPECT_FALSE(IsAsciiDigit(' ')); + EXPECT_FALSE(IsAsciiDigit('+')); + EXPECT_FALSE(IsAsciiDigit('-')); + EXPECT_FALSE(IsAsciiDigit('.')); + EXPECT_FALSE(IsAsciiDigit('a')); } -TEST(IsDigitTest, IsTrueForDigit) { - EXPECT_TRUE(IsDigit('0')); - EXPECT_TRUE(IsDigit('1')); - EXPECT_TRUE(IsDigit('5')); - EXPECT_TRUE(IsDigit('9')); +TEST(IsAsciiDigitTest, IsTrueForDigit) { + EXPECT_TRUE(IsAsciiDigit('0')); + EXPECT_TRUE(IsAsciiDigit('1')); + EXPECT_TRUE(IsAsciiDigit('5')); + EXPECT_TRUE(IsAsciiDigit('9')); } -TEST(IsPunctTest, IsFalseForNonPunct) { - EXPECT_FALSE(IsPunct('\0')); - EXPECT_FALSE(IsPunct(' ')); - EXPECT_FALSE(IsPunct('\n')); - EXPECT_FALSE(IsPunct('a')); - EXPECT_FALSE(IsPunct('0')); +TEST(IsAsciiPunctTest, IsFalseForNonPunct) { + EXPECT_FALSE(IsAsciiPunct('\0')); + EXPECT_FALSE(IsAsciiPunct(' ')); + EXPECT_FALSE(IsAsciiPunct('\n')); + EXPECT_FALSE(IsAsciiPunct('a')); + EXPECT_FALSE(IsAsciiPunct('0')); } -TEST(IsPunctTest, IsTrueForPunct) { +TEST(IsAsciiPunctTest, IsTrueForPunct) { for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) { - EXPECT_PRED1(IsPunct, *p); + EXPECT_PRED1(IsAsciiPunct, *p); } } @@ -421,47 +421,47 @@ TEST(IsRepeatTest, IsTrueForRepeatChar) { EXPECT_TRUE(IsRepeat('+')); } -TEST(IsWhiteSpaceTest, IsFalseForNonWhiteSpace) { - EXPECT_FALSE(IsWhiteSpace('\0')); - EXPECT_FALSE(IsWhiteSpace('a')); - EXPECT_FALSE(IsWhiteSpace('1')); - EXPECT_FALSE(IsWhiteSpace('+')); - EXPECT_FALSE(IsWhiteSpace('_')); +TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) { + EXPECT_FALSE(IsAsciiWhiteSpace('\0')); + EXPECT_FALSE(IsAsciiWhiteSpace('a')); + EXPECT_FALSE(IsAsciiWhiteSpace('1')); + EXPECT_FALSE(IsAsciiWhiteSpace('+')); + EXPECT_FALSE(IsAsciiWhiteSpace('_')); } -TEST(IsWhiteSpaceTest, IsTrueForWhiteSpace) { - EXPECT_TRUE(IsWhiteSpace(' ')); - EXPECT_TRUE(IsWhiteSpace('\n')); - EXPECT_TRUE(IsWhiteSpace('\r')); - EXPECT_TRUE(IsWhiteSpace('\t')); - EXPECT_TRUE(IsWhiteSpace('\v')); - EXPECT_TRUE(IsWhiteSpace('\f')); +TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) { + EXPECT_TRUE(IsAsciiWhiteSpace(' ')); + EXPECT_TRUE(IsAsciiWhiteSpace('\n')); + EXPECT_TRUE(IsAsciiWhiteSpace('\r')); + EXPECT_TRUE(IsAsciiWhiteSpace('\t')); + EXPECT_TRUE(IsAsciiWhiteSpace('\v')); + EXPECT_TRUE(IsAsciiWhiteSpace('\f')); } -TEST(IsWordCharTest, IsFalseForNonWordChar) { - EXPECT_FALSE(IsWordChar('\0')); - EXPECT_FALSE(IsWordChar('+')); - EXPECT_FALSE(IsWordChar('.')); - EXPECT_FALSE(IsWordChar(' ')); - EXPECT_FALSE(IsWordChar('\n')); +TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) { + EXPECT_FALSE(IsAsciiWordChar('\0')); + EXPECT_FALSE(IsAsciiWordChar('+')); + EXPECT_FALSE(IsAsciiWordChar('.')); + EXPECT_FALSE(IsAsciiWordChar(' ')); + EXPECT_FALSE(IsAsciiWordChar('\n')); } -TEST(IsWordCharTest, IsTrueForLetter) { - EXPECT_TRUE(IsWordChar('a')); - EXPECT_TRUE(IsWordChar('b')); - EXPECT_TRUE(IsWordChar('A')); - EXPECT_TRUE(IsWordChar('Z')); +TEST(IsAsciiWordCharTest, IsTrueForLetter) { + EXPECT_TRUE(IsAsciiWordChar('a')); + EXPECT_TRUE(IsAsciiWordChar('b')); + EXPECT_TRUE(IsAsciiWordChar('A')); + EXPECT_TRUE(IsAsciiWordChar('Z')); } -TEST(IsWordCharTest, IsTrueForDigit) { - EXPECT_TRUE(IsWordChar('0')); - EXPECT_TRUE(IsWordChar('1')); - EXPECT_TRUE(IsWordChar('7')); - EXPECT_TRUE(IsWordChar('9')); +TEST(IsAsciiWordCharTest, IsTrueForDigit) { + EXPECT_TRUE(IsAsciiWordChar('0')); + EXPECT_TRUE(IsAsciiWordChar('1')); + EXPECT_TRUE(IsAsciiWordChar('7')); + EXPECT_TRUE(IsAsciiWordChar('9')); } -TEST(IsWordCharTest, IsTrueForUnderscore) { - EXPECT_TRUE(IsWordChar('_')); +TEST(IsAsciiWordCharTest, IsTrueForUnderscore) { + EXPECT_TRUE(IsAsciiWordChar('_')); } TEST(IsValidEscapeTest, IsFalseForNonPrintable) { diff --git a/test/gtest-printers_test.cc b/test/gtest-printers_test.cc index 5dec871c..ce72421f 100644 --- a/test/gtest-printers_test.cc +++ b/test/gtest-printers_test.cc @@ -817,7 +817,7 @@ TEST(PrintStlContainerTest, HashMultiSet) { std::vector<int> numbers; for (size_t i = 0; i != result.length(); i++) { if (expected_pattern[i] == 'd') { - ASSERT_TRUE(isdigit(result[i]) != 0); + ASSERT_TRUE(isdigit(static_cast<unsigned char>(result[i])) != 0); numbers.push_back(result[i] - '0'); } else { EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " diff --git a/test/gtest_catch_exceptions_test.py b/test/gtest_catch_exceptions_test.py index c4d1756d..061c5c3d 100755 --- a/test/gtest_catch_exceptions_test.py +++ b/test/gtest_catch_exceptions_test.py @@ -43,6 +43,8 @@ import gtest_test_utils # Constants. LIST_TESTS_FLAG = '--gtest_list_tests' +CATCH_EXCEPTIONS_FLAG = '--gtest_catch_exceptions=1' +FILTER_FLAG='--gtest_filter' # Path to the gtest_catch_exceptions_ex_test_ binary, compiled with # exceptions enabled. @@ -59,10 +61,11 @@ TEST_LIST = gtest_test_utils.Subprocess([EXE_PATH, LIST_TESTS_FLAG]).output SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST if SUPPORTS_SEH_EXCEPTIONS: - BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH]).output - -EX_BINARY_OUTPUT = gtest_test_utils.Subprocess([EX_EXE_PATH]).output + BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH, + CATCH_EXCEPTIONS_FLAG]).output +EX_BINARY_OUTPUT = gtest_test_utils.Subprocess([EX_EXE_PATH, + CATCH_EXCEPTIONS_FLAG]).output # The tests. if SUPPORTS_SEH_EXCEPTIONS: @@ -199,5 +202,18 @@ class CatchCxxExceptionsTest(gtest_test_utils.TestCase): self.assert_('Unknown C++ exception thrown in the test body' in EX_BINARY_OUTPUT) + def testUnhandledCxxExceptionsAbortTheProgram(self): + # Filters out SEH exception tests on Windows. Unhandled SEH exceptions + # cause tests to show pop-up windows there. + FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*' + # By default, Google Test doesn't catch the exceptions. + uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess( + [EX_EXE_PATH, FITLER_OUT_SEH_TESTS_FLAG]).output + + self.assert_('Unhandled C++ exception terminating the program' + in uncaught_exceptions_ex_binary_output) + self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output) + + if __name__ == '__main__': gtest_test_utils.Main() diff --git a/test/gtest_catch_exceptions_test_.cc b/test/gtest_catch_exceptions_test_.cc index 86736811..2ba6eb44 100644 --- a/test/gtest_catch_exceptions_test_.cc +++ b/test/gtest_catch_exceptions_test_.cc @@ -35,17 +35,18 @@ #include <gtest/gtest.h> #include <stdio.h> // NOLINT +#include <stdlib.h> // For exit(). #if GTEST_HAS_SEH #include <windows.h> #endif #if GTEST_HAS_EXCEPTIONS +#include <exception> // For set_terminate(). #include <stdexcept> #endif using testing::Test; -using testing::GTEST_FLAG(catch_exceptions); #if GTEST_HAS_SEH @@ -287,12 +288,20 @@ TEST(CxxExceptionTest, ThrowsNonStdCxxException) { throw "C-string"; } +// This terminate handler aborts the program using exit() rather than abort(). +// This avoids showing pop-ups on Windows systems and core dumps on Unix-like +// ones. +void TerminateHandler() { + fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program."); + fflush(NULL); + exit(3); +} + #endif // GTEST_HAS_EXCEPTIONS int main(int argc, char** argv) { -#if GTEST_HAS_SEH - // Tells Google Test to catch SEH-style exceptions on Windows. - GTEST_FLAG(catch_exceptions) = true; +#if GTEST_HAS_EXCEPTIONS + std::set_terminate(&TerminateHandler); #endif testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc index d993c048..d7092495 100644 --- a/test/gtest_unittest.cc +++ b/test/gtest_unittest.cc @@ -193,19 +193,15 @@ using testing::internal::kReference; using testing::internal::kTestTypeIdInGoogleTest; using testing::internal::scoped_ptr; -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION using testing::internal::CaptureStdout; using testing::internal::GetCapturedStdout; -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif #if GTEST_IS_THREADSAFE using testing::internal::ThreadWithParam; #endif -#if GTEST_HAS_PROTOBUF_ -using ::testing::internal::TestMessage; -#endif // GTEST_HAS_PROTOBUF_ - class TestingVector : public std::vector<int> { }; @@ -5343,16 +5339,16 @@ class InitGoogleTestTest : public Test { const bool saved_help_flag = ::testing::internal::g_help_flag; ::testing::internal::g_help_flag = false; -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION CaptureStdout(); -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif // Parses the command line. internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1)); -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION const String captured_stdout = GetCapturedStdout(); -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif // Verifies the flag values. CheckFlags(expected); @@ -5365,7 +5361,7 @@ class InitGoogleTestTest : public Test { // help message for the flags it recognizes. EXPECT_EQ(should_print_help, ::testing::internal::g_help_flag); -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION const char* const expected_help_fragment = "This program contains tests written using"; if (should_print_help) { @@ -5374,7 +5370,7 @@ class InitGoogleTestTest : public Test { EXPECT_PRED_FORMAT2(IsNotSubstring, expected_help_fragment, captured_stdout); } -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif // GTEST_HAS_STREAM_REDIRECTION ::testing::internal::g_help_flag = saved_help_flag; } @@ -6887,13 +6883,10 @@ TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { } // Tests that IsAProtocolMessage<T>::value is true when T is -// ProtocolMessage or a sub-class of it. +// proto2::Message or a sub-class of it. TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); -#if GTEST_HAS_PROTOBUF_ - EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); -#endif // GTEST_HAS_PROTOBUF_ } // Tests that IsAProtocolMessage<T>::value is false when T is neither |