Fix leaks

This commit is contained in:
rfm 2024-11-15 20:48:09 +00:00
parent 8209fa428f
commit 77c66e3d95
23 changed files with 287 additions and 259 deletions

View file

@ -469,6 +469,7 @@ static SEL appSel;
if (objectCount != [keys count])
{
RELEASE(self);
[NSException raise: NSInvalidArgumentException
format: @"init with obj and key arrays of different sizes"];
}

View file

@ -91,11 +91,11 @@ static Class NSFileHandle_ssl_class = nil;
{
if (self == NSFileHandle_abstract_class)
{
return NSAllocateObject (NSFileHandle_concrete_class, 0, z);
return NSAllocateObject(NSFileHandle_concrete_class, 0, z);
}
else
{
return NSAllocateObject (self, 0, z);
return NSAllocateObject(self, 0, z);
}
}

View file

@ -989,7 +989,7 @@ static gs_mutex_t classLock = GS_MUTEX_INIT_STATIC;
errorHandler: handler
for: self];
return direnum;
return AUTORELEASE(direnum);
}
- (NSArray*) contentsOfDirectoryAtPath: (NSString*)path error: (NSError**)error

View file

@ -121,6 +121,7 @@
[_invocation getReturnValue: buffer];
result = [NSValue valueWithBytes: buffer objCType: returnType];
free(buffer);
}
}
return result;

View file

