dyld-625.13.tar.gz
[apple/dyld.git] / testing / test-cases / operator-new.dtest / main.cxx
1
2 // BUILD: $CXX main.cxx -o $BUILD_DIR/operator-new.exe
3
4 // RUN: ./operator-new.exe
5
6 #include <stdio.h>
7 #include <new>
8
9
10
11 //
12 // This test case verifies that calling operator new[] in libstdc++.dylib
13 // will turn around and call operator new in this main exectuable
14 //
15
16 static void* myLastNewAllocation;
17 static void* myLastDelete;
18
19 // Note: this is not weak. That is specifically suppported
20 void* operator new(size_t s) throw (std::bad_alloc)
21 {
22 myLastNewAllocation = malloc(s);
23 return myLastNewAllocation;
24 }
25
26 struct Foo {
27 int bytes[10];
28 };
29
30 // Note: this is weak and because it is in main executable should override OS
31 __attribute__((weak))
32 void operator delete(void* p) throw()
33 {
34 myLastDelete = p;
35 ::free(p);
36 }
37
38 int main()
39 {
40 printf("[BEGIN] operator-new\n");
41
42 // test that OS's operator new[] redirects to my operator new
43 myLastNewAllocation = NULL;
44 char* stuff = new char[24];
45 if ( (void*)stuff != myLastNewAllocation ) {
46 printf("[FAIL] operator-new system array allocator not redirected through my operator new\n");
47 return 0;
48 }
49
50 // test that program uses my operator new
51 myLastNewAllocation = NULL;
52 Foo* foo = new Foo();
53 if ( (void*)foo != myLastNewAllocation ) {
54 printf("[FAIL] operator-new allocation not redirected though my operator new\n");
55 return 0;
56 }
57
58 //
59 delete foo;
60 if ( (void*)foo != myLastDelete ) {
61 printf("[FAIL] operator-new deallocation not redirected though my operator delete\n");
62 return 0;
63 }
64
65 printf("[PASS] operator-new\n");
66 return 0;
67 }
68
69