libs-base/Tests/base/GarbageCollection/notifications.m
Richard Frith-MacDonald 7cf4d2bb21 garbage collection tweaks
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32425 72102866-910b-0410-8b05-ffd578937521
2011-03-01 10:51:42 +00:00

59 lines
1.4 KiB
Objective-C

#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;
}