[Avida-cvs] [avida-svn] r677 - in development/source: testsuites tools
kaben@myxo.css.msu.edu
kaben at myxo.css.msu.edu
Sat May 13 22:38:05 PDT 2006
Author: kaben
Date: 2006-05-14 01:38:05 -0400 (Sun, 14 May 2006)
New Revision: 677
Modified:
development/source/testsuites/full-unit-tests.cc
development/source/tools/cDataFile.cc
development/source/tools/cDataFile.h
Log:
Added archiving of cDataFile, and unit test of cDataFile archiving.
Modified: development/source/testsuites/full-unit-tests.cc
===================================================================
--- development/source/testsuites/full-unit-tests.cc 2006-05-12 02:11:01 UTC (rev 676)
+++ development/source/testsuites/full-unit-tests.cc 2006-05-14 05:38:05 UTC (rev 677)
@@ -1,4 +1,5 @@
#include "cDataEntry.h"
+#include "cDataFile.h"
#include "cFile.h"
#include "cInitFile.h"
#include "cRandom.h"
@@ -10,6 +11,7 @@
int main() {
nDataEntry::UnitTests(true);
+ nDataFile::UnitTests(true);
nFile::UnitTests(true);
nInitFile::UnitTests(true);
nRandom::UnitTests(true);
Modified: development/source/tools/cDataFile.cc
===================================================================
--- development/source/tools/cDataFile.cc 2006-05-12 02:11:01 UTC (rev 676)
+++ development/source/tools/cDataFile.cc 2006-05-14 05:38:05 UTC (rev 677)
@@ -130,3 +130,171 @@
}
else m_fp << endl;
}
+
+
+#ifdef ENABLE_UNIT_TESTS
+
+/*
+Unit tests
+*/
+#include "cFile.h"
+#include "cXMLArchive.h"
+
+#include <boost/detail/lightweight_test.hpp>
+
+#include <cstdio> // for std::remove() to remove temporary files.
+#include <iomanip>
+#include <iostream>
+#include <fstream>
+#include <string>
+
+namespace nDataFile {
+ /*
+ Test-helpers.
+ */
+ template <class T>
+ void save_stuff(const T &s, const char * filename){
+ std::ofstream ofs(filename);
+ cXMLOArchive oa(ofs);
+ oa.ArkvObj("cDataFile_Archive", s);
+ }
+
+ template <class T>
+ void restore_stuff(T &s, const char * filename) {
+ std::ifstream ifs(filename);
+ cXMLIArchive ia(ifs);
+ ia.ArkvObj("cDataFile_Archive", s);
+ }
+
+
+ namespace utDataFile_hello_world {
+ void test(){
+ BOOST_TEST(true);
+ BOOST_TEST(false);
+ }
+ }
+
+ namespace utDataFile_archiving {
+ void test(){
+ std::string filename("./cDataFile_basic_serialization.xml");
+ int linecount = 3;
+ std::string data_file_name("./cDataFile_data.txt");
+ cString dfn(data_file_name.c_str());
+
+ /*
+ Create a data file object, write two lines of data, archive the
+ data file object ...
+ */
+ {
+ cDataFile df(dfn);
+ df.WriteComment("Comment!");
+ df.Write(1, "descr1");
+ df.Write(1.0, "descr2");
+ df.Write("blah", "descr3");
+ df.Endl();
+ df.Write(2, "descr1");
+ df.Write(2.0, "descr2");
+ df.Write("blahblah", "descr3");
+ df.Endl();
+ save_stuff<>(df, filename.c_str());
+ }
+ /*
+ Sanity-check contents of file on disk.
+ */
+ {
+ cFile f(dfn);
+ BOOST_TEST(f.Good());
+
+ cString l[7];
+ for(int i = 0; i<7; i++){
+ f.ReadLine(l[i]);
+ //std::cout << "\"" << l[i] << "\"" << std::endl;
+ }
+ BOOST_TEST(cString("# Comment!") == l[0]);
+ BOOST_TEST(cString("# 1: descr1") == l[1]);
+ BOOST_TEST(cString("# 2: descr2") == l[2]);
+ BOOST_TEST(cString("# 3: descr3") == l[3]);
+ BOOST_TEST(cString("") == l[4]);
+ BOOST_TEST(cString("1 1.000000 blah ") == l[5]);
+ BOOST_TEST(cString("2 2 blahblah ") == l[6]);
+ }
+ /*
+ Reload the data file object, write another line of data ...
+ */
+ {
+ cDataFile df;
+ restore_stuff<>(df, filename.c_str());
+ df.Write(3, "descr1");
+ df.Write(3.0, "descr2");
+ df.Write("blahblahblah", "descr3");
+ }
+ /*
+ Verify that a new line was appended to the file on disk.
+ */
+ {
+ cFile f(dfn);
+ BOOST_TEST(f.Good());
+
+ cString l[8];
+ for(int i = 0; i<8; i++){
+ f.ReadLine(l[i]);
+ //std::cout << "\"" << l[i] << "\"" << std::endl;
+ }
+ BOOST_TEST(cString("# Comment!") == l[0]);
+ BOOST_TEST(cString("# 1: descr1") == l[1]);
+ BOOST_TEST(cString("# 2: descr2") == l[2]);
+ BOOST_TEST(cString("# 3: descr3") == l[3]);
+ BOOST_TEST(cString("") == l[4]);
+ BOOST_TEST(cString("1 1.000000 blah ") == l[5]);
+ BOOST_TEST(cString("2 2 blahblah ") == l[6]);
+ BOOST_TEST(cString("3 3 blahblahblah ") == l[7]);
+ }
+
+ /*
+ Create a new data file object with the same name. This should
+ overwrite the original file on disk.
+ */
+ {
+ cDataFile df(dfn);
+ df.WriteComment("This should be a new file.");
+ df.Write(1, "newdescr");
+ df.Endl();
+ }
+ /*
+ Verify that original file on disk has been overwritten.
+ */
+ {
+ cFile f(dfn);
+ BOOST_TEST(f.Good());
+
+ cString l[4];
+ for(int i = 0; i<4; i++){
+ f.ReadLine(l[i]);
+ //std::cout << "\"" << l[i] << "\"" << std::endl;
+ }
+ BOOST_TEST(cString("# This should be a new file.") == l[0]);
+ BOOST_TEST(cString("# 1: newdescr") == l[1]);
+ BOOST_TEST(cString("") == l[2]);
+ BOOST_TEST(cString("1 ") == l[3]);
+ }
+
+ std::remove(filename.c_str());
+ std::remove(data_file_name.c_str());
+ }
+ } // utDataFile_archiving
+
+
+ void UnitTests(bool full)
+ {
+ //if(full) {
+ // std::cout << "utDataFile_hello_world" << std::endl;
+ // utDataFile_hello_world::test();
+ //}
+ if(full) {
+ std::cout << "utDataFile_archiving" << std::endl;
+ utDataFile_archiving::test();
+ }
+ }
+} // nDataFile
+
+#endif // ENABLE_UNIT_TESTS
Modified: development/source/tools/cDataFile.h
===================================================================
--- development/source/tools/cDataFile.h 2006-05-12 02:11:01 UTC (rev 676)
+++ development/source/tools/cDataFile.h 2006-05-14 05:38:05 UTC (rev 677)
@@ -142,6 +142,71 @@
* This function makes sure that all cached data is written to the disk.
**/
void Flush() { m_fp.flush(); }
+
+
+ /**
+ * Save to archive
+ **/
+ template<class Archive>
+ void save(Archive & a, const unsigned int version) const {
+ /*
+ __is_open and __verbose are workarounds for bool-serialization bugs.
+ @kgn
+ */
+ int __m_descr_written = (m_descr_written == false)?(0):(1);
+ int __is_open = (m_fp.is_open() == false)?(0):(1);
+
+ a.ArkvObj("m_name", m_name);
+ a.ArkvObj("m_data", m_data);
+ a.ArkvObj("m_descr", m_descr);
+ a.ArkvObj("num_cols", num_cols);
+ a.ArkvObj("m_descr_written", __m_descr_written);
+ a.ArkvObj("is_open", __is_open);
+ if(__is_open){
+ /*
+ If the file is open, record current read-position.
+ */
+ int position = m_fp.rdbuf()->pubseekoff(0,std::ios::cur);
+ a.ArkvObj("position", position);
+ }
+ }
+
+ /**
+ * Load from archive
+ **/
+ template<class Archive>
+ void load(Archive & a, const unsigned int version){
+ int __m_descr_written;
+ int __is_open;
+
+ a.ArkvObj("m_name", m_name);
+ a.ArkvObj("m_data", m_data);
+ a.ArkvObj("m_descr", m_descr);
+ a.ArkvObj("num_cols", num_cols);
+ a.ArkvObj("m_descr_written", __m_descr_written);
+ a.ArkvObj("is_open", __is_open);
+
+ m_descr_written = (__m_descr_written == 0)?(false):(true);
+
+ if(__is_open){
+ /*
+ If the file is open, record current read-position.
+ */
+ int position;
+ a.ArkvObj("position", position);
+ m_fp.open(m_name, std::ios::out|std::ios::app);
+ m_fp.rdbuf()->pubseekpos(position);
+ }
+ }
+
+ /**
+ * Ask archive to handle loads and saves separately
+ **/
+ template<class Archive>
+ void serialize(Archive & a, const unsigned int version){
+ a.SplitLoadSave(*this, version);
+ }
+
};
More information about the Avida-cvs
mailing list