File: basic.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (169 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (2)
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
#import <Foundation/Foundation.h>
#import "ObjectTesting.h"
#import "../../../Source/GSFastEnumeration.h"

static SEL	add;
static SEL	set;
static SEL	key;

@implementation NSPointerArray (TestHelpers)
- (void) addObject: (id)anObject
{
  [self addPointer: anObject];
}
- (void) removeObject: (id)anObject
{
  int	i = [self count];

  while (i-- > 0)
    {
      if ([self pointerAtIndex: i] == (void*)anObject)
	{
	  [self removePointerAtIndex: i];
	}
    }
}
@end

void fast_enumeration_mutation_add(id mutableCollection)
{
  NSUInteger	i = 0;
  NSUInteger	c = [mutableCollection count]/2;

  FOR_IN(id, o, mutableCollection)
  if (i == c)
    {
      if ([mutableCollection respondsToSelector: set])
	{
	  [mutableCollection setObject: @"boom" forKey: @"boom"];
	}
      else
	{
	  [mutableCollection addObject: @"boom"];
	}
    }
  i++;
  END_FOR_IN(mutableCollection)
}

void fast_enumeration_mutation_remove(id mutableCollection)
{
  NSUInteger 	i = 0;
  NSUInteger	c = [mutableCollection count]/2;

  FOR_IN(id, o, mutableCollection)
  if (i == c)
    {
      if ([mutableCollection respondsToSelector: key])
        {
	  [mutableCollection removeObjectForKey: o];
	}
      else
	{
	  [mutableCollection removeObject: o];
	}
    }
  i++;
  END_FOR_IN(mutableCollection)
}

void test_fast_enumeration(id collection, NSArray *objects)
{
  NSMutableArray *returnedObjects = [NSMutableArray array];

  FOR_IN(id, o, collection)
  [returnedObjects addObject: o];
  END_FOR_IN(collection)
  if (!([collection isKindOfClass: [NSArray class]]
    || [collection isKindOfClass: [NSOrderedSet class]]))
    {
      [returnedObjects sortUsingSelector: @selector(compare:)];
    }
  PASS_EQUAL(returnedObjects, objects, "fast enumeration returns all objects")

  id mutableCollection;
  if ([collection respondsToSelector: @selector(mutableCopyWithZone:)])
    {
      mutableCollection = AUTORELEASE([collection mutableCopy]);
    }
  else if ([collection respondsToSelector: add]
    || [collection respondsToSelector: set])
    {
      mutableCollection = collection;	// It has a method to mutate it
    }
  else
    {
      return;				// No mutable version
    }
  PASS_EXCEPTION(
    fast_enumeration_mutation_add(mutableCollection),
    NSGenericException,
    "Fast enumeration mutation add properly calls @\"NSGenericException\"")
  PASS_EXCEPTION(
    fast_enumeration_mutation_remove(mutableCollection),
    NSGenericException,
    "Fast enumeration mutation remove properly calls @\"NSGenericException\"")
}

int main()
{
  NSAutoreleasePool	*arp = [NSAutoreleasePool new];
  NSMutableArray 	*objects = [NSMutableArray array];
  int 			i;

  add = @selector(addObject:);
  set = @selector(setObject:forKey:);
  key = @selector(removeObjectForKey:);

  for (i = 0; i < 1000; i++)
    {
      [objects addObject: [NSString stringWithFormat: @"%.4d", i]];
    }
  
  START_SET("NSArray")
  id array = [NSArray arrayWithArray: objects];
  test_fast_enumeration(array, objects);
  END_SET("NSArray")
  
  START_SET("NSSet")
  id set = [NSSet setWithArray: objects];
  test_fast_enumeration(set, objects);
  END_SET("NSSet")
  
  START_SET("NSOrderedSet")
  id orderedSet = [NSOrderedSet orderedSetWithArray: objects];
  test_fast_enumeration(orderedSet, objects);
  END_SET("NSOrderedSet")
  
  START_SET("NSDictionary")
  id dict = [NSDictionary dictionaryWithObjects: objects forKeys: objects];
  test_fast_enumeration(dict, objects);
  END_SET("NSDictionary")
  
  START_SET("NSMapTable")
  id map = [NSMapTable strongToStrongObjectsMapTable];
  FOR_IN(id, o, objects)
  [map setObject: o forKey: o];
  END_FOR_IN(objects)
  test_fast_enumeration(map, objects);
  END_SET("NSMapTable")
  
  START_SET("NSHashTable")
  id table = [NSHashTable weakObjectsHashTable];
  FOR_IN(id, o, objects)
  [table addObject: o];
  END_FOR_IN(objects)
  test_fast_enumeration(table, objects);
  END_SET("NSHashTable")
  
  START_SET("NSPointerArray")
  id array = [NSPointerArray weakObjectsPointerArray];
  FOR_IN(id, o, objects)
  [array addPointer: o];
  END_FOR_IN(objects)
  test_fast_enumeration(array, objects);
  END_SET("NSPointerArray")
  
  [arp release]; arp = nil;
  return 0;
}