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.
12
#include <Foundation/NSData.h>
13
#include <Foundation/NSException.h>
14
#include <Foundation/NSRange.h>
15
#include <Foundation/NSSerialization.h>
16
#include <Foundation/NSArchiver.h>
17
#include <Foundation/NSAutoreleasePool.h>
18
#include <Foundation/NSString.h>
20
/******************************************************************************
21
* Module : NSMutableData(NSData) --- Black Box test module for the
22
* *Data classes to make sure that methods that raise exceptions
23
* do so, and that the exceptions are raised properly.
25
* Author : John W. M. Stevens
27
...............................................................................
30
******************************************************************************/
32
/* Data for stuffing into *Data objects. I like printable data, as it
33
* gives a quick visual check mechanism, but it has the disadvantage
34
* of not checking for 8 bit cleanliness.
36
char *testString = "Test string for mutable data and archiver classes.";
37
char *subString = "Sub String";
39
/*-----------------------------------------------------------------------------
40
| Routine : TestNSMutableData() --- Create an instance of an NSMutableData
41
| class, initialize it with a C string (to have something
42
| printable for tests) and invoke the two methods that
43
| should raise NSRangeException exceptions using ranges that
44
| cross both edges of the buffer boundary.
46
| Notes : Please see work logs for discussion.
47
-----------------------------------------------------------------------------*/
50
TestNSMutableData(void)
52
auto NSMutableData *nsMutData;
56
/* Allocate and initialize an instance of an NSMutableData
59
nsMutData = [NSMutableData dataWithLength: strlen( testString ) + 1];
60
str = (char *) [nsMutData mutableBytes];
61
strcpy(str, testString);
63
/* Get contents, display. */
65
str = (char *) [nsMutData mutableBytes];
66
printf("NSMutableData Test ---------------------------------------------"
68
"1) String: (%s)\n", str);
70
/* Attempt to force Range exception by having range start before
74
range = NSMakeRange(-2, strlen( subString ));
75
[nsMutData replaceBytesInRange: range
76
withBytes : subString ];
79
"%s %d : Exception %s - %s\n",
82
[[localException name] cString],
83
[[localException reason] cString]);
86
/* Attempt to force another Range exception. */
88
range = NSMakeRange(41, strlen( subString ));
89
[nsMutData replaceBytesInRange: range
90
withBytes : subString ];
93
"%s %d : Exception %s - %s\n",
96
[[localException name] cString],
97
[[localException reason] cString]);
100
/* Attempt to force another Range exception. */
102
range = NSMakeRange(42, strlen( subString ));
103
[nsMutData replaceBytesInRange: range
104
withBytes : subString ];
107
"%s %d : Exception %s - %s\n",
110
[[localException name] cString],
111
[[localException reason] cString]);
114
/* How about a length that is less than zero? */
116
range = NSMakeRange(6, -3.0);
117
[nsMutData replaceBytesInRange: range
118
withBytes : subString ];
121
"%s %d : Exception %s - %s\n",
124
[[localException name] cString],
125
[[localException reason] cString]);
128
/* Attempt to force Range exception by having range start before
132
range = NSMakeRange(-2, strlen( subString ));
133
[nsMutData resetBytesInRange: range];
136
"%s %d : Exception %s - %s\n",
139
[[localException name] cString],
140
[[localException reason] cString]);
143
/* Attempt to force another Range exception. */
145
range = NSMakeRange(41, strlen( subString ));
146
[nsMutData resetBytesInRange: range];
149
"%s %d : Exception %s - %s\n",
152
[[localException name] cString],
153
[[localException reason] cString]);
156
/* Attempt to force another Range exception. */
158
range = NSMakeRange(42, strlen( subString ));
159
[nsMutData resetBytesInRange: range];
162
"%s %d : Exception %s - %s\n",
165
[[localException name] cString],
166
[[localException reason] cString]);
169
/* How about a length less than zero? */
171
range = NSMakeRange(6.0, -3.0);
172
[nsMutData resetBytesInRange: range];
175
"%s %d : Exception %s - %s\n",
178
[[localException name] cString],
179
[[localException reason] cString]);
182
/* Get contents, display. */
184
str = (char *) [nsMutData mutableBytes];
185
printf("2) String: (%s)\n", str);
187
/* Attempt to force an out of memory exception. */
192
[nsMutData appendBytes: testString
193
length : strlen( testString ) + 1];
195
/* Show current value. */
196
printf("%9u\r", [nsMutData length]);
201
/*-----------------------------------------------------------------------------
202
| Routine : TestNSData() --- Create an instance of an NSData
203
| class, initialize it with a C string (to have something
204
| printable for tests) and invoke the two methods that
205
| should raise NSRangeException exceptions using ranges that
206
| cross both edges of the buffer boundary.
208
| Notes : Please see work logs for discussion.
209
-----------------------------------------------------------------------------*/
215
auto NSData *newNsData;
220
/* Allocate and initialize an instance of an NSData
223
nsData = [NSData dataWithBytes: testString
224
length : (unsigned int) strlen( testString ) + 1];
226
/* Get contents, display. */
227
str = (char *) [nsData bytes];
228
printf("NSData Test ----------------------------------------------------"
230
"1) String: (%s)\n", str);
232
/* Attempt to force Range exception by having range start before
236
/* Get buffer piece. */
237
range = NSMakeRange(-2.0, 6.0);
238
[nsData getBytes: bfr
241
/* Print buffer piece. */
243
printf(" A) Buffer: (%s)\n", bfr);
246
"%s %d : Exception %s - %s\n",
249
[[localException name] cString],
250
[[localException reason] cString]);
253
/* Attempt to force another Range exception. */
256
range = NSMakeRange(41, strlen( subString ));
257
[nsData getBytes: bfr
260
/* Print buffer piece. */
261
bfr[strlen( subString )] = '\0';
262
printf(" B) Buffer: (%s)\n", bfr);
265
"%s %d : Exception %s - %s\n",
268
[[localException name] cString],
269
[[localException reason] cString]);
272
/* Attempt to force another Range exception. */
274
range = NSMakeRange(42, strlen( subString ));
275
[nsData getBytes: bfr
278
/* Print buffer piece. */
279
bfr[strlen( subString )] = '\0';
280
printf(" C) Buffer: (%s)\n", bfr);
283
"%s %d : Exception %s - %s\n",
286
[[localException name] cString],
287
[[localException reason] cString]);
290
/* How about less than zero length? */
292
range = NSMakeRange(5.0, -4.0);
293
[nsData getBytes: bfr
296
/* Print buffer piece. */
297
bfr[strlen( subString )] = '\0';
298
printf(" C) Buffer: (%s)\n", bfr);
301
"%s %d : Exception %s - %s\n",
304
[[localException name] cString],
305
[[localException reason] cString]);
308
/*=================== subDataWithRange ======================================*/
309
/* Attempt to force Range exception by having range start before
313
/* Get buffer piece. */
314
range = NSMakeRange(-2.0, 6.0);
315
newNsData = [nsData subdataWithRange: range];
317
/* Print buffer piece. */
318
[newNsData getBytes: bfr];
320
printf(" D) Buffer: (%s)\n", bfr);
323
"%s %d : Exception %s - %s\n",
326
[[localException name] cString],
327
[[localException reason] cString]);
330
/* Attempt to force another Range exception. */
332
/* Get buffer piece. */
333
range = NSMakeRange(41, strlen( subString ));
334
newNsData = [nsData subdataWithRange: range];
336
/* Print buffer piece. */
337
[newNsData getBytes: bfr];
338
bfr[strlen( subString )] = '\0';
339
printf(" E) Buffer: (%s)\n", bfr);
342
"%s %d : Exception %s - %s\n",
345
[[localException name] cString],
346
[[localException reason] cString]);
349
/* Attempt to force another Range exception. */
351
/* Get buffer piece. */
352
range = NSMakeRange(42, strlen( subString ));
353
newNsData = [nsData subdataWithRange: range];
355
/* Print buffer piece. */
356
[newNsData getBytes: bfr];
357
bfr[strlen( subString )] = '\0';
358
printf(" F) Buffer: (%s)\n", bfr);
361
"%s %d : Exception %s - %s\n",
364
[[localException name] cString],
365
[[localException reason] cString]);
368
/* How about a length less than zero? */
370
/* Get buffer piece. */
371
range = NSMakeRange(9.0, -6.0);
372
newNsData = [nsData subdataWithRange: range];
374
/* Print buffer piece. */
375
[newNsData getBytes: bfr];
376
bfr[strlen( subString )] = '\0';
377
printf(" F) Buffer: (%s)\n", bfr);
380
"%s %d : Exception %s - %s\n",
383
[[localException name] cString],
384
[[localException reason] cString]);
387
/* Get contents, display. */
389
str = (char *) [nsData bytes];
390
printf("2) String: (%s)\n", str);
401
[NSAutoreleasePool enableDoubleReleaseCheck:YES];
403
pool = [[NSAutoreleasePool alloc] init];
405
d = [NSData dataWithContentsOfMappedFile:@"nsdata.m"];
407
printf("Unable to map file");
408
printf("Mapped %d bytes\n", [d length]);
411
printf("Copied %d bytes\n", [o length]);
415
printf("Copied %d bytes\n", [o length]);
418
d = [NSData dataWithContentsOfFile:@"nsdata.m"];
420
printf("Unable to read file");
421
printf("Read %d bytes\n", [d length]);
424
printf("Copied %d bytes\n", [o length]);
428
printf("Copied %d bytes\n", [o length]);
431
d = [NSData dataWithSharedBytes: [d bytes] length: [d length]];
433
printf("Unable to make shared data");
434
printf("Shared data of %d bytes\n", [d length]);
437
printf("Copied %d bytes\n", [o length]);
441
printf("Copied %d bytes\n", [o length]);
444
d = [NSMutableData dataWithSharedBytes: [d bytes] length: [d length]];
446
printf("Unable to make mutable shared data");
447
printf("Mutable shared data of %d bytes\n", [d length]);
450
printf("Copied %d bytes\n", [o length]);
454
printf("Copied %d bytes\n", [o length]);
457
[d appendBytes: "Hello world" length: 11];
458
printf("Extended by 11 bytes to %d bytes\n", [d length]);
460
d = [NSMutableData dataWithShmID: [d shmID] length: [d length]];
462
printf("Unable to make mutable data with old ID\n");
463
printf("data with shmID gives data length %d\n", [d length]);
465
a = [[NSArchiver new] autorelease];
466
[a encodeRootObject: d];
467
printf("Encoded data into archive\n");
468
a = [[NSUnarchiver alloc] initForReadingWithData: [a archiverData]];
469
o = [a decodeObject];
470
printf("Decoded data from archive - length %d\n", [o length]);
473
[d setCapacity: 2000000];
474
printf("Set capacity of shared memory item to %d\n", [d capacity]);
476
/* Test NSMutableData. */