]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/tempfile.cpp
Avoid needless second string conversion when adding files to memory FS.
[wxWidgets.git] / tests / streams / tempfile.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/tempfile.cpp
3 // Purpose: Test wxTempFileOutputStream
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) 2005 Mike Wetherell
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 // for all others, include the necessary headers
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/wfstream.h"
22 #include "wx/filename.h"
23 #include "bstream.h"
24
25 #if wxUSE_STREAMS && wxUSE_FILE
26
27 #include "testfile.h"
28
29 ///////////////////////////////////////////////////////////////////////////////
30 // The test case
31
32 class tempStream : public CppUnit::TestCase
33 {
34 CPPUNIT_TEST_SUITE(tempStream);
35 CPPUNIT_TEST(DoNothing);
36 CPPUNIT_TEST(Close);
37 CPPUNIT_TEST(Commit);
38 CPPUNIT_TEST(Discard);
39 CPPUNIT_TEST_SUITE_END();
40
41 void DoNothing() { DoTest(DONOTHING, false); }
42 void Close() { DoTest(CLOSE, true); }
43 void Commit() { DoTest(COMMIT, true); }
44 void Discard() { DoTest(DISCARD, false); }
45
46 enum Action { DONOTHING, CLOSE, COMMIT, DISCARD };
47 void DoTest(Action action, bool shouldHaveCommited);
48 };
49
50 // the common test code
51 //
52 void tempStream::DoTest(Action action, bool shouldHaveCommited)
53 {
54 TestFile temp;
55
56 {
57 wxTempFileOutputStream out(temp.GetName());
58 out.Write("Affer", 5);
59 CPPUNIT_ASSERT(out.SeekO(2) == 2);
60 out.Write("t", 1);
61 CPPUNIT_ASSERT(out.IsSeekable());
62 CPPUNIT_ASSERT(out.GetLength() == 5);
63 CPPUNIT_ASSERT(out.TellO() == 3);
64
65 switch (action) {
66 case DONOTHING: break;
67 case COMMIT: out.Commit(); break;
68 case DISCARD: out.Discard(); break;
69 case CLOSE: out.Close();
70 }
71 }
72
73 wxFileInputStream in(temp.GetName());
74 char buf[32];
75 in.Read(buf, sizeof(buf));
76 buf[in.LastRead()] = 0;
77 CPPUNIT_ASSERT(strcmp(buf, shouldHaveCommited ? "After" : "Before") == 0);
78 }
79
80
81 // Register the stream sub suite, by using some stream helper macro.
82 // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite())
83 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(tempStream)
84
85 #endif // wxUSE_STREAMS && wxUSE_FILE