asniotest.cpp
来自「ncbi源码」· C++ 代码 · 共 672 行 · 第 1/2 页
CPP
672 行
m2.SetScale_factor() = 15; m2.SetX().push_back(21); m2.SetX().push_back(931); if (!m2.IsSetX() || !m2.CanGetX() || m2.GetX().size() != 2) ADD_ERR_RETURN("bad x state after SetX()"); m2.SetY().push_back(0); INFOMSG("trying to write incomplete Model-space-points - should fail"); if (WriteASNToFilesTxtBin(m2, &err)) ADD_ERR("shouldn't be able to write incomplete Model-space-points"); m2.SetZ(); // set but empty! if (!WriteASNToFilesTxtBin(m2, &err)) ADD_ERR("failed to write complete Model-space-points: " << err); // missing a mandatory list CModel_space_points m3; INFOMSG("trying to read incomplete Model-space-points - should fail"); if (ReadASNFromFile("badMSP.txt", &m3, false, &err)) ADD_ERR("shouldn't be able to read incomplete Model-space-points: " << err); // OPTIONAL list (coordinate-indices from Region-coordinates) CRegion_coordinates r1; if (!ReadASNFromFile("goodRC.txt", &r1, false, &err)) ADD_ERR_RETURN("failed to read goodRC.txt: " << err); if (r1.IsSetCoordinate_indices() || !r1.CanGetCoordinate_indices() || r1.GetCoordinate_indices().size() != 0) ADD_ERR("bad state for missing OPTIONAL list");END_TEST_FUNCTION// test OPTIONAL fields (using Global-id type)BEGIN_TEST_FUNCTION(OptionalField) // object read from file CGlobal_id g1; if (!ReadASNFromFile("goodGID.txt", &g1, false, &err)) ADD_ERR_RETURN("failed to read goodGID.txt: " << err); if (g1.IsSetRelease() || g1.CanGetRelease()) ADD_ERR("bad release state"); SHOULD_THROW_EXCEPTION(g1, GetRelease); if (!g1.IsSetVersion() || !g1.CanGetVersion() || g1.GetVersion() != 37) ADD_ERR("bad version state"); // new object CGlobal_id g2; if (g2.IsSetRelease() || g2.CanGetRelease()) ADD_ERR("bad release state in new object"); SHOULD_THROW_EXCEPTION(g2, GetRelease); g2.SetAccession("something"); g2.SetRelease("something else"); if (!g2.IsSetRelease() || !g2.CanGetRelease()) ADD_ERR("bad release state after SetRelease()"); if (!WriteASNToFilesTxtBin(g2, &err)) ADD_ERR("failed to write good Global-id: " << err);END_TEST_FUNCTION// check fields with DEFAULT valueBEGIN_TEST_FUNCTION(DefaultField) CCn3d_color c1; if (!ReadASNFromFile("goodColor.txt", &c1, false, &err)) ADD_ERR_RETURN("failed to read goodColor.txt: " << err); if (!c1.IsSetScale_factor() || !c1.CanGetScale_factor() || c1.GetScale_factor() != 17) ADD_ERR("bad scale-factor access"); if (c1.IsSetAlpha() || !c1.CanGetAlpha() || c1.GetAlpha() != 255) ADD_ERR("bad alpha access"); CCn3d_color c2; if (c2.IsSetAlpha() || !c2.CanGetAlpha() || c2.GetAlpha() != 255) ADD_ERR("bad alpha state after construction"); c2.SetAlpha(37); if (!c2.IsSetAlpha() || !c2.CanGetAlpha() || c2.GetAlpha() != 37) ADD_ERR("bad alpha state after SetAlpha()"); c2.SetRed(1); c2.SetGreen(2); c2.SetBlue(3); if (!WriteASNToFilesTxtBin(c2, &err)) ADD_ERR("failed to write good Cn3d-color: " << err);END_TEST_FUNCTION// check problem with zero Real dataBEGIN_TEST_FUNCTION(ZeroReal) CUser_object originalUO, newUO; if(!ReadASNFromFile("userObject.txt", &originalUO, false, &err)) ADD_ERR_RETURN("failed to read userObject.txt: " << err); if (!WriteASNToFilesTxtBin(originalUO, &err)) ADD_ERR("failed to write original User-object: " << err); // copy via streams CNcbiStrstream asnIOstream; ncbi::CObjectOStreamAsnBinary outObject(asnIOstream); outObject << originalUO; ncbi::CObjectIStreamAsnBinary inObject(asnIOstream); inObject >> newUO; if (!WriteASNToFilesTxtBin(newUO, &err)) ADD_ERR("failed to write new User-object: " << err);END_TEST_FUNCTIONBEGIN_TEST_FUNCTION(FullBlobs) CNcbiIfstream gilist (CDirEntry::MakePath(pathToFiles, "representativeGIs.txt").c_str()); string line; CID1Client id1; CRandom rng(CurrentTime().GetTimeT()); id1.SetAllowDeadEntries(true); while (NcbiGetlineEOL(gilist, line)) { if (line.empty() || !isdigit(line[0]) ) { continue; } int gi = NStr::StringToInt(line, 10, NStr::eCheck_Skip); if (gi <= 0) { continue; } INFOMSG("Trying GI " << gi); CRef<CSeq_entry> se; try { CID1server_maxcomplex req; req.SetGi(gi); req.SetMaxplex(eEntry_complexities_entry); se = id1.AskGetsefromgi(req); } catch (exception& e) { ADD_ERR("failed to retrieve blob for GI " << gi << ": " << e.what()); continue; } ESerialDataFormat sdf = eSerial_None; string sdf_name; switch (rng.GetRand(0, 2)) { case 0: sdf = eSerial_AsnText; sdf_name = "text ASN.1"; break; case 1: sdf = eSerial_AsnBinary; sdf_name = "binary ASN.1"; break; case 2: sdf = eSerial_Xml; sdf_name = "XML"; break; } CConn_MemoryStream stream; try { auto_ptr<CObjectOStream> out(CObjectOStream::Open(sdf, stream)); *out << *se; } catch (exception& e) { ADD_ERR("failed to generate " << sdf_name << " for GI " << gi << ": " << e.what()); continue; } CSeq_entry se2; try { auto_ptr<CObjectIStream> in(CObjectIStream::Open(sdf, stream)); *in >> se2; } catch (exception& e) { ADD_ERR("failed to parse " << sdf_name << " for GI " << gi << ": " << e.what()); continue; } if (!SerialEquals(*se, se2)) { ADD_ERR("failed equality after " << sdf_name << " round trip for GI " << gi); } }END_TEST_FUNCTION// test to make sure that a value of -1 converted to an unsigned int will still read/writeBEGIN_TEST_FUNCTION(UnsignedInt) int signedInt = -1; CRef < CSeq_loc > seqloc(new CSeq_loc()); seqloc->SetInt().SetFrom(signedInt); seqloc->SetInt().SetTo(signedInt); seqloc->SetInt().SetId().SetGi(0); if (!WriteASNToFile(*seqloc, false, &err)) ADD_ERR_RETURN("output of seqloc with '-1' failed: " << err); // load latest test output file CNcbiOstrstream oss; oss << "test_" << filesCreated.size() << ".txt"; string filename = CNcbiOstrstreamToString(oss); CRef < CSeq_loc > seqloc2(new CSeq_loc()); if (!ReadASNFromFile(filename.c_str(), seqloc2.GetPointer(), false, &err)) ADD_ERR_RETURN("input of seqloc with '-1' failed: " << err); if (!seqloc2->Equals(*seqloc)) ADD_ERR("seqloc with '-1' Equals test failed"); CRef < CSeq_loc > seqloc3(new CSeq_loc()); seqloc3->Assign(*seqloc); if (!WriteASNToFile(*seqloc3, false, &err)) ADD_ERR("output of Assign'ed seqloc with '-1' failed: " << err); if (((int) seqloc3->GetInt().GetFrom()) != -1) ADD_ERR("seqloc post-Assign value test failed"); CRef < CSeq_loc > seqloc4(new CSeq_loc()); if (!ReadASNFromFile("seqLocTest.txt", seqloc4.GetPointer(), false, &err)) ADD_ERR_RETURN("reading seqLocTest.txt failed: " << err); if (seqloc4->GetInt().GetFrom() != ((unsigned int) 4294967295)) ADD_ERR("seqloc unsigned int value test failed");END_TEST_FUNCTION// to call test functions, counting errors#define RUN_TEST(func) \ do { \ int errors = func(); \ nErrors += errors; \ if (errors) \ ERRORMSG(#func " test failed"); \ } while (0)int ASNIOTestApp::Run(void){ if (GetEnvironment().Get("ASNIOTEST_FILES").size() > 0) { pathToFiles = GetEnvironment().Get("ASNIOTEST_FILES"); } else if (CDirEntry("data/pdbSeqId.txt").Exists()) { pathToFiles = "data"; } INFOMSG("Looking for sample files in " << pathToFiles); int nErrors = 0; try { INFOMSG("Running tests..."); // individual tests RUN_TEST(BasicFileIO); RUN_TEST(AssignAndOutput); RUN_TEST(MandatoryField); RUN_TEST(ListField); RUN_TEST(OptionalField); RUN_TEST(DefaultField); RUN_TEST(ZeroReal); RUN_TEST(FullBlobs); RUN_TEST(UnsignedInt); } catch (exception& e) { ERRORMSG("uncaught exception: " << e.what()); nErrors++; } while (filesCreated.size() > 0) { CFile file(filesCreated.front()); if (!file.Remove()) { ERRORMSG("Couldn't remove file " << filesCreated.front()); nErrors++; } filesCreated.pop_front(); } if (nErrors == 0) INFOMSG("No errors encountered, all tests succeeded!"); else ERRORMSG(nErrors << " error" << ((nErrors == 1) ? "" : "s") << " encountered");#ifdef _DEBUG INFOMSG("(Debug mode build, " << __DATE__ << ")");#else INFOMSG("(Release mode build, " << __DATE__ << ")");#endif return nErrors;}END_NCBI_SCOPEUSING_NCBI_SCOPE;int main(int argc, const char* argv[]){ // diagnostic streams setup SetDiagStream(&NcbiCout); // send all diagnostic messages to cout SetDiagPostLevel(eDiag_Info); // show all messages // turn on C++ object verification CSerialObject::SetVerifyDataGlobal(eSerialVerifyData_Always); CObjectOStream::SetVerifyDataGlobal(eSerialVerifyData_Always); ASNIOTestApp app; return app.AppMain(argc, argv, NULL, eDS_Default, NULL); // don't use config file}/** ---------------------------------------------------------------------------* $Log: asniotest.cpp,v $* Revision 1000.2 2004/06/01 19:41:07 gouriano* PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.15** Revision 1.15 2004/05/21 21:42:51 gorelenk* Added PCH ncbi_pch.hpp** Revision 1.14 2004/03/09 21:19:53 ucko* Only warn about overwriting temporary files, since the previous run* may have crashed and left them behind.** Revision 1.13 2004/02/12 18:33:29 thiessen* add Assign test to UnsignedInt test** Revision 1.12 2004/02/12 17:59:58 thiessen* remove one gcc warning** Revision 1.11 2004/02/12 17:55:12 thiessen* add UnsignedInt test** Revision 1.10 2004/02/02 22:12:31 ucko* Test retrieval and round-trip conversion of a variety of representative blobs.** Revision 1.9 2004/01/23 16:29:52 ucko* Try to find test files in data/.** Revision 1.8 2004/01/15 14:35:13 thiessen* add date to output** Revision 1.7 2004/01/12 22:43:08 thiessen* add ZeroReal test** Revision 1.6 2003/12/11 17:52:10 thiessen* add Optional and Default field tests** Revision 1.5 2003/12/11 15:21:02 thiessen* add mandatory and list tests** Revision 1.4 2003/11/25 20:53:02 ucko* Make template-accessed variables non-static to fix compilation on WorkShop.** Revision 1.3 2003/11/25 14:36:16 thiessen* restructure macros** Revision 1.2 2003/11/21 15:26:12 thiessen* add Assign test** Revision 1.1 2003/11/21 14:48:51 thiessen* initial checkin**/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?