~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Testing/nsarray.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Test/example program for the base library
2
 
 
3
 
   Copyright (C) 2005 Free Software Foundation, Inc.
4
 
   
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.
8
 
 
9
 
   This file is part of the GNUstep Base Library.
10
 
*/
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>
17
 
 
18
 
int compare(id elem1, id elem2, void* context)
19
 
{
20
 
  return (int)[elem1 performSelector:@selector(compare:) withObject:elem2];
21
 
}
22
 
 
23
 
int
24
 
main()
25
 
{
26
 
  id a, b, c, d, e, f, g, h;                    /* arrays */
27
 
  id enumerator;
28
 
  id i;
29
 
  id s = @"Hello World\n";
30
 
  id pool;
31
 
  id o1, o2, o3;
32
 
  unsigned int p;
33
 
 
34
 
  //behavior_set_debug(0);
35
 
 
36
 
  [NSAutoreleasePool enableDoubleReleaseCheck:YES];
37
 
  pool = [[NSAutoreleasePool alloc] init];
38
 
 
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]);
50
 
 
51
 
  // NSArray tests
52
 
  {
53
 
    // Class methods for allocating and initializing an array
54
 
    printf("Method: +array\n");
55
 
    a = [NSArray array];
56
 
    if ([a count] == 0)
57
 
      printf("Empty array count is zero\n");
58
 
    else
59
 
      printf("Error: empty array count is not zero\n");
60
 
 
61
 
    printf("Method: +arrayWithObject:\n");
62
 
    b = [NSArray arrayWithObject: s];
63
 
    printf("NSArray has count %d\n", [b count]);
64
 
    if ([b count] != 1)
65
 
      printf("Error: count != 1\n");
66
 
 
67
 
    printf("Method: +arrayWithObjects:...\n");
68
 
    c = [NSArray arrayWithObjects:
69
 
                 [NSObject class],
70
 
                 [NSArray class],
71
 
                 [NSMutableArray class],
72
 
                 nil];
73
 
    printf("NSArray has count %d\n", [c count]);
74
 
    if ([c count] != 3)
75
 
      printf("Error: count != 3\n");
76
 
  }
77
 
 
78
 
  {
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]);
83
 
    if ([d count] != 4)
84
 
      printf("Error: count != 4\n");
85
 
 
86
 
    printf("Method: -arrayByAddingObjectsFromArray:\n");
87
 
    e = [c arrayByAddingObjectsFromArray: b];
88
 
    printf("NSArray has count %d\n", [c count]);
89
 
    if ([e count] != 4)
90
 
      printf("Error: count != 4\n");
91
 
  }
92
 
 
93
 
  {
94
 
    // Querying the arra
95
 
    NSCParameterAssert([c containsObject:[NSObject class]]);
96
 
 
97
 
    p = [e indexOfObject:@"Hello World\n"];
98
 
    if (p == NSNotFound)
99
 
      printf("Error: index of object not found\n");
100
 
    else
101
 
      printf("Index of object is %d\n", p);
102
 
 
103
 
    p = [e indexOfObjectIdenticalTo:s];
104
 
    if (p == NSNotFound)
105
 
      printf("Error: index of identical object not found\n");
106
 
    else
107
 
      printf("Index of identical object is %d\n", p);
108
 
 
109
 
    NSCParameterAssert([c lastObject]);
110
 
    printf("Class at index 2 is %s\n", [[[c objectAtIndex:2] description] cString]);
111
 
 
112
 
    printf("Forward enumeration\n");
113
 
    enumerator = [e objectEnumerator];
114
 
    while ((i = [enumerator nextObject]))
115
 
      printf("%s ", [[i description] cString]);
116
 
    printf("\n");
117
 
 
118
 
    printf("Reverse enumeration\n");
119
 
    enumerator = [e reverseObjectEnumerator];
120
 
    while ((i = [enumerator nextObject]))
121
 
      printf("%s ", [[i description] cString]);
122
 
    printf("\n");
123
 
  }
124
 
 
125
 
  {
126
 
    // Sending messages to elements
127
 
    [c makeObjectsPerform:@selector(description)];
128
 
 
129
 
    //[c makeObjectsPerform:@selector(isEqual:) withObject: @"NSArray"];
130
 
  }
131
 
 
132
 
  {
133
 
    // Comparing arrays
134
 
    NSCParameterAssert([d firstObjectCommonWithArray:e]);
135
 
 
136
 
    if ([d isEqualToArray: d])
137
 
      printf("NSArray is equal to itself\n");
138
 
    else
139
 
      printf("Error: NSArray is not equal to itself\n");
140
 
 
141
 
    if ([d isEqualToArray: e])
142
 
      printf("NSArrays are equal\n");
143
 
    else
144
 
      printf("Error: NSArrays are not equal\n");
145
 
  }