@ -75,8 +75,6 @@ static void *isFinishedCtxt = (void*)"isFinished";
static void *isReadyCtxt = (void*)"isReady";
static void *queuePriorityCtxt = (void*)"queuePriority";
static NSArray *empty = nil;
@interface NSOperation (Private)
- (void) _finish;
- (void) _updateReadyState;
@ -91,12 +89,6 @@ static NSArray *empty = nil;
return NO;
}
+ (void) initialize
{
empty = [NSArray new];
RELEASE([NSObject leakAt: &empty]);
}
- (void) addDependency: (NSOperation *)op
{
if (NO == [op isKindOfClass: [NSOperation class]])
@ -228,7 +220,7 @@ static NSArray *empty = nil;
if (internal->dependencies == nil)
{
a = empty; // OSX return an empty array
a = [NSArray array]; // OSX return an empty array
}
else
{

View file

@ -4,44 +4,46 @@
int main()
{
NSArray *obj;
NSMutableArray *testObjs = [[NSMutableArray alloc] init];
NSString *str;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSArray *obj;
NSMutableArray *testObjs = [NSMutableArray array];
NSString *str;
test_alloc(@"NSArray");
obj = [NSArray new];
obj = [NSArray array];
PASS((obj != nil && [obj count] == 0),"can create an empty array");
str = @"hello";
[testObjs addObject: obj];
obj = [NSArray arrayWithObject:str];
PASS((obj != nil && [obj count] == 1), "can create an array with one element");
obj = [NSArray arrayWithObject: str];
PASS((obj != nil && [obj count] == 1),
"can create an array with one element")
[testObjs addObject: obj];
test_NSObject(@"NSArray", testObjs);
test_NSCoding(testObjs);
test_keyed_NSCoding(testObjs);
test_NSCopying(@"NSArray",@"NSMutableArray",testObjs,YES,NO);
test_NSMutableCopying(@"NSArray",@"NSMutableArray",testObjs);
test_NSCopying(@"NSArray", @"NSMutableArray", testObjs, YES, NO);
test_NSMutableCopying(@"NSArray", @"NSMutableArray", testObjs);
obj = [NSArray arrayWithContentsOfFile: @"test.plist"];
PASS((obj != nil && [obj count] > 0),"can create an array from file");
PASS((obj != nil && [obj count] > 0),"can create an array from file")
#if 1
/* The apple foundation is arguably buggy in that it seems to create a
* mutable array ... we currently copy that
*/
PASS([obj isKindOfClass: [NSMutableArray class]] == YES,"array mutable");
PASS_RUNS([(NSMutableArray*)obj addObject: @"x"],"can add to array");
PASS([obj isKindOfClass: [NSMutableArray class]] == YES, "array mutable")
PASS_RUNS([(NSMutableArray*)obj addObject: @"x"], "can add to array")
#else
PASS([obj isKindOfClass: [NSMutableArray class]] == NO,"array immutable");
PASS([obj isKindOfClass: [NSMutableArray class]] == NO, "array immutable")
#endif
obj = [obj objectAtIndex: 0];
PASS([obj isKindOfClass: [NSMutableArray class]] == YES,"array mutable");
PASS([obj isKindOfClass: [NSMutableArray class]] == YES, "array mutable")
START_SET("NSArray subscripting")
# ifndef __has_feature
# define __has_feature(x) 0
# endif
#if __has_feature(objc_subscripting)
NSArray *a = @[ @"foo", @"bar" ];
PASS([@"foo" isEqualToString:a[0]], "Array subscripting works");
PASS([@"foo" isEqualToString:a[0]], "Array subscripting works")
# else
SKIP("No collection subscripting support in the compiler.")
# endif

View file

@ -3,8 +3,10 @@
#import "ObjectTesting.h"
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSArray *arr = [NSArray arrayWithObject:[NSAttributedString new]];
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSArray *arr;
arr = [NSArray arrayWithObject: AUTORELEASE([NSAttributedString new])];
test_alloc(@"NSAttributedString");
test_NSObject(@"NSAttributedString", arr);

View file

@ -5,46 +5,47 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSAttributedString *s;
NSString *key1, *val1, *str1;
NSRange r = NSMakeRange(0,6);
NSAttributedString *astr1, *astr2;
NSDictionary *dict1;
NSRange range = NSMakeRange(0,0);
id obj;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSAttributedString *s;
NSString *key1, *val1, *str1;
NSRange r = NSMakeRange(0,6);
NSAttributedString *astr1, *astr2;
NSDictionary *dict1;
NSRange range = NSMakeRange(0,0);
id obj;
s = [[[NSAttributedString alloc] initWithString: @"string"] autorelease];
PASS_EQUAL([s string], @"string", "equality OK for string value");
PASS([s length] == 6, "length reported correctly");
s = AUTORELEASE([[NSAttributedString alloc] initWithString: @"string"]);
PASS_EQUAL([s string], @"string", "equality OK for string value")
PASS([s length] == 6, "length reported correctly")
PASS_EQUAL([s attributesAtIndex: 0 effectiveRange: NULL],
[NSDictionary dictionary], "returnsempty attributes dictionary, not nil");
[NSDictionary dictionary], "returnsempty attributes dictionary, not nil")
key1 = @"Helvetica 12-point";
val1 = @"NSFontAttributeName";
str1 = @"Attributed string test";
dict1 = [NSDictionary dictionaryWithObject:val1 forKey:key1];
dict1 = [NSDictionary dictionaryWithObject: val1 forKey: key1];
astr1 = [[NSAttributedString alloc] initWithString: str1 attributes: dict1];
PASS(astr1 != nil && [astr1 isKindOfClass: [NSAttributedString class]] &&
[[astr1 string] isEqual: str1],"-initWithString:attributes: works");
astr1 = AUTORELEASE([[NSAttributedString alloc]
initWithString: str1 attributes: dict1]);
PASS(astr1 != nil && [astr1 isKindOfClass: [NSAttributedString class]]
&& [[astr1 string] isEqual: str1], "-initWithString:attributes: works")
obj = [astr1 attributesAtIndex: 0 effectiveRange: &range];
PASS(obj != nil && [obj isKindOfClass: [NSDictionary class]] &&
[obj count] == 1 && range.length != 0,
"-attributesAtIndex:effectiveRange: works");
PASS(obj != nil && [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 1 && range.length != 0,
"-attributesAtIndex:effectiveRange: works")
obj = [astr1 attribute: key1 atIndex: 0 effectiveRange: &range];
PASS(obj != nil && [obj isEqual: val1] && range.length != 0,
"-attribute:atIndex:effectiveRange: works");
"-attribute:atIndex:effectiveRange: works")
obj = [astr1 attributedSubstringFromRange: r];
PASS(obj != nil && [obj isKindOfClass: [NSAttributedString class]] &&
[obj length] == r.length,"-attributedSubstringFromRange works");
PASS(obj != nil && [obj isKindOfClass: [NSAttributedString class]]
&& [obj length] == r.length, "-attributedSubstringFromRange works")
r = NSMakeRange(0,[astr1 length]);
r = NSMakeRange(0, [astr1 length]);
astr2 = [astr1 attributedSubstringFromRange: r];
PASS(astr2 != nil && [astr1 isEqualToAttributedString: astr2],
"extract and compare using -isEqualToAttributedString works");
"extract and compare using -isEqualToAttributedString works")
[arp release]; arp = nil;
return 0;

View file

@ -3,13 +3,12 @@
#import <Foundation/NSException.h>
@interface TestClass : NSObject
- (void)runTest;
- (void)exceptionThrowingMethod;
- (void) runTest;
- (void) exceptionThrowingMethod;
@end
@implementation TestClass
- (void)runTest
- (void) runTest
{
int c = 0;
int i;
@ -19,21 +18,26 @@
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NS_DURING
[self exceptionThrowingMethod];
[self exceptionThrowingMethod];
NS_HANDLER
c++;
c++;
NS_ENDHANDLER
[pool release];
}
PASS(c == 10, "Caught the correct number of exceptions without breaking the autorelease pool\n");
PASS(c == 10, "Caught the correct number of exceptions"
" without breaking the autorelease pool")
}
- (void)exceptionThrowingMethod
- (void) exceptionThrowingMethod
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
[@"Hello" stringByAppendingString: @" something to create a autoreleased object"];
NSAutoreleasePool *pool = [NSAutoreleasePool new];
[@"Hello" stringByAppendingString:
@" something to create a autoreleased object"];
NSLog(@"Throwing an exception");
[[NSException exceptionWithName: @"MyFunException" reason: @"it was always meant to happen" userInfo: [NSDictionary dictionary]] raise];
[[NSException exceptionWithName: @"MyFunException"
reason: @"it was always meant to happen"
userInfo: [NSDictionary dictionary]] raise];
[pool release]; // Obviously this doesn't get run, but the [NSAutorelease new] at the top causes the problem
}
@ -41,8 +45,9 @@
int main(int argc, char** argv)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
TestClass *testClass = [[TestClass new] autorelease];
NSAutoreleasePool *pool = [NSAutoreleasePool new];
TestClass *testClass = [[TestClass new] autorelease];
[testClass runTest];
[pool release];
PASS(1, "Destroying pools in the wrong order didn't break anything...");

