Skip to content

Commit

Permalink
Written the unit test for version 2
Browse files Browse the repository at this point in the history
Createa s unit test for the new format based on the old test.

Compared to the original the unit test is now split into two.

Refs #7732
  • Loading branch information
keithnbrown committed Oct 18, 2013
1 parent aca508f commit 1ab3746
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions Code/Mantid/Framework/DataHandling/test/SaveAscii2Test.h
Expand Up @@ -2,7 +2,7 @@
#define SAVEASCIITEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/SaveAscii.h"
#include "MantidDataHandling/SaveAscii2.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/FrameworkManager.h"
#include <fstream>
Expand Down Expand Up @@ -56,7 +56,7 @@ class SaveAscii2Test : public CxxTest::TestSuite
std::string filename = "SaveAsciiTestFile.dat";
std::string filename_nohead ="SaveAsciiTestFileWithoutHeader.dat";

SaveAscii save;
SaveAscii2 save;
TS_ASSERT_THROWS_NOTHING(save.initialize());
TS_ASSERT( save.isInitialized() )
TS_ASSERT_THROWS_NOTHING(save.setPropertyValue("Filename", filename));
Expand All @@ -68,19 +68,56 @@ class SaveAscii2Test : public CxxTest::TestSuite
TS_ASSERT( Poco::File(filename).exists() );

// Now make some checks on the content of the file
std::ifstream in(filename.c_str());
std::string header1, header2, header3, header4;
std::string separator;
std::ifstream in(filename.c_str());
int specID;
std::string header1, header2, header3, separator, comment;

// Currently we just test that the first few column headers and a separator are as expected
in >> header1 >> separator >> header2 >> separator >> header3 >> separator >> header4;
in >> specID >> comment >> header1 >> separator >> header2 >> separator >> header3;
TS_ASSERT_EQUALS(specID, 1 );
TS_ASSERT_EQUALS(comment,"#" );
TS_ASSERT_EQUALS(separator,"," );
TS_ASSERT_EQUALS(header1,"X" );
TS_ASSERT_EQUALS(header2,"Y0" );
TS_ASSERT_EQUALS(header3,"E0" );
TS_ASSERT_EQUALS(header4,"Y1" );
TS_ASSERT_EQUALS(header2,"Y" );
TS_ASSERT_EQUALS(header3,"E" );
in.close();

Poco::File(filename).remove();
AnalysisDataService::Instance().remove(name);
}

void testExec_no_header()
{
Mantid::DataObjects::Workspace2D_sptr wsToSave = boost::dynamic_pointer_cast<
Mantid::DataObjects::Workspace2D>(WorkspaceFactory::Instance().create("Workspace2D", 2, 3, 3));
for (int i = 0; i < 2; i++)
{
std::vector<double>& X = wsToSave->dataX(i);
std::vector<double>& Y = wsToSave->dataY(i);
std::vector<double>& E = wsToSave->dataE(i);
for (int j = 0; j < 3; j++)
{
X[j] = 1.5 * j / 0.9;
Y[j] = (i + 1) * (2. + 4. * X[j]);
E[j] = 1.;
}
}
const std::string name = "SaveAsciiWS";
AnalysisDataService::Instance().add(name, wsToSave);

std::string filename = "SaveAsciiTestFile.dat";
std::string filename_nohead ="SaveAsciiTestFileWithoutHeader.dat";

SaveAscii2 save;
TS_ASSERT_THROWS_NOTHING(save.initialize());
TS_ASSERT( save.isInitialized() )
TS_ASSERT_THROWS_NOTHING(save.setPropertyValue("Filename", filename));
filename = save.getPropertyValue("Filename"); //Get absolute path
TS_ASSERT_THROWS_NOTHING(save.setPropertyValue("InputWorkspace", name));
TS_ASSERT_THROWS_NOTHING(save.execute());

// has the algorithm written a file to disk?
TS_ASSERT( Poco::File(filename).exists() );

// Test ColumnHeader property - perhaps this should be a separate test
save.setPropertyValue("Filename", filename_nohead);
Expand All @@ -98,15 +135,17 @@ class SaveAscii2Test : public CxxTest::TestSuite
std::ifstream in2(filename_nohead.c_str());
std::string line1noheader;
getline(in1,line2header);
getline(in1,line2header); // 2nd line of file with header
getline(in2,line1noheader); // 1st line of file without header
getline(in1,line2header);
getline(in1,line2header); // 3rd line of file with header
getline(in2,line1noheader);
getline(in2,line1noheader); // 2nd line of file without header
TS_ASSERT_EQUALS(line1noheader,line2header);
in1.close();
in2.close();

// Remove files
Poco::File(filename).remove();
Poco::File(filename_nohead).remove();
AnalysisDataService::Instance().remove(name);
}
};

Expand Down

0 comments on commit 1ab3746

Please sign in to comment.