2011-02-16 08:21:17 +00:00
|
|
|
#import "Testing.h"
|
|
|
|
#import "ObjectTesting.h"
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
|
|
|
int main()
|
2016-07-26 23:01:11 +00:00
|
|
|
{
|
2011-02-16 08:21:17 +00:00
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
char *str1,*str2;
|
|
|
|
NSData *data1, *data2;
|
|
|
|
NSMutableData *mutable;
|
2011-08-03 08:41:26 +00:00
|
|
|
char *hold;
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
str1 = "Test string for data classes";
|
2016-07-26 23:01:11 +00:00
|
|
|
str2 = (char *) malloc(sizeof("Test string for data classes not copied"));
|
2011-02-16 08:21:17 +00:00
|
|
|
strcpy(str2,"Test string for data classes not copied");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
mutable = [NSMutableData dataWithLength:100];
|
2016-07-26 23:01:11 +00:00
|
|
|
hold = [mutable mutableBytes];
|
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
/* hmpf is this correct */
|
|
|
|
data1 = [NSData dataWithBytes:str1 length:(strlen(str1) * sizeof(void*))];
|
|
|
|
PASS(data1 != nil &&
|
|
|
|
[data1 isKindOfClass:[NSData class]] &&
|
|
|
|
[data1 length] == (strlen(str1) * sizeof(void*)) &&
|
|
|
|
[data1 bytes] != str1 &&
|
|
|
|
strcmp(str1,[data1 bytes]) == 0,
|
|
|
|
"+dataWithBytes:length: works");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
data2 = [NSData dataWithBytesNoCopy:str2 length:(strlen(str2) * sizeof(void*))];
|
|
|
|
PASS(data2 != nil && [data2 isKindOfClass:[NSData class]] &&
|
|
|
|
[data2 length] == (strlen(str2) * sizeof(void*)) &&
|
|
|
|
[data2 bytes] == str2,
|
|
|
|
"+dataWithBytesNoCopy:length: works");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
data1 = [NSData dataWithBytes:nil length:0];
|
2016-07-26 23:01:11 +00:00
|
|
|
PASS(data1 != nil && [data1 isKindOfClass:[NSData class]] &&
|
|
|
|
[data1 length] == 0,
|
2011-02-16 08:21:17 +00:00
|
|
|
"+dataWithBytes:length works with 0 length");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
|
|
|
[data2 getBytes:hold range:NSMakeRange(2,6)];
|
2011-02-16 08:21:17 +00:00
|
|
|
PASS(strcmp(hold,"st str") == 0, "-getBytes:range works");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
|
|
|
PASS_EXCEPTION([data2 getBytes:hold
|
2011-02-16 08:21:17 +00:00
|
|
|
range:NSMakeRange(strlen(str2)*sizeof(void*),1)];,
|
2011-02-24 16:26:01 +00:00
|
|
|
NSRangeException,
|
2011-02-16 08:21:17 +00:00
|
|
|
"getBytes:range: with bad location");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
|
|
|
PASS_EXCEPTION([data2 getBytes:hold
|
2011-02-16 08:21:17 +00:00
|
|
|
range:NSMakeRange(1,(strlen(str2)*sizeof(void*)))];,
|
2011-02-24 16:26:01 +00:00
|
|
|
NSRangeException,
|
2011-02-16 08:21:17 +00:00
|
|
|
"getBytes:range: with bad length");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-24 16:26:01 +00:00
|
|
|
PASS_EXCEPTION([data2 subdataWithRange:NSMakeRange((strlen(str2)*sizeof(void*)),1)];,
|
|
|
|
NSRangeException,
|
2011-02-16 08:21:17 +00:00
|
|
|
"-subdataWithRange: with bad location");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-24 16:26:01 +00:00
|
|
|
PASS_EXCEPTION([data2 subdataWithRange:NSMakeRange(1,(strlen(str2)*sizeof(void*)))];,
|
|
|
|
NSRangeException,
|
2011-02-16 08:21:17 +00:00
|
|
|
"-subdataWithRange: with bad length");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
|
|
|
data2 = [NSData dataWithBytesNoCopy:str1
|
2011-02-16 08:21:17 +00:00
|
|
|
length:(strlen(str1) * sizeof(void*))
|
|
|
|
freeWhenDone:NO];
|
|
|
|
PASS(data2 != nil && [data2 isKindOfClass:[NSData class]] &&
|
|
|
|
[data2 length] == (strlen(str1) * sizeof(void*)) &&
|
2016-07-26 23:01:11 +00:00
|
|
|
[data2 bytes] == str1,
|
2011-02-16 08:21:17 +00:00
|
|
|
"+dataWithBytesNoCopy:length:freeWhenDone: works");
|
2016-07-26 23:01:11 +00:00
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
[arp release]; arp = nil;
|
2016-07-26 23:01:11 +00:00
|
|
|
|
|
|
|
{
|
2011-02-16 08:21:17 +00:00
|
|
|
BOOL didNotSegfault = YES;
|
|
|
|
PASS(didNotSegfault, "+dataWithBytesNoCopy:length:freeWhenDone:NO doesn't free memory");
|
|
|
|
}
|
2016-07-26 23:01:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
START_SET("deallocator blocks")
|
|
|
|
# ifndef __has_feature
|
|
|
|
# define __has_feature(x) 0
|
|
|
|
# endif
|
|
|
|
# if __has_feature(blocks)
|
|
|
|
uint8_t stackBuf[4] = { 1, 2, 3, 5 };
|
|
|
|
__block NSUInteger called = 0;
|
|
|
|
NSData *immutable =
|
|
|
|
[[NSData alloc] initWithBytesNoCopy: stackBuf
|
|
|
|
length: 4
|
|
|
|
deallocator: ^(void* bytes, NSUInteger length) {
|
|
|
|
called++;
|
|
|
|
}];
|
|
|
|
PASS_RUNS([immutable release]; immutable = nil;,
|
|
|
|
"No free() error with custom deallocator");
|
|
|
|
PASS(called == 1, "Deallocator block called");
|
|
|
|
uint8_t *buf = malloc(4 * sizeof(uint8_t));
|
|
|
|
NSMutableData *mutable =
|
|
|
|
[[NSMutableData alloc] initWithBytesNoCopy: buf
|
|
|
|
length: 4
|
|
|
|
deallocator: ^(void *bytes, NSUInteger len)
|
|
|
|
{
|
|
|
|
free(bytes);
|
|
|
|
called++;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
PASS_RUNS([mutable release]; mutable = nil;,
|
|
|
|
"No free() error with custom deallocator on mutable data");
|
|
|
|
PASS(called == 2, "Deallocator block called on -dealloc of mutable data");
|
|
|
|
buf = malloc(4 * sizeof(uint8_t));
|
|
|
|
mutable =
|
|
|
|
[[NSMutableData alloc] initWithBytesNoCopy: buf
|
|
|
|
length: 4
|
|
|
|
deallocator: ^(void *bytes, NSUInteger len)
|
|
|
|
{
|
|
|
|
free(bytes);
|
|
|
|
called++;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
PASS_RUNS([mutable setCapacity: 10];,
|
|
|
|
"Can set capactiy with custom deallocator on mutable data");
|
|
|
|
PASS(called == 3,
|
|
|
|
"Deallocator block called on -setCapacity: of mutable data");
|
|
|
|
PASS_RUNS([mutable release]; mutable = nil;,
|
|
|
|
"No free() error with custom deallocator on mutable data "
|
|
|
|
"after capacity change");
|
|
|
|
PASS(called == 3, "Deallocator block not called on -dealloc of mutable data "
|
|
|
|
"after its capacity has been changed");
|
|
|
|
# else
|
|
|
|
SKIP("No Blocks support in the compiler.")
|
|
|
|
# endif
|
|
|
|
END_SET("deallocator blocks")
|
2011-02-16 08:21:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|