OSDN Git Service

In libobjc/:
[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 - (void) method;
39 @end
40
41 @implementation MySubClass
42 - (void) setVariable: (id)value { variable_ivar = value; }
43 - (id) variable { return variable_ivar; }
44 - (void) method { return; }
45 @end
46
47
48 int main ()
49 {
50   /* Functions are tested in alphabetical order.  */
51
52   std::cout << "Testing sel_copyTypedSelectorList ()...\n";
53   {
54     unsigned int count;
55     SEL * list = sel_copyTypedSelectorList ("method", &count);
56
57     /* There should only be two, since 'method' is referenced twice,
58        once with types and once without (in this very test).  */
59     if (count != 2)
60       abort ();
61
62     /* Check that both selectors are not-NULL, and have the correct
63        name.  We use @selector() here, which wouldn't really be
64        needed, just to register a second, untyped selector with name
65        'method'.  */
66     if (std::strcmp (sel_getName (list[0]), sel_getName (@selector (method))) != 0)
67       abort ();
68
69     if (std::strcmp (sel_getName (list[1]), sel_getName (@selector (method))) != 0)
70       abort ();
71     
72     if (list[2] != NULL)
73       abort ();
74   }
75
76   std::cout << "Testing sel_getName () ...\n";
77   {
78     if (std::strcmp (sel_getName (@selector (variable)), "variable") != 0)
79       abort ();
80
81     if (std::strcmp (sel_getName (NULL), "<null selector>") != 0)
82       abort ();
83   }
84
85   std::cout << "Testing sel_getTypeEncoding () ...\n";
86   {
87     /* Get a selector from a real class, so it has interesting
88        types.  */
89     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
90                                              @selector (variable));
91     
92     if (std::strcmp (sel_getTypeEncoding (method_getName (method)),
93                 method_getTypeEncoding (method)) != 0)
94       abort ();
95
96     if (sel_getTypeEncoding (NULL) != NULL)
97       abort ();
98   }
99
100   std::cout << "Testing sel_getTypedSelector () ...\n";
101   {
102     /* First try with a selector where we know that a typed one has
103        been registered.  */
104     SEL selector = sel_getTypedSelector ("variable");
105
106     if (selector == NULL)
107       abort ();
108
109     if (sel_getTypeEncoding (selector) == NULL)
110       abort ();
111
112     /* Now try a selector which was never registered.  */
113     selector = sel_getTypedSelector ("not_registered");
114
115     if (selector != NULL)
116       abort ();
117
118     /* Now try registering a selector with no types.  The following
119        line is just a way to have an unused '@selector()' expression
120        without the compiler complaining.  */
121     if (@selector (registered_with_no_types) == NULL)
122       abort ();
123
124     /* Try getting it.  Nothing should be returned because it is
125        untyped.  */
126     selector = sel_getTypedSelector ("registered_with_no_types");
127
128     if (selector != NULL)
129       abort ();
130   }
131
132   std::cout << "Testing sel_getUid () ...\n";
133   {
134     if (std::strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
135       abort ();
136   }
137
138   std::cout << "Testing sel_isEqual () ...\n";
139   {
140     if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
141       abort ();
142   }
143   
144   std::cout << "Testing sel_registerName () ...\n";
145   {
146     if (std::strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
147       abort ();
148   }
149
150   std::cout << "Testing set_registerTypedName () ...\n";
151   {
152     const char *types = method_getTypeEncoding (class_getInstanceMethod 
153                                                 (objc_getClass ("MySubClass"),
154                                                  @selector (variable)));
155     SEL selector = sel_registerTypedName ("aMethod", types);
156     
157     if (std::strcmp (sel_getName (selector), "aMethod") != 0)
158       abort ();
159
160     if (std::strcmp (sel_getTypeEncoding (selector), types) != 0)
161       abort ();
162   }
163
164   return (0);
165 }