OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [objc.dg/] [foreach-5.m] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 704 jeremybenn
/* Test basic Objective-C foreach syntax.  This tests that if you
2
   define your own NSFastEnumeration struct, the compiler picks it up.
3
*/
4
/* { dg-do run } */
5
/* { dg-skip-if "No NeXT fast enum. pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
6
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
7
/* { dg-options "-mno-constant-cfstrings" { target *-*-darwin* } } */
8
/* { dg-additional-sources "../objc-obj-c++-shared/nsconstantstring-class-impl.m" } */
9
 
10
#import "../objc-obj-c++-shared/TestsuiteObject.m"
11
#ifndef __NEXT_RUNTIME__
12
#include <objc/NXConstStr.h>
13
#else
14
#include "../objc-obj-c++-shared/nsconstantstring-class.h"
15
#endif
16
 
17
extern int printf (const char *, ...);
18
#include <stdlib.h>
19
 
20
typedef struct
21
{
22
  unsigned long state;
23
  id            *itemsPtr;
24
  unsigned long *mutationsPtr;
25
  unsigned long extra[5];
26
} NSFastEnumerationState;
27
 
28
/* A mini-array implementation that can be used to test fast
29
   enumeration.  You create the array with some objects; you can
30
   mutate the array, and you can fast-enumerate it.
31
*/
32
@interface MyArray : TestsuiteObject
33
{
34
  unsigned int length;
35
  id *objects;
36
  unsigned long mutated;
37
}
38
- (id) initWithLength: (unsigned int)l  objects: (id *)o;
39
- (void) mutate;
40
- (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState *)state
41
                                     objects:(id *)stackbuf
42
                                       count:(unsigned long)len;
43
@end
44
 
45
@implementation MyArray : TestsuiteObject
46
- (id) initWithLength: (unsigned int)l
47
              objects: (id *)o
48
{
49
  length = l;
50
  objects = o;
51
  mutated = 0;
52
  return self;
53
}
54
- (void) mutate
55
{
56
  mutated = 1;
57
}
58
- (unsigned long)countByEnumeratingWithState: (NSFastEnumerationState*)state
59
                                     objects: (id*)stackbuf
60
                                       count: (unsigned long)len
61
{
62
  unsigned long i, batch_size;
63
 
64
  /* We keep how many objects we served in the state->state counter.  So the next batch
65
     will contain up to length - state->state objects.  */
66
  batch_size = length - state->state;
67
 
68
  /* Make obvious adjustments.  */
69
  if (batch_size < 0)
70
    batch_size = 0;
71
 
72
  if (batch_size > len)
73
    batch_size = len;
74
 
75
  /* Copy the objects.  */
76
  for (i = 0; i < batch_size; i++)
77
    stackbuf[i] = objects[i];
78
 
79
  state->state += batch_size;
80
  state->itemsPtr = stackbuf;
81
  state->mutationsPtr = &mutated;
82
 
83
  return batch_size;
84
}
85
@end
86
 
