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