View file

@ -7,7 +7,8 @@ int main()
NSAutoreleasePool *arp = [NSAutoreleasePool new];
test_alloc(@"NSBundle");
test_NSObject(@"NSBundle", [NSArray arrayWithObject:[NSBundle new]]);
test_NSObject(@"NSBundle",
[NSArray arrayWithObject: AUTORELEASE([NSBundle new])]);
[arp release]; arp = nil;
return 0;
}

View file

@ -31,29 +31,38 @@ int main()
START_SET("NSCalendar getEra:year:month:day:fromDate and getHour:minute:second:nanosecond:fromDate tests");
/* Test getEra:year:month:day:fromDate:
*/
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier:
[NSLocale canonicalLocaleIdentifierFromString: @"en_US"]]];
dateFormatter = AUTORELEASE([[NSDateFormatter alloc] init]);
[dateFormatter setLocale: AUTORELEASE([[NSLocale alloc]
initWithLocaleIdentifier: [NSLocale
canonicalLocaleIdentifierFromString: @"en_US"]])];
cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneWithName: @"America/New_York"]];
[cal setTimeZone: [NSTimeZone timeZoneWithName: @"America/New_York"]];
[dateFormatter setDateFormat: @"d MMM yyyy HH:mm:ss Z"];
date = [dateFormatter dateFromString:@"22 Nov 1969 08:15:00 Z"];
date = [dateFormatter dateFromString: @"22 Nov 1969 08:15:00 Z"];
[cal getEra:&era year:&year month:&month day:&day fromDate:date];
[cal getEra: &era year: &year month: &month day: &day fromDate: date];
PASS(era == 1, "getEra:year:month:day:fromDate: returns correct era");
PASS(year == 1969, "getEra:year:month:day:fromDate: returns correct year");
PASS(month == 11, "getEra:year:month:day:fromDate: returns correct month");
PASS(day == 22, "getEra:year:month:day:fromDate: returns correct day");
PASS(era == 1, "getEra:year:month:day:fromDate: returns correct era")
PASS(year == 1969, "getEra:year:month:day:fromDate: returns correct year")
PASS(month == 11, "getEra:year:month:day:fromDate: returns correct month")
PASS(day == 22, "getEra:year:month:day:fromDate: returns correct day")
/* Test getHour:minute:second:nanosecond:fromDate:
*/
[cal getHour:&hour minute:&min second:&sec nanosecond:&nano fromDate:date];
[cal getHour: &hour
minute: &min
second: &sec
nanosecond: &nano
fromDate: date];
PASS(hour == 3, "getHour:minute:second:nanosecond:fromDate: returns correct hour");
PASS(min == 15, "getHour:minute:second:nanosecond:fromDate: returns correct minute");
PASS(sec == 0, "getHour:minute:second:nanosecond:fromDate: returns correct second");
PASS(nano == 0, "getHour:minute:second:nanosecond:fromDate: returns correct nanosecond");
PASS(hour == 3, "getHour:minute:second:nanosecond:fromDate:"
" returns correct hour")
PASS(min == 15, "getHour:minute:second:nanosecond:fromDate:"
" returns correct minute")
PASS(sec == 0, "getHour:minute:second:nanosecond:fromDate:"
" returns correct second")
PASS(nano == 0, "getHour:minute:second:nanosecond:fromDate:"
" returns correct nanosecond")
END_SET("NSCalendar getEra:year:month:day:fromDate and getHour:minute:second:nanosecond:fromDate tests");
return 0;

View file

@ -5,8 +5,8 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
id testObj = [NSCalendarDate new];
NSAutoreleasePool *arp = [NSAutoreleasePool new];
id testObj = AUTORELEASE([NSCalendarDate new]);
test_NSObject(@"NSCalendarDate", [NSArray arrayWithObject: testObj]);
test_NSCoding([NSArray arrayWithObject: testObj]);

