/[escript]/trunk/esysUtils/test/EsysFileWriterTestCase.cpp
ViewVC logotype

Contents of /trunk/esysUtils/test/EsysFileWriterTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4480 - (show annotations)
Mon Jun 24 05:09:52 2013 UTC (6 years, 7 months ago) by caltinay
File size: 2837 byte(s)
More valgrind suppressions and memory leak fixes identified in various places.

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15
16
17 #include "EsysFileWriterTestCase.h"
18 #include "esysUtils/esysFileWriter.h"
19 #include <cppunit/TestCaller.h>
20 #include <fstream>
21 #include <sstream>
22
23 #include "esysUtils/Esys_MPI.h"
24
25 using namespace CppUnit;
26 using namespace std;
27
28 using esysUtils::FileWriter;
29
30 void EsysFileWriterTestCase::testAll()
31 {
32 const string filename("fwtest_file");
33 int mpisize=1, mpirank=0;
34 FileWriter* fw;
35 #ifdef ESYS_MPI
36 MPI_Comm_size(MPI_COMM_WORLD, &mpisize);
37 MPI_Comm_rank(MPI_COMM_WORLD, &mpirank);
38 fw = new FileWriter(MPI_COMM_WORLD);
39 #else
40 fw = new FileWriter();
41 #endif
42
43 const char crank = (const char)mpirank;
44 const char data[] = {crank,crank,crank,crank};
45 ostringstream oss;
46 oss.write(data, 4);
47
48 cout << endl;
49 cout << "\tTest open file." << endl;
50 CPPUNIT_ASSERT(fw->openFile(filename) == true);
51 cout << "\tTest writeOrdered." << endl;
52 CPPUNIT_ASSERT(fw->writeOrdered(oss) == true);
53 CPPUNIT_ASSERT(oss.str().length() == 0);
54 fw->close();
55 CPPUNIT_ASSERT(fileSize(filename) == 4*mpisize);
56
57 cout << "\tTest open file with initial size." << endl;
58 CPPUNIT_ASSERT(fw->openFile(filename, 100) == true);
59 fw->close();
60 CPPUNIT_ASSERT(fileSize(filename) == 100);
61
62 CPPUNIT_ASSERT(fw->openFile(filename) == true);
63 oss.write(data, 4);
64 cout << "\tTest writeShared." << endl;
65 CPPUNIT_ASSERT(fw->writeShared(oss) == true);
66 CPPUNIT_ASSERT(oss.str().length() == 0);
67 fw->close();
68 CPPUNIT_ASSERT(fileSize(filename) == 4*mpisize);
69
70 CPPUNIT_ASSERT(fw->openFile(filename) == true);
71 oss.write(data, 4);
72 cout << "\tTest writeAt." << endl;
73 CPPUNIT_ASSERT(fw->writeAt(oss, 16*(mpirank+1)) == true);
74 CPPUNIT_ASSERT(oss.str().length() == 0);
75 fw->close();
76 CPPUNIT_ASSERT(fileSize(filename) == 16*mpisize+4);
77 delete fw;
78 }
79
80 long EsysFileWriterTestCase::fileSize(string filename)
81 {
82 ifstream f(filename.c_str());
83 f.seekg(0, f.end);
84 long pos = f.tellg();
85 f.close();
86 #ifdef ESYS_MPI
87 MPI_Barrier(MPI_COMM_WORLD);
88 #endif
89 return pos;
90 }
91
92 TestSuite* EsysFileWriterTestCase::suite()
93 {
94 TestSuite *testSuite = new TestSuite("EsysFileWriterTestCase");
95 testSuite->addTest(new TestCaller<EsysFileWriterTestCase>(
96 "testAll",&EsysFileWriterTestCase::testAll));
97 return testSuite;
98 }
99

  ViewVC Help
Powered by ViewVC 1.1.26