NCBI C++ ToolKit
unit_test_obj_sniff.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: unit_test_obj_sniff.cpp 75232 2016-10-31 14:47:01Z gouriano $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * Sample unit tests file for class CObjectsSniffer.
30 *
31 * ===========================================================================
32 */
33 
34 #include <ncbi_pch.hpp>
35 
36 #include <corelib/ncbi_system.hpp>
37 
38 // This macro should be defined before inclusion of test_boost.hpp in all
39 // "*.cpp" files inside executable except one. It is like function main() for
40 // non-Boost.Test executables is defined only in one *.cpp file - other files
41 // should not include it. If NCBI_BOOST_NO_AUTO_TEST_MAIN will not be defined
42 // then test_boost.hpp will define such "main()" function for tests.
43 //
44 // Usually if your unit tests contain only one *.cpp file you should not
45 // care about this macro at all.
46 //
47 //#define NCBI_BOOST_NO_AUTO_TEST_MAIN
48 
49 
50 // This header must be included before all Boost.Test headers if there are any
51 #include <corelib/test_boost.hpp>
52 
53 #include <serial/objistr.hpp>
55 
56 #include <objects/seq/Bioseq.hpp>
60 
63 
64 BOOST_AUTO_TEST_CASE(Test_Text_Object1)
65 {
66  string file_name = "test_data/object1.asn";
67  ifstream file_in(file_name.c_str());
68  BOOST_REQUIRE(file_in);
70  BOOST_REQUIRE(!in->EndOfData());
71  BOOST_REQUIRE_EQUAL(in->GetStreamPos(), NcbiInt8ToStreampos(0));
72  objects::CObjectsSniffer sniff;
73  sniff.AddCandidate(CObject_id::GetTypeInfo());
74  sniff.AddCandidate(CSeq_id::GetTypeInfo());
75  sniff.AddCandidate(CBioseq::GetTypeInfo());
76  sniff.AddCandidate(CSeq_entry::GetTypeInfo());
77  sniff.SetReportDataErrors();
78  sniff.Probe(*in);
79  NcbiCout << "Found " << sniff.GetTopLevelMap().size()
80  << " object(s) in " << file_name << NcbiEndl;
81  BOOST_REQUIRE(in->EndOfData());
82  BOOST_REQUIRE(in->GetStreamPos() != NcbiInt8ToStreampos(0));
83  BOOST_REQUIRE_EQUAL(sniff.GetTopLevelMap().size(), 5u);
84 }
85 
86 
87 BOOST_AUTO_TEST_CASE(Test_Bin_Object1)
88 {
89  string file_name = "test_data/object1.asb";
90  ifstream file_in(file_name.c_str(), IOS_BASE::binary);
91  BOOST_REQUIRE(file_in);
93  BOOST_REQUIRE(!in->EndOfData());
94  BOOST_REQUIRE_EQUAL(in->GetStreamPos(), NcbiInt8ToStreampos(0));
95  objects::CObjectsSniffer sniff;
96  sniff.AddCandidate(CObject_id::GetTypeInfo());
97  sniff.AddCandidate(CSeq_id::GetTypeInfo());
98  sniff.AddCandidate(CBioseq::GetTypeInfo());
99  sniff.AddCandidate(CSeq_entry::GetTypeInfo());
100  sniff.Probe(*in);
101  NcbiCout << "Found " << sniff.GetTopLevelMap().size()
102  << " object(s) in " << file_name << NcbiEndl;
103  BOOST_REQUIRE(in->EndOfData());
104  BOOST_REQUIRE(in->GetStreamPos() != NcbiInt8ToStreampos(0));
105  BOOST_REQUIRE_EQUAL(sniff.GetTopLevelMap().size(), 4u);
106 }
107 
108 
109 BOOST_AUTO_TEST_CASE(Test_Text_Object1_Fail)
110 {
111  string file_name = "test_data/object1.asn";
112  ifstream file_in(file_name.c_str());
113  BOOST_REQUIRE(file_in);
115  BOOST_REQUIRE(!in->EndOfData());
116  BOOST_REQUIRE_EQUAL(in->GetStreamPos(), NcbiInt8ToStreampos(0));
117  objects::CObjectsSniffer sniff;
118  sniff.AddCandidate(CObject_id::GetTypeInfo());
119  sniff.AddCandidate(CBioseq::GetTypeInfo());
120  sniff.AddCandidate(CSeq_entry::GetTypeInfo());
121  sniff.Probe(*in);
122  NcbiCout << "Found " << sniff.GetTopLevelMap().size()
123  << " object(s) in " << file_name << NcbiEndl;
124  BOOST_REQUIRE(!in->EndOfData());
125  BOOST_REQUIRE(in->GetStreamPos() != NcbiInt8ToStreampos(0));
126  BOOST_REQUIRE_EQUAL(sniff.GetTopLevelMap().size(), 1u);
127 }
128 
129 
130 BOOST_AUTO_TEST_CASE(Test_Bin_Object1_Fail)
131 {
132  string file_name = "test_data/object1.asb";
133  ifstream file_in(file_name.c_str(), IOS_BASE::binary);
134  BOOST_REQUIRE(file_in);
136  BOOST_REQUIRE(!in->EndOfData());
137  BOOST_REQUIRE_EQUAL(in->GetStreamPos(), NcbiInt8ToStreampos(0));
138  objects::CObjectsSniffer sniff;
139  sniff.AddCandidate(CObject_id::GetTypeInfo());
140  sniff.AddCandidate(CBioseq::GetTypeInfo());
141  sniff.AddCandidate(CSeq_entry::GetTypeInfo());
142  sniff.Probe(*in);
143  NcbiCout << "Found " << sniff.GetTopLevelMap().size()
144  << " object(s) in " << file_name << NcbiEndl;
145  BOOST_REQUIRE(!in->EndOfData());
146  BOOST_REQUIRE(in->GetStreamPos() != NcbiInt8ToStreampos(0));
147  BOOST_REQUIRE_EQUAL(sniff.GetTopLevelMap().size(), 1u);
148 }
149 
150 
151 BOOST_AUTO_TEST_CASE(Test_PipeText_Object1)
152 {
153  string file_name = "test_data/object1.asn";
154  ifstream file_in(file_name.c_str());
155  BOOST_REQUIRE(file_in);
157  BOOST_REQUIRE(!in->EndOfData());
158  BOOST_REQUIRE_EQUAL(in->GetStreamPos(), NcbiInt8ToStreampos(0));
159  objects::CObjectsSniffer sniff;
160  sniff.AddCandidate(CObject_id::GetTypeInfo());
161  sniff.AddCandidate(CSeq_id::GetTypeInfo());
162  sniff.AddCandidate(CBioseq::GetTypeInfo());
163  sniff.AddCandidate(CSeq_entry::GetTypeInfo());
164  sniff.Probe(*in);
165  NcbiCout << "Found " << sniff.GetTopLevelMap().size()
166  << " object(s) in " << file_name << NcbiEndl;
167  BOOST_REQUIRE(in->EndOfData());
168  BOOST_REQUIRE(in->GetStreamPos() != NcbiInt8ToStreampos(0));
169  BOOST_REQUIRE_EQUAL(sniff.GetTopLevelMap().size(), 5u);
170 }
const char * file_name[]
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
#define NcbiEndl
Definition: ncbistre.hpp:548
#define NcbiCout
Definition: ncbistre.hpp:543
NCBI_NS_STD::char_traits< char >::pos_type NcbiInt8ToStreampos(Int8 pos)
Convert plain numeric stream position (offset) into stream position usable with STL stream library.
Definition: ncbistre.hpp:782
std::istream & in(std::istream &in_, double &x_)
Utility stuff for more convenient using of Boost.Test library.
USING_SCOPE(objects)
BOOST_AUTO_TEST_CASE(Test_Text_Object1)
USING_NCBI_SCOPE
Modified on Fri Sep 20 14:57:52 2024 by modify_doxy.py rev. 669887