View file

@ -9,15 +9,15 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSMutableArray *tmpArray;
NSMutableDictionary *myLocale;
NSCalendarDate *myBirthday;
NSCalendarDate *anotherDay;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSMutableArray *tmpArray;
NSMutableDictionary *myLocale;
NSCalendarDate *myBirthday;
NSCalendarDate *anotherDay;
myLocale = westernLocale();
tmpArray = [NSMutableArray new];
tmpArray = [NSMutableArray array];
[tmpArray addObject: @"Gen"];
[tmpArray addObject: @"Feb"];
[tmpArray addObject: @"Mar"];
@ -32,7 +32,7 @@ int main()
[tmpArray addObject: @"Dic"];
[myLocale setObject: tmpArray forKey: NSShortMonthNameArray];
ASSIGN(tmpArray,[NSMutableArray new]);
tmpArray = [NSMutableArray array];
[tmpArray addObject: @"Gennaio"];
[tmpArray addObject: @"Febbraio"];
[tmpArray addObject: @"Marzo"];
@ -47,7 +47,7 @@ int main()
[tmpArray addObject: @"Dicembre"];
[myLocale setObject: tmpArray forKey: NSMonthNameArray];
ASSIGN(tmpArray,[NSMutableArray new]);
tmpArray = [NSMutableArray array];
[tmpArray addObject: @"Dom"];
[tmpArray addObject: @"Lun"];
[tmpArray addObject: @"Mar"];
@ -57,7 +57,7 @@ int main()
[tmpArray addObject: @"Sab"];
[myLocale setObject: tmpArray forKey: NSShortWeekDayNameArray];
ASSIGN(tmpArray,[NSMutableArray new]);
tmpArray = [NSMutableArray array];
[tmpArray addObject: @"Domencia"];
[tmpArray addObject: @"Lunedi"];
[tmpArray addObject: @"Martedi"];
@ -67,7 +67,7 @@ int main()
[tmpArray addObject: @"Sabato"];
[myLocale setObject: tmpArray forKey: NSWeekDayNameArray];
ASSIGN(tmpArray,[NSMutableArray new]);
tmpArray = [NSMutableArray array];
[tmpArray addObject: @"AM"];
[tmpArray addObject: @"PM"];
[myLocale setObject: tmpArray forKey: NSAMPMDesignation];

View file

@ -81,6 +81,7 @@ int main()
strEnc = [data base64EncodedStringWithOptions:0];
data = [[NSData alloc] initWithBase64EncodedString: strEnc options: 0];
str2 = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
[data release];
PASS_EQUAL(str1, str2, "Encode / Decode no lines")
[str2 release];
@ -90,6 +91,7 @@ int main()
data = [[NSData alloc] initWithBase64EncodedString: strEnc
options: NSDataBase64DecodingIgnoreUnknownCharacters];
str2 = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
[data release];
PASS_EQUAL(str1, str2, "Encode / Decode 64 - LF")
[str2 release];
@ -99,6 +101,7 @@ int main()
data = [[NSData alloc] initWithBase64EncodedString: strEnc
options: NSDataBase64DecodingIgnoreUnknownCharacters];
str2 = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
[data release];
PASS_EQUAL(str1, str2, "Encode / Decode 76 - LF")
[str2 release];
@ -108,6 +111,7 @@ int main()
data = [[NSData alloc] initWithBase64EncodedString: strEnc
options: NSDataBase64DecodingIgnoreUnknownCharacters];
str2 = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
[data release];
PASS_EQUAL(str1, str2, "Encode / Decode 64 - CR")
[str2 release];
@ -117,6 +121,7 @@ int main()
data = [[NSData alloc] initWithBase64EncodedString: strEnc
options: NSDataBase64DecodingIgnoreUnknownCharacters];
str2 = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
[data release];
PASS_EQUAL(str1, str2, "Encode / Decode 64 - implicit CR LF")
[str2 release];

View file

@ -4,18 +4,19 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
id testObject = [NSData new];
NSAutoreleasePool *arp = [NSAutoreleasePool new];
id testObject = AUTORELEASE([NSData new]);
test_alloc(@"NSData");
test_NSObject(@"NSData",[NSArray arrayWithObject:testObject]);
test_NSCoding([NSArray arrayWithObject:testObject]);
test_keyed_NSCoding([NSArray arrayWithObject:testObject]);
test_NSObject(@"NSData", [NSArray arrayWithObject: testObject]);
test_NSCoding([NSArray arrayWithObject: testObject]);
test_keyed_NSCoding([NSArray arrayWithObject: testObject]);
test_NSCopying(@"NSData",
@"NSMutableData",
[NSArray arrayWithObject:testObject], NO, NO);
[NSArray arrayWithObject: testObject], NO, NO);
test_NSMutableCopying(@"NSData",
@"NSMutableData",
[NSArray arrayWithObject:testObject]);
[NSArray arrayWithObject: testObject]);
[arp release]; arp = nil;
return 0;

