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