| 1 |
/* Contributed by Nicola Pero <[email protected]>, October 2010. */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 4 |
|
| 5 |
/* Test retain and copy synthesized methods. */ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <objc/objc.h> |
| 9 |
#include <objc/runtime.h> |
| 10 |
|
| 11 |
@interface MyRootClass |
| 12 |
{ |
| 13 |
Class isa; |
| 14 |
int copy_count; |
| 15 |
id a; |
| 16 |
id b; |
| 17 |
} |
| 18 |
@property (copy) id a; |
| 19 |
@property (retain) id b; |
| 20 |
+ (id) initialize; |
| 21 |
+ (id) alloc; |
| 22 |
- (id) init; |
| 23 |
- (id) copyWithZone: (void *)zone; |
| 24 |
- (int) copyCount; |
| 25 |
- (id) autorelease; |
| 26 |
- (oneway void) release; |
| 27 |
- (id) retain; |
| 28 |
@end |
| 29 |
|
| 30 |
/* This class implements copyWithZone, which doesn't do anything other |
| 31 |
than increasing a counter of how many copies were made. */ |
| 32 |
@implementation MyRootClass |
| 33 |
+ (id) initialize { return self; } |
| 34 |
+ (id) alloc { return class_createInstance (self, 0); } |
| 35 |
- (id) init { return self; } |
| 36 |
- (id) copyWithZone: (void *)zone { copy_count++; return self; } |
| 37 |
- (int) copyCount { return copy_count; } |
| 38 |
- (id) autorelease { return self; } |
| 39 |
- (oneway void) release { return; } |
| 40 |
- (id) retain { return self; } |
| 41 |
@synthesize a; |
| 42 |
@synthesize b; |
| 43 |
@end |
| 44 |
|
| 45 |
int main (void) |
| 46 |
{ |
| 47 |
MyRootClass *object = [[MyRootClass alloc] init]; |
| 48 |
MyRootClass *argument = [[MyRootClass alloc] init]; |
| 49 |
|
| 50 |
/* This should copy argument. */ |
| 51 |
object.a = argument; |
| 52 |
if (object.a != argument) |
| 53 |
abort (); |
| 54 |
|
| 55 |
/* Test that it was copied. */ |
| 56 |
if ([object.a copyCount] != 1) |
| 57 |
abort (); |
| 58 |
|
| 59 |
/* We just test that the retain accessors seem to work and that they |
| 60 |
don't copy. We don't test that retain was actually called, |
| 61 |
because if garbage collection is enabled, it may never be |
| 62 |
called! */ |
| 63 |
object.b = argument; |
| 64 |
if (object.b != argument) |
| 65 |
abort (); |
| 66 |
|
| 67 |
/* Test that it was not copied. */ |
| 68 |
if ([object.b copyCount] != 1) |
| 69 |
abort (); |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |