/[escript]/release/4.0/esysUtils/test/EsysFileWriterTestCase.cpp
ViewVC logotype

Contents of /release/4.0/esysUtils/test/EsysFileWriterTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5379 - (show annotations)
Mon Dec 15 23:58:17 2014 UTC (6 years, 6 months ago) by jfenwick
File size: 2928 byte(s)
branching for release prep

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2014 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 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17
18 #include "EsysFileWriterTestCase.h"
19 #include "esysUtils/esysFileWriter.h"
20 #include <cppunit/TestCaller.h>
21 #include <fstream>
22 #include <sstream>
23
24 #include "esysUtils/Esys_MPI.h"
25
26 using namespace CppUnit;
27 using namespace std;
28
29 using esysUtils::FileWriter;
30
31 void EsysFileWriterTestCase::testAll()
32 {
33 const string filename("fwtest_file");
34 int mpisize=1, mpirank=0;
35 FileWriter* fw;
36 #ifdef ESYS_MPI
37 MPI_Comm_size(MPI_COMM_WORLD, &mpisize);
38 MPI_Comm_rank(MPI_COMM_WORLD, &mpirank);
39 fw = new FileWriter(MPI_COMM_WORLD);
40 #else
41 fw = new FileWriter();
42 #endif
43
44 const char crank = static_cast<char>(mpirank<128?mpirank:128);
45 const char data[] = {crank,crank,crank,crank};
46 ostringstream oss;
47 oss.write(data, 4);
48
49 cout << endl;
50 cout << "\tTest open file." << endl;
51 CPPUNIT_ASSERT(fw->openFile(filename) == true);
52 cout << "\tTest writeOrdered." << endl;
53 CPPUNIT_ASSERT(fw->writeOrdered(oss) == true);
54 CPPUNIT_ASSERT(oss.str().length() == 0);
55 fw->close();
56 CPPUNIT_ASSERT(fileSize(filename) == 4*mpisize);
57
58 cout << "\tTest open file with initial size." << endl;
59 CPPUNIT_ASSERT(fw->openFile(filename, 100) == true);
60 fw->close();
61 CPPUNIT_ASSERT(fileSize(filename) == 100);
62
63 CPPUNIT_ASSERT(fw->openFile(filename) == true);
64 oss.write(data, 4);
65 cout << "\tTest writeShared." << endl;
66 CPPUNIT_ASSERT(fw->writeShared(oss) == true);
67 CPPUNIT_ASSERT(oss.str().length() == 0);
68 fw->close();
69 CPPUNIT_ASSERT(fileSize(filename) == 4*mpisize);
70
71 CPPUNIT_ASSERT(fw->openFile(filename) == true);
72 oss.write(data, 4);
73 cout << "\tTest writeAt." << endl;
74 CPPUNIT_ASSERT(fw->writeAt(oss, 16*(mpirank+1)) == true);
75 CPPUNIT_ASSERT(oss.str().length() == 0);
76 fw->close();
77 CPPUNIT_ASSERT(fileSize(filename) == 16*mpisize+4);
78 delete fw;
79 }
80
81 long EsysFileWriterTestCase::fileSize(string filename)
82 {
83 ifstream f(filename.c_str());
84 f.seekg(0, f.end);
85 long pos = f.tellg();
86 f.close();
87 #ifdef ESYS_MPI
88 MPI_Barrier(MPI_COMM_WORLD);
89 #endif
90 return pos;
91 }
92
93 TestSuite* EsysFileWriterTestCase::suite()
94 {
95 TestSuite *testSuite = new TestSuite("EsysFileWriterTestCase");
96 testSuite->addTest(new TestCaller<EsysFileWriterTestCase>(
97 "testAll",&EsysFileWriterTestCase::testAll));
98 return testSuite;
99 }
100

  ViewVC Help
Powered by ViewVC 1.1.26