wxWidgets/tests/file/filetest.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
4.6 KiB
C++
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////
// Name: tests/file/filetest.cpp
// Purpose: wxFile unit test
// Author: Vadim Zeitlin
// Created: 2009-09-12
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#if wxUSE_FILE
#include "wx/file.h"
#include "testfile.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class FileTestCase : public CppUnit::TestCase
{
public:
FileTestCase() { }
private:
CPPUNIT_TEST_SUITE( FileTestCase );
CPPUNIT_TEST( ReadAll );
#if wxUSE_UNICODE
CPPUNIT_TEST( RoundTripUTF8 );
CPPUNIT_TEST( RoundTripUTF16 );
CPPUNIT_TEST( RoundTripUTF32 );
#endif // wxUSE_UNICODE
CPPUNIT_TEST( TempFile );
CPPUNIT_TEST_SUITE_END();
void ReadAll();
#if wxUSE_UNICODE
void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
#endif // wxUSE_UNICODE
void DoRoundTripTest(const wxMBConv& conv);
void TempFile();
wxDECLARE_NO_COPY_CLASS(FileTestCase);
};
// ----------------------------------------------------------------------------
// CppUnit macros
// ----------------------------------------------------------------------------
CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
// ----------------------------------------------------------------------------
// tests implementation
// ----------------------------------------------------------------------------
void FileTestCase::ReadAll()
{
TestFile tf;
const char* text = "Ream\nde";
{
wxFile fout(tf.GetName(), wxFile::write);
CPPUNIT_ASSERT( fout.IsOpened() );
fout.Write(text, strlen(text));
CPPUNIT_ASSERT( fout.Close() );
}
{
wxFile fin(tf.GetName(), wxFile::read);
CPPUNIT_ASSERT( fin.IsOpened() );
wxString s;
CPPUNIT_ASSERT( fin.ReadAll(&s) );
CPPUNIT_ASSERT_EQUAL( text, s );
}
}
#if wxUSE_UNICODE
void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
{
TestFile tf;
// Explicit length is needed because of the embedded NUL.
const wxString data("Hello\0UTF!", 10);
{
wxFile fout(tf.GetName(), wxFile::write);
CPPUNIT_ASSERT( fout.IsOpened() );
CPPUNIT_ASSERT( fout.Write(data, conv) );
}
{
wxFile fin(tf.GetName(), wxFile::read);
CPPUNIT_ASSERT( fin.IsOpened() );
const ssize_t len = fin.Length();
wxCharBuffer buf(len);
CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
wxString dataReadBack(buf, conv, len);
CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
}
{
wxFile fin(tf.GetName(), wxFile::read);
CPPUNIT_ASSERT( fin.IsOpened() );
wxString dataReadBack;
CPPUNIT_ASSERT( fin.ReadAll(&dataReadBack, conv) );
CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
}
}
#endif // wxUSE_UNICODE
void FileTestCase::TempFile()
{
wxTempFile tmpFile;
Replace CppUnit with Catch for unit tests Drop the legacy CppUnit testing framework used for the unit tests. Replacing it with Catch has the advantage of not requiring CppUnit libraries to be installed on the system in order to be able to run tests (Catch is header-only and a copy of it is now included in the main repository itself) and, in the future, of being able to write the tests in a much more natural way. For now, however, avoid changing the existing tests code as much as [reasonably] possible to avoid introducing bugs in them and provide the CppUnit compatibility macros in the new wx/catch_cppunit.h header which allow to preserve the 99% of the existing code unchanged. Some of the required changes are: - Decompose asserts using "a && b" conditions into multiple asserts checking "a" and "b" independently. This would have been better even with CppUnit (to know which part of condition exactly failed) and is required with Catch. - Use extra parentheses around such conditions when they can't be easily decomposed in the arrays test, due to the use of macros. This is not ideal from the point of view of messages given when the tests fail but will do for now. - Rewrite asserts using "a || b" as a combination of condition checks and assert macros. Again, this is better anyhow, and is required with Catch. Incidentally, this allowed to fix a bug in the "exec" unit test which didn't leave enough time for the new process to be launched before trying to kill it. - Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros, our emulation of this macro can be used only once. - Provide string conversions using Catch-specific StringMaker for a couple of types. - Replace custom wxImage comparison with a Catch-specific matcher class. - Remove most of test running logic from test.cpp, in particular don't parse command line ourselves any longer but use Catch built-in command line parser. This is a source of a minor regression: previously, both "Foo" and "FooTestCase" could be used as the name of the test to run, but now only the latter is accepted.
2017-11-01 14:15:24 -04:00
CPPUNIT_ASSERT( tmpFile.Open(wxT("test2")) );
CPPUNIT_ASSERT( tmpFile.Write(wxT("the answer is 42")) );
CPPUNIT_ASSERT( tmpFile.Commit() );
CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );
}
#ifdef __LINUX__
// Check that GetSize() works correctly for special files.
TEST_CASE("wxFile::Special", "[file][linux][special-file]")
{
// We can't test /proc/kcore here, unlike in the similar
// wxFileName::GetSize() test, as wxFile must be able to open it (at least
// for reading) and usually we don't have the permissions to do it.
// This file is not seekable and has 0 size, but can still be read.
wxFile fileProc("/proc/cpuinfo");
CHECK( fileProc.IsOpened() );
wxString s;
CHECK( fileProc.ReadAll(&s) );
CHECK( !s.empty() );
// All files in /sys have the size of one kernel page, even if they don't
// have that much data in them.
const long pageSize = sysconf(_SC_PAGESIZE);
wxFile fileSys("/sys/power/state");
CHECK( fileSys.Length() == pageSize );
CHECK( fileSys.IsOpened() );
CHECK( fileSys.ReadAll(&s) );
CHECK( !s.empty() );
CHECK( s.length() < pageSize );
}
#endif // __LINUX__
#endif // wxUSE_FILE