// UniqueId_t.cxx #include "dataset_id/UniqueId.h" #include #include #include #include #include "dataset_util/XmlElement.h" using std::string; using std::cout; using std::endl; #if ( __GNUC__ != 2 ) using namespace std::rel_ops; #endif void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } int UniqueId_t() { msg("Check index size"); assert( sizeof(UniqueId::Index) == 4 ); msg("convert index"); { UniqueId::Index idx(12345678); { string sidx = index_to_string(idx); cout << "(" << sidx << ")" << endl; assert( sidx.size() == 8 ); } { string sidx = index_to_string(idx, '0'); cout << "(" << sidx << ")" << endl; assert( sidx.size() == 10 ); } { string sidx = index_to_string(idx, '-'); cout << "(" << sidx << ")" << endl; assert( sidx.size() == 10 ); } } msg("Default constructor."); { UniqueId id; cout << id << endl; assert( ! id.is_valid() ); } msg("Create and destroy."); { UniqueId id(11,222); cout << id << endl; assert( id.is_valid() ); assert( id.is_global() ); assert( id.collection() == 11 ); assert( id.entry() == 222 ); } msg("Convert to string and back"); { UniqueId id(123, 369); cout << id << endl; msg("... without pad"); string snopad = id.to_string(); cout << snopad << endl; cout << snopad.size() << endl; assert( snopad.size() == 7 ); { UniqueId id2(snopad); cout << id2 << endl; assert( id2 == id ); } msg("... with pad"); string spad = id.to_string(true); cout << spad << endl; cout << spad.size() << endl; assert( spad.size() == 21 ); { UniqueId id2(spad); cout << id2 << endl; assert( id2 == id ); } } msg("Convert to path and back."); { UniqueId id(0xAB012034, 0x12345); cout << id << endl; string path = id.to_path(); cout << path << endl; UniqueId id2(path); cout << id2 << endl; assert( id2 == id ); } msg("Ordering."); assert( UniqueId(10,10) < UniqueId(12,8) ); assert( UniqueId(10,10) < UniqueId(10,12) ); msg("Next."); { UniqueId bad = UniqueId(888,0).next(); assert( ! bad.is_valid() ); UniqueId good = UniqueId(888,123).next(); assert( good.is_valid() ); assert( good.entry() == 124 ); } msg("Equality"); { UniqueId id0(1,2); UniqueId id1(1,2); assert( id0 == id1 ); } msg("Other relops"); { UniqueId id0(1,1); UniqueId id1(1,1); UniqueId id2(2,1); assert( id1 < id2 ); assert( id0 == id1 ); assert( id1 != id2 ); assert( id1 <= id0 ); assert( id1 <= id2 ); assert( id2 > id1 ); assert( id2 >= id1 ); assert( id0 >= id1 ); } msg("Write XML"); { UniqueId id(123,456); cout << id << endl; const XmlElement* pele = id.xml(); cout << *pele << endl; msg("Read XML"); UniqueId id2(*pele); cout << id2 << endl; assert( id2 == id ); delete pele; } msg("All tests passed."); return 0; } #ifdef CTEST_MAIN int main() { return UniqueId_t(); } #endif