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

Go to the SVN repository for this file.

1 /* $Id: mockseqsrc2_unit_test.cpp 100942 2023-10-03 17:36:50Z ucko $
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: Christiam Camacho
27 *
28 * File Description:
29 * Unit tests for mock implementation(s) of the BlastSeqSrc that fails
30 * randomly
31 *
32 * ===========================================================================
33 */
34 #include <ncbi_pch.hpp>
35 
36 #define BLAST_SECONDARY_SOURCE 1
37 
38 #include <corelib/test_boost.hpp>
40 #include "seqsrc_mock.hpp"
41 #include "test_objmgr.hpp"
42 
44 using namespace blast;
45 
46 /// Initializes m_SeqSrc with a BlastSeqSrc that fails randomly
49 
52  }
53 
55  seqsrc.Reset(NULL);
56  }
57 };
58 
59 BOOST_FIXTURE_TEST_SUITE(mockseqsrc2, RandomlyFailingMockSeqSrcTestFixture)
60 
61 BOOST_AUTO_TEST_CASE(TestNumberOfSeqs) {
62  for (int i = 0; i < 10; i++) {
63  int rv = BlastSeqSrcGetNumSeqs(seqsrc);
64  BOOST_REQUIRE(rv == BLAST_SEQSRC_ERROR ||
66  }
67 }
68 
69 BOOST_AUTO_TEST_CASE(TestMaxSeqLen) {
70  for (int i = 0; i < 10; i++) {
71  int rv = BlastSeqSrcGetMaxSeqLen(seqsrc);
72  BOOST_REQUIRE(rv == BLAST_SEQSRC_ERROR ||
74  }
75 }
76 
77 BOOST_AUTO_TEST_CASE(TestAvgSeqLen) {
78  for (int i = 0; i < 10; i++) {
79  int rv = BlastSeqSrcGetAvgSeqLen(seqsrc);
80  BOOST_REQUIRE(rv == BLAST_SEQSRC_ERROR ||
82  }
83 }
84 
85 BOOST_AUTO_TEST_CASE(TestTotLen) {
86  for (int i = 0; i < 10; i++) {
87  Int8 rv = BlastSeqSrcGetTotLen(seqsrc);
88  BOOST_REQUIRE(rv == BLAST_SEQSRC_ERROR ||
90  }
91 }
92 
93 BOOST_AUTO_TEST_CASE(TestGetName) {
94  for (int i = 0; i < 10; i++) {
95  const char* str = BlastSeqSrcGetName(seqsrc);
96  if (str == NULL) {
97  BOOST_REQUIRE(true);
98  } else {
100  string actual(str);
101  BOOST_REQUIRE_EQUAL(expected, actual);
102  }
103  }
104 }
105 
106 BOOST_AUTO_TEST_CASE(TestIsProtein) {
107  Boolean rv = BlastSeqSrcGetIsProt(seqsrc);
108  BOOST_REQUIRE(rv == TRUE || rv == FALSE); // duh!
109 
110  // repeated calls to this function should return the same value
111  Boolean rv2 = BlastSeqSrcGetIsProt(seqsrc);
112  BOOST_REQUIRE(rv == rv2);
113 }
114 
115 BOOST_AUTO_TEST_CASE(TestGetSequenceValidOid)
116 {
117  BlastSeqSrcGetSeqArg seq_arg;
118  memset((void*) &seq_arg, 0, sizeof(seq_arg));
120 
121  bool successful_return = false;
122  for (int i = 0; i < 100; i++) {
123  int rv = BlastSeqSrcGetSequence(seqsrc, &seq_arg);
124  if (rv != BLAST_SEQSRC_SUCCESS) {
125  continue;
126  } else {
127  successful_return = true;
128  break;
129  }
130  }
131 
132  if (successful_return) {
133  BOOST_REQUIRE(seq_arg.seq);
135  seq_arg.seq->length);
136  }/* else {
137  // you'd had to be unlucky to get this
138  BOOST_REQUIRE(false);
139  } */
140 
141  BlastSeqSrcReleaseSequence(seqsrc, &seq_arg);
142 }
143 
144 BOOST_AUTO_TEST_CASE(TestGetSequenceAnyOid)
145 {
146  BlastSeqSrcGetSeqArg seq_arg;
147  memset((void*) &seq_arg, 0, sizeof(seq_arg));
148  seq_arg.oid = 66; // request some random oid
149 
150  for (int i = 0; i < 10; i++) {
151  int rv = BlastSeqSrcGetSequence(seqsrc, &seq_arg);
152  BOOST_REQUIRE(rv == BLAST_SEQSRC_ERROR);
153  }
154 
155  BlastSeqSrcReleaseSequence(seqsrc, &seq_arg);
156 }
157 
158 BOOST_AUTO_TEST_CASE(TestGetSequenceEOF)
159 {
160  BlastSeqSrcGetSeqArg seq_arg;
161  memset((void*) &seq_arg, 0, sizeof(seq_arg));
162  seq_arg.oid = BLAST_SEQSRC_EOF;
163 
164  for (int i = 0; i < 10; i++) {
165  int rv = BlastSeqSrcGetSequence(seqsrc, &seq_arg);
166  if (rv != BLAST_SEQSRC_ERROR) {
167  BOOST_REQUIRE_EQUAL(BLAST_SEQSRC_EOF, rv);
168  break;
169  }
170  }
171 
172  BlastSeqSrcReleaseSequence(seqsrc, &seq_arg);
173 }
174 
175 BOOST_AUTO_TEST_CASE(TestGetSeqLenValidOid)
176 {
178  int rv = BlastSeqSrcGetSeqLen(seqsrc, (void*) &oid);
179  BOOST_REQUIRE(CRandomlyFailMockBlastSeqSrc::kDefaultInt4 == rv);
180 }
181 
182 BOOST_AUTO_TEST_CASE(TestGetSeqLenAnyOid)
183 {
184  Int4 oid = 66; // request some random oid
185  int rv = BlastSeqSrcGetSeqLen(seqsrc, (void*) &oid);
186  BOOST_REQUIRE(rv == BLAST_SEQSRC_ERROR);
187 }
188 
189 // Note that initialization of a IMockBlastSeqSrc which fails randomly
190 // always succeeds
191 BOOST_AUTO_TEST_CASE(TestGetInitError) {
192  const char* rv = BlastSeqSrcGetInitError(seqsrc);
193  BOOST_REQUIRE(rv == NULL);
194 }
195 
196 BOOST_AUTO_TEST_CASE(TestIterationUseCase)
197 {
199  BlastSeqSrcGetSeqArg seq_arg;
200 
201  memset((void*) &seq_arg, 0, sizeof(seq_arg));
202 
203  while ( (seq_arg.oid = BlastSeqSrcIteratorNext(seqsrc, itr)) !=
205  }
206  BOOST_REQUIRE(true);
207 }
208 
Contains C++ wrapper classes to structures in algo/blast/core as well as some auxiliary functions to ...
#define BLAST_SEQSRC_ERROR
Error while retrieving sequence.
Definition: blast_seqsrc.h:291
Int4 BlastSeqSrcIteratorNext(const BlastSeqSrc *seq_src, BlastSeqSrcIterator *itr)
Increments the BlastSeqSrcIterator.
Definition: blast_seqsrc.c:425
Int4 BlastSeqSrcGetSeqLen(const BlastSeqSrc *seq_src, void *oid)
Retrieve sequence length (number of residues/bases)
Definition: blast_seqsrc.c:281
Boolean BlastSeqSrcGetIsProt(const BlastSeqSrc *seq_src)
Find if the Blast Sequence Source contains protein or nucleotide sequences.
Definition: blast_seqsrc.c:243
#define BLAST_SEQSRC_SUCCESS
Successful sequence retrieval.
Definition: blast_seqsrc.h:293
void BlastSeqSrcReleaseSequence(const BlastSeqSrc *seq_src, BlastSeqSrcGetSeqArg *getseq_arg)
Deallocate individual sequence.
Definition: blast_seqsrc.c:289
BlastSeqSrcIterator * BlastSeqSrcIteratorNew(void)
Allocate and initialize an iterator over a BlastSeqSrc with a default chunk size for MT-safe iteratio...
Definition: blast_seqsrc.c:380
Int4 BlastSeqSrcGetAvgSeqLen(const BlastSeqSrc *seq_src)
Get the average length of all sequences in the sequence source.
Definition: blast_seqsrc.c:211
char * BlastSeqSrcGetInitError(const BlastSeqSrc *seq_src)
Function to retrieve NULL terminated string containing the description of an initialization error or ...
Definition: blast_seqsrc.c:159
Int4 BlastSeqSrcGetNumSeqs(const BlastSeqSrc *seq_src)
Get the number of sequences contained in the sequence source.
Definition: blast_seqsrc.c:177
Int8 BlastSeqSrcGetTotLen(const BlastSeqSrc *seq_src)
Get the total length of all sequences in the sequence source.
Definition: blast_seqsrc.c:219
Int4 BlastSeqSrcGetMaxSeqLen(const BlastSeqSrc *seq_src)
Get the length of the longest sequence in the sequence source.
Definition: blast_seqsrc.c:193
const char * BlastSeqSrcGetName(const BlastSeqSrc *seq_src)
Get the Blast Sequence source name (e.g.
Definition: blast_seqsrc.c:235
Int2 BlastSeqSrcGetSequence(const BlastSeqSrc *seq_src, BlastSeqSrcGetSeqArg *getseq_arg)
Retrieve an individual sequence.
Definition: blast_seqsrc.c:271
#define BLAST_SEQSRC_EOF
No more sequences available.
Definition: blast_seqsrc.h:292
BOOST_AUTO_TEST_SUITE_END() static int s_GetSegmentFlags(const CBioseq &bioseq)
Wrapper class for BlastSeqSrcIterator .
Definition: blast_aux.hpp:351
Wrapper class for BlastSeqSrc .
Definition: blast_aux.hpp:350
static const char * kDefaultString
Definition: seqsrc_mock.hpp:98
static const Int4 kDefaultOid
static const Int8 kDefaultInt8
Definition: seqsrc_mock.hpp:97
static const Int4 kDefaultInt4
Definition: seqsrc_mock.hpp:96
static const char * expected[]
Definition: bcp.c:42
static const char * str(char *buf, int n)
Definition: stats.c:84
void Reset(BlastSeqSrc *p=NULL)
Definition: blast_aux.hpp:350
#define NULL
Definition: ncbistd.hpp:225
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
int i
BOOST_AUTO_TEST_CASE(TestNumberOfSeqs)
Uint1 Boolean
bool replacment for C
Definition: ncbi_std.h:94
#define TRUE
bool replacment for C indicating true.
Definition: ncbi_std.h:97
#define FALSE
bool replacment for C indicating false.
Definition: ncbi_std.h:101
BlastSeqSrc * MockBlastSeqSrcInit(EMockBlastSeqSrcMode mode)
Mock BlastSeqSrc initialization function.
Mock object which implements the BlastSeqSrc interface for testing purposes.
@ eMBSS_RandomlyFail
returns failure randomly on operations
Definition: seqsrc_mock.hpp:42
Int4 length
Length of sequence.
Definition: blast_def.h:246
Structure used as the second argument to functions satisfying the GetSeqBlkFnPtr signature,...
Definition: blast_seqsrc.h:257
Int4 oid
Oid in BLAST database, index in an array of sequences, etc [in].
Definition: blast_seqsrc.h:259
BLAST_SequenceBlk * seq
Sequence to return, if NULL, it should allocated by GetSeqBlkFnPtr (using BlastSeqBlkNew or BlastSetU...
Definition: blast_seqsrc.h:284
Initializes m_SeqSrc with a BlastSeqSrc that fails randomly.
Utility stuff for more convenient using of Boost.Test library.
Modified on Tue Apr 23 07:38:43 2024 by modify_doxy.py rev. 669887