| 1 |
/* Test the Modern GNU Objective-C Runtime API. |
| 2 |
|
| 3 |
This is test 'ivar', covering all functions starting with 'ivar'. */ |
| 4 |
|
| 5 |
/* { dg-do run } */ |
| 6 |
/* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */ |
| 7 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 8 |
/* { dg-additional-options "-Wno-objc-root-class" } */ |
| 9 |
|
| 10 |
/* To get the modern GNU Objective-C Runtime API, you include |
| 11 |
objc/runtime.h. */ |
| 12 |
#include <objc/runtime.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
@interface MyRootClass |
| 18 |
{ Class isa; } |
| 19 |
+ alloc; |
| 20 |
- init; |
| 21 |
+ initialize; |
| 22 |
@end |
| 23 |
|
| 24 |
@implementation MyRootClass |
| 25 |
+ alloc { return class_createInstance (self, 0); } |
| 26 |
- init { return self; } |
| 27 |
+ initialize { return self; } |
| 28 |
@end |
| 29 |
|
| 30 |
@protocol MyProtocol |
| 31 |
- (id) variable; |
| 32 |
@end |
| 33 |
|
| 34 |
@protocol MySecondProtocol |
| 35 |
- (id) setVariable: (id)value; |
| 36 |
@end |
| 37 |
|
| 38 |
@interface MySubClass : MyRootClass <MyProtocol> |
| 39 |
{ id variable_ivar; } |
| 40 |
- (void) setVariable: (id)value; |
| 41 |
- (id) variable; |
| 42 |
@end |
| 43 |
|
| 44 |
@implementation MySubClass |
| 45 |
- (void) setVariable: (id)value { variable_ivar = value; } |
| 46 |
- (id) variable { return variable_ivar; } |
| 47 |
@end |
| 48 |
|
| 49 |
|
| 50 |
int main(int argc, void **args) |
| 51 |
{ |
| 52 |
/* Functions are tested in alphabetical order. */ |
| 53 |
|
| 54 |
printf ("Testing ivar_getName () ...\n"); |
| 55 |
{ |
| 56 |
Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), |
| 57 |
"variable_ivar"); |
| 58 |
if (strcmp (ivar_getName (ivar), "variable_ivar") != 0) |
| 59 |
abort (); |
| 60 |
|
| 61 |
ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), |
| 62 |
"variable"); |
| 63 |
if (ivar != 0) |
| 64 |
abort (); |
| 65 |
} |
| 66 |
|
| 67 |
printf ("Testing ivar_getOffset () ...\n"); |
| 68 |
{ |
| 69 |
Ivar ivar = class_getInstanceVariable (objc_getClass ("MyRootClass"), |
| 70 |
"isa"); |
| 71 |
if (ivar_getOffset (ivar) != 0) |
| 72 |
abort (); |
| 73 |
} |
| 74 |
|
| 75 |
printf ("Testing ivar_getTypeEncoding () ...\n"); |
| 76 |
{ |
| 77 |
Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), |
| 78 |
"variable_ivar"); |
| 79 |
if (strcmp (ivar_getTypeEncoding (ivar), "@") != 0) |
| 80 |
abort (); |
| 81 |
} |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |