testsuite_hooks.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // -*- C++ -*-
  2. // Utility subroutines for the C++ library testsuite.
  3. //
  4. // Copyright (C) 2000-2022 Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. //
  21. // This file provides the following:
  22. //
  23. // 1) VERIFY()
  24. //
  25. // 2) set_memory_limits()
  26. // set_memory_limits() uses setrlimit() to restrict dynamic memory
  27. // allocation. We provide a default memory limit if none is passed by the
  28. // calling application. The argument to set_memory_limits() is the
  29. // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
  30. // not #defined before including this header, then no limiting is attempted.
  31. //
  32. // 3) object_counter
  33. // This is a POD with a static data member, object_counter::count,
  34. // which starts at zero, increments on instance construction, and decrements
  35. // on instance destruction. "assert_count(n)" can be called to VERIFY()
  36. // that the count equals N.
  37. //
  38. // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
  39. // A class with nontrivial ctor/dtor that provides the ability to track the
  40. // number of copy ctors and dtors, and will throw on demand during copy.
  41. #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
  42. #define _GLIBCXX_TESTSUITE_HOOKS_H
  43. #include <bits/c++config.h>
  44. #include <bits/functexcept.h>
  45. #include <ctime>
  46. #include <stdio.h>
  47. #ifdef _GLIBCXX_HAVE_SYS_STAT_H
  48. #include <sys/stat.h>
  49. #endif
  50. #ifdef stderr
  51. # define _VERIFY_PRINT(S, F, L, P, C) __builtin_fprintf(stderr, S, F, L, P, C)
  52. #else
  53. # define _VERIFY_PRINT(S, F, L, P, C) __builtin_printf(S, F, L, P, C)
  54. #endif
  55. #define VERIFY(fn) \
  56. do \
  57. { \
  58. if (! (fn)) \
  59. { \
  60. _VERIFY_PRINT("%s:%d: %s: Assertion '%s' failed.\n", \
  61. __FILE__, __LINE__, __PRETTY_FUNCTION__, #fn); \
  62. __builtin_abort(); \
  63. } \
  64. } while (false)
  65. #ifdef _GLIBCXX_HAVE_UNISTD_H
  66. # include <unistd.h>
  67. #else
  68. # define unlink(x)
  69. #endif
  70. #if defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__
  71. # define ISO_8859(part,langTERR) #langTERR ".ISO8859-" #part
  72. #else
  73. # define ISO_8859(part,langTERR) ((part) == 15 ?\
  74. #langTERR ".ISO8859-" #part "@euro" : #langTERR ".ISO8859-" #part)
  75. #endif
  76. #if __cplusplus < 201103L
  77. # define THROW(X) throw(X)
  78. #else
  79. # define THROW(X) noexcept(false)
  80. #endif
  81. #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT || _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
  82. // Correct order of thread_local destruction needs __cxa_thread_atexit_impl
  83. // or similar support from libc.
  84. # define CORRECT_THREAD_LOCAL_DTORS 1
  85. #endif
  86. namespace __gnu_test
  87. {
  88. // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
  89. // from c++config.h
  90. // Set memory limits if possible, if not set to 0.
  91. #ifndef _GLIBCXX_RES_LIMITS
  92. # define MEMLIMIT_MB 0
  93. #else
  94. # ifndef MEMLIMIT_MB
  95. # define MEMLIMIT_MB 16.0
  96. # endif
  97. #endif
  98. extern void
  99. set_memory_limits(float __size = MEMLIMIT_MB);
  100. extern void
  101. set_file_limit(unsigned long __size);
  102. // Check mangled name demangles (using __cxa_demangle) as expected.
  103. void
  104. verify_demangle(const char* mangled, const char* wanted);
  105. // Simple callback structure for variable numbers of tests (all with
  106. // same signature). Assume all unit tests are of the signature
  107. // void test01();
  108. class func_callback
  109. {
  110. public:
  111. typedef void (*test_type) (void);
  112. private:
  113. int _M_size;
  114. test_type _M_tests[15];
  115. func_callback&
  116. operator=(const func_callback&);
  117. func_callback(const func_callback&);
  118. public:
  119. func_callback(): _M_size(0) { }
  120. int
  121. size() const { return _M_size; }
  122. const test_type*
  123. tests() const { return _M_tests; }
  124. void
  125. push_back(test_type test)
  126. {
  127. _M_tests[_M_size] = test;
  128. ++_M_size;
  129. }
  130. };
  131. // Run select unit tests after setting global locale.
  132. void
  133. run_tests_wrapped_locale(const char*, const func_callback&);
  134. // Run select unit tests after setting environment variables.
  135. void
  136. run_tests_wrapped_env(const char*, const char*, const func_callback&);
  137. // Counting.
  138. struct object_counter
  139. {
  140. // Specifically and glaringly-obviously marked 'signed' so that
  141. // when COUNT mistakenly goes negative, we can track the patterns
  142. // of deletions more easily.
  143. typedef signed int size_type;
  144. static size_type count;
  145. object_counter() { ++count; }
  146. object_counter (const object_counter&) { ++count; }
  147. ~object_counter() { --count; }
  148. };
  149. #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
  150. // A (static) class for counting copy constructors and possibly throwing an
  151. // exception on a desired count.
  152. class copy_constructor
  153. {
  154. public:
  155. static unsigned int
  156. count() { return count_; }
  157. static void
  158. mark_call()
  159. {
  160. count_++;
  161. if (count_ == throw_on_)
  162. std::__throw_runtime_error("copy_constructor::mark_call");
  163. }
  164. static void
  165. reset()
  166. {
  167. count_ = 0;
  168. throw_on_ = 0;
  169. }
  170. static void
  171. throw_on(unsigned int count) { throw_on_ = count; }
  172. private:
  173. static unsigned int count_;
  174. static unsigned int throw_on_;
  175. };
  176. // A (static) class for counting assignment operator calls and
  177. // possibly throwing an exception on a desired count.
  178. class assignment_operator
  179. {
  180. public:
  181. static unsigned int
  182. count() { return count_; }
  183. static void
  184. mark_call()
  185. {
  186. count_++;
  187. if (count_ == throw_on_)
  188. std::__throw_runtime_error("assignment_operator::mark_call");
  189. }
  190. static void
  191. reset()
  192. {
  193. count_ = 0;
  194. throw_on_ = 0;
  195. }
  196. static void
  197. throw_on(unsigned int count) { throw_on_ = count; }
  198. private:
  199. static unsigned int count_;
  200. static unsigned int throw_on_;
  201. };
  202. // A (static) class for tracking calls to an object's destructor.
  203. class destructor
  204. {
  205. public:
  206. static unsigned int
  207. count() { return _M_count; }
  208. static void
  209. mark_call() { _M_count++; }
  210. static void
  211. reset() { _M_count = 0; }
  212. private:
  213. static unsigned int _M_count;
  214. };
  215. // A class of objects that can be used for validating various
  216. // behaviors and guarantees of containers and algorithms defined in
  217. // the standard library.
  218. class copy_tracker
  219. {
  220. public:
  221. // Creates a copy-tracking object with the given ID number. If
  222. // "throw_on_copy" is set, an exception will be thrown if an
  223. // attempt is made to copy this object.
  224. copy_tracker(int id = next_id_--, bool throw_on_copy = false)
  225. : id_(id) , throw_on_copy_(throw_on_copy) { }
  226. // Copy-constructs the object, marking a call to the copy
  227. // constructor and forcing an exception if indicated.
  228. copy_tracker(const copy_tracker& rhs)
  229. : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
  230. {
  231. if (throw_on_copy_)
  232. copy_constructor::throw_on(copy_constructor::count() + 1);
  233. copy_constructor::mark_call();
  234. }
  235. // Assigns the value of another object to this one, tracking the
  236. // number of times this member function has been called and if the
  237. // other object is supposed to throw an exception when it is
  238. // copied, well, make it so.
  239. copy_tracker&
  240. operator=(const copy_tracker& rhs)
  241. {
  242. id_ = rhs.id();
  243. if (rhs.throw_on_copy_)
  244. assignment_operator::throw_on(assignment_operator::count() + 1);
  245. assignment_operator::mark_call();
  246. return *this;
  247. }
  248. ~copy_tracker()
  249. { destructor::mark_call(); }
  250. int
  251. id() const { return id_; }
  252. static void
  253. reset()
  254. {
  255. copy_constructor::reset();
  256. assignment_operator::reset();
  257. destructor::reset();
  258. }
  259. private:
  260. int id_;
  261. const bool throw_on_copy_;
  262. static int next_id_;
  263. };
  264. inline bool
  265. operator==(const copy_tracker& lhs, const copy_tracker& rhs)
  266. { return lhs.id() == rhs.id(); }
  267. inline bool
  268. operator<(const copy_tracker& lhs, const copy_tracker& rhs)
  269. { return lhs.id() < rhs.id(); }
  270. // Class for checking required type conversions, implicit and
  271. // explicit for given library data structures.
  272. template<typename _Container>
  273. struct conversion
  274. {
  275. typedef typename _Container::const_iterator const_iterator;
  276. // Implicit conversion iterator to const_iterator.
  277. static void
  278. iterator_to_const_iterator()
  279. {
  280. _Container v;
  281. const_iterator i __attribute__((unused)) = const_iterator(v.begin());
  282. const_iterator j __attribute__((unused)) = true ? i : v.begin();
  283. #if __cplusplus >= 201103L
  284. const_iterator k __attribute__((unused)) { v.begin() };
  285. #endif
  286. }
  287. };
  288. // A binary semaphore for use across multiple processes.
  289. class semaphore
  290. {
  291. public:
  292. // Creates a binary semaphore. The semaphore is initially in the
  293. // unsignaled state.
  294. semaphore();
  295. // Destroy the semaphore.
  296. ~semaphore();
  297. // Signal the semaphore. If there are processes blocked in
  298. // "wait", exactly one will be permitted to proceed.
  299. void signal();
  300. // Wait until the semaphore is signaled.
  301. void wait();
  302. private:
  303. int sem_set_;
  304. pid_t pid_;
  305. };
  306. // For use in 22_locale/time_get and time_put.
  307. std::tm test_tm(int sec, int min, int hour, int mday, int mon,
  308. int year, int wday, int yday, int isdst);
  309. } // namespace __gnu_test
  310. #endif // _GLIBCXX_TESTSUITE_HOOKS_H