- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Models/Base/Io/SaveLoad/unit-tests/BMSerializerTest.cpp

    00001
    00002 //
    00003 // Copyright (c) 2005           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #include <tut.h>
    00012 #include <Mers/Models/Base/Io/SaveLoad/Serializer.h>
    00013 #include <Mers/Models/Base/BaseModel.h>
    00014 #include <Mers/Models/Base/unit-tests/BMHelper.h>
    00015 #include <Mers/Utils/Lang/String.h>
    00016 #include <Mers/Utils/Io/Xml/XercesSetup.h>
    00017 #include <Mers/Utils/Io/Xml/ScopedTranscoder.h>
    00018 #include <Mers/Utils/Io/Xml/Dom/XMLSaveLoad.h>
    00019 #include <xercesc/dom/DOM.hpp>
    00020 #include <memory>
    00021
    00023
    00024 using Mers::Models::Base::Io::SaveLoad::Serializer;
    00025 using tut::BMHelper;
    00026 using Mers::Models::Base::BaseModel;
    00027 using Mers::Models::Base::BaseModelValue;
    00028 using Mers::Models::Base::BaseModelValueArray;
    00029 using Mers::Models::Base::BaseModelDomain;
    00030 using Mers::Models::Base::BaseModelDomainArray;
    00031 using Mers::Models::Base::BaseModelVariable;
    00032 using Mers::Models::Base::BaseModelVariableArray;
    00033 using Mers::Models::Base::BaseModelVariableArrayArray;
    00034 using Mers::Models::Base::BaseModelAssignment;
    00035 using Mers::Models::Base::BaseModelAssignmentArray;
    00036 using Mers::Models::Base::BaseModelClause;
    00037 using Mers::Models::Base::BaseModelClauseArray;
    00038 using Mers::Models::Base::BaseModelSentence;
    00039 using Mers::Utils::Lang::String;
    00040 using Mers::Utils::Io::Xml::XercesSetup;
    00041 using Mers::Utils::Io::Xml::CScopedTranscoder;
    00042 using Mers::Utils::Io::Xml::XMLScopedTranscoder;
    00043 using Mers::Utils::Io::Xml::Dom::XMLSaveLoad;
    00044 using XERCES_CPP_NAMESPACE::DOMImplementation;
    00045 using XERCES_CPP_NAMESPACE::DOMDocument;
    00046 using XERCES_CPP_NAMESPACE::DOMElement;
    00047 using XERCES_CPP_NAMESPACE::DOMImplementationRegistry;
    00048 using XERCES_CPP_NAMESPACE::DOMAttr;
    00049 using XERCES_CPP_NAMESPACE::DOMNode;
    00050 using XERCES_CPP_NAMESPACE::DOMNodeList;
    00051 using tut::ensure;
    00052 using tut::ensure_equals;
    00053 using std::auto_ptr;
    00054
    00056
    00057 namespace {
    00058
    00062 class BMSerializerData : public BMHelper
    00063 {
    00064 public:
    00068         BMSerializerData() :
    00069                 m_model( "BMS_Model" ),
    00070                 m_serializer( &m_model ),
    00071                 m_xerces( NULL )
    00072         {
    00073                 // Setup Xerces.
    00074                 m_xerces = new XercesSetup;
    00075
    00076                 // Create an XML document.
    00077                 XMLScopedTranscoder sXMLV1("XML");
    00078                 m_implementation =
    00079                         DOMImplementationRegistry::getDOMImplementation( sXMLV1 );
    00080                 XMLScopedTranscoder sDocRootName("save_test");
    00081                 m_document =
    00082                         m_implementation->createDocument(NULL, sDocRootName, NULL);
    00083         }
    00084
    00085         ~BMSerializerData()
    00086         {
    00087                 m_document->release();
    00088                 delete m_xerces;
    00089         }
    00090
    00091 protected:
    00092
    00102         void createValues( BaseModelValueArray &values, const String &name,
    00103                 unsigned int numValues )
    00104         {
    00105                 // Create a set of values for the array of values.
    00106                 for( unsigned int i = 0; i < numValues; ++i ) {
    00107                         // Create a (unique) name for this value
    00108                         String valueName( name );
    00109                         valueName += i;
    00110                         // Create the value
    00111                         BaseModelValue * value = new BaseModelValue( valueName );
    00112                         // Add it to the array of values
    00113                         values.pushBack( value );
    00114                 }
    00115         }
    00116
    00117 protected:
    00118         // A model
    00119         BaseModel m_model;
    00120
    00121         // The object we're using to serialize.
    00122         Serializer m_serializer;
    00123
    00124         // Setup Xerces for the test.
    00125         XercesSetup * m_xerces;
    00126
    00127         // The DOM implementation of Xerces.
    00128         DOMImplementation * m_implementation;
    00129
    00130         // The document we're going to use for testing.
    00131         DOMDocument * m_document;
    00132 };
    00133
    00135
    00136 typedef tut::test_group<BMSerializerData, 10> t_SaveSerializationFactory;
    00137 typedef t_SaveSerializationFactory::object t_SaveSerializationObject;
    00138
    00140
    00141 t_SaveSerializationFactory ssFactory( "BaseModel Serialization Tests" );
    00142
    00144
    00145 } // End nameless namespace
    00146
    00148
    00149 // Save/Load Versions that we will test.
    00150 unsigned int kSaveLoadVersion_Initial = 1;
    00151
    00153
    00154 namespace tut {
    00155
    00159 template<> template<> void t_SaveSerializationObject::test<1>()
    00160 {
    00161         // The values in the domain.  We're just going to save the first one.
    00162         const char * domainVals[] = {"Val1", "Val2", NULL};
    00163         // The list of domains (just one)
    00164         const struct BMDomainEntry domains[] = {
    00165                 {"Dom1", domainVals}
    00166         };
    00167         // The version we're testing.
    00168         const unsigned int version = kSaveLoadVersion_Initial;
    00169
    00170         // The number of domains
    00171         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00172
    00173         // We're going to save/load into these variables.
    00174         BaseModelDomainArray savDomains( true ), refDomains( true ),
    00175                 loadDomains( true );
    00176
    00177         // Get our saving domain
    00178         loadDomainData( numDomains, domains, savDomains );
    00179         // Get our reference domain
    00180         loadDomainData( numDomains, domains, refDomains );
    00181         // Get our loaded domain (since we're only changing one value)
    00182         loadDomainData( numDomains, domains, loadDomains );
    00183
    00184         // Get a reference to the values we're dealing with.
    00185         BaseModelValue * savValue = (*savDomains[0])[0];
    00186         BaseModelValue * refValue = (*refDomains[0])[0];
    00187
    00188         // Make sure this is going to work
    00189         ensure_equals( "Two values created are equal.", *savValue, *refValue );
    00190
    00191         // Create our SaveLoad object for saving.
    00192         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00193
    00194         // Save the value.
    00195         savingSaveLoad.saveLoad( savValue, m_serializer );
    00196
    00197         // Make sure that we didn't mess up sValue.
    00198         ensure_equals( "Saving does not alter pointer",
    00199                 (*savDomains[0])[0], savValue );
    00200         // Make sure we didn't mess up the value.
    00201         ensure_equals( "Saving did not alter the data", *savValue, *refValue );
    00202
    00203         // Create our SaveLoad object for loading.
    00204         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00205         // Load a new value.
    00206         BaseModelValue * value( NULL );
    00207         loadingSaveLoad.saveLoad( value, m_serializer );
    00208
    00209         // Check that we were able to re-load our object.
    00210         // First, make sure it's not NULL.
    00211         ensure( "Value was loaded (not NULL)", value != NULL );
    00212
    00213         // Replace the domain value with this value.
    00214         delete (*loadDomains[0])[0];
    00215         (*loadDomains[0])[0] = value;
    00216         value->setDomain( loadDomains[0] );
    00217
    00218         // Make sure the values are the same.
    00219         ensure_equals( "Value is the same", *value, *refValue );
    00220 }
    00221
    00223
    00227 template<> template<> void t_SaveSerializationObject::test<2>()
    00228 {
    00229         // Our domain
    00230         const char * vals[] = {"BM_DomVal_0", "BM_DomVal_1", "BM_DomVal_2",
    00231                 "BM_DomVal_3" };
    00232         // The version we're testing.
    00233         const unsigned int version = kSaveLoadVersion_Initial;
    00234         // The number of values
    00235         const unsigned int numVals = sizeof( vals ) / sizeof( vals[0] );
    00236
    00237         // We're going to save/load into these variables.
    00238         auto_ptr<BaseModelValueArray>
    00239                 autoSavVals( new BaseModelValueArray( true ) ),
    00240                 refVals( new BaseModelValueArray( true ) );
    00241
    00242         // Load our values
    00243         for( unsigned int v = 0; v < numVals; ++v ) {
    00244                 autoSavVals->pushBack( new BaseModelValue( vals[v] ) );
    00245                 refVals->pushBack( new BaseModelValue( vals[v] ) );
    00246         }
    00247
    00248         // Get a reference to the values we're dealing with.
    00249         BaseModelValueArray * savVals = autoSavVals.get();
    00250
    00251         // These should be the same
    00252         ensure_equals( "Valss created are the same", *savVals, *refVals );
    00253
    00254         // Create our SaveLoad object for saving.
    00255         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00256
    00257         // Save the vals.
    00258         savingSaveLoad.saveLoad( savVals, m_serializer );
    00259
    00260         // Make sure that we didn't mess up savVals.
    00261         ensure_equals( "Saving does not alter pointer", savVals,
    00262                 autoSavVals.get() );
    00263
    00264         // Make sure we didn't mess up the vals.
    00265         ensure_equals( "Saving did not alter the data", *savVals, *refVals );
    00266
    00267         // Create our SaveLoad object for loading.
    00268         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00269         // Load a new value array
    00270         BaseModelValueArray * loadVals( NULL );
    00271         loadingSaveLoad.saveLoad( loadVals, m_serializer );
    00272
    00273         // auto-cleanup this loaded value.
    00274         auto_ptr<BaseModelValueArray> autoLoadVals( loadVals );
    00275
    00276         // Check that we were able to re-load our object.
    00277         // First, make sure it's not NULL.
    00278         ensure( "Domain was loaded (not NULL)", loadVals != NULL );
    00279
    00280         // The vals should match exactly.
    00281         ensure_equals( "values are the same", *loadVals, *refVals );
    00282 }
    00283
    00285
    00289 template<> template<> void t_SaveSerializationObject::test<3>()
    00290 {
    00291         // Our domain
    00292         const char * domainVals[] = {"BM_DomVal_0", "BM_DomVal_1", "BM_DomVal_2",
    00293                 "BM_DomVal_3", NULL};
    00294         const struct BMDomainEntry domains[] = {
    00295                 {"Dom1", domainVals}
    00296         };
    00297         // The version we're testing.
    00298         const unsigned int version = kSaveLoadVersion_Initial;
    00299
    00300         // The number of domains
    00301         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00302
    00303         // We're going to save/load into these variables.
    00304         BaseModelDomainArray savDomains( true ), refDomains( true ),
    00305                 loadDomains( true );
    00306
    00307         // Get our saving domain
    00308         loadDomainData( numDomains, domains, savDomains );
    00309         // Get our reference domain
    00310         loadDomainData( numDomains, domains, refDomains );
    00311
    00312         // Get a reference to the values we're dealing with.
    00313         BaseModelDomain * savDomain = savDomains[0];
    00314         BaseModelDomain * refDomain = refDomains[0];
    00315
    00316         // These should be the same
    00317         ensure_equals( "Domains created are the same", *savDomain, *refDomain );
    00318
    00319         // Create our SaveLoad object for saving.
    00320         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00321
    00322         // Save the domain.
    00323         savingSaveLoad.saveLoad( savDomain, m_serializer );
    00324
    00325         // Make sure that we didn't mess up sDomain.
    00326         ensure_equals( "Saving does not alter pointer", savDomain, savDomains[0] );
    00327
    00328         // Make sure we didn't mess up the domain.
    00329         ensure_equals( "Saving did not alter the data", *savDomain, *refDomain );
    00330
    00331         // Create our SaveLoad object for loading.
    00332         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00333         // Change the model, to be able to match the data.
    00334         BaseModel bModel( "TestModel" );
    00335         m_serializer.setModel( &bModel );
    00336         // Load a new domain.
    00337         BaseModelDomain * domain( NULL );
    00338         loadingSaveLoad.saveLoad( domain, m_serializer );
    00339
    00340         // Check that we were able to re-load our object.
    00341         // First, make sure it's not NULL.
    00342         ensure( "Domain was loaded (not NULL)", domain != NULL );
    00343
    00344         // The domain should match exactly (due to the way we construct
    00345         // them).
    00346         ensure_equals( "Domain is the same", *domain, *refDomain );
    00347 }
    00348
    00350
    00354 template<> template<> void t_SaveSerializationObject::test<4>()
    00355 {
    00356         // Our domain
    00357         const char * domainVals[] = {"BM_VarVal_0", "BM_VarVal_1", "BM_VarVal_2",
    00358                 "BM_VarVal_3", "BM_VarVal_4", NULL};
    00359         const struct BMDomainEntry domains[] = {
    00360                 {"FakeTestDomain", domainVals},
    00361                 {"TestDomain", domainVals}
    00362         };
    00363         enum Domain { FakeTestDomain = 0, TestDomain = 1 };
    00364
    00365         // The number of domains
    00366         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00367
    00368         // Our variable
    00369         const struct BMVariableEntry variables[] = {
    00370                 { "SomeVariable.Name", TestDomain, BaseModelVariable::State }
    00371         };
    00372
    00373         // The number of variables
    00374         const unsigned int numVars = sizeof( variables ) / sizeof( variables[0] );
    00375
    00376         // The version we're testing.
    00377         const unsigned int version = kSaveLoadVersion_Initial;
    00378
    00379         // Our domains
    00380         BaseModelDomainArray savDomains( true ), loadDomains( false );
    00381
    00382         // Our variables
    00383         BaseModelVariableArray savVariables( true ), refVariables( true );
    00384
    00385         // Create our domain data
    00386         loadDomainData( numDomains, domains, savDomains );
    00387         loadDomainData( numDomains, domains, loadDomains );
    00388         // Create our variable data
    00389         loadVariableData( numVars, variables, savDomains, savVariables );
    00390         loadVariableData( numVars, variables, savDomains, refVariables );
    00391
    00392         // Create our SaveLoad object for saving.
    00393         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00394
    00395         // Keep a copy of the original variable handy.
    00396         BaseModelVariable * refVar = refVariables[ 0 ];
    00397         BaseModelVariable * savVar = savVariables[ 0 ];
    00398
    00399         // Make sure that they're the same.
    00400         ensure_equals( "Data is initially the same", *savVar, *refVar );
    00401
    00402         // Save the variable.
    00403         savingSaveLoad.saveLoad( savVar, m_serializer );
    00404
    00405         // Make sure that we didn't mess up savVar.
    00406         ensure_equals( "Saving does not alter pointer", savVar, savVariables[0] );
    00407
    00408         // Make sure we didn't mess up the variable.
    00409         ensure_equals( "Saving did not alter the data", *savVar, *refVar );
    00410
    00411         // Create our SaveLoad object for loading.
    00412         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00413         // Change the model, to be able to match the data.
    00414         BaseModel bModel( "TestModel" );
    00415         m_serializer.setModel( &bModel );
    00416         // Add the load domains to the model.
    00417         bModel.addDomain( loadDomains[ 0 ] );
    00418         bModel.addDomain( loadDomains[ 1 ] );
    00419         // Load a new variable.
    00420         BaseModelVariable * loadVar( NULL );
    00421         loadingSaveLoad.saveLoad( loadVar, m_serializer );
    00422
    00423         // Check that we were able to re-load our object.
    00424         // First, make sure it's not NULL.
    00425         ensure( "Variable was loaded (not NULL)", loadVar != NULL );
    00426
    00427         // The variables should match.
    00428         ensure_equals( "Variable is the same", *loadVar, *refVar );
    00429 }
    00430
    00432
    00436 template<> template<> void t_SaveSerializationObject::test<5>()
    00437 {
    00438         // Our domain
    00439         const char * domainVals[] = {"BM_VarVal_0", "BM_VarVal_1", "BM_VarVal_2",
    00440                 "BM_VarVal_3", "BM_VarVal_4", NULL};
    00441         const struct BMDomainEntry domains[] = {
    00442                 {"TestDomain", domainVals}
    00443         };
    00444         enum Domain { TestDomain = 0 };
    00445
    00446         // The number of domains
    00447         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00448
    00449         // Our variable
    00450         const struct BMVariableEntry variables[] = {
    00451                 { "SomeVariable.Name", TestDomain, BaseModelVariable::State }
    00452         };
    00453
    00454         // The number of variables
    00455         const unsigned int numVars = sizeof( variables ) / sizeof( variables[0] );
    00456
    00457         // The version we're testing.
    00458         const unsigned int version = kSaveLoadVersion_Initial;
    00459
    00460         // The operator of our assignment.
    00461         BaseModelAssignment::OperatorType opType =
    00462                 BaseModelAssignment::Negative;
    00463
    00464         // Our domains
    00465         BaseModelDomainArray savDomains( true ), loadDomains( false );
    00466
    00467         // Our variables
    00468         BaseModelVariableArray savVariables( true ), loadVariables( false );
    00469
    00470         // Create our SaveLoad object for saving.
    00471         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00472
    00473         // Load our data
    00474         loadDomainData( numDomains, domains, savDomains );
    00475         loadDomainData( numDomains, domains, loadDomains );
    00476         loadVariableData( numVars, variables, savDomains, savVariables );
    00477         loadVariableData( numVars, variables, loadDomains, loadVariables );
    00478
    00479         // Create our assignments.
    00480         BaseModelVariable * savVar( savVariables[0] );
    00481         BaseModelVariable * loadVar( loadVariables[0] );
    00482         BaseModelValue * savVal( (*savVar->getDomain())[0] );
    00483         BaseModelValue * loadVal( (*loadVar->getDomain())[0] );
    00484         BaseModelAssignment * savAssign(
    00485                 new BaseModelAssignment( savVar, savVal, opType ) );
    00486         auto_ptr<BaseModelAssignment> autoSavAssign( savAssign );
    00487         auto_ptr<BaseModelAssignment> refAssign(
    00488                 new BaseModelAssignment( savVar, savVal, opType ) );
    00489
    00490         // Make sure that these are equal
    00491         ensure_equals( "Data is the same.", *savAssign, *refAssign );
    00492
    00493         // Save the assignment.
    00494         savingSaveLoad.saveLoad( savAssign, m_serializer );
    00495
    00496         // Make sure that we didn't mess up the pointer
    00497         ensure_equals( "Saving does not alter pointer", savAssign,
    00498                 autoSavAssign.get() );
    00499
    00500         // Make sure we didn't mess up the assignment.
    00501         ensure_equals( "Saving did not alter the data", *savAssign, *refAssign );
    00502
    00503         // Create our SaveLoad object for loading.
    00504         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00505         // Change the model, to be able to match the data.
    00506         BaseModel bModel( "TestModel" );
    00507         m_serializer.setModel( &bModel );
    00508         // Add the load domains to the model.
    00509         bModel.addDomain( loadDomains[ 0 ] );
    00510         bModel.addVariable( loadVariables[ 0 ] );
    00511         // Load a new assignment.
    00512         BaseModelAssignment * loadAssign( NULL );
    00513         loadingSaveLoad.saveLoad( loadAssign, m_serializer );
    00514
    00515         // Auto clean our loaded assignment
    00516         auto_ptr<BaseModelAssignment> autoLoadAssign( loadAssign );
    00517
    00518         // Check that we were able to re-load our object.
    00519         // First, make sure it's not NULL.
    00520         ensure( "Assignment was loaded (not NULL)", loadAssign != NULL );
    00521
    00522         // The assignments should match.  To make our life a bit easier,
    00523         // we'll construct an appropriate copy of what we wanted to load.
    00524         auto_ptr<BaseModelAssignment> loadRefAssign(
    00525                 new BaseModelAssignment( loadVar, loadVal, opType ) );
    00526         ensure_equals( "Assignment is the same", *loadAssign, *loadRefAssign );
    00527 }
    00528
    00530
    00534 template<> template<> void t_SaveSerializationObject::test<6>()
    00535 {
    00536         // Our domains
    00537         const char * domainVals1[] = {"BM_DomVal_0", "BM_DomVal_1", "BM_DomVal_2",
    00538                 "BM_DomVal_3", NULL};
    00539         const char * domainVals2[] = {"BM_Test_0", "BM_Test_1", "BM_Foo_2",
    00540                 "BM_Wow_3", "BM_No", NULL};
    00541         const struct BMDomainEntry domains[] = {
    00542                 {"Dom1", domainVals1},
    00543                 {"Dom2", domainVals2}
    00544         };
    00545         // The version we're testing.
    00546         const unsigned int version = kSaveLoadVersion_Initial;
    00547
    00548         // The number of domains
    00549         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00550
    00551         // We're going to save/load into these variables.
    00552         auto_ptr<BaseModelDomainArray>
    00553                 autoSavDomains( new BaseModelDomainArray( true ) ),
    00554                 refDomains( new BaseModelDomainArray( true ) );
    00555
    00556         // Get our domain data
    00557         loadDomainData( numDomains, domains, *autoSavDomains );
    00558         loadDomainData( numDomains, domains, *refDomains );
    00559
    00560         // Get a reference to our save data
    00561         BaseModelDomainArray * savDomains( autoSavDomains.get() );
    00562
    00563         // These should be the same
    00564         ensure_equals( "Domains created are the same", *savDomains, *refDomains );
    00565
    00566         // Create our SaveLoad object for saving.
    00567         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00568
    00569         // Save the domain.
    00570         savingSaveLoad.saveLoad( savDomains, m_serializer );
    00571
    00572         // Make sure that we didn't mess up sDomain.
    00573         ensure_equals( "Saving does not alter pointer", savDomains,
    00574                 autoSavDomains.get() );
    00575
    00576         // Make sure we didn't mess up the domain.
    00577         ensure_equals( "Saving did not alter the data", *savDomains, *refDomains );
    00578
    00579         // Create our SaveLoad object for loading.
    00580         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00581         // Change the model, to be able to match the data.
    00582         BaseModel bModel( "TestModel" );
    00583         m_serializer.setModel( &bModel );
    00584         // Load a new domain.
    00585         BaseModelDomainArray * loadDomains( NULL );
    00586         loadingSaveLoad.saveLoad( loadDomains, m_serializer );
    00587
    00588         // Auto-cleanup
    00589         auto_ptr<BaseModelDomainArray> autoLoadDomains( loadDomains );
    00590
    00591         // Check that we were able to re-load our object.
    00592         // First, make sure it's not NULL.
    00593         ensure( "Domain was loaded (not NULL)", loadDomains != NULL );
    00594
    00595         // The domain should match exactly (due to the way we construct
    00596         // them).
    00597         ensure_equals( "Domain is the same", *loadDomains, *refDomains );
    00598 }
    00599
    00601
    00605 template<> template<> void t_SaveSerializationObject::test<7>()
    00606 {
    00607         // Our domains
    00608         const char * domainVals1[] = {"BM_DomVal_0", "BM_DomVal_1", "BM_DomVal_2",
    00609                 "BM_DomVal_3", NULL};
    00610         const char * domainVals2[] = {"BM_Test_0", "BM_Test_1", "BM_Foo_2",
    00611                 "BM_Wow_3", "BM_No", NULL};
    00612         const struct BMDomainEntry domains[] = {
    00613                 {"Dom1", domainVals1},
    00614                 {"Dom2", domainVals2}
    00615         };
    00616         enum Domain {TestDomain, NextDomain};
    00617         // Our variables
    00618         const struct BMVariableEntry variables[] = {
    00619                 { "SomeVariable.Name", TestDomain, BaseModelVariable::State },
    00620                 { "VarA", NextDomain, BaseModelVariable::Control },
    00621                 { "VarQ", TestDomain, BaseModelVariable::State },
    00622                 { "VarC", NextDomain, BaseModelVariable::Control },
    00623                 { "VarM", NextDomain, BaseModelVariable::Dependent }
    00624         };
    00625
    00626         // The number of domains
    00627         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00628         // The number of variables
    00629         const unsigned int numVars = sizeof( variables ) / sizeof( variables[0] );
    00630
    00631         // The version we're testing.
    00632         const unsigned int version = kSaveLoadVersion_Initial;
    00633
    00634         // We're going to save/load our domain as well.
    00635         BaseModelDomainArray refDomains( true );
    00636         BaseModelDomainArray * pRefDomains( &refDomains );
    00637         // Get our domain data
    00638         loadDomainData( numDomains, domains, refDomains );
    00639
    00640         // We're going to save/load into these variables.
    00641         auto_ptr<BaseModelVariableArray>
    00642                 autoSavVariables( new BaseModelVariableArray( true ) ),
    00643                 refVariables( new BaseModelVariableArray( true ) );
    00644         // Our local save pointer
    00645         BaseModelVariableArray * savVariables( autoSavVariables.get() );
    00646
    00647         // Load our variable data
    00648         loadVariableData( numVars, variables, refDomains, *autoSavVariables );
    00649         loadVariableData( numVars, variables, refDomains, *refVariables );
    00650
    00651         // These should be the same
    00652         ensure_equals( "Variables created are the same",
    00653                 *savVariables, *refVariables );
    00654
    00655         // Create our SaveLoad object for saving.
    00656         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00657
    00658         // Save the domains & variables.
    00659         savingSaveLoad.saveLoad( pRefDomains, m_serializer );
    00660         savingSaveLoad.saveLoad( savVariables, m_serializer );
    00661
    00662         // Make sure that we didn't mess up sDomain.
    00663         ensure_equals( "Saving does not alter pointer", savVariables,
    00664                 autoSavVariables.get() );
    00665
    00666         // Make sure we didn't mess up the variable.
    00667         ensure_equals( "Saving did not alter the data",
    00668                 *savVariables, *refVariables );
    00669
    00670         // Create our SaveLoad object for loading.
    00671         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00672         // Change the model, to be able to match the data.
    00673         BaseModel bModel( "TestModel" );
    00674         m_serializer.setModel( &bModel );
    00675         // Load our domains and variables.
    00676         BaseModelDomainArray * loadDomains( NULL );
    00677         BaseModelVariableArray * loadVariables( NULL );
    00678         loadingSaveLoad.saveLoad( loadDomains, m_serializer );
    00679         loadingSaveLoad.saveLoad( loadVariables, m_serializer );
    00680
    00681         // Auto-cleanup
    00682         auto_ptr<BaseModelDomainArray> autoLoadDomains( loadDomains );
    00683         auto_ptr<BaseModelVariableArray> autoLoadVariables( loadVariables );
    00684
    00685         // Check that we were able to re-load our domain object.
    00686         // First, make sure it's not NULL.
    00687         ensure( "Domain was loaded (not NULL)", loadDomains != NULL );
    00688
    00689         // Create a new set of reference variables
    00690         BaseModelVariableArray loadRefVariables( true );
    00691         loadVariableData( numVars, variables, *loadDomains, loadRefVariables );
    00692
    00693         // Check that we were able to re-load our object.
    00694         // First, make sure it's not NULL.
    00695         ensure( "Variable was loaded (not NULL)", loadVariables != NULL );
    00696
    00697         // The variable list should match.
    00698         ensure_equals( "Domain is the same", *loadVariables, loadRefVariables );
    00699 }
    00700
    00702
    00706 template<> template<> void t_SaveSerializationObject::test<8>()
    00707 {
    00708         // Our domains
    00709         const char * domainVals1[] = {"BM_DomVal_0", "BM_DomVal_1", "BM_DomVal_2",
    00710                 "BM_DomVal_3", NULL};
    00711         const char * domainVals2[] = {"BM_Test_0", "BM_Test_1", "BM_Foo_2",
    00712                 "BM_Wow_3", "BM_No", NULL};
    00713         const struct BMDomainEntry domains[] = {
    00714                 {"Dom1", domainVals1},
    00715                 {"Dom2", domainVals2}
    00716         };
    00717         enum Domain {TestDomain, NextDomain};
    00718         // Our variables
    00719         const struct BMVariableEntry variables[] = {
    00720                 { "SomeVariable.Name", TestDomain, BaseModelVariable::State },
    00721                 { "VarA", NextDomain, BaseModelVariable::Control },
    00722                 { "VarQ", TestDomain, BaseModelVariable::State },
    00723                 { "VarC", NextDomain, BaseModelVariable::Control },
    00724                 { "VarM", NextDomain, BaseModelVariable::Dependent }
    00725         };
    00726
    00727         // The number of domains
    00728         const unsigned int numDomains = sizeof( domains ) / sizeof( domains[0] );
    00729         // The number of variables
    00730         const unsigned int numVars = sizeof( variables ) / sizeof( variables[0] );
    00731
    00732         // The version we're testing.
    00733         const unsigned int version = kSaveLoadVersion_Initial;
    00734
    00735         auto_ptr<BaseModel> autoSavModel( new BaseModel( "Test" ) ),
    00736                 refModel( new BaseModel( "Test" ) );
    00737         BaseModel * savModel( autoSavModel.get() );
    00738
    00739         // Get our model data
    00740         loadModelData( numDomains, domains, numVars, variables, *autoSavModel );
    00741         loadModelData( numDomains, domains, numVars, variables, *refModel );
    00742
    00743         // These should be the same
    00744         ensure_equals( "Models created are the same", *savModel, *refModel );
    00745
    00746         // Create our SaveLoad object for saving.
    00747         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave, version );
    00748         m_serializer.setModel( savModel );
    00749
    00750         // Save the model
    00751         savingSaveLoad.saveLoad( savModel, m_serializer );
    00752
    00753         // Make sure that we didn't mess up sDomain.
    00754         ensure_equals( "Saving does not alter pointer", savModel,
    00755                 autoSavModel.get() );
    00756
    00757         // Make sure we didn't mess up the variable.
    00758         ensure_equals( "Saving did not alter the data",
    00759                 *savModel, *refModel );
    00760
    00761         // Create our SaveLoad object for loading.
    00762         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00763         // Loading this model
    00764         BaseModel * loadModel( NULL );
    00765         m_serializer.setModel( loadModel );
    00766
    00767         // Load our model
    00768         loadingSaveLoad.saveLoad( loadModel, m_serializer );
    00769
    00770         // Auto-cleanup
    00771         auto_ptr<BaseModel> autoLoadModel( loadModel );
    00772
    00773         // Check that we were able to re-load our model object.
    00774         // First, make sure it's not NULL.
    00775         ensure( "Model was loaded (not NULL)", loadModel != NULL );
    00776
    00777         // The model should match.
    00778         ensure_equals( "Model is the same", *loadModel, *refModel );
    00779 }
    00780
    00782
    00783 } // tut
    00784
    00786