| 1 |
/* PR objc/103639 */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-skip-if "No NeXT fast enum. pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */ |
| 4 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 5 |
/* { dg-additional-sources "../objc-obj-c++-shared/nsconstantstring-class-impl.m" } */ |
| 6 |
/* { dg-additional-options "-mno-constant-cfstrings" { target *-*-darwin* } } */ |
| 7 |
/* { dg-additional-options "-Wno-objc-root-class" } */ |
| 8 |
|
| 9 |
#import "../objc-obj-c++-shared/TestsuiteObject.m" |
| 10 |
#ifndef __NEXT_RUNTIME__ |
| 11 |
#include <objc/NXConstStr.h> |
| 12 |
#else |
| 13 |
#include "../objc-obj-c++-shared/nsconstantstring-class.h" |
| 14 |
#endif |
| 15 |
|
| 16 |
extern int printf (const char *, ...); |
| 17 |
#include <stdlib.h> |
| 18 |
|
| 19 |
/* A mini-array implementation that can be used to test fast |
| 20 |
enumeration. You create the array with some objects; you can |
| 21 |
mutate the array, and you can fast-enumerate it. |
| 22 |
*/ |
| 23 |
@interface MyArray : TestsuiteObject |
| 24 |
{ |
| 25 |
unsigned int length; |
| 26 |
id *objects; |
| 27 |
unsigned long mutated; |
| 28 |
} |
| 29 |
- (id) initWithLength: (unsigned int)l objects: (id *)o; |
| 30 |
- (void) mutate; |
| 31 |
- (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state |
| 32 |
objects:(id *)stackbuf |
| 33 |
count:(unsigned long)len; |
| 34 |
@end |
| 35 |
|
| 36 |
@implementation MyArray : TestsuiteObject |
| 37 |
- (id) initWithLength: (unsigned int)l |
| 38 |
objects: (id *)o |
| 39 |
{ |
| 40 |
length = l; |
| 41 |
objects = o; |
| 42 |
mutated = 0; |
| 43 |
return self; |
| 44 |
} |
| 45 |
- (void) mutate |
| 46 |
{ |
| 47 |
mutated = 1; |
| 48 |
} |
| 49 |
- (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state |
| 50 |
objects: (id*)stackbuf |
| 51 |
count: (unsigned long)len |
| 52 |
{ |
| 53 |
unsigned long i, batch_size; |
| 54 |
|
| 55 |
/* We keep how many objects we served in the state->state counter. So the next batch |
| 56 |
will contain up to length - state->state objects. */ |
| 57 |
batch_size = length - state->state; |
| 58 |
|
| 59 |
/* Make obvious adjustments. */ |
| 60 |
if (batch_size < 0) |
| 61 |
batch_size = 0; |
| 62 |
|
| 63 |
if (batch_size > len) |
| 64 |
batch_size = len; |
| 65 |
|
| 66 |
/* Copy the objects. */ |
| 67 |
for (i = 0; i < batch_size; i++) |
| 68 |
stackbuf[i] = objects[i]; |
| 69 |
|
| 70 |
state->state += batch_size; |
| 71 |
state->itemsPtr = stackbuf; |
| 72 |
state->mutationsPtr = &mutated; |
| 73 |
|
| 74 |
return batch_size; |
| 75 |
} |
| 76 |
@end |
| 77 |
|
| 78 |
int check = 0; |
| 79 |
|
| 80 |
int |
| 81 |
main() |
| 82 |
{ |
| 83 |
id *objects = malloc (sizeof (id) * 2); |
| 84 |
objects[0] = @"a"; |
| 85 |
objects[1] = @"b"; |
| 86 |
|
| 87 |
MyArray *array = [[MyArray alloc] initWithLength: 2 objects: objects]; |
| 88 |
|
| 89 |
int someVar = 0; |
| 90 |
for (id object in array) { |
| 91 |
switch (someVar) { |
| 92 |
case 0: |
| 93 |
break; |
| 94 |
} |
| 95 |
++check; |
| 96 |
} |
| 97 |
|
| 98 |
if (check != 2) |
| 99 |
abort (); |
| 100 |
return 0; |
| 101 |
} |