testsuite_hooks.cc [plain text]
#include <testsuite_hooks.h>
#ifdef _GLIBCXX_RES_LIMITS
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif
#include <list>
#include <string>
#include <stdexcept>
#include <clocale>
#include <locale>
#include <cxxabi.h>
#if defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
&& defined(_GLIBCXX_HAVE_SYS_IPC_H) \
&& defined(_GLIBCXX_HAVE_SYS_SEM_H)
#define _GLIBCXX_SYSV_SEM
#endif
#ifdef _GLIBCXX_SYSV_SEM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#endif
namespace __gnu_test
{
#ifdef _GLIBCXX_RES_LIMITS
void
set_memory_limits(float size)
{
struct rlimit r;
__typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
#if _GLIBCXX_HAVE_LIMIT_DATA
getrlimit(RLIMIT_DATA, &r);
r.rlim_cur = limit;
setrlimit(RLIMIT_DATA, &r);
#endif
#if _GLIBCXX_HAVE_LIMIT_RSS
getrlimit(RLIMIT_RSS, &r);
r.rlim_cur = limit;
setrlimit(RLIMIT_RSS, &r);
#endif
#if _GLIBCXX_HAVE_LIMIT_VMEM
getrlimit(RLIMIT_VMEM, &r);
r.rlim_cur = limit;
setrlimit(RLIMIT_VMEM, &r);
#endif
#if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
getrlimit(RLIMIT_AS, &r);
r.rlim_cur = limit;
setrlimit(RLIMIT_AS, &r);
#endif
}
#else
void
set_memory_limits(float) { }
#endif
#ifdef _GLIBCXX_RES_LIMITS
void
set_file_limit(unsigned long size)
{
#if _GLIBCXX_HAVE_LIMIT_FSIZE
struct rlimit r;
__typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);
getrlimit(RLIMIT_FSIZE, &r);
r.rlim_cur = limit;
setrlimit(RLIMIT_FSIZE, &r);
#endif
}
#else
void
set_file_limit(unsigned long) { }
#endif
void
verify_demangle(const char* mangled, const char* wanted)
{
int status = 0;
const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
if (!s)
{
switch (status)
{
case 0:
s = "error code = 0: success";
break;
case -1:
s = "error code = -1: memory allocation failure";
break;
case -2:
s = "error code = -2: invalid mangled name";
break;
case -3:
s = "error code = -3: invalid arguments";
break;
default:
s = "error code unknown - who knows what happened";
}
}
std::string w(wanted);
if (w != s)
std::__throw_runtime_error(s);
}
void
run_tests_wrapped_locale(const char* name, const func_callback& l)
{
using namespace std;
bool test = true;
locale loc_name = try_named_locale(name);
locale orig = locale::global(loc_name);
const char* res = setlocale(LC_ALL, name);
if (res != NULL)
{
string preLC_ALL = res;
const func_callback::test_type* tests = l.tests();
for (int i = 0; i < l.size(); ++i)
(*tests[i])();
string postLC_ALL= setlocale(LC_ALL, NULL);
VERIFY( preLC_ALL == postLC_ALL );
}
else
{
string s("LC_ALL for ");
s += name;
__throw_runtime_error(s.c_str());
}
}
void
run_tests_wrapped_env(const char* name, const char* env,
const func_callback& l)
{
using namespace std;
bool test = true;
#ifdef _GLIBCXX_HAVE_SETENV
locale loc_name = try_named_locale(name);
locale orig = locale::global(loc_name);
const char* oldENV = getenv(env);
if (!setenv(env, name, 1))
{
const func_callback::test_type* tests = l.tests();
for (int i = 0; i < l.size(); ++i)
(*tests[i])();
setenv(env, oldENV ? oldENV : "", 1);
}
else
{
string s(env);
s += string(" to ");
s += string(name);
__throw_runtime_error(s.c_str());
}
#endif
}
std::locale
try_named_locale(const char* name)
{
try
{
return std::locale(name);
}
#ifdef __EXCEPTIONS
catch (std::runtime_error& ex)
{
if (std::strstr(ex.what(), "name not valid"))
exit(0);
else
throw;
}
#endif
}
counter::size_type counter::count = 0;
unsigned int copy_constructor::count_ = 0;
unsigned int copy_constructor::throw_on_ = 0;
unsigned int assignment_operator::count_ = 0;
unsigned int assignment_operator::throw_on_ = 0;
unsigned int destructor::_M_count = 0;
int copy_tracker::next_id_ = 0;
#ifdef _GLIBCXX_SYSV_SEM
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
#endif
semaphore::semaphore()
{
#ifdef _GLIBCXX_SYSV_SEM
pid_ = getpid();
#ifndef SEM_R
#define SEM_R 0400
#endif
#ifndef SEM_A
#define SEM_A 0200
#endif
sem_set_ = semget(IPC_PRIVATE, 1, SEM_R | SEM_A);
if (sem_set_ == -1)
std::__throw_runtime_error("could not obtain semaphore set");
union semun val;
val.val = 0;
if (semctl(sem_set_, 0, SETVAL, val) == -1)
std::__throw_runtime_error("could not initialize semaphore");
#else
exit(0);
#endif
}
semaphore::~semaphore()
{
#ifdef _GLIBCXX_SYSV_SEM
union semun val;
if (pid_ == getpid())
semctl(sem_set_, 0, IPC_RMID, val);
#endif
}
void
semaphore::signal()
{
#ifdef _GLIBCXX_SYSV_SEM
struct sembuf op[1] =
{
{ 0, 1, 0 }
};
if (semop(sem_set_, op, 1) == -1)
std::__throw_runtime_error("could not signal semaphore");
#endif
}
void
semaphore::wait()
{
#ifdef _GLIBCXX_SYSV_SEM
struct sembuf op[1] =
{
{ 0, -1, SEM_UNDO }
};
if (semop(sem_set_, op, 1) == -1)
std::__throw_runtime_error("could not wait for semaphore");
#endif
}
};