iutest  1.17.1.0
iutest_port.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_PORT_HPP_7893F685_A1A9_477A_82E8_BF06237697FF_
16 #define INCG_IRIS_IUTEST_PORT_HPP_7893F685_A1A9_477A_82E8_BF06237697FF_
17 
18 //======================================================================
19 // include
20 #if defined(__MWERKS__)
21 # define _MSL_USING_NAMESPACE
22 #endif
23 
24 #include "iutest_internal_defs.hpp"
25 
26 #if defined(IUTEST_OS_LINUX) || defined(IUTEST_OS_CYGWIN) || defined(IUTEST_OS_MAC) || defined(IUTEST_OS_FREEBSD)
27 # include <unistd.h>
28 # include <locale.h>
29 #endif
30 
31 #if IUTEST_HAS_FILE_STAT
32 # include <sys/stat.h>
33 #endif
34 
35 //======================================================================
36 // define
37 #if !defined(IUTEST_MAX_PATH)
38 # if defined(MAX_PATH) && MAX_PATH
39 # define IUTEST_MAX_PATH MAX_PATH
40 # elif defined(PATH_MAX) && PATH_MAX
41 # define IUTEST_MAX_PATH PATH_MAX
42 # elif defined(FILENAME_MAX) && FILENAME_MAX
43 # define IUTEST_MAX_PATH FILENAME_MAX
44 # else
45 # define IUTEST_MAX_PATH 1024
46 # endif
47 #endif
48 
52 #define IUTEST_LOG_(level) \
53  ::iutest::detail::IUTestLog( \
54  ::iutest::detail::IUTestLog::LOG_##level, __FILE__, __LINE__).GetStream()
55 
59 #define IUTEST_CHECK_(condition) \
60  IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
61  if( !::iutest::detail::IsTrue(condition) ) \
62  IUTEST_LOG_(FATAL) << "Condition " #condition " failed. "
63 
64 namespace iutest {
65 
66 #ifdef IUTEST_OS_NACL
67 namespace nacl
68 {
69 
73 void vprint_message(const char *fmt, va_list va);
74 void print_message(const char *fmt, ...);
75 
76 }
77 #endif
78 
79 namespace internal {
80 namespace posix
81 {
82 
83 const char* GetEnv(const char* name);
84 int PutEnv(const char* expr);
85 int SetEnv(const char* name, const char* value, int overwrite);
86 
87 const char* GetCWD(char* buf, size_t length);
88 ::std::string GetCWD();
89 
90 void SleepMillisec(unsigned int millisec);
91 
92 #if defined(IUTEST_OS_WINDOWS_MOBILE)
93 void Abort();
94 #else
95 IUTEST_ATTRIBUTE_NORETURN_ void Abort();
96 inline void Abort() { abort(); }
97 #endif
98 
99 #if IUTEST_HAS_FILENO
100 
101 #if defined(_MSC_VER)
102 inline int Fileno(FILE* fp) { return _fileno(fp); }
103 #else
104 inline int Fileno(FILE* fp) { return fileno(fp); }
105 #endif
106 
107 #else
108 
109 inline int Fileno(FILE*) { return -1; }
110 
111 #endif
112 
113 #if IUTEST_HAS_FILE_STAT
114 
115 #if defined(IUTEST_OS_WINDOWS) && !defined(IUTEST_OS_WINDOWS_WINE)
116 
117 typedef struct _stat StatStruct;
118 
119 inline int FileStat(int fd, StatStruct* buf) { return _fstat(fd, buf); }
120 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
121 inline bool IsDir(const StatStruct& st) { return (st.st_mode & _S_IFDIR) != 0; }
122 
123 #else
124 
125 typedef struct stat StatStruct;
126 
127 inline int FileStat(int fd, StatStruct* buf) { return fstat(fd, buf); }
128 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
129 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
130 
131 #endif
132 
133 inline int Stat(FILE* fp, StatStruct* buf)
134 {
135  int fd = Fileno(fp);
136  return fd >= 0 ? FileStat(fd, buf) : fd;
137 }
138 
139 #endif
140 
141 } // end of namespace posix
142 
143 inline void SleepMilliseconds(int n) { posix::SleepMillisec(static_cast<unsigned int>(n)); }
144 
145 } // end of namespace internal
146 
147 namespace detail
148 {
149 
150 namespace posix = internal::posix;
151 
155 char GetPathSeparator() IUTEST_CXX_NOEXCEPT_SPEC;
156 
160 bool IsPathSeparator(char c) IUTEST_CXX_NOEXCEPT_SPEC;
161 
165 bool IsAltPathSeparator(char c) IUTEST_CXX_NOEXCEPT_SPEC;
166 
170 const char* FindLastPathSeparator(const char* path, size_t length) IUTEST_CXX_NOEXCEPT_SPEC;
171 
175 size_t FindLastPathSeparatorPosition(const char* path, size_t length) IUTEST_CXX_NOEXCEPT_SPEC;
176 
180 bool SetEnvironmentVariable(const char* name, const char* value);
181 
182 
189 bool GetEnvironmentVariable(const char* name, char* buf, size_t size);
190 
191 #if !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
192 
193 template<size_t SIZE>
194 inline bool GetEnvironmentVariable(const char* name, char (&buf)[SIZE])
195 {
196 return GetEnvironmentVariable(name, buf, SIZE);
197 }
198 
199 #endif
200 
207 bool IUTEST_ATTRIBUTE_UNUSED_ GetEnvironmentVariable(const char* name, ::std::string& var);
208 
215 bool IUTEST_ATTRIBUTE_UNUSED_ GetEnvironmentInt(const char* name, int& var);
216 
217 #if defined(IUTEST_OS_WINDOWS)
218 namespace win
219 {
220 
224  ::std::string IUTEST_ATTRIBUTE_UNUSED_ WideStringToMultiByteString(const wchar_t* wide_c_str);
225 
231  ::std::string IUTEST_ATTRIBUTE_UNUSED_ GetHResultString(HRESULT hr);
232 
233 } // end of namespace win
234 #endif
235 
239 class IUTestLog
240 {
241 public:
242  enum Level
243  {
244  LOG_INFO
245  , LOG_WARNING
246  , LOG_ERROR
247  , LOG_FATAL
248  , LOG_LEVEL_NUM
249  };
250 public:
251  IUTestLog(Level level, const char* file, int line);
252 
253  ~IUTestLog();
254 
255 public:
256  iu_stringstream& GetStream() { return m_stream; }
257 
258 public:
259  static int GetCount(Level level) { return GetCountTable().count[level]; }
260  static bool HasWarning() { return GetCount(LOG_WARNING) > 0; }
261  static bool HasError() { return GetCount(LOG_ERROR) > 0 || GetCount(LOG_FATAL) > 0; }
262 
263 private:
264  struct Count
265  {
266  int count[LOG_LEVEL_NUM];
267  };
268 
269  static Count& GetCountTable() { static Count count = { {0} }; return count; }
270  static void CountUp(int level);
271 
272 private:
273  const Level kLevel;
274  iu_stringstream m_stream;
275 
277 };
278 
279 #if IUTEST_HAS_STREAM_BUFFER
280 
284 template<int SIZE=BUFSIZ>
285 class IUStreamBuffer
286 {
287 public:
288  explicit IUStreamBuffer(FILE* fp)
289  : m_fp(fp)
290  {
291  m_buf[0] = '\0';
292  fflush(fp);
293  setvbuf(fp, m_buf, _IOFBF, SIZE);
294  }
295 
296  ~IUStreamBuffer()
297  {
298  fflush(m_fp);
299  setvbuf(m_fp, NULL, _IONBF, 0);
300  }
301 
302 public:
303  ::std::string GetStreamString() { return m_buf; }
304 
305 private:
306  FILE* m_fp;
307  char m_buf[SIZE];
308 };
309 
315 #endif
316 
317 } // end of namespace detail
318 } // end of namespace iutest
319 
320 #if !IUTEST_HAS_LIB
321 # include "../impl/iutest_port.ipp"
322 #endif
323 
324 #endif // INCG_IRIS_IUTEST_PORT_HPP_7893F685_A1A9_477A_82E8_BF06237697FF_
IUTEST_ATTRIBUTE_NORETURN_
#define IUTEST_ATTRIBUTE_NORETURN_
noreturn
Definition: iutest_compiler.hpp:1256
iutest_internal_defs.hpp
internal definition
iutest_config.hpp
iris unit test config
IUTEST_CXX_NOEXCEPT_SPEC
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:734
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
IUTEST_PP_DISALLOW_COPY_AND_ASSIGN
#define IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(TypeName)
コピー禁止定義
Definition: iutest_pp.hpp:25