| 1 |
/* Contributed by Nicola Pero <[email protected]>, November 2010. */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 4 |
|
| 5 |
/* Test @optional @properties. */ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <objc/objc.h> |
| 9 |
#include <objc/runtime.h> |
| 10 |
|
| 11 |
@interface MyRootClass |
| 12 |
{ |
| 13 |
Class isa; |
| 14 |
} |
| 15 |
+ (id) initialize; |
| 16 |
+ (id) alloc; |
| 17 |
- (id) init; |
| 18 |
@end |
| 19 |
|
| 20 |
@implementation MyRootClass |
| 21 |
+ (id) initialize { return self; } |
| 22 |
+ (id) alloc { return class_createInstance (self, 0); } |
| 23 |
- (id) init { return self; } |
| 24 |
@end |
| 25 |
|
| 26 |
/* Use a different getters/setters, so that the only way to compile |
| 27 |
object.countX is to find the actual @property. */ |
| 28 |
@protocol count |
| 29 |
@required |
| 30 |
/* @required + @synthesize. */ |
| 31 |
@property (getter=number1, setter=setNumber1:) int count1; |
| 32 |
/* @required + manual setters/getters. */ |
| 33 |
@property (getter=number2, setter=setNumber2:) int count2; |
| 34 |
|
| 35 |
@optional |
| 36 |
/* @optional + @synthesize. */ |
| 37 |
@property (getter=number3, setter=setNumber3:) int count3; |
| 38 |
/* @optional + manual setters/getters. */ |
| 39 |
@property (getter=number4, setter=setNumber4:) int count4; |
| 40 |
|
| 41 |
@optional |
| 42 |
/* @optional + readonly, with a setter added in the class itself. */ |
| 43 |
@property (readonly, getter=number5) int count5; |
| 44 |
@end |
| 45 |
|
| 46 |
@interface MySubClass : MyRootClass <count> |
| 47 |
{ |
| 48 |
int count1; |
| 49 |
int count2; |
| 50 |
int count3; |
| 51 |
int count4; |
| 52 |
int count5; |
| 53 |
} |
| 54 |
- (void) setCount5: (int)value; |
| 55 |
@end |
| 56 |
|
| 57 |
@implementation MySubClass |
| 58 |
@synthesize count1; |
| 59 |
- (int) number2 |
| 60 |
{ |
| 61 |
return count2; |
| 62 |
} |
| 63 |
- (void) setNumber2: (int)value |
| 64 |
{ |
| 65 |
count2 = value; |
| 66 |
} |
| 67 |
@synthesize count3; |
| 68 |
- (int) number4 |
| 69 |
{ |
| 70 |
return count4; |
| 71 |
} |
| 72 |
- (void) setNumber4: (int)value |
| 73 |
{ |
| 74 |
count4 = value; |
| 75 |
} |
| 76 |
- (int) number5 |
| 77 |
{ |
| 78 |
return count5; |
| 79 |
} |
| 80 |
- (void) setCount5: (int)value |
| 81 |
{ |
| 82 |
count5 = value; |
| 83 |
} |
| 84 |
@end |
| 85 |
|
| 86 |
int main (void) |
| 87 |
{ |
| 88 |
MySubClass *object = [[MySubClass alloc] init]; |
| 89 |
|
| 90 |
/* First, test that @required and @optional properties work as |
| 91 |
expected if implemented either via @synthesize or manually. */ |
| 92 |
object.count1 = 44; |
| 93 |
if (object.count1 != 44) |
| 94 |
abort (); |
| 95 |
|
| 96 |
object.count2 = 88; |
| 97 |
if (object.count2 != 88) |
| 98 |
abort (); |
| 99 |
|
| 100 |
object.count3 = 77; |
| 101 |
if (object.count3 != 77) |
| 102 |
abort (); |
| 103 |
|
| 104 |
object.count4 = 11; |
| 105 |
if (object.count4 != 11) |
| 106 |
abort (); |
| 107 |
|
| 108 |
/* Now, test the complication: @optional @property which is |
| 109 |
readonly, but which has a setter manually implemented. |
| 110 |
Apparently it is possible to use the dotsyntax and the @optional |
| 111 |
@property getter is used when reading, while the manual setter is |
| 112 |
used when writing. */ |
| 113 |
object.count5 = 99; |
| 114 |
if (object.count5 != 99) |
| 115 |
abort (); |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |