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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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