summaryrefslogtreecommitdiff
path: root/gcc/testsuite/objc.dg/property/property-encoding-1.m
blob: dc12c3137b93f2de2ec816b1b22ebbf55fd43e0b (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Contributed by Nicola Pero <nicola.pero@meta-innovation.com>, March 2011.  */
/* Test encoding properties.  */
/* { dg-do run } */
/* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */

#include <objc/runtime.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

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

@interface MySubClass : MyRootClass
{
  char char_property;
  short short_property;
  int int_property;
  long long_property;
  float float_property;
  double double_property;
  int *int_pointer_property;

  id propertyA;
  id propertyB;
  id propertyC;
  id propertyD;
  int propertyE;
  id propertyF;

  id other_variable;
}
@property char char_property;
@property short short_property;
@property int int_property;
@property long long_property;
@property float float_property;
@property double double_property;
@property int *int_pointer_property;

@property (assign, getter=getP, setter=setP:) id propertyA;
@property (assign) id propertyB;
@property (copy) id propertyC;
@property (retain) id propertyD;
@property (nonatomic) int propertyE;
@property (nonatomic, readonly, copy) id propertyF;

@property (assign) id propertyG;
@property (assign, readonly, getter=X) id propertyH;
@end

@implementation MySubClass
@synthesize char_property;
@synthesize short_property;
@synthesize int_property;
@synthesize long_property;
@synthesize float_property;
@synthesize double_property;
@synthesize int_pointer_property;

@synthesize propertyA;
@synthesize propertyB;
@synthesize propertyC;
@synthesize propertyD;
@synthesize propertyE;
@synthesize propertyF;

@synthesize propertyG = other_variable;
@dynamic propertyH;
@end

#ifdef __OBJC2__
void error (objc_property_t p)
{
  printf ("Error - property_getAttributes (\"%s\") returns \"%s\"\n",
	  property_getName (p),
	  property_getAttributes (p));
  abort ();
}

/* Concatenate 3 strings and return the result.  */
char *concat (const char *a, const char *b, const char *c)
{
  /* We happily leak memory here.  This is a test.  */
  char *x = (char *)malloc (sizeof (char) * 128);
  snprintf (x, 128, "%s%s%s", a, b, c);
  return x;
}

#endif

int main (void)
{
#ifdef __OBJC2__
  Class c = objc_getClass ("MySubClass");
  objc_property_t p;

  p = class_getProperty (c, "char_property");
  /* Usually we expect "Tc,Vchar_property", but if a char is of
     different size, it may be encoded differently than "c".  */
  if (strcmp (concat ("T", @encode (char), ",Vchar_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "short_property");
  if (strcmp (concat ("T", @encode (short), ",Vshort_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "int_property");
  if (strcmp (concat ("T", @encode (int), ",Vint_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "long_property");
  if (strcmp (concat ("T", @encode (long), ",Vlong_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "float_property");
  if (strcmp (concat ("T", @encode (float), ",Vfloat_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "double_property");
  if (strcmp (concat ("T", @encode (double), ",Vdouble_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "int_pointer_property");
  if (strcmp (concat ("T", @encode (int *), ",Vint_pointer_property"),
	      property_getAttributes (p)) != 0)
    error (p);

  /* Objects are always encoded as '@' hence the string does not
     depend on the architecture.  */
  p = class_getProperty (c, "propertyA");
  if (strcmp ("T@,GgetP,SsetP:,VpropertyA", property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyB");
  if (strcmp ("T@,VpropertyB", property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyC");
  if (strcmp ("T@,C,VpropertyC", property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyD");
  if (strcmp ("T@,&,VpropertyD", property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyE");
  if (strcmp (concat ("T", @encode (int), ",N,VpropertyE"),
	      property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyF");
  if (strcmp ("T@,R,C,N,VpropertyF", property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyG");
  if (strcmp ("T@,Vother_variable", property_getAttributes (p)) != 0)
    error (p);

  p = class_getProperty (c, "propertyH");
  if (strcmp ("T@,R,D,GX", property_getAttributes (p)) != 0)
    error (p);
#endif

  return 0;
}