1// GROUPS passed operator-new
2// Check that if there is a user defined class-specific operator
3// new for a given class, that operator is invoked when a new
4// object of the class is requested, regardless of whether or not
5// there is also a constructor for the given class, and regardless
6// of whether or not the constructor for the given class is defined
7// before or after the new operator is even declared.
8
9extern "C" int printf (const char *, ...);
10
11typedef __SIZE_TYPE__ size_t;
12
13struct base {
14	int i;
15
16	base ()
17	{
18		i = 7;
19	}
20
21	void * operator new (size_t size);
22	void operator delete (void*);
23};
24
25class derived : public base {
26	int j;
27};
28
29int new_call_count = 0;
30int expected_size = 0;
31int errors = 0;
32
33int main ()
34{
35	base*		base_ptr;
36	derived*	derived_ptr;
37
38	expected_size = sizeof (int);
39	base_ptr = new base;
40	expected_size = 2 * sizeof (int);
41	derived_ptr = new derived ();
42
43	if ((new_call_count != 2) || (errors != 0))
44	  { printf ("FAIL\n"); return 1; }
45	else
46	  printf ("PASS\n");
47
48	return 0;
49}
50
51int allocation_space[100];
52int* allocation_ptr = allocation_space;
53
54void base::operator delete (void* p)
55{
56}
57
58void *base::operator new (size_t size)
59{
60	int* return_value = allocation_ptr;
61
62	new_call_count++;
63	if (size != expected_size)
64		errors++;
65	allocation_ptr += (size + sizeof(int) - 1) / sizeof(int);
66	return (void*) return_value;
67}
68