]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/operator-new.dtest/main.cxx
dyld-851.27.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 <new>
7
8 #import "test_support.h"
9
10 //
11 // This test case verifies that calling operator new[] in libstdc++.dylib
12 // will turn around and call operator new in this main exectuable
13 //
14
15 static void* myLastNewAllocation;
16 static void* myLastDelete;
17
18 // Note: this is not weak. That is specifically suppported
19 void* operator new(size_t s) throw (std::bad_alloc)
20 {
21 myLastNewAllocation = malloc(s);
22 return myLastNewAllocation;
23 }
24
25 struct Foo {
26 int bytes[10];
27 };
28
29 // Note: this is weak and because it is in main executable should override OS
30 __attribute__((weak))
31 void operator delete(void* p) throw()
32 {
33 myLastDelete = p;
34 ::free(p);
35 }
36
37 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
38 // test that OS's operator new[] redirects to my operator new
39 myLastNewAllocation = NULL;
40 char* stuff = new char[24];
41 if ( (void*)stuff != myLastNewAllocation ) {
42 FAIL("system array allocator not redirected through my operator new");
43 }
44
45 // test that program uses my operator new
46 myLastNewAllocation = NULL;
47 Foo* foo = new Foo();
48 if ( (void*)foo != myLastNewAllocation ) {
49 FAIL("allocation not redirected though my operator new");
50 }
51
52 //
53 delete foo;
54 if ( (void*)foo != myLastDelete ) {
55 FAIL("deallocation not redirected though my operator delete");
56 }
57
58 PASS("Success");
59 }
60
61