146
 
 
147
 
  {
148
 
    // Deriving new arrays
149
 
    NSRange r = NSMakeRange(0, 3);
150
 
 
151
 
    f = [NSMutableArray array];
152
 
    [f addObject: @"Lions"];
153
 
    [f addObject: @"Tigers"];
154
 
    [f addObject: @"Bears"];
155
 
    [f addObject: @"Penguins"];
156
 
    [f addObject: @"Giraffes"];
157
 
 
158
 
    enumerator = [f objectEnumerator];
159
 
    while ((i = [enumerator nextObject]))
160
 
      printf("%s ", [i cString]);
161
 
    printf("\n");
162
 
 
163
 
    printf("Method: -sortedArrayUsingSelector:\n");
164
 
    g = [f sortedArrayUsingSelector: @selector(compare:)];
165
 
    printf("Method: -sortedArrayUsingFunction:context:\n");
166
 
    h = [f sortedArrayUsingFunction: compare context: NULL];
167
 
 
168
 
    enumerator = [g objectEnumerator];
169
 
    while ((i = [enumerator nextObject]))
170
 
      printf("%s ", [i cString]);
171
 
    printf("\n");
172
 
 
173
 
    if (([g isEqualToArray: h]) && (![g isEqualToArray: f]))
174
 
      printf("Sorted arrays are correct\n");
175
 
    else
176
 
      printf("Error: Sorted arrays are not correct\n");
177
 
 
178
 
    printf("Method: -subarrayWithRange:\n");
179
 
    f = [e subarrayWithRange: r];
180
 
 
181
 
    printf("NSArray has count %d\n", [f count]);
182
 
    if ([f count] != 3)
183
 
      printf("Error: count != 3\n");
184
 
 
185
 
    enumerator = [f objectEnumerator];
186
 
    while ((i = [enumerator nextObject]))
187
 
      printf("%s ", [[i description] cString]);
188
 
    printf("\n");
189
 
 
190
 
    if ([f isEqualToArray: c])
191
 
      printf("Subarray is correct\n");
192
 
    else
193
 
      printf("Error: Subarray does not have correct elements\n");
194
 
  }
195
 
 
196
 
  {
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]);
202
 
    else
203
 
      {
204
 
        printf("Error: %s is not correct\n", [i cString]);
205
 
        printf("Should be NSObject/NSArray/NSMutableArray\n");
206
 
      }
207
 
  }
208
 
 
209
 
  {
210
 
    // Creating a string description of the array
211
 
    /* What do the -description methods do?
212
 
       [e description]
213
 
       [e descriptionWithLocale:]
214
 
       [e descriptionWithLocale: indent:]
215
 
       */
216
 
  }
217
 
 
218
 
  // NSMutableArray tests
219
 
  printf("*** Start of NSMutableArray tests\n");
220
 
  {
221
 
    // Creating and initializing an NSMutableArray
222
 
    f = [NSMutableArray arrayWithCapacity: 10];
223
 
    NSCParameterAssert(f);
224
 
    f = [[NSMutableArray alloc] initWithCapacity: 10];
225
 
    [f release];
226
 
    NSCParameterAssert(f);
227
 
  }
228
 
 
229
 
  {
230
 
    // Adding objects
231
 
    f = [e mutableCopy];
232
 
    NSCParameterAssert([f count]);
233
 
 
234
 
    printf("Method -addObject:[NSObject class]\n");
235
 
    [f addObject:[NSObject class]];
236
 
    printf("NSMutableArray has count %d\n", [f count]);
237
 
    if ([f count] != 5)
238
 
      printf("Error: count != 5\n");
239
 
 
240
 
    printf("Method -addObjectsFromArray:\n");
241
 
    [f addObjectsFromArray: c];
242
 
    printf("NSMutableArray has count %d\n", [f count]);
243
 
    if ([f count] != 8)
244
 
      printf("Error: count != 8\n");
245
 
 
246
 
    enumerator = [f objectEnumerator];
247
 
    while ((i = [enumerator nextObject]))
248
 
      printf("%s ", [[i description] cString]);
249
 
    printf("\n");
250
 
 
251
 
    printf("Method -insertObject: [NSMutableArray class] atIndex: 2\n");
252
 
    [f insertObject: [NSMutableArray class] atIndex: 2];
253
 
 
254
 
    enumerator = [f objectEnumerator];
255
 
    while ((i = [enumerator nextObject]))
256
 
      printf("%s ", [[i description] cString]);
257
 
    printf("\n");
258
 
 
259
 
  }
