1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LLVM_LICENSE_HEADER@
5 */
6
7// TEST_CFLAGS -framework Foundation
8
9// rdar://6275956
10
11#import <objc/objc-auto.h>
12#import <Foundation/Foundation.h>
13#import <Block.h>
14#import "test.h"
15
16int recovered = 0;
17int constructors = 0;
18int destructors = 0;
19
20#define CONST const
21
22class TestObject
23{
24public:
25	TestObject(CONST TestObject& inObj);
26	TestObject();
27	~TestObject();
28
29#define EQUAL 1
30#if EQUAL
31	TestObject& operator=(CONST TestObject& inObj);
32#endif
33        void test(void);
34
35	int version() CONST { return _version; }
36private:
37	mutable int _version;
38};
39
40TestObject::TestObject(CONST TestObject& inObj)
41
42{
43        ++constructors;
44        _version = inObj._version;
45	printf("%p (%d) -- TestObject(const TestObject&) called", this, _version);
46}
47
48
49TestObject::TestObject()
50{
51        _version = ++constructors;
52	//printf("%p (%d) -- TestObject() called\n", this, _version);
53}
54
55
56TestObject::~TestObject()
57{
58	//printf("%p -- ~TestObject() called\n", this);
59        ++destructors;
60}
61
62#if EQUAL
63TestObject& TestObject::operator=(CONST TestObject& inObj)
64{
65	printf("%p -- operator= called", this);
66        _version = inObj._version;
67	return *this;
68}
69#endif
70
71void TestObject::test(void)  {
72    void (^b)(void) = ^{ recovered = this->_version; };
73    void (^b2)(void) = [b copy];
74    b2();
75}
76
77void testRoutine() {
78    TestObject *one = new TestObject();
79
80    void (^b)(void) = [^{ recovered = one->version(); } copy];
81    b();
82    [b release];
83    delete one;
84}
85
86
87
88int main() {
89    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
90    testRoutine();
91    objc_collect(OBJC_EXHAUSTIVE_COLLECTION | OBJC_WAIT_UNTIL_DONE);
92    [pool drain];
93
94    if (recovered != 1) {
95        fail("%s: *** didn't recover byref block variable");
96    }
97
98    succeed(__FILE__);
99}
100