1/* Test the Modern GNU Objective-C Runtime API. 2 3 This is test 'object', covering all functions starting with 'object'. */ 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 9/* To get the modern GNU Objective-C Runtime API, you include 10 objc/runtime.h. */ 11#include <objc/runtime.h> 12#include <stdlib.h> 13#include <iostream> 14#include <cstring> 15 16@interface MyRootClass 17{ Class isa; } 18+ alloc; 19- init; 20+ initialize; 21@end 22 23@implementation MyRootClass 24+ alloc { return class_createInstance (self, 0); } 25- init { return self; } 26+ initialize { return self; } 27@end 28 29@protocol MyProtocol 30- (id) variable; 31@end 32 33@protocol MySecondProtocol 34- (id) setVariable: (id)value; 35@end 36 37@interface MySubClass : MyRootClass <MyProtocol> 38{ id variable_ivar; } 39- (void) setVariable: (id)value; 40- (id) variable; 41@end 42 43@implementation MySubClass 44- (void) setVariable: (id)value { variable_ivar = value; } 45- (id) variable { return variable_ivar; } 46@end 47 48@interface MySubSubClass : MySubClass 49- (id) test; 50@end 51 52@implementation MySubSubClass 53- (id) test { return self; } 54@end 55 56 57 58int main () 59{ 60 /* Functions are tested in alphabetical order. */ 61 62 std::cout << "Testing object_copy () ...\n"; 63 { 64 MySubClass *object_a = [[MySubClass alloc] init]; 65 MySubClass *object_b = object_copy (object_a, 0); 66 67 [object_b setVariable: object_a]; 68 if ([object_b variable] != object_a) 69 abort (); 70 } 71 72 std::cout << "Testing object_dispose () ...\n"; 73 { 74 MySubClass *object = [[MySubClass alloc] init]; 75 76 object_dispose (object); 77 } 78 79 std::cout << "Testing object_getClass () ...\n"; 80 { 81 MyRootClass *o = [[MySubClass alloc] init]; 82 83 if (object_getClass (o) != objc_getClass ("MySubClass")) 84 abort (); 85 } 86 87 std::cout << "Testing object_getClassName () ...\n"; 88 { 89 MyRootClass *o = [[MyRootClass alloc] init]; 90 91 if (std::strcmp (object_getClassName (o), "MyRootClass") != 0) 92 abort (); 93 } 94 95 std::cout << "Testing object_getIndexedIvars () ...\n"; 96 { 97 if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL) 98 abort (); 99 } 100 101 std::cout << "Testing object_getInstanceVariable () ...\n"; 102 { 103 MySubClass *o = [[MySubClass alloc] init]; 104 id value; 105 106 [o setVariable: o]; 107 108 if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL) 109 abort (); 110 111 if (value != o) 112 abort (); 113 } 114 115 std::cout << "Testing object_getIvar () ...\n"; 116 { 117 MySubClass *o = [[MySubClass alloc] init]; 118 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar"); 119 120 [o setVariable: o]; 121 122 if (object_getIvar (o, ivar) != o) 123 abort (); 124 } 125 126 std::cout << "Testing object_setClass () ...\n"; 127 { 128 MySubClass *o = [[MySubClass alloc] init]; 129 130 object_setClass (o, objc_getClass ("MySubSubClass")); 131 132 if ([(MySubSubClass *)o test] != o) 133 abort (); 134 } 135 136 std::cout << "Testing object_setInstanceVariable () ...\n"; 137 { 138 MySubClass *o = [[MySubClass alloc] init]; 139 140 [o setVariable: nil]; 141 142 if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL) 143 abort (); 144 145 if ([o variable] != o) 146 abort (); 147 } 148 149 std::cout << "Testing object_setIvar () ...\n"; 150 { 151 MySubClass *o = [[MySubClass alloc] init]; 152 MySubClass *value = [[MySubClass alloc] init]; 153 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar"); 154 155 [o setVariable: o]; 156 157 object_setIvar (o, ivar, value); 158 159 if ([o variable] != value) 160 abort (); 161 } 162 163 return (0); 164} 165