Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / protocols / tests / HTBP / Reactor_Tests / test_config.h
blobf65e014bbca7ff9aa1d5f4e6bc66ecba1952dabb
1 // -*- C++ -*-
4 // ============================================================================
5 /**
6 * @file test_config.h
8 * This file factors out common macros and other utilities used by the
9 * ACE automated regression tests.
11 * @author Prashant Jain <pjain@cs.wustl.edu>
12 * @author Tim Harrison <harrison@cs.wustl.edu>
13 * @author David Levine <levine@cs.wustl.edu>
15 // ============================================================================
17 #ifndef ACE_TEST_CONFIG_H
18 #define ACE_TEST_CONFIG_H
20 #include "ace/config-all.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 # pragma once
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #if defined (ACE_NLOGGING)
27 // ACE_NLOGGING must not be set if the tests are to produce any output.
28 #undef ACE_NLOGGING
29 #endif /* ACE_NLOGGING */
31 // This first #undef protects against command-line definitions.
32 #undef ACE_NDEBUG
33 #include "ace/streams.h"
34 #include "ace/Singleton.h"
35 #include "ace/Synch.h"
36 #include "ace/Log_Msg.h"
37 #include "ace/ACE.h"
39 // The second #undef protects against being reset in a config.h file.
40 #undef ACE_NDEBUG
42 #if defined (ACE_WIN32)
43 # define ACE_LOG_DIRECTORY ACE_TEXT ("log\\")
44 # define MAKE_PIPE_NAME(X) ACE_TEXT ("\\\\.\\pipe\\"#X)
45 #else
46 # define ACE_LOG_DIRECTORY ACE_TEXT ("log/")
47 # define MAKE_PIPE_NAME(X) ACE_TEXT (X)
48 #endif /* ACE_WIN32 */
50 #define ACE_LOG_FILE_EXT_NAME ACE_TEXT (".log")
52 const size_t ACE_MAX_CLIENTS = 30;
53 const size_t ACE_NS_MAX_ENTRIES = 1000;
54 const size_t ACE_DEFAULT_USECS = 1000;
55 const size_t ACE_MAX_TIMERS = 4;
56 const size_t ACE_MAX_DELAY = 10;
57 const size_t ACE_MAX_INTERVAL = 0;
58 const size_t ACE_MAX_ITERATIONS = 10;
59 const size_t ACE_MAX_PROCESSES = 10;
60 const size_t ACE_MAX_THREADS = 4;
62 #define ACE_START_TEST(NAME) \
63 const ACE_TCHAR *program = NAME; \
64 ACE_LOG_MSG->open (program, ACE_Log_Msg::OSTREAM | ACE_Log_Msg::VERBOSE_LITE); \
65 if (ace_file_stream::instance()->set_output (program) != 0) \
66 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("set_output failed")), -1); \
67 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Starting %s test at %D\n"), program))
69 #define ACE_END_TEST \
70 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Ending %s test at %D\n"), program)); \
71 ace_file_stream::instance()->close ()
73 #define ACE_CLOSE_TEST_LOG ace_file_stream::instance()->close ()
75 #if !defined (ACE_WIN32)
76 #define ACE_APPEND_LOG(NAME) \
77 const ACE_TCHAR *program = NAME; \
78 ACE_LOG_MSG->open (program, ACE_Log_Msg::OSTREAM | ACE_Log_Msg::VERBOSE_LITE); \
79 ace_file_stream::instance()->close (); \
80 if (ace_file_stream::instance()->set_output (program, 1) != 0) \
81 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("set_output failed")), -1); \
82 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Starting %s test at %D\n"), program));
83 #else /* ACE_WIN32 */
84 #define ACE_APPEND_LOG(NAME) \
85 const ACE_TCHAR *program = NAME; \
86 ACE_LOG_MSG->open (program, ACE_Log_Msg::OSTREAM | ACE_Log_Msg::VERBOSE_LITE); \
87 if (ace_file_stream::instance()->set_output (program, 1) != 0) \
88 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("set_output failed")), -1); \
89 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Starting %s test at %D\n"), program));
90 #endif /* ACE_WIN32 */
92 #define ACE_END_LOG \
93 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Ending %s test at %D\n\n"), program)); \
94 ACE_LOG_MSG->set_flags(ACE_Log_Msg::SILENT); \
95 ace_file_stream::instance()->close ();
97 #if defined (VXWORKS)
98 // This is the only way I could figure out to avoid an error
99 // about attempting to unlink a non-existent file.
100 #define ACE_INIT_LOG(NAME) \
101 ACE_TCHAR temp[MAXPATHLEN]; \
102 ACE_OS::sprintf (temp, ACE_TEXT ("%s%s%s"), \
103 ACE_LOG_DIRECTORY, \
104 ACE::basename (NAME, ACE_DIRECTORY_SEPARATOR_CHAR), \
105 ACE_LOG_FILE_EXT_NAME); \
106 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Deleting old log file %s (if any)\n\n"), temp)); \
107 int fd_init_log; \
108 if ((fd_init_log = ACE_OS::open (temp, \
109 O_WRONLY|O_CREAT, \
110 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) != ERROR) \
112 ACE_OS::close (fd_init_log); \
113 ACE_OS::unlink (temp); \
116 #if defined (ghs)
117 # // Rename main to ace_main for compatibility with run_tests.vxworks.
118 # undef ACE_MAIN
119 # define ACE_MAIN ace_main
120 #endif /* ghs */
121 #else /* ! VXWORKS */
122 #define ACE_INIT_LOG(NAME) \
123 ACE_TCHAR temp[MAXPATHLEN]; \
124 ACE_OS::sprintf (temp, ACE_TEXT ("%s%s%s"), \
125 ACE_LOG_DIRECTORY, \
126 ACE::basename (NAME, ACE_DIRECTORY_SEPARATOR_CHAR), \
127 ACE_LOG_FILE_EXT_NAME); \
128 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Deleting old log file %s (if any)\n\n"), temp)); \
129 ACE_OS::unlink (temp);
130 #endif /* ! VXWORKS */
132 #if defined (ACE_LACKS_IOSTREAM_TOTALLY)
133 #define OFSTREAM FILE
134 #else
135 #define OFSTREAM ofstream
136 #endif /* ACE_LACKS_IOSTREAM_TOTALLY */
138 class ACE_Test_Output
140 public:
141 ACE_Test_Output ();
142 ~ACE_Test_Output ();
143 int set_output (const ACE_TCHAR *filename, int append = 0);
144 OFSTREAM *output_file ();
145 void close ();
147 private:
148 OFSTREAM *output_file_;
151 inline ACE_Test_Output::ACE_Test_Output ()
152 : output_file_ (0)
154 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
155 this->output_file_ = new OFSTREAM;
156 #endif /* ACE_LACKS_IOSTREAM_TOTALLY */
159 inline ACE_Test_Output::~ACE_Test_Output ()
161 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
162 ACE_LOG_MSG->msg_ostream (&cerr);
163 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
165 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::OSTREAM);
166 ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
168 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
169 delete this->output_file_;
170 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
173 inline OFSTREAM *
174 ACE_Test_Output::output_file ()
176 return this->output_file_;
179 inline int
180 ACE_Test_Output::set_output (const ACE_TCHAR *filename, int append)
182 ACE_TCHAR temp[MAXPATHLEN];
183 // Ignore the error value since the directory may already exist.
184 const ACE_TCHAR *test_dir {};
186 test_dir = ACE_OS::getenv (ACE_TEXT ("ACE_TEST_DIR"));
188 if (test_dir == 0)
189 test_dir = ACE_TEXT ("");
191 ACE_OS::sprintf (temp,
192 ACE_TEXT ("%s%s%s%s"),
193 test_dir,
194 ACE_LOG_DIRECTORY,
195 ACE::basename (filename, ACE_DIRECTORY_SEPARATOR_CHAR),
196 ACE_LOG_FILE_EXT_NAME);
198 #if defined (VXWORKS)
199 // This is the only way I could figure out to avoid a console
200 // warning about opening an existing file (w/o O_CREAT), or
201 // attempting to unlink a non-existent one.
202 ACE_HANDLE fd = ACE_OS::open (temp,
203 O_WRONLY|O_CREAT,
204 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
205 if (fd != ERROR)
207 ACE_OS::close (fd);
208 ACE_OS::unlink (temp);
210 # else /* ! VXWORKS */
211 // This doesn't seem to work on VxWorks if the directory doesn't
212 // exist: it creates a plain file instead of a directory. If the
213 // directory does exist, it causes a weird console error message
214 // about "cat: input error on standard input: Is a directory". So,
215 // VxWorks users must create the directory manually.
216 ACE_OS::mkdir (ACE_LOG_DIRECTORY);
217 # endif /* ! VXWORKS */
219 # if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
220 this->output_file_->open (ACE_TEXT_ALWAYS_CHAR (temp),
221 ios::out | (append ? ios::app : ios::trunc));
222 if (this->output_file_->bad ())
223 return -1;
224 #else /* when ACE_LACKS_IOSTREAM_TOTALLY */
225 ACE_TCHAR *fmode = 0;
226 if (append)
227 fmode = ACE_TEXT ("a");
228 else
229 fmode = ACE_TEXT ("w");
230 this->output_file_ = ACE_OS::fopen (temp, fmode);
231 # endif /* ACE_LACKS_IOSTREAM_TOTALLY */
233 ACE_LOG_MSG->msg_ostream (this->output_file ());
234 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER );
235 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
237 return 0;
240 inline void
241 ACE_Test_Output::close ()
243 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
244 this->output_file_->flush ();
245 this->output_file_->close ();
246 #else
247 ACE_OS::fflush (this->output_file_);
248 ACE_OS::fclose (this->output_file_);
249 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
250 ACE_LOG_MSG->msg_ostream (0);
253 inline void
254 randomize (int array[], size_t size)
256 size_t i;
258 for (i = 0; i < size; i++)
259 array [i] = static_cast<int> (i);
261 // See with a fixed number so that we can produce "repeatable"
262 // random numbers.
263 ACE_OS::srand (0);
265 // Generate an array of random numbers from 0 .. size - 1.
267 for (i = 0; i < size; i++)
269 size_t index = ACE_OS::rand() % size--;
270 int temp = array [index];
271 array [index] = array [size];
272 array [size] = temp;
276 typedef ACE_Singleton<ACE_Test_Output, ACE_Null_Mutex> ace_file_stream;
278 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
279 template ACE_Singleton<ACE_Test_Output, ACE_Null_Mutex> *
280 ACE_Singleton<ACE_Test_Output, ACE_Null_Mutex>::singleton_;
281 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
283 #endif /* ACE_TEST_CONFIG_H */