View file

@ -6,67 +6,69 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
char *str1,*str2;
NSData *data1, *data2;
NSMutableData *mutable;
char *hold;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
char *str1, *str2;
NSData *data1, *data2;
NSMutableData *mutable;
char *hold;
str1 = "Test string for data classes";
str2 = (char *) malloc(sizeof("Test string for data classes not copied"));
strcpy(str2,"Test string for data classes not copied");
mutable = [NSMutableData dataWithLength:100];
mutable = [NSMutableData dataWithLength: 100];
hold = [mutable mutableBytes];
/* 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");
data1 = [NSData dataWithBytes: str1 length: strlen(str1) + 1];
PASS(data1 != nil
&& [data1 isKindOfClass: [NSData class]]
&& [data1 length] == strlen(str1) + 1
&& [data1 bytes] != str1
&& strcmp(str1, [data1 bytes]) == 0,
"+dataWithBytes:length: works")
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");
data2 = [NSData dataWithBytesNoCopy: str2
length: strlen(str2) + 1];
PASS(data2 != nil && [data2 isKindOfClass: [NSData class]]
&& [data2 length] == strlen(str2) + 1
&& [data2 bytes] == str2,
"+dataWithBytesNoCopy:length: works")
data1 = [NSData dataWithBytes:nil length:0];
PASS(data1 != nil && [data1 isKindOfClass:[NSData class]] &&
[data1 length] == 0,
"+dataWithBytes:length works with 0 length");
data1 = [NSData dataWithBytes: nil length: 0];
PASS(data1 != nil && [data1 isKindOfClass: [NSData class]]
&& [data1 length] == 0,
"+dataWithBytes:length works with 0 length")
[data2 getBytes:hold range:NSMakeRange(2,6)];
PASS(strcmp(hold,"st str") == 0, "-getBytes:range works");
[data2 getBytes: hold range: NSMakeRange(2,6)];
PASS(strcmp(hold, "st str") == 0, "-getBytes:range works")
PASS_EXCEPTION([data2 getBytes:hold
range:NSMakeRange(strlen(str2)*sizeof(void*),1)];,
NSRangeException,
"getBytes:range: with bad location");
PASS_EXCEPTION([data2 getBytes: hold
range: NSMakeRange(strlen(str2) + 1, 1)];,
NSRangeException,
"getBytes:range: with bad location")
PASS_EXCEPTION([data2 getBytes:hold
range:NSMakeRange(1,(strlen(str2)*sizeof(void*)))];,
NSRangeException,
"getBytes:range: with bad length");
PASS_EXCEPTION([data2 getBytes: hold
range: NSMakeRange(1, strlen(str2) + 1)];,
NSRangeException,
"getBytes:range: with bad length")
PASS_EXCEPTION([data2 subdataWithRange:NSMakeRange((strlen(str2)*sizeof(void*)),1)];,
NSRangeException,
"-subdataWithRange: with bad location");
PASS_EXCEPTION([data2 subdataWithRange:
NSMakeRange(strlen(str2) + 1, 1)];,
NSRangeException,
"-subdataWithRange: with bad location")
PASS_EXCEPTION([data2 subdataWithRange:NSMakeRange(1,(strlen(str2)*sizeof(void*)))];,
NSRangeException,
"-subdataWithRange: with bad length");
PASS_EXCEPTION([data2 subdataWithRange:
NSMakeRange(1, strlen(str2) + 1)];,
NSRangeException,
"-subdataWithRange: with bad length")
data2 = [NSData dataWithBytesNoCopy:str1
length:(strlen(str1) * sizeof(void*))
freeWhenDone:NO];
PASS(data2 != nil && [data2 isKindOfClass:[NSData class]] &&
[data2 length] == (strlen(str1) * sizeof(void*)) &&
[data2 bytes] == str1,
"+dataWithBytesNoCopy:length:freeWhenDone: works");
data2 = [NSData dataWithBytesNoCopy: str1
length: strlen(str1) + 1
freeWhenDone: NO];
PASS(data2 != nil && [data2 isKindOfClass: [NSData class]]
&& [data2 length] == (strlen(str1) + 1)
&& [data2 bytes] == str1,
"+dataWithBytesNoCopy:length:freeWhenDone: works")
[arp release]; arp = nil;

View file

@ -7,22 +7,24 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSMutableArray *testObjs = [NSMutableArray new];
NSDictionary *obj;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSMutableArray *testObjs = [NSMutableArray array];
NSDictionary *obj;
test_alloc(@"NSDictionary");
obj = [NSDictionary new];
[testObjs addObject:obj];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 0,
"can create an empty dictionary");
obj = [NSDictionary dictionaryWithObject:@"Hello" forKey:@"Key"];
[testObjs addObject:obj];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 1,
"can create a dictionary with one element");
obj = [NSDictionary dictionary];
[testObjs addObject: obj];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 0,
"can create an empty dictionary")
obj = [NSDictionary dictionaryWithObject: @"Hello" forKey: @"Key"];
[testObjs addObject: obj];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 1,
"can create a dictionary with one element")
test_NSObject(@"NSDictionary", testObjs);
test_NSCoding(testObjs);
@ -36,7 +38,7 @@ int main()
#if __has_feature(objc_subscripting)
NSDictionary *dictionary = @{@123 : @123.4 ,
@"date" : @"today" };
PASS([dictionary[@123] isEqual: @123.4], "Dictionary subscripting works");
PASS([dictionary[@123] isEqual: @123.4], "Dictionary subscripting works")
# else
SKIP("No dictionary subscripting support in the compiler.")
# endif

View file

@ -4,75 +4,77 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSString *key1, *key2, *key3, *val1, *val2, *val3;
NSArray *keys1, *keys2, *keys3, *vals1, *vals2, *vals3;
NSDictionary *obj,*old;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSString *key1, *key2, *key3, *val1, *val2, *val3;
NSArray *keys1, *keys2, *keys3, *vals1, *vals2, *vals3;
NSDictionary *obj,*old;
old = nil;
key1 = @"Key1";
key2 = @"Key2";
key3 = @"Key3";
keys1 = [NSArray arrayWithObjects:key1, key2, nil];
keys2 = [NSArray arrayWithObjects:key1, key2, key3, nil];
keys1 = [NSArray arrayWithObjects: key1, key2, nil];
keys2 = [NSArray arrayWithObjects: key1, key2, key3, nil];
/* duplicate keys */
keys3 = [NSArray arrayWithObjects:key1, key2, key2, nil];
keys3 = [NSArray arrayWithObjects: key1, key2, key2, nil];
val1 = @"Kidnapped";
val2 = @"tied up";
val3 = @"taken away and helf for ransom";
vals1 = [NSArray arrayWithObjects:val1, val2, nil];
vals1 = [NSArray arrayWithObjects: val1, val2, nil];
/* duplicate values */
vals2 = [NSArray arrayWithObjects:val1, val2, val2, nil];
vals3 = [NSArray arrayWithObjects:val1, val2, val3, nil];
obj = [NSDictionary new];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 0,
"+new creates an empty dictionary");
vals2 = [NSArray arrayWithObjects: val1, val2, val2, nil];
vals3 = [NSArray arrayWithObjects: val1, val2, val3, nil];
obj = [NSDictionary dictionary];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 0,
"+new creates an empty dictionary")
obj = [NSDictionary dictionary];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 0,
"+dictionary creates an empty dictionary");
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 0,
"+dictionary creates an empty dictionary")
PASS_EXCEPTION([NSDictionary dictionaryWithObject:val1 forKey:nil];,
NSInvalidArgumentException,
"+dictionaryWithObject:forKey: with nil key");
PASS_EXCEPTION([NSDictionary dictionaryWithObject: val1 forKey: nil];,
NSInvalidArgumentException,
"+dictionaryWithObject:forKey: with nil key")
PASS_EXCEPTION([NSDictionary dictionaryWithObject:nil forKey:key1];,
NSInvalidArgumentException,
"+dictionaryWithObject:forKey: with nil value");
PASS_EXCEPTION([NSDictionary dictionaryWithObject: nil forKey: key1];,
NSInvalidArgumentException,
"+dictionaryWithObject:forKey: with nil value")
obj = [NSDictionary dictionaryWithObject:val1 forKey:key1];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 1,
"+dictionaryWithObject:forKey: builds minimal dictionary");
obj = [NSDictionary dictionaryWithObject: val1 forKey: key1];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 1,
"+dictionaryWithObject:forKey: builds minimal dictionary")
obj = [NSDictionary dictionaryWithObjects: vals1 forKeys: keys1];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 2,
"+dictionaryWithObjects:forKeys: builds a dictionary")
PASS_EXCEPTION([NSDictionary dictionaryWithObjects: vals1 forKeys: keys2];,
NSInvalidArgumentException,
"+dictionaryWithObjects:forKeys: with arrays of different sizes")
obj = [NSDictionary dictionaryWithObjects: vals2 forKeys: keys2];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 3,
"we can have multiple identical objects in a dictionary")
obj = [NSDictionary dictionaryWithObjects:vals1 forKeys:keys1];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 2,
"+dictionaryWithObjects:forKeys: builds a dictionary");
PASS_EXCEPTION([NSDictionary dictionaryWithObjects:vals1 forKeys:keys2];,
NSInvalidArgumentException,
"+dictionaryWithObjects:forKeys: with arrays of different sizes");
obj = [NSDictionary dictionaryWithObjects:vals2 forKeys:keys2];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 3,
"we can have multiple identical objects in a dictionary");
obj = [NSDictionary dictionaryWithObjects:vals3 forKeys:keys3];
PASS(obj != nil &&
[obj isKindOfClass:[NSDictionary class]] &&
[obj count] == 2,
"we can't have multiple identical keys in a dictionary");
obj = [NSDictionary dictionaryWithObjects: vals3 forKeys: keys3];
PASS(obj != nil
&& [obj isKindOfClass: [NSDictionary class]]
&& [obj count] == 2,
"we can't have multiple identical keys in a dictionary")
old = obj;
obj = [NSDictionary dictionaryWithDictionary:old];
PASS(obj != nil &&
[obj isEqual: old], "+dictionaryWithDictionary: copies dictionary");
obj = [NSDictionary dictionaryWithDictionary: old];
PASS(obj != nil
&& [obj isEqual: old], "+dictionaryWithDictionary: copies dictionary")
[arp release]; arp = nil;
return 0;

View file

@ -11,7 +11,7 @@ int main()
NSDistributedLock *lock2;
test_NSObject(@"NSDistributedLock",
[NSArray arrayWithObject: [NSDistributedLock new]]);
[NSArray arrayWithObject: AUTORELEASE([NSDistributedLock new])]);
path = [[NSFileManager defaultManager] currentDirectoryPath];
path = [path stringByAppendingPathComponent: @"MyLock"];

View file

@ -11,10 +11,10 @@ void fast_enumeration_mutation_add(id mutableCollection)
NSUInteger i = 0;
FOR_IN(id, o, mutableCollection)
if (i == [mutableCollection count]/2) {
if ([mutableCollection isKindOfClass:[NSMutableDictionary class]]) {
[mutableCollection setObject:@"boom" forKey:@"boom"];
if ([mutableCollection isKindOfClass: [NSMutableDictionary class]]) {
[mutableCollection setObject: @"boom" forKey: @"boom"];
} else {
[mutableCollection addObject:@"boom"];
[mutableCollection addObject: @"boom"];
}
}
i++;
@ -26,10 +26,10 @@ void fast_enumeration_mutation_remove(id mutableCollection)
NSUInteger i = 0;
FOR_IN(id, o, mutableCollection)
if (i == [mutableCollection count]/2) {
if ([mutableCollection isKindOfClass:[NSMutableDictionary class]]) {
[mutableCollection removeObjectForKey:o];
if ([mutableCollection isKindOfClass: [NSMutableDictionary class]]) {
[mutableCollection removeObjectForKey: o];
} else {
[mutableCollection removeObject:o];
[mutableCollection removeObject: o];
}
}
i++;
@ -38,25 +38,27 @@ void fast_enumeration_mutation_remove(id mutableCollection)
void test_fast_enumeration(id collection, NSArray *objects)
{
NSMutableArray *returnedObjects = [[NSMutableArray alloc] init];
NSMutableArray *returnedObjects = [NSMutableArray array];
FOR_IN(id, o, collection)
[returnedObjects addObject:o];
[returnedObjects addObject: o];
END_FOR_IN(collection)
if (!([collection isKindOfClass:[NSArray class]] ||
[collection isKindOfClass:[NSOrderedSet class]])) {
[returnedObjects sortUsingSelector:@selector(compare:)];
}
PASS_EQUAL(returnedObjects, objects, "fast enumeration returns all objects");
if (!([collection isKindOfClass: [NSArray class]]
|| [collection isKindOfClass: [NSOrderedSet class]]))
{
[returnedObjects sortUsingSelector: @selector(compare:)];
}
PASS_EQUAL(returnedObjects, objects, "fast enumeration returns all objects")
id mutableCollection = [collection mutableCopy];
PASS_EXCEPTION(
fast_enumeration_mutation_add(mutableCollection),
NSGenericException,
"Fast enumeration mutation add properly calls @\"NSGenericException\"");
"Fast enumeration mutation add properly calls @\"NSGenericException\"")
PASS_EXCEPTION(
fast_enumeration_mutation_remove(mutableCollection),
NSGenericException,
"Fast enumeration mutation remove properly calls @\"NSGenericException\"");
"Fast enumeration mutation remove properly calls @\"NSGenericException\"")
[mutableCollection release];
}
@ -67,26 +69,26 @@ int main()
NSMutableArray *objects = [NSMutableArray array];
int i;
for (i = 0; i < 10000; i++) {
[objects addObject:[NSString stringWithFormat:@"%.4d", i]];
[objects addObject: [NSString stringWithFormat: @"%.4d", i]];
}
START_SET("NSArray")
id array = [NSArray arrayWithArray:objects];
id array = [NSArray arrayWithArray: objects];
test_fast_enumeration(array, objects);
END_SET("NSArray")
START_SET("NSSet")
id set = [NSSet setWithArray:objects];
id set = [NSSet setWithArray: objects];
test_fast_enumeration(set, objects);
END_SET("NSSet")
START_SET("NSOrderedSet")
id orderedSet = [NSOrderedSet orderedSetWithArray:objects];
id orderedSet = [NSOrderedSet orderedSetWithArray: objects];
test_fast_enumeration(orderedSet, objects);
END_SET("NSOrderedSet")
START_SET("NSDictionary")
id dict = [NSDictionary dictionaryWithObjects:objects forKeys:objects];
id dict = [NSDictionary dictionaryWithObjects: objects forKeys: objects];
test_fast_enumeration(dict, objects);
END_SET("NSDictionary")

