OSDN Git Service

2010-11-05 Steve Ellcey <sje@cup.hp.com>
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / gnu-api-2-ivar.m
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 "" { *-*-* } { "-fnext-runtime" } { "" } } */
7
8 /* To get the modern GNU Objective-C Runtime API, you include
9    objc/runtime.h.  */
10 #include <objc/runtime.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 @interface MyRootClass
16 { Class isa; }
17 + alloc;
18 - init;
19 @end
20
21 @implementation MyRootClass
22 + alloc { return class_createInstance (self, 0); }
23 - init  { return self; }
24 @end
25
26 @protocol MyProtocol
27 - (id) variable;
28 @end
29
30 @protocol MySecondProtocol
31 - (id) setVariable: (id)value;
32 @end
33
34 @interface MySubClass : MyRootClass <MyProtocol>
35 { id variable_ivar; }
36 - (void) setVariable: (id)value;
37 - (id) variable;
38 @end
39
40 @implementation MySubClass
41 - (void) setVariable: (id)value { variable_ivar = value; }
42 - (id) variable { return variable_ivar; }
43 @end
44
45
46 int main(int argc, void **args)
47 {
48   /* Functions are tested in alphabetical order.  */
49
50   printf ("Testing ivar_getName () ...\n");
51   {
52     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"),
53                                            "variable_ivar");
54    if (strcmp (ivar_getName (ivar), "variable_ivar") != 0)
55       abort ();
56
57    ivar = class_getInstanceVariable (objc_getClass ("MySubClass"),
58                                      "variable");
59    if (ivar != 0)
60       abort ();
61   }
62
63   printf ("Testing ivar_getOffset () ...\n");
64   {
65     Ivar ivar = class_getInstanceVariable (objc_getClass ("MyRootClass"),
66                                            "isa");
67     if (ivar_getOffset (ivar) != 0)
68       abort ();
69   }
70
71   printf ("Testing ivar_getTypeEncoding () ...\n");
72   {
73     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"),
74                                            "variable_ivar");
75     if (strcmp (ivar_getTypeEncoding (ivar), "@") != 0)
76       abort ();
77   }
78
79   return 0;
80 }