2011-02-16 08:21:17 +00:00
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2024-11-12 12:03:24 +00:00
|
|
|
#import <Foundation/NSHashTable.h>
|
2011-02-16 08:21:17 +00:00
|
|
|
#import "ObjectTesting.h"
|
|
|
|
|
2024-11-12 12:03:24 +00:00
|
|
|
@interface MyClass: NSObject
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MyClass
|
2024-11-12 14:27:06 +00:00
|
|
|
- (NSUInteger) hash
|
|
|
|
{
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
{
|
|
|
|
return [other isKindOfClass: [self class]];
|
|
|
|
}
|
2024-11-12 12:03:24 +00:00
|
|
|
#if 0
|
|
|
|
- (oneway void) release
|
|
|
|
{
|
|
|
|
NSLog(@"releasing %u", (unsigned)[self retainCount]);
|
|
|
|
[super release];
|
|
|
|
}
|
|
|
|
- (id) retain
|
|
|
|
{
|
|
|
|
id result = [super retain];
|
|
|
|
|
|
|
|
NSLog(@"retained %u", (unsigned)[self retainCount]);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
@end
|
|
|
|
|
2011-02-16 08:21:17 +00:00
|
|
|
int main()
|
|
|
|
{
|
2024-11-12 12:03:24 +00:00
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
NSHashTable *t;
|
|
|
|
MyClass *o;
|
2024-11-12 14:27:06 +00:00
|
|
|
MyClass *o2;
|
2024-11-12 12:03:24 +00:00
|
|
|
int c;
|
|
|
|
|
|
|
|
t = [[NSHashTable alloc] initWithOptions: NSHashTableObjectPointerPersonality
|
|
|
|
capacity: 10];
|
|
|
|
|
|
|
|
o = [MyClass new];
|
|
|
|
c = [o retainCount];
|
|
|
|
PASS(c == 1, "initial retain count is one")
|
|
|
|
|
|
|
|
[t addObject: @"a"];
|
|
|
|
[t addObject: o];
|
|
|
|
PASS([o retainCount] == c + 1, "add to hash table increments retain count")
|
|
|
|
|
|
|
|
PASS(NSHashGet(t, o) == o, "object found in table")
|
|
|
|
|
|
|
|
[t removeObject: o];
|
|
|
|
PASS([o retainCount] == c, "remove from hash table decrements retain count")
|
|
|
|
|
|
|
|
RELEASE(t);
|
|
|
|
RELEASE(o);
|
|
|
|
|
|
|
|
t = NSCreateHashTable(NSObjectHashCallBacks, 10);
|
|
|
|
|
|
|
|
o = [MyClass new];
|
|
|
|
c = [o retainCount];
|
|
|
|
PASS(c == 1, "initial retain count is one")
|
|
|
|
|
|
|
|
[t addObject: @"a"];
|
|
|
|
[t addObject: o];
|
|
|
|
PASS([o retainCount] == c + 1, "add to hash table increments retain count")
|
|
|
|
|
|
|
|
PASS(NSHashGet(t, o) == o, "object found in table")
|
|
|
|
|
|
|
|
[t removeObject: o];
|
|
|
|
PASS([o retainCount] == c, "remove from hash table decrements retain count")
|
|
|
|
|
2024-11-12 14:27:06 +00:00
|
|
|
o2 = [MyClass new];
|
|
|
|
PASS([o2 retainCount] == 1, "initial retain count of second object OK")
|
|
|
|
|
|
|
|
[t addObject: o];
|
|
|
|
[t addObject: o2];
|
|
|
|
PASS([o retainCount] == 1, "first object was removed")
|
|
|
|
PASS([o2 retainCount] == 2, "second object was added")
|
|
|
|
|
2024-11-12 12:03:24 +00:00
|
|
|
RELEASE(t);
|
|
|
|
RELEASE(o);
|
2024-11-12 14:27:06 +00:00
|
|
|
RELEASE(o2);
|
2011-02-16 08:21:17 +00:00
|
|
|
|
|
|
|
[arp release]; arp = nil;
|
|
|
|
return 0;
|
|
|
|
}
|