1 /* $Id: test_fstream_pushback.cpp 573946 2018-11-04 18:24:56Z lavr $
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:  Anton Lavrentiev
27  *
28  * File Description:
29  *   Test CStreamUtils::Pushback() interface.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include "pbacktest.hpp"
35 #include <corelib/ncbidiag.hpp>
36 #include <corelib/test_mt.hpp>
37 #include <stdio.h>                 // remove()
38 
39 #include <common/test_assert.h>  // This header must go last
40 
41 
42 BEGIN_NCBI_SCOPE
43 
44 
45 class CTestApp : public CThreadedApp
46 {
47 public:
48     virtual bool Thread_Run(int idx);
49 
50 private:
51     static const char sm_Filename[];
52 };
53 
54 
55 const char CTestApp::sm_Filename[] = "test_fstream_pushback.data";
56 
57 
Thread_Run(int idx)58 bool CTestApp::Thread_Run(int idx)
59 {
60     string id = NStr::IntToString(idx);
61 
62     string filename = sm_Filename + string(1, '.') + id;
63 
64     PushDiagPostPrefix(("@" + id).c_str());
65 
66     CNcbiFstream fs(filename.c_str(),
67                     IOS_BASE::in    | IOS_BASE::out   |
68                     IOS_BASE::trunc | IOS_BASE::binary);
69 
70     int ret = TEST_StreamPushback(fs, true/*rewind*/);
71 
72     PopDiagPostPrefix();
73 
74     if (ret)
75         return false;
76 
77     remove(filename.c_str());
78     return true;
79 }
80 
81 
82 END_NCBI_SCOPE
83 
84 
main(int argc,char * argv[])85 int main(int argc, char* argv[])
86 {
87     USING_NCBI_SCOPE;
88 
89 #ifdef NCBI_OS_MSWIN
90     static char buf[4096];
91     cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
92     cerr.unsetf(ios_base::unitbuf);
93 #endif /*NCBI_OS_MSWIN*/
94 
95     SetDiagTrace(eDT_Enable);
96     SetDiagPostLevel(eDiag_Info);
97     SetDiagPostAllFlags((SetDiagPostAllFlags(eDPF_Default) & ~eDPF_All)
98                         | eDPF_DateTime    | eDPF_Severity
99                         | eDPF_OmitInfoSev | eDPF_ErrorID
100                         | eDPF_Prefix);
101     SetDiagTraceAllFlags(SetDiagPostAllFlags(eDPF_Default));
102 
103     s_NumThreads = 2; // default is small
104 
105     return CTestApp().AppMain(argc, argv);
106 }
107