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

« back to all changes in this revision

Viewing changes to Testing/nsdata.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 <stdio.h>
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>
19
 
 
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.
24
 
*
25
 
* Author    :   John W. M. Stevens
26
 
 
27
 
...............................................................................
28
 
15 April 1997
29
 
 
30
 
******************************************************************************/
31
 
 
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.
35
 
*/
36
 
char    *testString = "Test string for mutable data and archiver classes.";
37
 
char    *subString  = "Sub String";
38
 
 
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.
45
 
|
46
 
| Notes     :   Please see work logs for discussion.
47
 
-----------------------------------------------------------------------------*/
48
 
 
49
 
void
50
 
TestNSMutableData(void)
51
 
{
52
 
    auto    NSMutableData   *nsMutData;
53
 
    auto    char            *str;
54
 
    auto    NSRange         range;
55
 
 
56
 
    /*  Allocate and initialize an instance of an NSMutableData
57
 
    *   class.
58
 
    */
59
 
    nsMutData = [NSMutableData dataWithLength: strlen( testString ) + 1];
60
 
    str = (char *) [nsMutData mutableBytes];
61
 
    strcpy(str, testString);
62
 
 
63
 
    /*  Get contents, display.  */
64
 
    str = NULL;
65
 
    str = (char *) [nsMutData mutableBytes];
66
 
    printf("NSMutableData Test ---------------------------------------------"
67
 
           "---------------\n"
68
 
           "1) String: (%s)\n", str);
69
 
 
70
 
    /*  Attempt to force Range exception by having range start before
71
 
    *   zero.
72
 
    */
73
 
NS_DURING
74
 
    range = NSMakeRange(-2, strlen( subString ));
75
 
    [nsMutData replaceBytesInRange: range
76
 
               withBytes          : subString ];
77
 
NS_HANDLER
78
 
    fprintf(stderr,
79
 
            "%s %d : Exception %s - %s\n",
80
 
            __FILE__,
81
 
            __LINE__,
82
 
            [[localException name]   cString],
83
 
            [[localException reason] cString]);
84
 
NS_ENDHANDLER
85
 
 
86
 
    /*  Attempt to force another Range exception.   */
87
 
NS_DURING
88
 
    range = NSMakeRange(41, strlen( subString ));
89
 
    [nsMutData replaceBytesInRange: range
90
 
               withBytes          : subString ];
91
 
NS_HANDLER
92
 
    fprintf(stderr,
93
 
            "%s %d : Exception %s - %s\n",
94
 
            __FILE__,
95
 
            __LINE__,
96
 
            [[localException name]   cString],
97
 
            [[localException reason] cString]);
98
 
NS_ENDHANDLER
99
 
 
100
 
    /*  Attempt to force another Range exception.   */
101
 
NS_DURING
102
 
    range = NSMakeRange(42, strlen( subString ));
103
 
    [nsMutData replaceBytesInRange: range
104
 
               withBytes          : subString ];
105
 
NS_HANDLER
106
 
    fprintf(stderr,
107
 
            "%s %d : Exception %s - %s\n",
108
 
            __FILE__,
109
 
            __LINE__,
110
 
            [[localException name]   cString],
111
 
            [[localException reason] cString]);
112
 
NS_ENDHANDLER
113
 
 
114
 
    /*  How about a length that is less than zero?  */
115
 
NS_DURING
116
 
    range = NSMakeRange(6, -3.0);
117
 
    [nsMutData replaceBytesInRange: range
118
 
               withBytes          : subString ];
119
 
NS_HANDLER
120
 
    fprintf(stderr,
121
 
            "%s %d : Exception %s - %s\n",
122
 
            __FILE__,
123
 
            __LINE__,
124
 
            [[localException name]   cString],
125
 
            [[localException reason] cString]);
126
 
NS_ENDHANDLER
127
 
 
128
 
    /*  Attempt to force Range exception by having range start before
129
 
    *   zero.
130
 
    */
131
 
NS_DURING
132
 
    range = NSMakeRange(-2, strlen( subString ));
133
 
    [nsMutData resetBytesInRange: range];
134
 
NS_HANDLER
135
 
    fprintf(stderr,
136
 
            "%s %d : Exception %s - %s\n",
137
 
            __FILE__,
138
 
            __LINE__,
139
 
            [[localException name]   cString],
140
 
            [[localException reason] cString]);
141
 
NS_ENDHANDLER
142
 
 
143
 
    /*  Attempt to force another Range exception.   */
144
 
NS_DURING
145
 
    range = NSMakeRange(41, strlen( subString ));
146
 
    [nsMutData resetBytesInRange: range];
147
 
NS_HANDLER
148
 
    fprintf(stderr,
149
 
            "%s %d : Exception %s - %s\n",
150
 
            __FILE__,
151
 
            __LINE__,
152
 
            [[localException name]   cString],
153
 
            [[localException reason] cString]);
154
 
NS_ENDHANDLER
155
 
 
156
 
    /*  Attempt to force another Range exception.   */
157
 
NS_DURING
158
 
    range = NSMakeRange(42, strlen( subString ));
159
 
    [nsMutData resetBytesInRange: range];
160
 
NS_HANDLER
161
 
    fprintf(stderr,
162
 
            "%s %d : Exception %s - %s\n",
163
 
            __FILE__,
164
 
            __LINE__,
165
 
            [[localException name]   cString],
166
 
            [[localException reason] cString]);
167
 
NS_ENDHANDLER
168
 
 
169
 
    /*  How about a length less than zero?  */
170
 
NS_DURING
171
 
    range = NSMakeRange(6.0, -3.0);
172
 
    [nsMutData resetBytesInRange: range];
173
 
NS_HANDLER
174
 
    fprintf(stderr,
175
 
            "%s %d : Exception %s - %s\n",
176
 
            __FILE__,
177
 
            __LINE__,
178
 
            [[localException name]   cString],
179
 
            [[localException reason] cString]);
180
 
NS_ENDHANDLER
181
 
 
182
 
    /*  Get contents, display.  */
183
 
    str = NULL;
184
 
    str = (char *) [nsMutData mutableBytes];
185
 
    printf("2) String: (%s)\n", str);
186
 
 
187
 
    /*  Attempt to force an out of memory exception.    */
188
 
#if 0
189
 
    for ( ; ; )
190
 
    {
191
 
        /*  Append. */
192
 
        [nsMutData appendBytes: testString
193
 
                   length     : strlen( testString ) + 1];
194
 
 
195
 
        /*  Show current value. */
196
 
        printf("%9u\r", [nsMutData length]);
197
 
    }
198
 
#endif
199
 
}
200
 
 
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.
207
 
