1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#import <Foundation/Foundation.h>
#import "ObjectTesting.h"
@interface MyClass : NSObject
+ (unsigned) finalisationCounter;
+ (unsigned) notificationCounter;
- (void) notified: (NSNotification*)n;
@end
@implementation MyClass
static unsigned notificationCounter = 0;
static unsigned finalisationCounter = 0;
+ (unsigned) finalisationCounter
{
return finalisationCounter;
}
+ (unsigned) notificationCounter
{
return notificationCounter;
}
- (void) finalize
{
finalisationCounter++;
}
- (void) notified: (NSNotification*)n
{
notificationCounter++;
}
@end
int
main()
{
NSGarbageCollector *collector;
NSNotificationCenter *center;
MyClass *object;
collector = [NSGarbageCollector defaultCollector];
if (collector == nil) return 0;
START_SET("Garbage Collection");
center = [NSNotificationCenter defaultCenter];
object = [MyClass new];
[center addObserver: object
selector: @selector(notified:)
name: @"Notification"
object: nil];
[center postNotificationName: @"Notification" object: nil];
PASS([MyClass notificationCounter] == 1, "simple notification works")
object = nil;
[collector collectExhaustively];
PASS([MyClass finalisationCounter] == 1, "finalisation done")
[center postNotificationName: @"Notification" object: nil];
PASS([MyClass notificationCounter] == 1, "automatic removal works")
END_SET("Garbage Collection");
return 0;
}
|