View file

@ -36,43 +36,43 @@ int main()
NSFileHandle *stdInFH = [NSFileHandle fileHandleWithStandardInput];
NSFileHandle *stdNullFH = [NSFileHandle fileHandleWithNullDevice];
NSFileHandle *t1FH, *t2FH;
NSString *tPath = [NSString stringWithFormat:@"%@/%@",NSTemporaryDirectory(),[[NSProcessInfo processInfo]globallyUniqueString]];
NSData *t1Data = [tPath dataUsingEncoding:NSUTF8StringEncoding];
NSString *tPath = [NSString stringWithFormat: @"%@/%@",NSTemporaryDirectory(),[[NSProcessInfo processInfo]globallyUniqueString]];
NSData *t1Data = [tPath dataUsingEncoding: NSUTF8StringEncoding];
NSData *t2Data;
PASS([stdInFH isKindOfClass:[NSFileHandle class]],
"NSFileHandle understands +fileHandleWithStandardInput");
PASS([stdInFH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands +fileHandleWithStandardInput")
PASS([stdInFH fileDescriptor]==0,
"NSFileHandle +fileHandleWithStandardInput has 0 as fileDescriptor");
"NSFileHandle +fileHandleWithStandardInput has 0 as fileDescriptor")
PASS([stdOutFH isKindOfClass:[NSFileHandle class]],
"NSFileHandle understands +fileHandleWithStandardOutput");
PASS([stdOutFH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands +fileHandleWithStandardOutput")
PASS([stdOutFH fileDescriptor]==1,
"NSFileHandle +fileHandleWithStandardOutput has 1 as fileDescriptor");
"NSFileHandle +fileHandleWithStandardOutput has 1 as fileDescriptor")
PASS([stdErrFH isKindOfClass:[NSFileHandle class]],
"NSFileHandle understands +fileHandleWithStandardError");
PASS([stdErrFH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands +fileHandleWithStandardError")
PASS([stdErrFH fileDescriptor]==2,
"NSFileHandle +fileHandleWithStandardError has 2 as fileDescriptor");
PASS([stdNullFH isKindOfClass:[NSFileHandle class]],
"NSFileHandle understands +fileHandleWithNullDevice");
PASS([stdNullFH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands +fileHandleWithNullDevice")
t1FH = [[NSFileHandle alloc] initWithFileDescriptor: 0];
PASS([t1FH isKindOfClass:[NSFileHandle class]],
"NSFileHandle understands -initWithFileDescriptor:");
t1FH = AUTORELEASE([[NSFileHandle alloc] initWithFileDescriptor: 0]);
PASS([t1FH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands -initWithFileDescriptor:")
t1FH = [NSFileHandle fileHandleForWritingAtPath: tPath];
PASS(t1FH == nil,
"NSFileHandle +fileHandleForWritingAtPath: with non-existing file return nil");
PASS(t1FH == nil, "NSFileHandle +fileHandleForWritingAtPath:"
" with non-existing file return nil")
[@"" writeToFile: tPath atomically: YES];
t1FH = [NSFileHandle fileHandleForWritingAtPath: tPath];
PASS([t1FH isKindOfClass:[NSFileHandle class]],
PASS([t1FH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands +fileHandleForWritingAtPath:");
t2FH = [NSFileHandle fileHandleForReadingAtPath: tPath];
PASS([t2FH isKindOfClass:[NSFileHandle class]],
PASS([t2FH isKindOfClass: [NSFileHandle class]],
"NSFileHandle understands +fileHandleForReadingAtPath:");
[t1FH writeData: t1Data];

View file

@ -56,7 +56,7 @@ int main()
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *dir = @"NSFileManagerTestDir";
MyHandler *handler = [MyHandler new];
MyHandler *handler = AUTORELEASE([MyHandler new]);
NSDictionary *attr;
NSString *dirInDir;
NSString *str1;

View file

@ -16,7 +16,7 @@ int main()
NSInteger length;
NSString *hello = @"hello";
NSString *uppercaseHello;
NSOperationQueue *queue = [NSOperationQueue new];
NSOperationQueue *queue = AUTORELEASE([NSOperationQueue new]);
op = [[NSInvocationOperation alloc] initWithTarget: hello
selector: @selector(length)