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

Go to the SVN repository for this file.

1 /* $Id: unit_test_field.cpp 96072 2022-02-03 17:40:37Z vasilche $
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, NCBI
27  *
28  * File Description:
29  * Unit test for CUser_object and CUser_field
30  *
31  * ===========================================================================
32  */
33 
34 #include <ncbi_pch.hpp>
35 
37 
38 #include <corelib/ncbiapp.hpp>
39 #include <corelib/test_boost.hpp>
40 
41 #include <util/util_exception.hpp>
42 
43 #include <common/test_assert.h> /* This header must go last */
44 
47 
48 static string ToASNString(const CSerialObject& obj)
49 {
51  out << MSerial_AsnText << obj;
53 }
54 
55 
57 {
59  str << MSerial_Format(format) << obj;
60  CUser_object obj2;
61  str >> MSerial_Format(format) >> obj2;
62  return ToASNString(obj2);
63 }
64 
65 
66 void TestAllFormats(const CUser_object& obj, const char* asn_text)
67 {
68  BOOST_CHECK_EQUAL(ToASNString(obj), asn_text);
69  BOOST_CHECK_EQUAL(ToASNString(obj, eSerial_AsnText), asn_text);
70  BOOST_CHECK_EQUAL(ToASNString(obj, eSerial_AsnBinary), asn_text);
71  BOOST_CHECK_EQUAL(ToASNString(obj, eSerial_Xml), asn_text);
72 }
73 
74 
75 BOOST_AUTO_TEST_CASE(s_TestInt8_1)
76 {
78  obj->SetType().SetId(1);
79 
80  obj->ResetData();
81  obj->AddField("number", "123456789", CUser_object::eParse_Number);
82  TestAllFormats(*obj,
83  "User-object ::= {\n"
84  " type id 1,\n"
85  " data {\n"
86  " {\n"
87  " label str \"number\",\n"
88  " data int 123456789\n"
89  " }\n"
90  " }\n"
91  "}\n");
92 
93  obj->ResetData();
94  obj->AddField("number", "-123456789", CUser_object::eParse_Number);
95  TestAllFormats(*obj,
96  "User-object ::= {\n"
97  " type id 1,\n"
98  " data {\n"
99  " {\n"
100  " label str \"number\",\n"
101  " data int -123456789\n"
102  " }\n"
103  " }\n"
104  "}\n");
105 
106  obj->ResetData();
107  obj->AddField("double", 123456789.);
108  BOOST_CHECK_EQUAL(ToASNString(*obj),
109  "User-object ::= {\n"
110  " type id 1,\n"
111  " data {\n"
112  " {\n"
113  " label str \"double\",\n"
114  " data real { 123456789, 10, 0 }\n"
115  " }\n"
116  " }\n"
117  "}\n");
118 
119  obj->ResetData();
120  obj->AddField("double", -123456789.);
121  BOOST_CHECK_EQUAL(ToASNString(*obj),
122  "User-object ::= {\n"
123  " type id 1,\n"
124  " data {\n"
125  " {\n"
126  " label str \"double\",\n"
127  " data real { -123456789, 10, 0 }\n"
128  " }\n"
129  " }\n"
130  "}\n");
131 
132  obj->ResetData();
133  obj->AddField("Int8", Int8(123456789));
134  BOOST_CHECK_EQUAL(ToASNString(*obj),
135  "User-object ::= {\n"
136  " type id 1,\n"
137  " data {\n"
138  " {\n"
139  " label str \"Int8\",\n"
140  " data int 123456789\n"
141  " }\n"
142  " }\n"
143  "}\n");
144 
145  obj->ResetData();
146  obj->AddField("Int8", Int8(-123456789));
147  BOOST_CHECK_EQUAL(ToASNString(*obj),
148  "User-object ::= {\n"
149  " type id 1,\n"
150  " data {\n"
151  " {\n"
152  " label str \"Int8\",\n"
153  " data int -123456789\n"
154  " }\n"
155  " }\n"
156  "}\n");
157 }
158 
159 
160 BOOST_AUTO_TEST_CASE(s_TestInt8_2)
161 {
163  obj->SetType().SetId(1);
164 
165  obj->ResetData();
166  obj->AddField("number", "12345678901235", CUser_object::eParse_Number);
167  BOOST_CHECK_EQUAL(ToASNString(*obj),
168  "User-object ::= {\n"
169  " type id 1,\n"
170  " data {\n"
171  " {\n"
172  " label str \"number\",\n"
173  " data str \"12345678901235\"\n"
174  " }\n"
175  " }\n"
176  "}\n");
177 
178  obj->ResetData();
179  obj->AddField("number", "-12345678901235", CUser_object::eParse_Number);
180  BOOST_CHECK_EQUAL(ToASNString(*obj),
181  "User-object ::= {\n"
182  " type id 1,\n"
183  " data {\n"
184  " {\n"
185  " label str \"number\",\n"
186  " data str \"-12345678901235\"\n"
187  " }\n"
188  " }\n"
189  "}\n");
190 
191  obj->ResetData();
192  obj->AddField("double", 12345678901235.);
193  BOOST_CHECK_EQUAL(ToASNString(*obj),
194  "User-object ::= {\n"
195  " type id 1,\n"
196  " data {\n"
197  " {\n"
198  " label str \"double\",\n"
199  " data real { 12345678901235, 10, 0 }\n"
200  " }\n"
201  " }\n"
202  "}\n");
203 
204  obj->ResetData();
205  obj->AddField("double", -12345678901235.);
206  BOOST_CHECK_EQUAL(ToASNString(*obj),
207  "User-object ::= {\n"
208  " type id 1,\n"
209  " data {\n"
210  " {\n"
211  " label str \"double\",\n"
212  " data real { -12345678901235, 10, 0 }\n"
213  " }\n"
214  " }\n"
215  "}\n");
216 
217  obj->ResetData();
218  obj->AddField("Int8", Int8(12345678901235));
219  BOOST_CHECK_EQUAL(ToASNString(*obj),
220  "User-object ::= {\n"
221  " type id 1,\n"
222  " data {\n"
223  " {\n"
224  " label str \"Int8\",\n"
225  " data str \"12345678901235\"\n"
226  " }\n"
227  " }\n"
228  "}\n");
229 
230  obj->ResetData();
231  obj->AddField("Int8", Int8(-12345678901235));
232  BOOST_CHECK_EQUAL(ToASNString(*obj),
233  "User-object ::= {\n"
234  " type id 1,\n"
235  " data {\n"
236  " {\n"
237  " label str \"Int8\",\n"
238  " data str \"-12345678901235\"\n"
239  " }\n"
240  " }\n"
241  "}\n");
242 }
243 
244 
245 BOOST_AUTO_TEST_CASE(s_TestInt8_3)
246 {
248  obj->SetType().SetId(1);
249 
250  obj->ResetData();
251  obj->AddField("number", "123456789012345679", CUser_object::eParse_Number);
252  TestAllFormats(*obj,
253  "User-object ::= {\n"
254  " type id 1,\n"
255  " data {\n"
256  " {\n"
257  " label str \"number\",\n"
258  " data str \"123456789012345679\"\n"
259  " }\n"
260  " }\n"
261  "}\n");
262 
263  obj->ResetData();
264  obj->AddField("number", "-123456789012345679", CUser_object::eParse_Number);
265  TestAllFormats(*obj,
266  "User-object ::= {\n"
267  " type id 1,\n"
268  " data {\n"
269  " {\n"
270  " label str \"number\",\n"
271  " data str \"-123456789012345679\"\n"
272  " }\n"
273  " }\n"
274  "}\n");
275 
276  obj->ResetData();
277  obj->AddField("double", 123456789012345679.);
278  TestAllFormats(*obj,
279  "User-object ::= {\n"
280  " type id 1,\n"
281  " data {\n"
282  " {\n"
283  " label str \"double\",\n"
284  " data real { 123456789012346, 10, 3 }\n"
285  " }\n"
286  " }\n"
287  "}\n");
288 
289  obj->ResetData();
290  obj->AddField("double", -123456789012345679.);
291  TestAllFormats(*obj,
292  "User-object ::= {\n"
293  " type id 1,\n"
294  " data {\n"
295  " {\n"
296  " label str \"double\",\n"
297  " data real { -123456789012346, 10, 3 }\n"
298  " }\n"
299  " }\n"
300  "}\n");
301 
302  obj->ResetData();
303  obj->AddField("Int8", Int8(123456789012345679));
304  TestAllFormats(*obj,
305  "User-object ::= {\n"
306  " type id 1,\n"
307  " data {\n"
308  " {\n"
309  " label str \"Int8\",\n"
310  " data str \"123456789012345679\"\n"
311  " }\n"
312  " }\n"
313  "}\n");
314 
315  obj->ResetData();
316  obj->AddField("Int8", Int8(-123456789012345679));
317  TestAllFormats(*obj,
318  "User-object ::= {\n"
319  " type id 1,\n"
320  " data {\n"
321  " {\n"
322  " label str \"Int8\",\n"
323  " data str \"-123456789012345679\"\n"
324  " }\n"
325  " }\n"
326  "}\n");
327 }
328 
329 
331 {
333  obj->SetType().SetId(1);
334 
335  obj->ResetData();
336  obj->AddField("GI", GI_CONST(123456789));
337  BOOST_CHECK_EQUAL(ToASNString(*obj),
338  "User-object ::= {\n"
339  " type id 1,\n"
340  " data {\n"
341  " {\n"
342  " label str \"GI\",\n"
343  " data int 123456789\n"
344  " }\n"
345  " }\n"
346  "}\n");
347 
348  obj->ResetData();
349  obj->AddField("GI", GI_CONST(-123456789));
350  BOOST_CHECK_EQUAL(ToASNString(*obj),
351  "User-object ::= {\n"
352  " type id 1,\n"
353  " data {\n"
354  " {\n"
355  " label str \"GI\",\n"
356  " data int -123456789\n"
357  " }\n"
358  " }\n"
359  "}\n");
360 }
361 
362 
363 #ifdef NCBI_INT8_GI
364 BOOST_AUTO_TEST_CASE(s_TestGi_2)
365 {
367  obj->SetType().SetId(1);
368 
369  obj->ResetData();
370  obj->AddField("GI", GI_CONST(12345678901235));
371  BOOST_CHECK_EQUAL(ToASNString(*obj),
372  "User-object ::= {\n"
373  " type id 1,\n"
374  " data {\n"
375  " {\n"
376  " label str \"GI\",\n"
377  " data str \"12345678901235\"\n"
378  " }\n"
379  " }\n"
380  "}\n");
381 
382  obj->ResetData();
383  obj->AddField("GI", GI_CONST(-12345678901235));
384  BOOST_CHECK_EQUAL(ToASNString(*obj),
385  "User-object ::= {\n"
386  " type id 1,\n"
387  " data {\n"
388  " {\n"
389  " label str \"GI\",\n"
390  " data str \"-12345678901235\"\n"
391  " }\n"
392  " }\n"
393  "}\n");
394 }
395 
396 
397 BOOST_AUTO_TEST_CASE(s_TestGi_3)
398 {
400  obj->SetType().SetId(1);
401 
402  obj->ResetData();
403  obj->AddField("GI", GI_CONST(123456789012345679));
404  TestAllFormats(*obj,
405  "User-object ::= {\n"
406  " type id 1,\n"
407  " data {\n"
408  " {\n"
409  " label str \"GI\",\n"
410  " data str \"123456789012345679\"\n"
411  " }\n"
412  " }\n"
413  "}\n");
414 
415  obj->ResetData();
416  obj->AddField("GI", GI_CONST(-123456789012345679));
417  TestAllFormats(*obj,
418  "User-object ::= {\n"
419  " type id 1,\n"
420  " data {\n"
421  " {\n"
422  " label str \"GI\",\n"
423  " data str \"-123456789012345679\"\n"
424  " }\n"
425  " }\n"
426  "}\n");
427 }
428 #endif
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
Base class for all serializable objects.
Definition: serialbase.hpp:150
CUser_object & AddField(const string &label, const string &value, EParseField parse=eParse_String)
add a data field to the user object that holds a given value
@ eParse_Number
Parse a real or integer number, otherwise string.
Definition: User_object.hpp:62
std::ofstream out("events_result.xml")
main entry point for tests
static const char * str(char *buf, int n)
Definition: stats.c:84
#define GI_CONST(gi)
Definition: ncbimisc.hpp:1087
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
ESerialDataFormat
Data file format.
Definition: serialdef.hpp:71
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_Xml
XML.
Definition: serialdef.hpp:75
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
void SetType(TType &value)
Assign a value to Type data member.
void ResetData(void)
Reset Data data member.
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
static Format format
Definition: njn_ioutil.cpp:53
Utility stuff for more convenient using of Boost.Test library.
USING_SCOPE(objects)
BOOST_AUTO_TEST_CASE(s_TestInt8_1)
USING_NCBI_SCOPE
void TestAllFormats(const CUser_object &obj, const char *asn_text)
static string ToASNString(const CSerialObject &obj)
Modified on Fri Sep 20 14:57:20 2024 by modify_doxy.py rev. 669887