[Avida-cvs] [avida-svn] r678 - in development/source: testsuites tools
kaben@myxo.css.msu.edu
kaben at myxo.css.msu.edu
Sun May 14 00:29:44 PDT 2006
Author: kaben
Date: 2006-05-14 03:29:44 -0400 (Sun, 14 May 2006)
New Revision: 678
Added:
development/source/tools/cFixedBlock.cc
development/source/tools/cFixedCoords.cc
Modified:
development/source/testsuites/full-unit-tests.cc
development/source/tools/CMakeLists.txt
development/source/tools/SConscript
development/source/tools/cChangeList.cc
development/source/tools/cChangeList.h
development/source/tools/cFixedBlock.h
development/source/tools/cFixedCoords.h
Log:
Added serialization and unit tests of serialization to cChangeList,
cFixedCoords, cFixedBlock; but cFixedBlock doesn't appear to be used in
Avida code, so I may need to remove some of the files I've just added.
Modified: development/source/testsuites/full-unit-tests.cc
===================================================================
--- development/source/testsuites/full-unit-tests.cc 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/testsuites/full-unit-tests.cc 2006-05-14 07:29:44 UTC (rev 678)
@@ -1,6 +1,9 @@
+#include "cChangeList.h"
#include "cDataEntry.h"
#include "cDataFile.h"
#include "cFile.h"
+//#include "cFixedBlock.h"
+#include "cFixedCoords.h"
#include "cInitFile.h"
#include "cRandom.h"
#include "cString.h"
@@ -10,9 +13,13 @@
int main() {
+ nChangeList::UnitTests(true);
+ //nBlockStruct::UnitTests(true);
nDataEntry::UnitTests(true);
nDataFile::UnitTests(true);
nFile::UnitTests(true);
+ //nFixedBlock::UnitTests(true);
+ nFixedCoords::UnitTests(true);
nInitFile::UnitTests(true);
nRandom::UnitTests(true);
nString::UnitTests(true);
Modified: development/source/tools/CMakeLists.txt
===================================================================
--- development/source/tools/CMakeLists.txt 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/CMakeLists.txt 2006-05-14 07:29:44 UTC (rev 678)
@@ -1,5 +1,5 @@
SET(libtools_a_SOURCES
- cBlockStruct.cc
+ #cBlockStruct.cc
cChangeList.cc
cConstSchedule.cc
cDataEntry.cc
@@ -9,6 +9,8 @@
cDefaultMessageDisplay.cc
cDoubleSum.cc
cFile.cc
+ #cFixedBlock.cc
+ cFixedCoords.cc
cHelpAlias.cc
cHelpManager.cc
cHelpType.cc
Modified: development/source/tools/SConscript
===================================================================
--- development/source/tools/SConscript 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/SConscript 2006-05-14 07:29:44 UTC (rev 678)
@@ -3,7 +3,7 @@
environment.Library('tools',
[
- 'cBlockStruct.cc',
+ #'cBlockStruct.cc',
'cChangeList.cc',
'cConstSchedule.cc',
'cDataEntry.cc',
@@ -12,6 +12,8 @@
'cDataManager_Base.cc',
'cDefaultMessageDisplay.cc',
'cDoubleSum.cc',
+ #'cFixedBlock.cc',
+ 'cFixedCoords.cc',
'cFile.cc',
'cHelpAlias.cc',
'cHelpManager.cc',
Modified: development/source/tools/cChangeList.cc
===================================================================
--- development/source/tools/cChangeList.cc 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/cChangeList.cc 2006-05-14 07:29:44 UTC (rev 678)
@@ -58,3 +58,105 @@
}
m_change_count = 0;
}
+
+
+#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 nChangeList {
+ /*
+ Test-helpers.
+ */
+ template <class T>
+ void save_stuff(const T &s, const char * filename){
+ std::ofstream ofs(filename);
+ cXMLOArchive oa(ofs);
+ oa.ArkvObj("cChangeList_Archive", s);
+ }
+
+ template <class T>
+ void restore_stuff(T &s, const char * filename) {
+ std::ifstream ifs(filename);
+ cXMLIArchive ia(ifs);
+ ia.ArkvObj("cChangeList_Archive", s);
+ }
+
+
+ namespace utChangeList_hello_world {
+ void test(){
+ BOOST_TEST(true);
+ BOOST_TEST(false);
+ }
+ }
+
+ namespace utChangeList_archiving {
+ void test(){
+ std::string filename("./cChangeList_basic_serialization.xml");
+ int changelist_size = 10;
+ int change_count = 5;
+ tArray<int> recorded_changes(change_count);
+
+ {
+ cChangeList cl(changelist_size);
+ BOOST_TEST(cl.GetSize() == changelist_size);
+
+ BOOST_TEST(cl.GetChangeCount() == 0);
+ cl.PushChange(1);
+ cl.PushChange(7);
+ cl.PushChange(3);
+ cl.PushChange(5);
+ cl.PushChange(2);
+ BOOST_TEST(cl.GetChangeCount() == change_count);
+
+ for(int i = 0; i < change_count; i++) {
+ recorded_changes[i] = cl.CheckChangeAt(i);
+ }
+ save_stuff<>(cl, filename.c_str());
+ }
+
+ {
+ cChangeList cl;
+ BOOST_TEST(cl.GetSize() == 0);
+ BOOST_TEST(cl.GetChangeCount() == 0);
+
+ restore_stuff<>(cl, filename.c_str());
+ BOOST_TEST(cl.GetChangeCount() == change_count);
+ for(int i = 0; i < change_count; i++) {
+ BOOST_TEST(recorded_changes[i] == cl.CheckChangeAt(i));
+ }
+ }
+
+ //std::remove(filename.c_str());
+ }
+ } // utChangeList_archiving
+
+
+
+ void UnitTests(bool full)
+ {
+ //if(full) {
+ // std::cout << "utChangeList_hello_world" << std::endl;
+ // utChangeList_hello_world::test();
+ //}
+ if(full) {
+ std::cout << "utChangeList_archiving" << std::endl;
+ utChangeList_archiving::test();
+ }
+ }
+} // nChangeList
+
+#endif // ENABLE_UNIT_TESTS
+
Modified: development/source/tools/cChangeList.h
===================================================================
--- development/source/tools/cChangeList.h 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/cChangeList.h 2006-05-14 07:29:44 UTC (rev 678)
@@ -72,6 +72,59 @@
void PushChange(int changed_index);
void Reset();
+
+ /**
+ * Save to archive
+ **/
+ template<class Archive>
+ void save(Archive & a, const unsigned int version) const {
+ /*
+ Must convert tArray<bool> m_change_tracking into tArray<int>
+ change_tracking, because our serializer doesn't like bools...
+ */
+ int count = m_change_tracking.GetSize();
+ tArray<int> change_tracking(count);
+ while(count--) change_tracking[count] = (m_change_tracking[count] == false)?(0):(1);
+
+ a.ArkvObj("m_change_count", m_change_count);
+ a.ArkvObj("m_change_list", m_change_list);
+ a.ArkvObj("m_change_tracking", change_tracking);
+ }
+
+ /**
+ * Load from archive
+ **/
+ template<class Archive>
+ void load(Archive & a, const unsigned int version){
+ tArray<int> change_tracking;
+
+ a.ArkvObj("m_change_count", m_change_count);
+ a.ArkvObj("m_change_list", m_change_list);
+ a.ArkvObj("m_change_tracking", change_tracking);
+
+ int count = change_tracking.GetSize();
+ m_change_tracking.Resize(count);
+ while(count--) m_change_tracking[count] = (change_tracking[count] == 0)?(false):(true);
+ }
+
+ /**
+ * Ask archive to handle loads and saves separately
+ **/
+ template<class Archive>
+ void serialize(Archive & a, const unsigned int version){
+ a.SplitLoadSave(*this, version);
+ }
};
+#ifdef ENABLE_UNIT_TESTS
+namespace nChangeList {
+ /**
+ * Run unit tests
+ *
+ * @param full Run full test suite; if false, just the fast tests.
+ **/
+ void UnitTests(bool full = false);
+}
+#endif
+
#endif
Added: development/source/tools/cFixedBlock.cc
===================================================================
--- development/source/tools/cFixedBlock.cc 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/cFixedBlock.cc 2006-05-14 07:29:44 UTC (rev 678)
@@ -0,0 +1,96 @@
+/*
+ * cFixedBlock.cc
+ * Avida
+ *
+ * Copyright 2005-2006 Michigan State University. All rights reserved.
+ * Copyright 1993-2003 California Institute of Technology.
+ *
+ */
+
+#include "cFixedBlock.h"
+
+#ifdef ENABLE_UNIT_TESTS
+
+/*
+Unit tests
+*/
+#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 nFixedBlock {
+ /*
+ Test-helpers.
+ */
+ template <class T>
+ void save_stuff(const T &s, const char * filename){
+ std::ofstream ofs(filename);
+ cXMLOArchive oa(ofs);
+ oa.ArkvObj("cFixedBlock_Archive", s);
+ }
+
+ template <class T>
+ void restore_stuff(T &s, const char * filename) {
+ std::ifstream ifs(filename);
+ cXMLIArchive ia(ifs);
+ ia.ArkvObj("cFixedBlock_Archive", s);
+ }
+
+
+ namespace utFixedBlock_hello_world {
+ void test(){
+ BOOST_TEST(true);
+ BOOST_TEST(false);
+ }
+ }
+
+ namespace utFixedBlock_archiving {
+ void test(){
+ std::string filename("./cFixedBlock_basic_serialization.xml");
+
+ /*
+ Create and archive a cFixedBlock object.
+ */
+ {
+ cFixedBlock fb;
+ BOOST_TEST(0 == fb.GetStart());
+ fb.SetStart(2);
+ BOOST_TEST(2 == fb.GetStart());
+ save_stuff<>(fb, filename.c_str());
+ }
+ /*
+ Reload archived cFixedBlock object.
+ */
+ {
+ cFixedBlock fb;
+ BOOST_TEST(0 == fb.GetStart());
+ restore_stuff<>(fb, filename.c_str());
+ BOOST_TEST(2 == fb.GetStart());
+ }
+
+ std::remove(filename.c_str());
+ }
+ } // utFixedBlock_archiving
+
+
+ void UnitTests(bool full)
+ {
+ //if(full) {
+ // std::cout << "utFixedBlock_hello_world" << std::endl;
+ // utFixedBlock_hello_world::test();
+ //}
+ if(full) {
+ std::cout << "utFixedBlock_archiving" << std::endl;
+ utFixedBlock_archiving::test();
+ }
+ }
+} // nFixedBlock
+
+#endif // ENABLE_UNIT_TESTS
+
Modified: development/source/tools/cFixedBlock.h
===================================================================
--- development/source/tools/cFixedBlock.h 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/cFixedBlock.h 2006-05-14 07:29:44 UTC (rev 678)
@@ -24,6 +24,14 @@
inline int GetStart() { return start_point; }
inline void SetStart(int in_sp) { start_point = in_sp; }
+
+ /**
+ * Serialize to and from archive.
+ **/
+ template<class Archive>
+ void serialize(Archive & a, const unsigned int version){
+ a.ArkvObj("start_point", start_point);
+ }
};
Added: development/source/tools/cFixedCoords.cc
===================================================================
--- development/source/tools/cFixedCoords.cc 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/cFixedCoords.cc 2006-05-14 07:29:44 UTC (rev 678)
@@ -0,0 +1,101 @@
+/*
+ * cFixedCoords.cc
+ * Avida
+ *
+ * Copyright 2005-2006 Michigan State University. All rights reserved.
+ * Copyright 1993-2003 California Institute of Technology.
+ *
+ */
+
+#include "cFixedCoords.h"
+
+#ifdef ENABLE_UNIT_TESTS
+
+/*
+Unit tests
+*/
+#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 nFixedCoords {
+ /*
+ Test-helpers.
+ */
+ template <class T>
+ void save_stuff(const T &s, const char * filename){
+ std::ofstream ofs(filename);
+ cXMLOArchive oa(ofs);
+ oa.ArkvObj("cFixedCoords_Archive", s);
+ }
+
+ template <class T>
+ void restore_stuff(T &s, const char * filename) {
+ std::ifstream ifs(filename);
+ cXMLIArchive ia(ifs);
+ ia.ArkvObj("cFixedCoords_Archive", s);
+ }
+
+
+ namespace utFixedCoords_hello_world {
+ void test(){
+ BOOST_TEST(true);
+ BOOST_TEST(false);
+ }
+ }
+
+ namespace utFixedCoords_archiving {
+ void test(){
+ std::string filename("./cFixedCoords_basic_serialization.xml");
+
+ /*
+ Create and archive a cFixedCoords object.
+ */
+ {
+ cFixedCoords fc;
+ BOOST_TEST(0 == fc.GetBlockNum());
+ BOOST_TEST(0 == fc.GetOffset());
+ fc(2,3);
+ BOOST_TEST(2 == fc.GetBlockNum());
+ BOOST_TEST(3 == fc.GetOffset());
+ save_stuff<>(fc, filename.c_str());
+ }
+ /*
+ Reload archived cFixedCoords object.
+ */
+ {
+ cFixedCoords fc;
+ BOOST_TEST(0 == fc.GetBlockNum());
+ BOOST_TEST(0 == fc.GetOffset());
+ restore_stuff<>(fc, filename.c_str());
+ BOOST_TEST(2 == fc.GetBlockNum());
+ BOOST_TEST(3 == fc.GetOffset());
+ }
+
+ std::remove(filename.c_str());
+ }
+ } // utFixedCoords_archiving
+
+
+ void UnitTests(bool full)
+ {
+ //if(full) {
+ // std::cout << "utFixedCoords_hello_world" << std::endl;
+ // utFixedCoords_hello_world::test();
+ //}
+ if(full) {
+ std::cout << "utFixedCoords_archiving" << std::endl;
+ utFixedCoords_archiving::test();
+ }
+ }
+} // nFixedCoords
+
+#endif // ENABLE_UNIT_TESTS
+
+
Modified: development/source/tools/cFixedCoords.h
===================================================================
--- development/source/tools/cFixedCoords.h 2006-05-14 05:38:05 UTC (rev 677)
+++ development/source/tools/cFixedCoords.h 2006-05-14 07:29:44 UTC (rev 678)
@@ -84,6 +84,15 @@
block_num += offset / fixed_size;
offset %= fixed_size;
}
+
+ /**
+ * Serialize to and from archive.
+ **/
+ template<class Archive>
+ void serialize(Archive & a, const unsigned int version){
+ a.ArkvObj("block_num", block_num);
+ a.ArkvObj("offset", offset);
+ }
};
More information about the Avida-cvs
mailing list