Author: rfm
Date: Tue Jul 12 12:50:50 2016
New Revision: 39979
URL: http://svn.gna.org/viewcvs/gnustep?rev=39979&view=rev
Log:
add tests for timer firing
Modified:
libs/base/trunk/Tests/base/NSRunLoop/general.m
Modified: libs/base/trunk/Tests/base/NSRunLoop/general.m
URL:
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Tests/base/NSRunLoop/general.m?rev=39979&r1=39978&r2=39979&view=diff
==============================================================================
--- libs/base/trunk/Tests/base/NSRunLoop/general.m (original)
+++ libs/base/trunk/Tests/base/NSRunLoop/general.m Tue Jul 12 12:50:50 2016
@@ -3,34 +3,77 @@
#import <Foundation/NSInvocation.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSTimer.h>
+#import <Foundation/NSThread.h>
+
+static unsigned counter = 0;
+@interface MyClass : NSObject
+- (void) incrementCounter;
+@end
+@implementation MyClass
+- (void) incrementCounter
+{
+ counter = counter + 1;
+ NSLog(@"Counter is %u", counter);
+}
+@end
int main()
{
- NSAutoreleasePool *arp = [NSAutoreleasePool new];
- NSMethodSignature *sig;
- NSInvocation *inv;
- NSTimer *tim;
- NSRunLoop *run;
- NSDate *date;
+ NSAutoreleasePool *arp = [NSAutoreleasePool new];
+ NSString *customMode = @"CustomRunLoopMode";
+ MyClass *dummy = [MyClass new];
+ NSMethodSignature *sig;
+ NSInvocation *inv;
+ NSTimer *tim;
+ NSRunLoop *run;
+ NSDate *date;
+ NSTimeInterval ti;
- sig = [NSTimer instanceMethodSignatureForSelector:@selector(isValid)];
+ sig = [dummy methodSignatureForSelector: @selector(incrementCounter)];
inv = [NSInvocation invocationWithMethodSignature: sig];
+ [inv setSelector: @selector(incrementCounter)];
+ [inv setTarget: dummy];
run = [NSRunLoop currentRunLoop];
PASS(run != nil, "NSRunLoop understands [+currentRunLoop]");
PASS([run currentMode] == nil, "-currentMode returns nil");
-
- PASS_RUNS(date = [NSDate dateWithTimeIntervalSinceNow:3];
- [run runUntilDate:date];,
- "-runUntilDate: works");
- PASS_RUNS(date = [NSDate dateWithTimeIntervalSinceNow:5];
- tim = [NSTimer scheduledTimerWithTimeInterval: 2.0
- invocation:inv
- repeats:YES];,
- "-runUntilDate: works with a timer");
-
-
-
+
+ ti = [NSDate timeIntervalSinceReferenceDate];
+ PASS_RUNS(date = [NSDate dateWithTimeIntervalSinceNow: 1.0];
+ [run runUntilDate: date];,
+ "-runUntilDate: works");
+ ti = [NSDate timeIntervalSinceReferenceDate] - ti;
+ PASS(ti >= 1.0 && ti < 1.5, "-runUntilDate: takes the correct time");
+
+ ti = [NSDate timeIntervalSinceReferenceDate];
+ PASS_RUNS([run runUntilDate: [NSDate distantPast]];,
+ "-runUntilDate: works for distant past");
+ ti = [NSDate timeIntervalSinceReferenceDate] - ti;
+ PASS(ti < 0.2, "-runUntilDate: takes very short time");
+
+ tim = [NSTimer scheduledTimerWithTimeInterval: 0.005
+ invocation: inv
+ repeats: NO];
+ [NSThread sleepForTimeInterval: 0.01];
+ [run runUntilDate: [NSDate distantPast]];
+ PASS(1 == counter, "-runUntilDate: for distant past fires timer");
+
+ /* We run in a custom mode to ensure there are no other timers or
+ * input sources.
+ */
+ tim = [NSTimer timerWithTimeInterval: 2.0
+ invocation: inv
+ repeats: NO];
+ [run addTimer: tim forMode: customMode];
+ ti = [NSDate timeIntervalSinceReferenceDate];
+ PASS_RUNS(date = [NSDate dateWithTimeIntervalSinceNow: 5.0];
+ [run acceptInputForMode: customMode beforeDate: date];,
+ "-acceptInputForMode:beforeDate: works with a timer");
+ ti = [NSDate timeIntervalSinceReferenceDate] - ti;
+ PASS(ti >= 2.0 && ti < 2.5,
+ "-acceptInputForMode:beforeDate: takes the correct time");
+ PASS(2 == counter, "-acceptInputForMode:beforeDate: fires timer");
+
[arp release]; arp = nil;
return 0;
}
_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs