OSDN Git Service

In gcc/objc/:
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-sel.mm
1 /* Test the Modern GNU Objective-C Runtime API.
2
3   This is test 'sel', covering all functions starting with 'sel'.  */
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 <iostream>
13 #include <cstring>
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 ()
47 {
48   /* Functions are tested in alphabetical order.  */
49
50   std::cout << "Testing sel_getName () ...\n";
51   {
52     if (std::strcmp (sel_getName (@selector (variable)), "variable") != 0)
53       abort ();
54
55     if (std::strcmp (sel_getName (NULL), "<null selector>") != 0)
56       abort ();
57   }
58
59   std::cout << "Testing sel_getType () ...\n";
60   {
61     /* Get a selector from a real class, so it has interesting
62        types.  */
63     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
64                                              @selector (variable));
65     
66     if (std::strcmp (sel_getType (method_getName (method)), method_getTypeEncoding (method)) != 0)
67       abort ();
68   }
69
70   std::cout << "Testing sel_getUid () ...\n";
71   {
72     if (std::strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
73       abort ();
74   }
75
76   std::cout << "Testing sel_isEqual () ...\n";
77   {
78     if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
79       abort ();
80   }
81   
82   std::cout << "Testing sel_registerName () ...\n";
83   {
84     if (std::strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
85       abort ();
86   }
87
88   std::cout << "Testing set_registerTypedName () ...\n";
89   {
90     const char *types = method_getTypeEncoding (class_getInstanceMethod 
91                                                 (objc_getClass ("MySubClass"),
92                                                  @selector (variable)));
93     SEL selector = sel_registerTypedName ("aMethod", types);
94             
95     if (std::strcmp (sel_getName (selector), "aMethod") != 0)
96       abort ();
97
98     if (std::strcmp (sel_getType (selector), types) != 0)
99       abort ();
100   }
101
102   return (0);
103 }