summaryrefslogtreecommitdiff
path: root/gcc/testsuite/obj-c++.dg/gnu-api-2-object.mm
blob: a2702d64707c8717bd45a153af6e63b0c40a87d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Test the Modern GNU Objective-C Runtime API.

  This is test 'object', covering all functions starting with 'object'.  */

/* { dg-do run } */
/* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */

/* To get the modern GNU Objective-C Runtime API, you include
   objc/runtime.h.  */
#include <objc/runtime.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>

@interface MyRootClass
{ Class isa; }
+ alloc;
- init;
+ initialize;
@end

@implementation MyRootClass
+ alloc { return class_createInstance (self, 0); }
- init  { return self; }
+ initialize { return self; }
@end

@protocol MyProtocol
- (id) variable;
@end

@protocol MySecondProtocol
- (id) setVariable: (id)value;
@end

@interface MySubClass : MyRootClass <MyProtocol>
{ id variable_ivar; }
- (void) setVariable: (id)value;
- (id) variable;
@end

@implementation MySubClass
- (void) setVariable: (id)value { variable_ivar = value; }
- (id) variable { return variable_ivar; }
@end

@interface MySubSubClass : MySubClass
- (id) test;
@end

@implementation MySubSubClass
- (id) test { return self; }
@end



int main ()
{
  /* Functions are tested in alphabetical order.  */
  
  std::cout << "Testing object_copy () ...\n";
  {
    MySubClass *object_a = [[MySubClass alloc] init];
    MySubClass *object_b = object_copy (object_a, 0);

    [object_b setVariable: object_a];
    if ([object_b variable] != object_a)
      abort ();
  }

  std::cout << "Testing object_dispose () ...\n";
  {
    MySubClass *object = [[MySubClass alloc] init];

    object_dispose (object);
  }

  std::cout << "Testing object_getClass () ...\n";
  {
    MyRootClass *o = [[MySubClass alloc] init];

    if (object_getClass (o) != objc_getClass ("MySubClass"))
      abort ();
  }

  std::cout << "Testing object_getClassName () ...\n";
  {
    MyRootClass *o = [[MyRootClass alloc] init];

    if (std::strcmp (object_getClassName (o), "MyRootClass") != 0)
      abort ();
  }

  std::cout << "Testing object_getIndexedIvars () ...\n";
  {
    if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
      abort ();
  }
  
  std::cout << "Testing object_getInstanceVariable () ...\n";
  {
    MySubClass *o = [[MySubClass alloc] init];
    id value;

    [o setVariable: o];

    if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
      abort ();

    if (value != o)
      abort ();
  }

  std::cout << "Testing object_getIvar () ...\n";
  {
    MySubClass *o = [[MySubClass alloc] init];
    Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");

    [o setVariable: o];

    if (object_getIvar (o, ivar) != o)
      abort ();
  }

  std::cout << "Testing object_setClass () ...\n";
  {
    MySubClass *o = [[MySubClass alloc] init];

    object_setClass (o, objc_getClass ("MySubSubClass"));

    if ([(MySubSubClass *)o test] != o)
      abort ();
  }

  std::cout << "Testing object_setInstanceVariable () ...\n";
  {
    MySubClass *o = [[MySubClass alloc] init];
    
    [o setVariable: nil];

    if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
      abort ();

    if ([o variable] != o)
      abort ();
  }

  std::cout << "Testing object_setIvar () ...\n";
  {
    MySubClass *o = [[MySubClass alloc] init];
    MySubClass *value = [[MySubClass alloc] init];
    Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
    
    [o setVariable: o];

    object_setIvar (o, ivar, value);

    if ([o variable] != value)
      abort ();
  }  

  return (0);
}