|
208
 
| Notes     :   Please see work logs for discussion.
209
 
-----------------------------------------------------------------------------*/
210
 
 
211
 
void
212
 
TestNSData(void)
213
 
{
214
 
    auto    NSData          *nsData;
215
 
    auto    NSData          *newNsData;
216
 
    auto    char            *str;
217
 
    auto    char            bfr[128];
218
 
    auto    NSRange         range;
219
 
 
220
 
    /*  Allocate and initialize an instance of an NSData
221
 
    *   class.
222
 
    */
223
 
    nsData = [NSData dataWithBytes: testString
224
 
                     length       : (unsigned int) strlen( testString ) + 1];
225
 
 
226
 
    /*  Get contents, display.  */
227
 
    str = (char *) [nsData bytes];
228
 
    printf("NSData Test ----------------------------------------------------"
229
 
           "---------------\n"
230
 
           "1) String: (%s)\n", str);
231
 
 
232
 
    /*  Attempt to force Range exception by having range start before
233
 
    *   zero.
234
 
    */
235
 
NS_DURING
236
 
    /*  Get buffer piece.   */
237
 
    range = NSMakeRange(-2.0, 6.0);
238
 
    [nsData getBytes: bfr
239
 
            range   : range];
240
 
 
241
 
    /*  Print buffer piece. */
242
 
    bfr[6] = '\0';
243
 
    printf("    A) Buffer: (%s)\n", bfr);
244
 
NS_HANDLER
245
 
    fprintf(stderr,
246
 
            "%s %d : Exception %s - %s\n",
247
 
            __FILE__,
248
 
            __LINE__,
249
 
            [[localException name]   cString],
250
 
            [[localException reason] cString]);
251
 
NS_ENDHANDLER
252
 
 
253
 
    /*  Attempt to force another Range exception.   */
254
 
NS_DURING
255
 
    /*  Get piece.  */
256
 
    range = NSMakeRange(41, strlen( subString ));
257
 
    [nsData getBytes: bfr
258
 
            range   : range];
259
 
 
260
 
    /*  Print buffer piece. */
261
 
    bfr[strlen( subString )] = '\0';
262
 
    printf("    B) Buffer: (%s)\n", bfr);
263
 
NS_HANDLER
264
 
    fprintf(stderr,
265
 
            "%s %d : Exception %s - %s\n",
266
 
            __FILE__,
267
 
            __LINE__,
268
 
            [[localException name]   cString],
269
 
            [[localException reason] cString]);
270
 
NS_ENDHANDLER
271
 
 
272
 
    /*  Attempt to force another Range exception.   */
273
 
NS_DURING
274
 
    range = NSMakeRange(42, strlen( subString ));
275
 
    [nsData getBytes: bfr
276
 
            range   : range];
277
 
 
278
 
    /*  Print buffer piece. */
279
 
    bfr[strlen( subString )] = '\0';
280
 
    printf("    C) Buffer: (%s)\n", bfr);
281
 
NS_HANDLER
282
 
    fprintf(stderr,
283
 
            "%s %d : Exception %s - %s\n",
284
 
            __FILE__,
285
 
            __LINE__,
286
 
            [[localException name]   cString],
287
 
            [[localException reason] cString]);
288
 
NS_ENDHANDLER
289
 
 
290
 
    /*  How about less than zero length?    */
291
 
NS_DURING
292
 
    range = NSMakeRange(5.0, -4.0);
293
 
    [nsData getBytes: bfr
294
 
            range   : range];
295
 
 
296
 
    /*  Print buffer piece. */
297
 
    bfr[strlen( subString )] = '\0';
298
 
    printf("    C) Buffer: (%s)\n", bfr);
299
 
NS_HANDLER
300
 
    fprintf(stderr,
301
 
            "%s %d : Exception %s - %s\n",
302
 
            __FILE__,
303
 
            __LINE__,
304
 
            [[localException name]   cString],
305
 
            [[localException reason] cString]);
306
 
NS_ENDHANDLER
307
 
 
308
 
/*=================== subDataWithRange ======================================*/
309
 
    /*  Attempt to force Range exception by having range start before
310
 
    *   zero.
311
 
    */
312
 
NS_DURING
313
 
    /*  Get buffer piece.   */
314
 
    range = NSMakeRange(-2.0, 6.0);
315
 
    newNsData = [nsData subdataWithRange: range];
316
 
 
317
 
    /*  Print buffer piece. */
318
 
    [newNsData getBytes: bfr];
319
 
    bfr[6] = '\0';
320
 
    printf("    D) Buffer: (%s)\n", bfr);
321
 
NS_HANDLER
322
 
    fprintf(stderr,
323
 
            "%s %d : Exception %s - %s\n",
324
 
            __FILE__,
325
 
            __LINE__,
326
 
            [[localException name]   cString],
327
 
            [[localException reason] cString]);
328
 
NS_ENDHANDLER
329
 
 
330
 
    /*  Attempt to force another Range exception.   */
331
 
NS_DURING
332
 
    /*  Get buffer piece.   */
333
 
    range = NSMakeRange(41, strlen( subString ));
334
 
    newNsData = [nsData subdataWithRange: range];
335
 
 
336
 
    /*  Print buffer piece. */
337
 
    [newNsData getBytes: bfr];
338
 
    bfr[strlen( subString )] = '\0';
339
 
    printf("    E) Buffer: (%s)\n", bfr);
340
 
NS_HANDLER
341
 
    fprintf(stderr,
342
 
            "%s %d : Exception %s - %s\n",
343
 
            __FILE__,
344
 
            __LINE__,
345
 
            [[localException name]   cString],
346
 
            [[localException reason] cString]);
347
 
NS_ENDHANDLER
348
 
 
349
 
    /*  Attempt to force another Range exception.   */
350
 
NS_DURING
351
 
    /*  Get buffer piece.   */
352
 
    range = NSMakeRange(42, strlen( subString ));
353
 
    newNsData = [nsData subdataWithRange: range];
354
 
 
355
 
    /*  Print buffer piece. */
356
 
    [newNsData getBytes: bfr];
357
 
    bfr[strlen( subString )] = '\0';
358
 
    printf("    F) Buffer: (%s)\n", bfr);
359
 
NS_HANDLER
360
 
    fprintf(stderr,
361
 
            "%s %d : Exception %s - %s\n",
362
 
            __FILE__,
363
 
            __LINE__,
364
 
            [[localException name]   cString],
365
 
            [[localException reason] cString]);
366
 
NS_ENDHANDLER
367
 
 
368
 
    /*  How about a length less than zero?  */
369
 
NS_DURING
370
 
    /*  Get buffer piece.   */
371
 
    range = NSMakeRange(9.0, -6.0);
372
 
    newNsData = [nsData subdataWithRange: range];
373
 
 
374
 
    /*  Print buffer piece. */
375
 
    [newNsData getBytes: bfr];
376
 
    bfr[strlen( subString )] = '\0';
377
 
    printf("    F) Buffer: (%s)\n", bfr);
378
 
NS_HANDLER
379
 
    fprintf(stderr,
380
 
            "%s %d : Exception %s - %s\n",
381
 
            __FILE__,
382
 
            __LINE__,
383
 
            [[localException name]   cString],
384
 
            [[localException reason] cString]);
385
 
NS_ENDHANDLER
386
 
 
387
 
    /*  Get contents, display.  */
388
 
    str = NULL;
389
 
    str = (char *) [nsData bytes];
390
 
    printf("2) String: (%s)\n", str);
391
 
}
392
 
 
393
 
int
394
 
main()
395
 
{
396
 
  id a;
397
 
  id d;
398
 
  id o;
399
 
  id pool;
400
 
 
401
 
  [NSAutoreleasePool enableDoubleReleaseCheck:YES];
402
 
 
403
 
  pool = [[NSAutoreleasePool alloc] init];
404
 
 
405
 
  d = [NSData dataWithContentsOfMappedFile:@"nsdata.m"];
406
 
  if (d == nil)
407
 
    printf("Unable to map file");
408
 
  printf("Mapped %d bytes\n", [d length]);
409
 
 
410
 
  o = [d copy];
411
 
  printf("Copied %d bytes\n", [o length]);
412
 
  [o release];
413
 
 
414
 
  o = [d mutableCopy];
415
 
  printf("Copied %d bytes\n", [o length]);
416
 
  [o release];
417
 
 
418
 
  d = [NSData dataWithContentsOfFile:@"nsdata.m"];
419
 
  if (d == nil)
420
 
    printf("Unable to read file");
421
 
  printf("Read %d bytes\n", [d length]);
422
 
 
423
 
  o = [d copy];
424
 
  printf("Copied %d bytes\n", [o length]);
425
 
  [o release];
426
 
 
427
 
  o = [d mutableCopy];
428
 
  printf("Copied %d bytes\n", [o length]);
429
 
  [o release];
430
 
 
431
 
  d = [NSData dataWithSharedBytes: [d bytes] length: [d length]];
432
 
  if (d == nil)
433
 
    printf("Unable to make shared data");
434
 
  printf("Shared data of %d bytes\n", [d length]);
435
 
 
436
 
  o = [d copy];
437
 
  printf("Copied %d bytes\n", [o length]);
438
 
  [o release];
439
 
 
440
 
  o = [d mutableCopy];
441
 
  printf("Copied %d bytes\n", [o length]);
442
 
  [o release];
443
 
 
444
 
  d = [NSMutableData dataWithSharedBytes: [d bytes] length: [d length]];
445
 
  if (d == nil)
446
 
    printf("Unable to make mutable shared data");
447
 
  printf("Mutable shared data of %d bytes\n", [d length]);
448
 
 
449
 
  o = [d copy];
450
 
  printf("Copied %d bytes\n", [o length]);
451
 
  [o release];
452
 
 
453
 
  o = [d mutableCopy];
454
 
  printf("Copied %d bytes\n", [o length]);
455
 
  [o release];
456
 
 
457
 
  [d appendBytes: "Hello world" length: 11];
458
 
  printf("Extended by 11 bytes to %d bytes\n", [d length]);
459
 
 
460
 
  d = [NSMutableData dataWithShmID: [d shmID] length: [d length]];
461
 
  if (d == nil)
462
 
    printf("Unable to make mutable data with old ID\n");
463
 
  printf("data with shmID gives data length %d\n", [d length]);
464
 
 
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]);
471
 
  [a release];
472
 
 
473
 
  [d setCapacity: 2000000];
474
 
  printf("Set capacity of shared memory item to %d\n", [d capacity]);
475
 
 
476
 
  /*  Test NSMutableData. */
477
 
  TestNSMutableData();
478
 
 
479
 
  /*  Test NSData.    */
480
 
  TestNSData();
481
 
 
482
 
  [pool release];
483
 
 
484
 
  exit(0);
485
 
}
486