From f5d12dab447e28a551d83b3f7da618904b359d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Thu, 6 Feb 2014 10:15:06 +0000 Subject: [PATCH] Fix failing TextFileTestCase::ReadMixedWithFuzzing(). The test failed with 33% probability because it didn't account for trailing non-newline character. Fixed and also changed the test to repeat itself a hundred times, to increase the probability of catching problems like this. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75823 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/textfile/textfiletest.cpp | 47 ++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/tests/textfile/textfiletest.cpp b/tests/textfile/textfiletest.cpp index 5fb53e2706..1dccc80deb 100644 --- a/tests/textfile/textfiletest.cpp +++ b/tests/textfile/textfiletest.cpp @@ -219,29 +219,34 @@ void TextFileTestCase::ReadMixed() void TextFileTestCase::ReadMixedWithFuzzing() { - // Create a random buffer with lots of newlines. This is intended to catch - // bad parsing in unexpected situations such as the one from ReadCRCRLF() - // (which is so common it deserves a test of its own). - static const char CHOICES[] = {'\r', '\n', 'X'}; - - unsigned linesCnt = 0; - const size_t BUF_LEN = 20000; - char data[BUF_LEN + 1]; - data[0] = 'X'; - data[BUF_LEN] = '\0'; - for ( size_t i = 1; i < BUF_LEN; i++ ) + for ( int iteration = 0; iteration < 100; iteration++) { - char ch = CHOICES[rand() % WXSIZEOF(CHOICES)]; - data[i] = ch; - if ( ch == '\r' || (ch == '\n' && data[i-1] != '\r') ) - linesCnt++; + // Create a random buffer with lots of newlines. This is intended to catch + // bad parsing in unexpected situations such as the one from ReadCRCRLF() + // (which is so common it deserves a test of its own). + static const char CHOICES[] = {'\r', '\n', 'X'}; + + const size_t BUF_LEN = 100; + char data[BUF_LEN + 1]; + data[0] = 'X'; + data[BUF_LEN] = '\0'; + unsigned linesCnt = 0; + for ( size_t i = 1; i < BUF_LEN; i++ ) + { + char ch = CHOICES[rand() % WXSIZEOF(CHOICES)]; + data[i] = ch; + if ( ch == '\r' || (ch == '\n' && data[i-1] != '\r') ) + linesCnt++; + } + if (data[BUF_LEN-1] != '\r' && data[BUF_LEN-1] != '\n') + linesCnt++; // last line was unterminated + + CreateTestFile(data); + + wxTextFile f; + CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); + CPPUNIT_ASSERT_EQUAL( (size_t)linesCnt, f.GetLineCount() ); } - - CreateTestFile(data); - - wxTextFile f; - CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); - CPPUNIT_ASSERT_EQUAL( (size_t)linesCnt, f.GetLineCount() ); } void TextFileTestCase::ReadCRCRLF()