/[escript]/trunk/weipa/test/EscriptDatasetTestCase.cpp
ViewVC logotype

Contents of /trunk/weipa/test/EscriptDatasetTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3151 - (show annotations)
Fri Sep 3 05:46:12 2010 UTC (11 years, 1 month ago) by caltinay
File size: 6836 byte(s)
More weipa tests including saveVTK.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include "escript/DataFactory.h"
16 #include "finley/CppAdapter/MeshAdapterFactory.h"
17 #include "weipa/EscriptDataset.h"
18 #include "EscriptDatasetTestCase.h"
19
20 using namespace CppUnitTest;
21 using namespace escript;
22 using namespace finley;
23 using namespace weipa;
24 using namespace std;
25
26 void EscriptDatasetTestCase::setUp()
27 {
28 // This is called before each test is run
29 }
30
31 void EscriptDatasetTestCase::tearDown()
32 {
33 // This is called after each test has been run
34 }
35
36 void EscriptDatasetTestCase::testAll()
37 {
38 cout << endl;
39 cout << "\tTest default constructor." << endl;
40 EscriptDataset_ptr dataset(new EscriptDataset());
41
42 cout << "\tTest saveSilo without data." << endl;
43 assert(dataset->saveSilo("dummy") == false);
44
45 cout << "\tTest saveVTK without data." << endl;
46 assert(dataset->saveVTK("dummy") == false);
47
48 cout << "\tTest getConvertedDomain without data." << endl;
49 assert(dataset->getConvertedDomain().size() == 0);
50
51 cout << "\tTest getVariables without data." << endl;
52 assert(dataset->getVariables().size() == 0);
53
54 cout << "\tTest getMeshVariables without data." << endl;
55 assert(dataset->getMeshVariables().size() == 0);
56
57 // instantiate a domain and data
58 Domain_ptr dom(brick());
59 escript::Data data = Scalar(0.0, continuousFunction(*dom), true);
60
61 cout << "\tTest addData with NULL domain." << endl;
62 assert(dataset->addData(data, "foo", "bar") == false);
63
64 cout << "\tTest setDomain." << endl;
65 assert(dataset->setDomain(dom.get()) == true);
66 assert(dataset->getMeshVariables().size() > 0);
67
68 cout << "\tTest bogus setDomain call." << endl;
69 assert(dataset->setDomain(dom.get()) == false);
70
71 cout << "\tTest getConvertedDomain." << endl;
72 MeshBlocks blocks = dataset->getConvertedDomain();
73 assert(blocks.size() > 0);
74
75 StringVec varfiles, varnames;
76 varfiles.push_back("testvar%04d.nc");
77 varnames.push_back("testvar");
78 cout << "\tTest bogus loadNetCDF call 1." << endl;
79 assert(dataset->loadNetCDF("mesh%04d.nc", varfiles, varnames, 1) == false);
80
81 cout << "\tTest bogus loadNetCDF call 2." << endl;
82 assert(dataset->loadNetCDF(blocks, varfiles, varnames) == false);
83
84 cout << "\tTest addData with valid data." << endl;
85 assert(dataset->addData(data, "testvar", "cm") == true);
86 assert(dataset->getVariables().size() == 1);
87
88 cout << "\tTest set/getCycleAndTime." << endl;
89 dataset->setCycleAndTime(42, 3.1415);
90 assert(dataset->getCycle() == 42);
91 assert(dataset->getTime()-3.1415 < 0.001);
92
93 dataset->setMetadataSchemaString("xmlns:test=\"http://myschema.com/test\"",
94 "<MyValue>4711</MyValue>");
95 dataset->setMeshLabels("x-axis", "y-axis", "z-axis");
96 dataset->setMeshUnits("km", "cm", "mm");
97
98 #if USE_SILO
99 cout << "\tTest saveSilo." << endl;
100 assert(dataset->saveSilo("weipatest.silo") == true);
101 ifstream f("weipatest.silo");
102 assert(f.is_open());
103 f.close();
104 #endif
105
106 cout << "\tTest saveVTK." << endl;
107 assert(dataset->saveVTK("weipatest.vtu") == true);
108 checkVTKfile("weipatest.vtu");
109
110 //varnames.push_back("dummy");
111 //cout << "\tTest loadNetCDF with invalid params." << endl;
112 //assert(dataset->loadNetCDF(blocks, varfiles, varnames) == false);
113 }
114
115 TestSuite* EscriptDatasetTestCase::suite()
116 {
117 //
118 // create the suite of tests to perform.
119 TestSuite *testSuite = new TestSuite("EscriptDatasetTestCase");
120
121 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
122 "testAll",&EscriptDatasetTestCase::testAll));
123 return testSuite;
124 }
125
126 int EscriptDatasetTestCase::getDataArrayLength(std::istream& is)
127 {
128 int length=0;
129 char line[256];
130 while (is.good()) {
131 is.getline(line, 256);
132 string s(line);
133 if (s.find("</DataArray") != 0)
134 length++;
135 else
136 break;
137 }
138 return length;
139 }
140
141 void EscriptDatasetTestCase::checkVTKfile(std::string filename)
142 {
143 ifstream f(filename.c_str());
144 assert(f.is_open());
145
146 char line[256];
147 int numPoints=0, numCells=0;
148 while (f.good()) {
149 f.getline(line, 256);
150 string s(line);
151 size_t pp = s.find("NumberOfPoints=");
152 size_t cp = s.find("NumberOfCells=");
153 if (pp!=s.npos && cp!=s.npos) {
154 stringstream ss;
155 string tmp(s.substr(pp+16));
156 ss.str(tmp);
157 ss >> numPoints;
158 tmp = s.substr(cp+15);
159 ss.str(tmp);
160 ss >> numCells;
161 break;
162 }
163 }
164 assert(numPoints>0);
165 assert(numCells>0);
166
167 bool pointsFound=false, cellsFound=false;
168 int numPointData=0, numCellData=0;
169
170 while (f.good()) {
171 f.getline(line, 256);
172 string s(line);
173 if (s.compare("<Points>") == 0) {
174 pointsFound=true;
175 // check node coordinates
176 while (f.good() && s.find("</Points>") != 0) {
177 f.getline(line, 256);
178 s = line;
179 if (s.find("<DataArray") == 0) {
180 assertLongsEqual(numPoints, getDataArrayLength(f));
181 }
182 }
183 } else if (s.find("<Cells>") == 0) {
184 cellsFound=true;
185 // check cell info (connectivity, offsets, types)
186 while (f.good() && s.find("</Cells>") != 0) {
187 f.getline(line, 256);
188 s = line;
189 if (s.find("<DataArray") == 0) {
190 assertLongsEqual(numCells, getDataArrayLength(f));
191 }
192 }
193 } else if (s.compare("<PointData>") == 0) {
194 // check nodal data
195 while (f.good() && s.find("</PointData>") != 0) {
196 f.getline(line, 256);
197 s = line;
198 if (s.find("<DataArray") == 0) {
199 numPointData++;
200 assertLongsEqual(numPoints, getDataArrayLength(f));
201 }
202 }
203 } else if (s.find("<CellData>") == 0) {
204 // check cell data
205 while (f.good() && s.find("</CellData>") != 0) {
206 f.getline(line, 256);
207 s = line;
208 if (s.find("<DataArray") == 0) {
209 numCellData++;
210 assertLongsEqual(numCells, getDataArrayLength(f));
211 }
212 }
213 }
214 }
215
216 assert(pointsFound);
217 assert(cellsFound);
218 assert(numPointData>0);
219 assert(numCellData>0);
220 }
221

  ViewVC Help
Powered by ViewVC 1.1.26