1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LLVM_LICENSE_HEADER@
5 */
6
7//
8//  byrefcopyid.m
9//  testObjects
10//
11//  Created by Blaine Garst on 5/13/08.
12//  Copyright 2008 __MyCompanyName__. All rights reserved.
13//
14
15// Tests copying of blocks with byref ints and an id
16// TEST_CFLAGS -framework Foundation
17
18#import <Foundation/Foundation.h>
19#import <Block.h>
20#import <Block_private.h>
21#import "test.h"
22
23
24int CalledRetain = 0;
25int CalledRelease = 0;
26int CalledSelf = 0;
27int CalledDealloc = 0;
28
29
30@interface DumbObject : NSObject {
31}
32@end
33
34@implementation DumbObject
35- (id)retain {
36    CalledRetain = 1;
37    return [super retain];
38}
39- (oneway void)release {
40    CalledRelease = 1;
41    [super release];
42}
43- (id)self {
44    CalledSelf = 1;
45    return self;
46}
47
48- (void)dealloc {
49    CalledDealloc = 1;
50    [super dealloc];
51}
52
53@end
54
55
56void callVoidVoid(void (^closure)(void)) {
57    closure();
58}
59
60void (^dummy)(void);
61
62
63
64id testRoutine(const char *whoami) {
65    __block id  dumbo = [DumbObject new];
66    dummy = ^{
67        [dumbo self];
68    };
69
70
71    //doHack(dummy);
72    id copy = Block_copy(dummy);
73
74    callVoidVoid(copy);
75    if (CalledSelf == 0) {
76        fail("copy helper of byref id didn't call self", whoami);
77    }
78
79    return copy;
80}
81
82int main(int argc __unused, char *argv[]) {
83    id copy = testRoutine(argv[0]);
84    Block_release(copy);
85    if (CalledRetain != 0) {
86        fail("copy helper of byref retained the id");
87    }
88    if (CalledRelease != 0) {
89        fail("copy helper of byref id did release the id");
90    }
91
92    succeed(__FILE__);
93}
94