In gcc/testsuite/: 2011-03-04 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / testsuite / objc.dg / property / property-encoding-1.m
blob64b35e7690e7506673f41de7e8e2d8a11fdcc9c5
1 /* Contributed by Nicola Pero <nicola.pero@meta-innovation.com>, March 2011.  */
2 /* Test encoding properties.  */
3 /* { dg-do run } */
4 /* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
6 #include <objc/runtime.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
11 @interface MyRootClass
12 { Class isa; }
13 + alloc;
14 - init;
15 + initialize;
16 @end
18 @implementation MyRootClass
19 + alloc { return class_createInstance (self, 0); }
20 - init  { return self; }
21 + initialize { return self; }
22 @end
24 @interface MySubClass : MyRootClass
26   char char_property;
27   short short_property;
28   int int_property;
29   long long_property;
30   float float_property;
31   double double_property;
32   int *int_pointer_property;
34   id propertyA;
35   id propertyB;
36   id propertyC;
37   id propertyD;
38   int propertyE;
39   id propertyF;
41   id other_variable;
43 @property char char_property;
44 @property short short_property;
45 @property int int_property;
46 @property long long_property;
47 @property float float_property;
48 @property double double_property;
49 @property int *int_pointer_property;
51 @property (assign, getter=getP, setter=setP:) id propertyA;
52 @property (assign) id propertyB;
53 @property (copy) id propertyC;
54 @property (retain) id propertyD;
55 @property (nonatomic) int propertyE;
56 @property (nonatomic, readonly, copy) id propertyF;
58 @property (assign) id propertyG;
59 @property (assign, readonly, getter=X) id propertyH;
60 @end
62 @implementation MySubClass
63 @synthesize char_property;
64 @synthesize short_property;
65 @synthesize int_property;
66 @synthesize long_property;
67 @synthesize float_property;
68 @synthesize double_property;
69 @synthesize int_pointer_property;
71 @synthesize propertyA;
72 @synthesize propertyB;
73 @synthesize propertyC;
74 @synthesize propertyD;
75 @synthesize propertyE;
76 @synthesize propertyF;
78 @synthesize propertyG = other_variable;
79 @dynamic propertyH;
80 @end
82 #ifdef __OBJC2__
83 void error (objc_property_t p)
85   printf ("Error - property_getAttributes (\"%s\") returns \"%s\"\n",
86           property_getName (p),
87           property_getAttributes (p));
88   abort ();
91 /* Concatenate 3 strings and return the result.  */
92 char *concat (char *a, char *b, char *c)
94   /* We happily leak memory here.  This is a test.  */
95   char *x = malloc (sizeof (char) * 128);
96   snprintf (x, 128, "%s%s%s", a, b, c);
97   return x;
100 #endif
102 int main(int argc, void **args)
104 #ifdef __OBJC2__
105   Class c = objc_getClass ("MySubClass");
106   objc_property_t p;
107   const char *expected_result;
109   p = class_getProperty (c, "char_property");
110   /* Usually we expect "Tc,Vchar_property", but if a char is of
111      different size, it may be encoded differently than "c".  */
112   if (strcmp (concat ("T", @encode (char), ",Vchar_property"),
113               property_getAttributes (p)) != 0)
114     error (p);
116   p = class_getProperty (c, "short_property");
117   if (strcmp (concat ("T", @encode (short), ",Vshort_property"),
118               property_getAttributes (p)) != 0)
119     error (p);
121   p = class_getProperty (c, "int_property");
122   if (strcmp (concat ("T", @encode (int), ",Vint_property"),
123               property_getAttributes (p)) != 0)
124     error (p);
126   p = class_getProperty (c, "long_property");
127   if (strcmp (concat ("T", @encode (long), ",Vlong_property"),
128               property_getAttributes (p)) != 0)
129     error (p);
131   p = class_getProperty (c, "float_property");
132   if (strcmp (concat ("T", @encode (float), ",Vfloat_property"),
133               property_getAttributes (p)) != 0)
134     error (p);
136   p = class_getProperty (c, "double_property");
137   if (strcmp (concat ("T", @encode (double), ",Vdouble_property"),
138               property_getAttributes (p)) != 0)
139     error (p);
141   p = class_getProperty (c, "int_pointer_property");
142   if (strcmp (concat ("T", @encode (int *), ",Vint_pointer_property"),
143               property_getAttributes (p)) != 0)
144     error (p);
146   /* Objects are always encoded as '@' hence the string does not
147      depend on the architecture.  */
148   p = class_getProperty (c, "propertyA");
149   if (strcmp ("T@,GgetP,SsetP:,VpropertyA", property_getAttributes (p)) != 0)
150     error (p);
152   p = class_getProperty (c, "propertyB");
153   if (strcmp ("T@,VpropertyB", property_getAttributes (p)) != 0)
154     error (p);
156   p = class_getProperty (c, "propertyC");
157   if (strcmp ("T@,C,VpropertyC", property_getAttributes (p)) != 0)
158     error (p);
160   p = class_getProperty (c, "propertyD");
161   if (strcmp ("T@,&,VpropertyD", property_getAttributes (p)) != 0)
162     error (p);
164   p = class_getProperty (c, "propertyE");
165   if (strcmp (concat ("T", @encode (int), ",N,VpropertyE"),
166               property_getAttributes (p)) != 0)
167     error (p);
169   p = class_getProperty (c, "propertyF");
170   if (strcmp ("T@,R,C,N,VpropertyF", property_getAttributes (p)) != 0)
171     error (p);
173   p = class_getProperty (c, "propertyG");
174   if (strcmp ("T@,Vother_variable", property_getAttributes (p)) != 0)
175     error (p);
177   p = class_getProperty (c, "propertyH");
178   if (strcmp ("T@,R,D,GX", property_getAttributes (p)) != 0)
179     error (p);
180 #endif
182   return 0;