1
/* Test/example program for the base library
3
Copyright (C) 2005 Free Software Foundation, Inc.
5
Copying and distribution of this file, with or without modification,
6
are permitted in any medium without royalty provided the copyright
7
notice and this notice are preserved.
9
This file is part of the GNUstep Base Library.
11
#include <Foundation/NSAutoreleasePool.h>
12
#include <Foundation/NSArray.h>
13
#include <Foundation/NSEnumerator.h>
14
#include <Foundation/NSValue.h>
15
#include <Foundation/NSString.h>
16
#include <Foundation/NSException.h>
18
int compare(id elem1, id elem2, void* context)
20
return (int)[elem1 performSelector:@selector(compare:) withObject:elem2];
26
id a, b, c, d, e, f, g, h; /* arrays */
29
id s = @"Hello World\n";
34
//behavior_set_debug(0);
36
[NSAutoreleasePool enableDoubleReleaseCheck:YES];
37
pool = [[NSAutoreleasePool alloc] init];
39
o1 = [NSNumber numberWithInt:1];
40
o2 = [NSNumber numberWithInt:2];
41
o3 = [NSNumber numberWithInt:3];
42
a = [[[NSArray arrayWithObject:o1] arrayByAddingObject:o2] arrayByAddingObject:o3];
43
printf("%u,%u,%u\n", [o1 retainCount], [o2 retainCount], [o3 retainCount]);
44
b = [[a copy] autorelease];
45
printf("%u,%u,%u\n", [o1 retainCount], [o2 retainCount], [o3 retainCount]);
46
c = [[b mutableCopy] autorelease];
47
printf("%u,%u,%u\n", [o1 retainCount], [o2 retainCount], [o3 retainCount]);
48
d = [[c copy] autorelease];
49
printf("%u,%u,%u\n", [o1 retainCount], [o2 retainCount], [o3 retainCount]);
53
// Class methods for allocating and initializing an array
54
printf("Method: +array\n");
57
printf("Empty array count is zero\n");
59
printf("Error: empty array count is not zero\n");
61
printf("Method: +arrayWithObject:\n");
62
b = [NSArray arrayWithObject: s];
63
printf("NSArray has count %d\n", [b count]);
65
printf("Error: count != 1\n");
67
printf("Method: +arrayWithObjects:...\n");
68
c = [NSArray arrayWithObjects:
71
[NSMutableArray class],
73
printf("NSArray has count %d\n", [c count]);
75
printf("Error: count != 3\n");
79
// Instance methods for allocating and initializing an array
80
printf("Method: -arrayByAddingObject:\n");
81
d = [c arrayByAddingObject: s];
82
printf("NSArray has count %d\n", [c count]);
84
printf("Error: count != 4\n");
86
printf("Method: -arrayByAddingObjectsFromArray:\n");
87
e = [c arrayByAddingObjectsFromArray: b];
88
printf("NSArray has count %d\n", [c count]);
90
printf("Error: count != 4\n");
95
NSCParameterAssert([c containsObject:[NSObject class]]);
97
p = [e indexOfObject:@"Hello World\n"];
99
printf("Error: index of object not found\n");
101
printf("Index of object is %d\n", p);
103
p = [e indexOfObjectIdenticalTo:s];
105
printf("Error: index of identical object not found\n");
107
printf("Index of identical object is %d\n", p);
109
NSCParameterAssert([c lastObject]);
110
printf("Class at index 2 is %s\n", [[[c objectAtIndex:2] description] cString]);
112
printf("Forward enumeration\n");
113
enumerator = [e objectEnumerator];
114
while ((i = [enumerator nextObject]))
115
printf("%s ", [[i description] cString]);
118
printf("Reverse enumeration\n");
119
enumerator = [e reverseObjectEnumerator];
120
while ((i = [enumerator nextObject]))
121
printf("%s ", [[i description] cString]);
126
// Sending messages to elements
127
[c makeObjectsPerform:@selector(description)];
129
//[c makeObjectsPerform:@selector(isEqual:) withObject: @"NSArray"];
134
NSCParameterAssert([d firstObjectCommonWithArray:e]);
136
if ([d isEqualToArray: d])
137
printf("NSArray is equal to itself\n");
139
printf("Error: NSArray is not equal to itself\n");
141
if ([d isEqualToArray: e])
142
printf("NSArrays are equal\n");
144
printf("Error: NSArrays are not equal\n");
148
// Deriving new arrays
149
NSRange r = NSMakeRange(0, 3);
151
f = [NSMutableArray array];
152
[f addObject: @"Lions"];
153
[f addObject: @"Tigers"];
154
[f addObject: @"Bears"];
155
[f addObject: @"Penguins"];
156
[f addObject: @"Giraffes"];
158
enumerator = [f objectEnumerator];
159
while ((i = [enumerator nextObject]))
160
printf("%s ", [i cString]);
163
printf("Method: -sortedArrayUsingSelector:\n");
164
g = [f sortedArrayUsingSelector: @selector(compare:)];
165
printf("Method: -sortedArrayUsingFunction:context:\n");
166
h = [f sortedArrayUsingFunction: compare context: NULL];
168
enumerator = [g objectEnumerator];
169
while ((i = [enumerator nextObject]))
170
printf("%s ", [i cString]);
173
if (([g isEqualToArray: h]) && (![g isEqualToArray: f]))
174
printf("Sorted arrays are correct\n");
176
printf("Error: Sorted arrays are not correct\n");
178
printf("Method: -subarrayWithRange:\n");
179
f = [e subarrayWithRange: r];
181
printf("NSArray has count %d\n", [f count]);
183
printf("Error: count != 3\n");
185
enumerator = [f objectEnumerator];
186
while ((i = [enumerator nextObject]))
187
printf("%s ", [[i description] cString]);
190
if ([f isEqualToArray: c])
191
printf("Subarray is correct\n");
193
printf("Error: Subarray does not have correct elements\n");
197
// Joining string elements
198
printf("Method: -componentsJoinedByString:\n");
199
i = [c componentsJoinedByString: @"/"];
200
if ([i isEqual: @"NSObject/NSArray/NSMutableArray"])
201
printf("%s is correct\n", [i cString]);
204
printf("Error: %s is not correct\n", [i cString]);
205
printf("Should be NSObject/NSArray/NSMutableArray\n");
210
// Creating a string description of the array
211
/* What do the -description methods do?
213
[e descriptionWithLocale:]
214
[e descriptionWithLocale: indent:]
218
// NSMutableArray tests
219
printf("*** Start of NSMutableArray tests\n");
221
// Creating and initializing an NSMutableArray
222
f = [NSMutableArray arrayWithCapacity: 10];
223
NSCParameterAssert(f);
224
f = [[NSMutableArray alloc] initWithCapacity: 10];
226
NSCParameterAssert(f);
232
NSCParameterAssert([f count]);
234
printf("Method -addObject:[NSObject class]\n");
235
[f addObject:[NSObject class]];
236
printf("NSMutableArray has count %d\n", [f count]);
238
printf("Error: count != 5\n");
240
printf("Method -addObjectsFromArray:\n");
241
[f addObjectsFromArray: c];
242
printf("NSMutableArray has count %d\n", [f count]);
244
printf("Error: count != 8\n");
246
enumerator = [f objectEnumerator];
247
while ((i = [enumerator nextObject]))
248
printf("%s ", [[i description] cString]);
251
printf("Method -insertObject: [NSMutableArray class] atIndex: 2\n");
252
[f insertObject: [NSMutableArray class] atIndex: 2];
254
enumerator = [f objectEnumerator];
255
while ((i = [enumerator nextObject]))
256
printf("%s ", [[i description] cString]);
266
unsigned int ind[7] = {7, 4, 1, 3, 5, 0, 6};
268
printf("Method -removeAllObjects\n");
269
printf("Array count is %d\n", [h count]);
270
[h removeAllObjects];
271
printf("Array count is %d\n", [h count]);
273
printf("Error: count != 0\n");
277
printf("Method -removeLastObject\n");
278
[f removeLastObject];
280
enumerator = [f objectEnumerator];
281
while ((i = [enumerator nextObject]))
282
printf("%s ", [[i description] cString]);
285
printf("Method -removeObject: [NSObject class]\n");
286
[f removeObject: [NSObject class]];
288
enumerator = [f objectEnumerator];
289
while ((i = [enumerator nextObject]))
290
printf("%s ", [[i description] cString]);
293
printf("Method -removeObjectIdenticalTo: [NSArray class]\n");
294
[f removeObjectIdenticalTo: [NSArray class]];
296
enumerator = [f objectEnumerator];
297
while ((i = [enumerator nextObject]))
298
printf("%s ", [[i description] cString]);
301
printf("Method -removeObjectAtIndex: 2\n");
302
[f removeObjectAtIndex: 2];
304
enumerator = [f objectEnumerator];
305
while ((i = [enumerator nextObject]))
306
printf("%s ", [[i description] cString]);
309
printf("Method -removeObjectsFromIndices: {7,4,1,3,5,0,6} "
311
enumerator = [g objectEnumerator];
312
while ((i = [enumerator nextObject]))
313
printf("%s ", [[i description] cString]);
315
[g removeObjectsFromIndices: ind numIndices: 7];
316
enumerator = [g objectEnumerator];
317
while ((i = [enumerator nextObject]))
318
printf("%s ", [[i description] cString]);
321
if ([f isEqualToArray: g])
322
printf("Remove methods worked properly\n");
324
printf("Error: remove methods failed\n");
326
printf("Method -removeObjectsInArray:\n");
327
printf("Receiver array\n");
328
enumerator = [h objectEnumerator];
329
while ((i = [enumerator nextObject]))
330
printf("%s ", [[i description] cString]);
332
printf("Removing objects in this array\n");
333
enumerator = [c objectEnumerator];
334
while ((i = [enumerator nextObject]))
335
printf("%s ", [[i description] cString]);
338
[h removeObjectsInArray: c];
340
printf("Array count is %d\n", [h count]);
342
printf("Error: count != 1\n");
344
printf("%s", [[h objectAtIndex: 0] cString]);
345
if ([[h objectAtIndex: 0] isEqual: s])
346
printf("-removeObjectsInArray: worked correctly\n");
348
printf("Error: object in array is not correct\n");
353
c = [[c mutableCopy] autorelease];
354
printf("Method -replaceObjectAtIndex: 2 withObject:[NSString class]\n");
355
enumerator = [c objectEnumerator];
356
while ((i = [enumerator nextObject]))
357
printf("%s ", [[i description] cString]);
359
[c replaceObjectAtIndex: 2 withObject:[NSString class]];
360
enumerator = [c objectEnumerator];
361
while ((i = [enumerator nextObject]))
362
printf("%s ", [[i description] cString]);
365
printf("Method -setArray:\n");
367
enumerator = [h objectEnumerator];
368
while ((i = [enumerator nextObject]))
369
printf("%s ", [[i description] cString]);
371
if ([h isEqualToArray: h])
372
printf("-setArray worked properly\n");
374
printf("Error: array is incorrect\n");
379
//[ sortUsingFunction: context:];
380
//[ sortUsingSelector:];