2009-09-12 18:40:42 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 );
|
2012-10-04 18:47:44 -04:00
|
|
|
CPPUNIT_TEST( ReadAll );
|
2010-10-16 19:05:20 -04:00
|
|
|
#if wxUSE_UNICODE
|
2009-09-12 18:40:42 -04:00
|
|
|
CPPUNIT_TEST( RoundTripUTF8 );
|
|
|
|
CPPUNIT_TEST( RoundTripUTF16 );
|
|
|
|
CPPUNIT_TEST( RoundTripUTF32 );
|
2010-10-16 19:05:20 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2010-06-13 10:30:55 -04:00
|
|
|
CPPUNIT_TEST( TempFile );
|
2009-09-12 18:40:42 -04:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2012-10-04 18:47:44 -04:00
|
|
|
void ReadAll();
|
2010-10-16 19:05:20 -04:00
|
|
|
#if wxUSE_UNICODE
|
2009-09-12 18:40:42 -04:00
|
|
|
void RoundTripUTF8() { DoRoundTripTest(wxConvUTF8); }
|
|
|
|
void RoundTripUTF16() { DoRoundTripTest(wxMBConvUTF16()); }
|
|
|
|
void RoundTripUTF32() { DoRoundTripTest(wxMBConvUTF32()); }
|
2010-10-16 19:05:20 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
2009-09-12 18:40:42 -04:00
|
|
|
|
|
|
|
void DoRoundTripTest(const wxMBConv& conv);
|
2010-06-13 10:30:55 -04:00
|
|
|
void TempFile();
|
2009-09-12 18:40:42 -04:00
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(FileTestCase);
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// CppUnit macros
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( FileTestCase );
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileTestCase, "FileTestCase" );
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// tests implementation
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2012-10-04 18:47:44 -04:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-16 19:05:20 -04:00
|
|
|
#if wxUSE_UNICODE
|
|
|
|
|
2009-09-12 18:40:42 -04:00
|
|
|
void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
|
|
|
|
{
|
|
|
|
TestFile tf;
|
|
|
|
|
2010-10-16 19:05:26 -04:00
|
|
|
// Explicit length is needed because of the embedded NUL.
|
|
|
|
const wxString data("Hello\0UTF!", 10);
|
2009-09-12 18:40:42 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
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() );
|
|
|
|
|
2009-09-13 08:26:03 -04:00
|
|
|
const ssize_t len = fin.Length();
|
2009-09-12 18:40:42 -04:00
|
|
|
wxCharBuffer buf(len);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );
|
|
|
|
|
2010-10-16 19:05:26 -04:00
|
|
|
wxString dataReadBack(buf, conv, len);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
|
2009-09-12 18:40:42 -04:00
|
|
|
}
|
2016-02-18 20:46:26 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
wxFile fin(tf.GetName(), wxFile::read);
|
|
|
|
CPPUNIT_ASSERT( fin.IsOpened() );
|
|
|
|
|
|
|
|
wxString dataReadBack;
|
|
|
|
CPPUNIT_ASSERT( fin.ReadAll(&dataReadBack, conv) );
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( data, dataReadBack );
|
|
|
|
}
|
2009-09-12 18:40:42 -04:00
|
|
|
}
|
|
|
|
|
2010-10-16 19:05:20 -04:00
|
|
|
#endif // wxUSE_UNICODE
|
|
|
|
|
2010-06-13 10:30:55 -04:00
|
|
|
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")) );
|
2010-06-13 10:30:55 -04:00
|
|
|
CPPUNIT_ASSERT( tmpFile.Commit() );
|
|
|
|
CPPUNIT_ASSERT( wxRemoveFile(wxT("test2")) );
|
|
|
|
}
|
|
|
|
|
2017-12-15 12:34:43 -05:00
|
|
|
#ifdef __LINUX__
|
|
|
|
|
|
|
|
// Check that GetSize() works correctly for special files.
|
|
|
|
TEST_CASE("wxFile::Special", "[file][linux][special-file]")
|
|
|
|
{
|
2017-12-15 11:19:09 -05:00
|
|
|
// 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.
|
|
|
|
|
2017-12-15 12:34:43 -05:00
|
|
|
// This file is not seekable and has 0 size, but can still be read.
|
2020-04-07 07:41:21 -04:00
|
|
|
wxFile fileProc("/proc/cpuinfo");
|
2017-12-15 12:34:43 -05:00
|
|
|
CHECK( fileProc.IsOpened() );
|
|
|
|
|
|
|
|
wxString s;
|
|
|
|
CHECK( fileProc.ReadAll(&s) );
|
2021-04-04 08:37:09 -04:00
|
|
|
CHECK( !s.empty() );
|
2017-12-15 12:34:43 -05:00
|
|
|
|
2020-04-07 07:38:34 -04:00
|
|
|
// 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);
|
|
|
|
|
2017-12-15 12:34:43 -05:00
|
|
|
wxFile fileSys("/sys/power/state");
|
2020-04-07 07:38:34 -04:00
|
|
|
CHECK( fileSys.Length() == pageSize );
|
2017-12-15 12:34:43 -05:00
|
|
|
CHECK( fileSys.IsOpened() );
|
|
|
|
CHECK( fileSys.ReadAll(&s) );
|
|
|
|
CHECK( !s.empty() );
|
2020-04-07 07:38:34 -04:00
|
|
|
CHECK( s.length() < pageSize );
|
2017-12-15 12:34:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // __LINUX__
|
|
|
|
|
2009-09-12 18:40:42 -04:00
|
|
|
#endif // wxUSE_FILE
|