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

Go to the SVN repository for this file.

1 /* $Id: seqsrc_mock.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  */
29 
30 /** @file seqsrc_mock.cpp
31  * Mock object implementations of the BlastSeqSrc interface.
32  */
33 
34 #include <ncbi_pch.hpp>
35 
36 #define BLAST_SECONDARY_SOURCE 1
37 
38 #include "seqsrc_mock.hpp"
39 
42 #include "test_objmgr.hpp"
43 #include <blast_objmgr_priv.hpp>
44 
45 #include <corelib/ncbistr.hpp>
46 
52 
54  : m_RandGen(new ncbi::CRandom((CRandom::TValue)time(NULL)))
55 {
57 }
58 
60 {
61  delete m_RandGen;
62 }
63 
64 Int4
66 {
68  static_cast<Int4>(BLAST_SEQSRC_ERROR));
69 }
70 
71 Int4
73 {
75  static_cast<Int4>(BLAST_SEQSRC_ERROR));
76 }
77 
78 Int4
80 {
82  static_cast<Int4>(BLAST_SEQSRC_ERROR));
83 }
84 
85 Int8
87 {
89  static_cast<Int8>(BLAST_SEQSRC_ERROR));
90 }
91 
92 const char*
94 {
96 }
97 
98 Boolean
100 {
101  return m_IsProtein;
102 }
103 
104 Int4
106 {
108  static_cast<Int4>(BLAST_SEQSRC_EOF));
109 }
110 
111 Int4
113 {
114  return (oid == kDefaultOid ? kDefaultInt4 : BLAST_SEQSRC_ERROR);
115 }
116 
117 Int2
119 {
121  return BLAST_SEQSRC_ERROR;
122  } else {
123  if (seq_arg->oid == BLAST_SEQSRC_EOF) {
124  return BLAST_SEQSRC_EOF;
125  } else if (seq_arg->oid == kDefaultOid) {
126  try { x_PopulateBLAST_SequenceBlk(seq_arg); }
127  catch (const CException&) { return BLAST_SEQSRC_ERROR; }
128  return BLAST_SEQSRC_SUCCESS;
129  } else {
130  return BLAST_SEQSRC_ERROR;
131  }
132  }
133 }
134 
135 void
137 {
138  ASSERT(seq_arg);
139  seq_arg->seq = BlastSequenceBlkFree(seq_arg->seq);
140 }
141 
142 void
144  seq_arg)
145 {
146  std::string seqid_string(seq_arg->encoding == eBlastEncodingProtein ?
147  "gi|129295" : "gi|555");
148  Int4 sequence_length = kDefaultInt4;
149  pair<TSeqPos, TSeqPos> sequence_range(0, kDefaultInt4);
150 
151  objects::CSeq_id seqid(seqid_string);
152  unique_ptr<blast::SSeqLoc> sl
153  (CTestObjMgr::Instance().CreateSSeqLoc(seqid, sequence_range));
154 
155  blast::SBlastSequence seq =
156  blast::GetSequence(*sl->seqloc, seq_arg->encoding, sl->scope);
157 
158  // no exceptions in the code below
159  Int2 rv = BlastSeqBlkNew(&seq_arg->seq);
160  ASSERT(rv == 0);
161  rv = BlastSeqBlkSetSequence(seq_arg->seq, seq.data.release(),
162  sequence_length);
163  ASSERT(rv == 0);
164  (void)rv; /* to pacify compiler warning */
165 }
166 
167 extern "C" {
168 
169 static Int2
171 {
172  ASSERT(mock_handle);
173  ASSERT(arg);
174 
175  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
176  return impl->GetSequence(arg);
177 }
178 
179 static Int4
180 s_MockBlastSeqSrcGetSeqLen(void* mock_handle, void* oid)
181 {
182  ASSERT(mock_handle);
183  ASSERT(oid);
184 
185  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
186  Int4 ordinal_id = *(static_cast<Int4*>(oid));
187  return impl->GetSequenceLength(ordinal_id);
188 }
189 
190 static Int4
191 s_MockBlastSeqSrcGetNumSeqs(void* mock_handle, void*)
192 {
193  ASSERT(mock_handle);
194  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
195  return impl->GetNumSeqs();
196 }
197 
198 static Int4
199 s_MockBlastSeqSrcGetMaxSeqLen(void* mock_handle, void*)
200 {
201  ASSERT(mock_handle);
202  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
203  return impl->GetMaxSeqLen();
204 }
205 
206 static Int4
207 s_MockBlastSeqSrcGetAvgSeqLen(void* mock_handle, void*)
208 {
209  ASSERT(mock_handle);
210  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
211  return impl->GetAvgSeqLen();
212 }
213 
214 static Int8
215 s_MockBlastSeqSrcGetTotLen(void* mock_handle, void*)
216 {
217  ASSERT(mock_handle);
218  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
219  return impl->GetTotLen();
220 }
221 
222 static const char*
223 s_MockBlastSeqSrcGetSeqSrcName(void* mock_handle, void*)
224 {
225  ASSERT(mock_handle);
226  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
227  return impl->GetSeqSrcName();
228 }
229 
230 static Boolean
231 s_MockBlastSeqSrcGetIsProt(void* mock_handle, void*)
232 {
233  ASSERT(mock_handle);
234  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
235  return impl->GetIsProt();
236 }
237 
238 static void
240 {
241  ASSERT(mock_handle);
242  ASSERT(arg);
243  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
244  impl->ReleaseSequence(arg);
245 }
246 
247 static Int4
249 {
250  ASSERT(mock_handle);
251  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>(mock_handle);
252  return impl->IteratorNext(itr);
253 }
254 
255 // Destructor
257 {
258  if ( !seq_src ) {
259  return NULL;
260  }
261 
262  const BlastSeqSrc* seqsrc = static_cast<const BlastSeqSrc*>(seq_src);
263  IMockBlastSeqSrc* impl = static_cast<IMockBlastSeqSrc*>
265  delete impl;
266  return NULL;
267 }
268 
269 // Constructor
270 static BlastSeqSrc*
271 s_MockBlastSeqSrcNew(BlastSeqSrc* retval, void* args)
272 {
273  ASSERT(retval);
274 
275  EMockBlastSeqSrcMode mode = *(static_cast<EMockBlastSeqSrcMode*>(args));
277 
278  switch (mode) {
279  case eMBSS_AlwaysFail:
281  break;
282  case eMBSS_RandomlyFail:
284  break;
285  default:
286  {
287  std::string msg = "Invalid EMockBlastSeqSrcMode: " +
288  ncbi::NStr::IntToString(static_cast<int>(mode));
289  _BlastSeqSrcImpl_SetInitErrorStr(retval, strdup(msg.c_str()));
290  }
291  return retval;
292  }
293 
295  _BlastSeqSrcImpl_SetDataStructure (retval, (void*) impl);
307 
308  return retval;
309 }
310 
311 } // end extern "C"
312 
315 {
316  BlastSeqSrc* retval = NULL;
317  BlastSeqSrcNewInfo bssn_info;
318 
319  bssn_info.constructor = &s_MockBlastSeqSrcNew;
320  bssn_info.ctor_argument = static_cast<void*>(&mode);
321 
322  retval = BlastSeqSrcNew(&bssn_info);
323 
324  return retval;
325 }
Definitions which are dependant on the NCBI C++ Object Manager.
#define BLAST_SEQSRC_ERROR
Error while retrieving sequence.
Definition: blast_seqsrc.h:291
BlastSeqSrc * BlastSeqSrcNew(const BlastSeqSrcNewInfo *bssn_info)
Allocates memory for a BlastSeqSrc structure and then invokes the constructor function defined in its...
Definition: blast_seqsrc.c:90
#define BLAST_SEQSRC_SUCCESS
Successful sequence retrieval.
Definition: blast_seqsrc.h:293
#define BLAST_SEQSRC_EOF
No more sequences available.
Definition: blast_seqsrc.h:292
Definitions needed for implementing the BlastSeqSrc interface and low level details of the implementa...
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetReleaseSequence(BlastSeqSrc *var, ReleaseSeqBlkFnPtr arg)
Definition: blast_seqsrc.c:574
NCBI_XBLAST_EXPORT void * _BlastSeqSrcImpl_GetDataStructure(const BlastSeqSrc *var)
Definition: blast_seqsrc.c:555
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetSequence(BlastSeqSrc *var, GetSeqBlkFnPtr arg)
Definition: blast_seqsrc.c:572
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetTotLen(BlastSeqSrc *var, GetInt8FnPtr arg)
Definition: blast_seqsrc.c:563
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetSeqLen(BlastSeqSrc *var, GetInt4FnPtr arg)
Definition: blast_seqsrc.c:573
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetIsProt(BlastSeqSrc *var, GetBoolFnPtr arg)
Definition: blast_seqsrc.c:567
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetDataStructure(BlastSeqSrc *var, void *arg)
Definition: blast_seqsrc.c:555
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetAvgSeqLen(BlastSeqSrc *var, GetInt4FnPtr arg)
Definition: blast_seqsrc.c:562
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetIterNext(BlastSeqSrc *var, AdvanceIteratorFnPtr arg)
Definition: blast_seqsrc.c:576
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetInitErrorStr(BlastSeqSrc *var, char *arg)
Definition: blast_seqsrc.c:556
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetDeleteFnPtr(BlastSeqSrc *var, BlastSeqSrcDestructor arg)
Definition: blast_seqsrc.c:552
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetName(BlastSeqSrc *var, GetStrFnPtr arg)
Definition: blast_seqsrc.c:566
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetMaxSeqLen(BlastSeqSrc *var, GetInt4FnPtr arg)
Definition: blast_seqsrc.c:560
NCBI_XBLAST_EXPORT void _BlastSeqSrcImpl_SetGetNumSeqs(BlastSeqSrc *var, GetInt4FnPtr arg)
Definition: blast_seqsrc.c:558
Various auxiliary BLAST utility functions.
BLAST_SequenceBlk * BlastSequenceBlkFree(BLAST_SequenceBlk *seq_blk)
Deallocate memory for a sequence block.
Definition: blast_util.c:245
Int2 BlastSeqBlkSetSequence(BLAST_SequenceBlk *seq_blk, const Uint1 *sequence, Int4 seqlen)
Stores the sequence in the sequence block structure.
Definition: blast_util.c:147
Int2 BlastSeqBlkNew(BLAST_SequenceBlk **retval)
Allocates a new sequence block structure.
Definition: blast_util.c:133
CRandom::
Definition: random_gen.hpp:66
Mock BlastSeqSrc implementation which fails randomly.
Definition: seqsrc_mock.hpp:90
Int4 GetSequenceLength(Int4 oid)
static const char * kDefaultString
Definition: seqsrc_mock.hpp:98
Boolean m_IsProtein
Determine whether this mock BlastSeqSrc contains protein or nucleotide sequences.
Int4 IteratorNext(BlastSeqSrcIterator *itr)
static const Int4 kDefaultOid
static const char * kNullString
Definition: seqsrc_mock.hpp:99
T x_SelectRandomlyBetween(T success_value, T failure_value)
Auxiliary function to randomly select a value among the two possible options passed in as arguments.
static const Int8 kDefaultInt8
Definition: seqsrc_mock.hpp:97
void x_PopulateBLAST_SequenceBlk(BlastSeqSrcGetSeqArg *seq_arg)
Populate structure with some dummy data.
void ReleaseSequence(BlastSeqSrcGetSeqArg *seq_arg)
Int2 GetSequence(BlastSeqSrcGetSeqArg *seq_arg)
ncbi::CRandom * m_RandGen
The random number generator.
static const Int4 kDefaultInt4
Definition: seqsrc_mock.hpp:96
static CTestObjMgr & Instance()
Definition: test_objmgr.cpp:69
The NCBI C++ standard methods for dealing with std::string.
SBlastSequence GetSequence(const objects::CSeq_loc &sl, EBlastEncoding encoding, objects::CScope *scope, objects::ENa_strand strand=objects::eNa_strand_plus, ESentinelType sentinel=eSentinels, std::string *warnings=NULL)
Retrieves a sequence using the object manager.
@ eBlastEncodingProtein
NCBIstdaa.
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
int16_t Int2
2-byte (16-bit) signed integer
Definition: ncbitype.h:100
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
mdb_mode_t mode
Definition: lmdb++.h:38
Magic spell ;-) needed for some weird compilers... very empiric.
#define strdup
Definition: ncbi_ansi_ext.h:70
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
#define ASSERT
macro for assert.
Definition: ncbi_std.h:107
BlastSeqSrc * MockBlastSeqSrcInit(EMockBlastSeqSrcMode mode)
Mock BlastSeqSrc initialization function.
static const char * s_MockBlastSeqSrcGetSeqSrcName(void *mock_handle, void *)
static Int2 s_MockBlastSeqSrcGetSequence(void *mock_handle, BlastSeqSrcGetSeqArg *arg)
static Int4 s_MockBlastSeqSrcItrNext(void *mock_handle, BlastSeqSrcIterator *itr)
BlastSeqSrc * s_MockBlastSeqSrcFree(BlastSeqSrc *seq_src)
static Int4 s_MockBlastSeqSrcGetMaxSeqLen(void *mock_handle, void *)
static Int4 s_MockBlastSeqSrcGetNumSeqs(void *mock_handle, void *)
static Int4 s_MockBlastSeqSrcGetSeqLen(void *mock_handle, void *oid)
static void s_MockBlastSeqSrcReleaseSequence(void *mock_handle, BlastSeqSrcGetSeqArg *arg)
static Int8 s_MockBlastSeqSrcGetTotLen(void *mock_handle, void *)
static Int4 s_MockBlastSeqSrcGetAvgSeqLen(void *mock_handle, void *)
static Boolean s_MockBlastSeqSrcGetIsProt(void *mock_handle, void *)
static BlastSeqSrc * s_MockBlastSeqSrcNew(BlastSeqSrc *retval, void *args)
Mock object which implements the BlastSeqSrc interface for testing purposes.
EMockBlastSeqSrcMode
Definition: seqsrc_mock.hpp:40
@ eMBSS_RandomlyFail
returns failure randomly on operations
Definition: seqsrc_mock.hpp:42
@ eMBSS_AlwaysFail
returns failure on all operations
Definition: seqsrc_mock.hpp:41
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
EBlastEncoding encoding
Encoding of sequence, i.e.
Definition: blast_seqsrc.h:263
BLAST_SequenceBlk * seq
Sequence to return, if NULL, it should allocated by GetSeqBlkFnPtr (using BlastSeqBlkNew or BlastSetU...
Definition: blast_seqsrc.h:284
Complete type definition of Blast Sequence Source Iterator.
Complete type definition of the structure used to create a new BlastSeqSrc.
BlastSeqSrcConstructor constructor
User-defined function to initialize a BlastSeqSrc structure.
void * ctor_argument
Argument to the above function.
Complete type definition of Blast Sequence Source ADT.
Definition: blast_seqsrc.c:43
Mock BlastSeqSrc implementation which always fails.
Definition: seqsrc_mock.hpp:69
Abstract base class which defines a common interface for mock BlastSeqSrc objects to implement.
Definition: seqsrc_mock.hpp:53
Modified on Tue May 28 05:54:26 2024 by modify_doxy.py rev. 669887