87
int main (void)
88
{
89
  MyArray *array;
90
  int test_variable, counter, i;
91
  id *objects;
92
 
93
  array = [[MyArray alloc] initWithLength: 0
94
                           objects: NULL];
95
 
96
  /* Test that an empty array does nothing.  */
97
  for (id object in array)
98
    abort ();
99
 
100
  /* Test iterating over 1 object.  */
101
  objects = malloc (sizeof (id) * 1);
102
  objects[0] = @"One Object";
103
 
104
  array = [[MyArray alloc] initWithLength: 1
105
                           objects: objects];
106
 
107
  for (id object in array)
108
    printf ("%p\n", object);
109
 
110
  /* Test iterating over 20 objects.  */
111
  objects = malloc (sizeof (id) * 20);
112
  for (i = 0; i < 20; i++)
113
    objects[i] = @"object";
114
 
115
  array = [[MyArray alloc] initWithLength: 20
116
                           objects: objects];
117
 
118
  for (id object in array)
119
    printf ("%p\n", object);
120
 
121
  /* Test iterating over 200 objects.  */
122
  objects = malloc (sizeof (id) * 200);
123
  for (i = 0; i < 200; i++)
124
    objects[i] = @"object";
125
 
126
  array = [[MyArray alloc] initWithLength: 200
127
                           objects: objects];
128
 
129
  counter = 0;
130
  for (id object in array)
131
    {
132
      if (object != nil)
133
        counter++;
134
    }
135
 
136
  if (counter != 200)
137
    abort ();
138
 
139
  printf ("Counter was %d (should be 200)\n", counter);
140
 
141
  /* Test iterating again over the same array.  */
142
  counter = 0;
143
  for (id object in array)
144
    {
145
      if (object != nil)
146
        counter++;
147
    }
148
 
149
  if (counter != 200)
150
    abort ();
151
 
152
  printf ("Counter was %d (should be 200)\n", counter);
153
 
154
  /* Test nested iterations.  */
155
  objects = malloc (sizeof (id) * 20);
156
  for (i = 0; i < 20; i++)
157
    objects[i] = @"object";
158
 
159
  array = [[MyArray alloc] initWithLength: 20
160
                           objects: objects];
161
  counter = 0;
162
  for (id object in array)
163
    {
164
      for (id another_object in array)
165
        if (another_object != nil)
166
          counter++;
167
    }
168
 
169
  printf ("Counter was %d (should be 400)\n", counter);
170
 
171
  if (counter != 400)
172
    abort ();
173
 
174
  /* Test 'continue'.  */
175
  objects = malloc (sizeof (id) * 20);
176
  for (i = 0; i < 20; i++)
177
    objects[i] = @"object";
178
 
179
  array = [[MyArray alloc] initWithLength: 20
180
                           objects: objects];
181
  counter = 0;
182
  for (id object in array)
183
    {
184
      if (counter == 15)
185
        continue;
186
 
187
      counter++;
188
    }
189
 
190
  printf ("Counter was %d (should be 15)\n", counter);
191
 
192
  if (counter != 15)
193
    abort ();
194
 
195
  /* Test 'break'.  */
196
  objects = malloc (sizeof (id) * 20);
197
  for (i = 0; i < 20; i++)
198
    objects[i] = @"object";
199
 
200
  array = [[MyArray alloc] initWithLength: 20
201
                           objects: objects];
202
  counter = 0;
203
  for (id object in array)
204
    {
205
      counter++;
206
 
207
      if (counter == 15)
208
        break;
209
    }
210
 
211
  printf ("Counter was %d (should be 15)\n", counter);
212
 
213
  if (counter != 15)
214
    abort ();
215
 
216
  /* Test 'break' and 'continue' in nested iterations.  */
217
  objects = malloc (sizeof (id) * 20);
218
  for (i = 0; i < 20; i++)
219
    objects[i] = @"object";
220
 
221
  array = [[MyArray alloc] initWithLength: 20
222
                           objects: objects];
223
  counter = 0;
224
  for (id object in array)
225
    {
226
      int local_counter = 0;
227
 
228
      /* Each internal loop should increase counter by 24.  */
229
      for (id another_object in array)
230
        {
231
          local_counter++;
232
 
233
          if (local_counter == 10)
234
            {
235
              counter = counter + 20;
236
              break;
237
            }
238
 
239
          if (local_counter >= 5)
240
            continue;
241
 
242
          counter++;
243
        }
244
 
245
      /* Exit after 4 iterations.  */
246
      if (counter == 96)
247
        break;
248
    }
249
 
250
  printf ("Counter was %d (should be 96)\n", counter);
251
 
252
  if (counter != 96)
253
    abort ();
254
 
255
  /* Test that C for loops still work.  */
256
  test_variable = 0;
257
 
258
  for (counter = 0; counter < 4; counter++)
259
    test_variable++;
260
 
261
  if (test_variable != 4)
262
    abort ();
263
 
264
  return 0;
265
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.