260
 
 
261
 
  g = [f mutableCopy];
262
 
  h = [f mutableCopy];
263
 
 
264
 
  {
265
 
    // Removing objects
266
 
    unsigned int ind[7] = {7, 4, 1, 3, 5, 0, 6};
267
 
 
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]);
272
 
    if ([h count] != 0)
273
 
      printf("Error: count != 0\n");
274
 
 
275
 
    h = [f mutableCopy];
276
 
 
277
 
    printf("Method -removeLastObject\n");
278
 
    [f removeLastObject];
279
 
 
280
 
    enumerator = [f objectEnumerator];
281
 
    while ((i = [enumerator nextObject]))
282
 
      printf("%s ", [[i description] cString]);
283
 
    printf("\n");
284
 
 
285
 
    printf("Method -removeObject: [NSObject class]\n");
286
 
    [f removeObject: [NSObject class]];
287
 
 
288
 
    enumerator = [f objectEnumerator];
289
 
    while ((i = [enumerator nextObject]))
290
 
      printf("%s ", [[i description] cString]);
291
 
    printf("\n");
292
 
 
293
 
    printf("Method -removeObjectIdenticalTo: [NSArray class]\n");
294
 
    [f removeObjectIdenticalTo: [NSArray class]];
295
 
 
296
 
    enumerator = [f objectEnumerator];
297
 
    while ((i = [enumerator nextObject]))
298
 
      printf("%s ", [[i description] cString]);
299
 
    printf("\n");
300
 
 
301
 
    printf("Method -removeObjectAtIndex: 2\n");
302
 
    [f removeObjectAtIndex: 2];
303
 
 
304
 
    enumerator = [f objectEnumerator];
305
 
    while ((i = [enumerator nextObject]))
306
 
      printf("%s ", [[i description] cString]);
307
 
    printf("\n");
308
 
 
309
 
    printf("Method -removeObjectsFromIndices: {7,4,1,3,5,0,6} "
310
 
           "numIndices: 6\n");
311
 
    enumerator = [g objectEnumerator];
312
 
    while ((i = [enumerator nextObject]))
313
 
      printf("%s ", [[i description] cString]);
314
 
    printf("\n");
315
 
    [g removeObjectsFromIndices: ind numIndices: 7];
316
 
    enumerator = [g objectEnumerator];
317
 
    while ((i = [enumerator nextObject]))
318
 
      printf("%s ", [[i description] cString]);
319
 
    printf("\n");
320
 
 
321
 
    if ([f isEqualToArray: g])
322
 
      printf("Remove methods worked properly\n");
323
 
    else
324
 
      printf("Error: remove methods failed\n");
325
 
 
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]);
331
 
    printf("\n");
332
 
    printf("Removing objects in this array\n");
333
 
    enumerator = [c objectEnumerator];
334
 
    while ((i = [enumerator nextObject]))
335
 
      printf("%s ", [[i description] cString]);
336
 
    printf("\n");
337
 
 
338
 
    [h removeObjectsInArray: c];
339
 
 
340
 
    printf("Array count is %d\n", [h count]);
341
 
    if ([h count] != 1)
342
 
      printf("Error: count != 1\n");
343
 
 
344
 
    printf("%s", [[h objectAtIndex: 0] cString]);
345
 
    if ([[h objectAtIndex: 0] isEqual: s])
346
 
      printf("-removeObjectsInArray: worked correctly\n");
347
 
    else
348
 
      printf("Error: object in array is not correct\n");
349
 
  }
350
 
 
351
 
  {
352
 
    // Replacing objects
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]);
358
 
    printf("\n");
359
 
    [c replaceObjectAtIndex: 2 withObject:[NSString class]];
360
 
    enumerator = [c objectEnumerator];
361
 
    while ((i = [enumerator nextObject]))
362
 
      printf("%s ", [[i description] cString]);
363
 
    printf("\n");
364
 
 
365
 
    printf("Method -setArray:\n");
366
 
    [h setArray: f];
367
 
    enumerator = [h objectEnumerator];
368
 
    while ((i = [enumerator nextObject]))
369
 
      printf("%s ", [[i description] cString]);
370
 
    printf("\n");
371
 
    if ([h isEqualToArray: h])
372
 
      printf("-setArray worked properly\n");
373
 
    else
374
 
      printf("Error: array is incorrect\n");
375
 
  }
376
 
 
377
 
  {
378
 
    // Sorting Elements
379
 
    //[ sortUsingFunction: context:];
380
 
    //[ sortUsingSelector:];
381
 
  }
382
 
 
383
 
  [pool release];
384
 
 
385
 
  exit(0);
386
 
}