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