1/** 2 * @file utility_tests.cpp 3 * tests utility.h and op_exception.h 4 * 5 * @remark Copyright 2003 OProfile authors 6 * @remark Read the file COPYING 7 * 8 * @author John Levon 9 * @author Philippe Elie 10 */ 11 12#include <stdlib.h> 13#include <new> 14#include <iostream> 15 16#include "utility.h" 17#include "op_exception.h" 18 19using namespace std; 20 21static int nb_new; 22static int nb_new_array; 23 24void* operator new(size_t size) throw(bad_alloc) 25{ 26 nb_new++; 27 return malloc(size); 28} 29 30void* operator new[](size_t size) throw(bad_alloc) 31{ 32 nb_new_array++; 33 return malloc(size); 34} 35 36void operator delete(void * p) throw() 37{ 38 nb_new--; 39 if (p) 40 free(p); 41} 42 43void operator delete[](void * p) throw() 44{ 45 nb_new_array--; 46 if (p) 47 free(p); 48} 49 50 51void check_alloc() 52{ 53 if (nb_new) { 54 cerr << "new(size_t) leak\n"; 55 exit(EXIT_FAILURE); 56 } 57 58 if (nb_new_array) { 59 cerr << "new[](size_t) leak\n"; 60 exit(EXIT_FAILURE); 61 } 62} 63 64 65struct A {}; 66 67template <typename Throw, typename Catch> 68void throw_tests() 69{ 70 scoped_ptr<A> a(new A); 71 try { 72 scoped_ptr<A> a(new A); 73 throw Throw(""); 74 } 75 catch (Catch const &) { 76 } 77} 78 79 80template <typename Throw, typename Catch> 81void throw_tests(bool) 82{ 83 scoped_array<A> b(new A[10]); 84 try { 85 scoped_array<A> a(new A[10]); 86 throw Throw(""); 87 } 88 catch (Catch const &) { 89 } 90} 91 92 93void tests_new() 94{ 95 throw_tests<op_fatal_error, op_fatal_error>(); 96 throw_tests<op_fatal_error, op_exception>(); 97 throw_tests<op_runtime_error, op_runtime_error>(); 98 throw_tests<op_runtime_error, runtime_error>(); 99 throw_tests<op_fatal_error, op_fatal_error>(true); 100 throw_tests<op_fatal_error, op_exception>(true); 101 throw_tests<op_runtime_error, op_runtime_error>(true); 102 throw_tests<op_runtime_error, runtime_error>(true); 103} 104 105 106int main() 107{ 108 try { 109 tests_new(); 110 check_alloc(); 111 } 112 catch (...) { 113 cerr << "unknown exception\n"; 114 return EXIT_FAILURE; 115 } 116 117 return EXIT_SUCCESS; 118} 119