| 1 | #include "advancedcpp.h" |
| 2 | |
| 3 | #include <stdexcept> |
| 4 | |
| 5 | |
| 6 | // for testing of default arguments |
| 7 | #define IMPLEMENT_DEFAULTERS(type, tname) \ |
| 8 | tname##_defaulter::tname##_defaulter(type a, type b, type c) { \ |
| 9 | m_a = a; m_b = b; m_c = c; \ |
| 10 | } \ |
| 11 | type tname##_defaulter_func(int idx, type a, type b, type c) { \ |
| 12 | if (idx == 0) return a; \ |
| 13 | if (idx == 1) return b; \ |
| 14 | if (idx == 2) return c; \ |
| 15 | return (type)idx; \ |
| 16 | } |
| 17 | IMPLEMENT_DEFAULTERS(short, short) |
| 18 | IMPLEMENT_DEFAULTERS(unsigned short, ushort) |
| 19 | IMPLEMENT_DEFAULTERS(int, int) |
| 20 | IMPLEMENT_DEFAULTERS(unsigned, uint) |
| 21 | IMPLEMENT_DEFAULTERS(long, long) |
| 22 | IMPLEMENT_DEFAULTERS(unsigned long, ulong) |
| 23 | IMPLEMENT_DEFAULTERS(long long, llong) |
| 24 | IMPLEMENT_DEFAULTERS(unsigned long long, ullong) |
| 25 | IMPLEMENT_DEFAULTERS(float, float) |
| 26 | IMPLEMENT_DEFAULTERS(double, double) |
| 27 | |
| 28 | std::string string_defaulter_func(int idx, const std::string& name1, std::string name2) { |
| 29 | if (idx == 0) return name1; |
| 30 | if (idx == 1) return name2; |
| 31 | return "mies"; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | // for esoteric inheritance testing |
| 36 | a_class* create_c1() { return new c_class_1; } |
| 37 | a_class* create_c2() { return new c_class_2; } |
| 38 | |
| 39 | int get_a( a_class& a ) { return a.m_a; } |
| 40 | int get_b( b_class& b ) { return b.m_b; } |
| 41 | int get_c( c_class& c ) { return c.m_c; } |
| 42 | int get_d( d_class& d ) { return d.m_d; } |
| 43 | |
| 44 | |
| 45 | // for namespace testing |
| 46 | int a_ns::g_a = 11; |
| 47 | int a_ns::b_class::s_b = 22; |
| 48 | int a_ns::b_class::c_class::s_c = 33; |
| 49 | int a_ns::d_ns::g_d = 44; |
| 50 | int a_ns::d_ns::e_class::s_e = 55; |
| 51 | int a_ns::d_ns::e_class::f_class::s_f = 66; |
| 52 | |
| 53 | int a_ns::get_g_a() { return g_a; } |
| 54 | int a_ns::d_ns::get_g_d() { return g_d; } |
| 55 | |
| 56 | |
| 57 | // for template testing |
| 58 | template class T1<int>; |
| 59 | template class T2<T1<int> >; |
| 60 | template class T3<int, double>; |
| 61 | template class T3<T1<int>, T2<T1<int> > >; |
| 62 | template class a_ns::T4<int>; |
| 63 | template class a_ns::T4<a_ns::T4<T3<int, double> > >; |
| 64 | |
| 65 | |
| 66 | // helpers for checking pass-by-ref |
| 67 | void set_int_through_ref(int& i, int val) { i = val; } |
| 68 | int pass_int_through_const_ref(const int& i) { return i; } |
| 69 | void set_long_through_ref(long& l, long val) { l = val; } |
| 70 | long pass_long_through_const_ref(const long& l) { return l; } |
| 71 | void set_double_through_ref(double& d, double val) { d = val; } |
| 72 | double pass_double_through_const_ref(const double& d) { return d; } |
| 73 | |
| 74 | |
| 75 | // for math conversions testing |
| 76 | bool operator==(const some_comparable& c1, const some_comparable& c2 ) |
| 77 | { |
| 78 | return &c1 != &c2; // the opposite of a pointer comparison |
| 79 | } |
| 80 | |
| 81 | bool operator!=( const some_comparable& c1, const some_comparable& c2 ) |
| 82 | { |
| 83 | return &c1 == &c2; // the opposite of a pointer comparison |
| 84 | } |
| 85 | |
| 86 | |
| 87 | // a couple of globals for access testing |
| 88 | double my_global_double = 12.; |
| 89 | double my_global_array[500]; |
| 90 | static double sd = 1234.; |
| 91 | double* my_global_ptr = &sd; |
| 92 | const char my_global_string2[] = "zus jet teun"; |
| 93 | some_int_holder my_global_int_holders[5] = { |
| 94 | some_int_holder(13), some_int_holder(42), some_int_holder(88), |
| 95 | some_int_holder(-1), some_int_holder(17) }; |
| 96 | |
| 97 | // for life-line and identity testing |
| 98 | int some_class_with_data::some_data::s_num_data = 0; |
| 99 | |
| 100 | |
| 101 | // for testing multiple inheritance |
| 102 | multi1::~multi1() {} |
| 103 | multi2::~multi2() {} |
| 104 | multi::~multi() {} |
| 105 | |
| 106 | |
| 107 | // for testing calls to overloaded new |
| 108 | int new_overloader::s_instances = 0; |
| 109 | |
| 110 | void* new_overloader::operator new(std::size_t size) { |
| 111 | ++s_instances; |
| 112 | return ::operator new(size); |
| 113 | } |
| 114 | |
| 115 | void* new_overloader::operator new(std::size_t, void* p) throw() { |
| 116 | // no ++s_instances, as no memory is allocated |
| 117 | return p; |
| 118 | } |
| 119 | |
| 120 | void new_overloader::operator delete(void* p, std::size_t) { |
| 121 | if (p == 0) return; |
| 122 | --s_instances; |
| 123 | ::operator delete(p); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | // overload order testing |
| 128 | int overload_one_way::gime() const { return 1; } |
| 129 | std::string overload_one_way::gime() { return "aap"; } |
| 130 | |
| 131 | std::string overload_the_other_way::gime() { return "aap"; } |
| 132 | int overload_the_other_way::gime() const { return 1; } |
| 133 | |
| 134 | |
| 135 | // exception handling testing |
| 136 | void Thrower::throw_anything() { |
| 137 | throw 1; |
| 138 | } |
| 139 | |
| 140 | void Thrower::throw_exception() { |
| 141 | throw std::runtime_error("C++ function failed"); |
| 142 | } |