Added CDSTEST_CFG envvar for stress test.
[libcds.git] / test / stress / framework / config.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13     list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <fstream>
32 #include <iostream>
33
34 #include <cds_test/stress_test.h>
35
36 namespace cds_test {
37
38     class config_file
39     {
40         std::map< std::string, config>  m_cfg;
41         config                          m_emptyCfg;
42
43     public:
44         void load( const char * fileName );
45
46         config const& operator[]( const std::string& testName ) const
47         {
48             auto it = m_cfg.find( testName );
49             if ( it != m_cfg.end() )
50                 return it->second;
51             return m_emptyCfg;
52         }
53     };
54
55     void config_file::load( const char * fileName )
56     {
57         std::ifstream s;
58         s.open( fileName );
59         if ( !s.is_open() ) {
60             std::cerr << "WARNING: Cannot open test cfg file " << fileName
61                 << "\n\tUse default settings"
62                 << std::endl;
63             return;
64         }
65
66         std::cout << "Using test config file: " << fileName << std::endl;
67
68         char buf[4096];
69
70         config * pMap = nullptr;
71         while ( !s.eof() ) {
72             s.getline( buf, sizeof( buf ) / sizeof( buf[0] ) );
73             char * pszStr = buf;
74             // trim left
75             while ( *pszStr != 0 && (*pszStr == ' ' || *pszStr == '\t') ) ++pszStr;
76             // trim right
77             char * pszEnd = strchr( pszStr, 0 );
78             if ( pszEnd == pszStr )    // empty srtring
79                 continue;
80             --pszEnd;
81             while ( pszEnd != pszStr && (*pszEnd == ' ' || *pszEnd == '\t' || *pszEnd == '\n' || *pszEnd == '\r') ) --pszEnd;
82
83             if ( pszStr == pszEnd )    // empty string
84                 continue;
85
86             pszEnd[1] = 0;
87
88             if ( *pszStr == '#' )    // comment
89                 continue;
90
91             if ( *pszStr == '[' && *pszEnd == ']' ) {    // chapter header
92                 *pszEnd = 0;
93                 pMap = &(m_cfg[pszStr + 1]);
94                 continue;
95             }
96
97             if ( !pMap )
98                 continue;
99
100             char * pszEq = strchr( pszStr, '=' );
101             if ( !pszEq )
102                 continue;
103             if ( pszEq == pszStr )
104                 continue;
105
106             pszEnd = pszEq;
107             while ( pszStr <= --pszEnd && (*pszEnd == ' ' || *pszEnd == '\t' || *pszEnd == '\n' || *pszEnd == '\r') );
108
109             if ( pszEnd <= pszStr )
110                 continue;
111             pszEnd[1] = 0;
112             pMap->m_Cfg[pszStr] = pszEq + 1;
113         }
114         s.close();
115     }
116
117     static config_file s_cfg;
118
119     void init_config( int argc, char **argv )
120     {
121 #if defined(_DEBUG) || !defined(NDEBUG)
122         char const * default_cfg_file = "./test-debug.conf";
123 #else
124         char const * default_cfg_file = "./test.conf";
125 #endif
126         char const * cfg_file = NULL;
127         for ( int i = 0; i < argc; ++i ) {
128             char * arg = argv[i];
129             char * eq = strchr( arg, '=' );
130             if ( eq ) {
131                 if ( strncmp( arg, "--cfg", eq - arg ) == 0 )
132                     cfg_file = eq + 1;
133             }
134         }
135
136         if ( !cfg_file ) {
137             // Get cfg filename from environment variable
138             cfg_file = getenv( "CDSTEST_CFG" );
139         }
140
141         if ( !cfg_file || *cfg_file == 0 )
142             cfg_file = default_cfg_file;
143
144         ::testing::Test::RecordProperty( "config_file", cfg_file );
145         s_cfg.load( cfg_file );
146     }
147
148     /*static*/ config const& stress_fixture::get_config( char const * slot )\r
149     {\r
150         return s_cfg[std::string( slot )];\r
151     }\r
152 \r
153     /*static*/ config const& stress_fixture::get_config( std::string const& slot )\r
154     {\r
155         return s_cfg[ slot ];\r
156     }\r
157
158 } // namespace cds_test