Coroutines/MYCoroutineTest.m
changeset 1 2475f871c218
parent 0 deb0ee0c5b21
     1.1 --- a/Coroutines/MYCoroutineTest.m	Tue Apr 29 17:05:32 2008 -0700
     1.2 +++ b/Coroutines/MYCoroutineTest.m	Wed Apr 30 14:18:49 2008 -0700
     1.3 @@ -7,6 +7,7 @@
     1.4  //
     1.5  
     1.6  #import "MYCoroutine.h"
     1.7 +#import "CoroX.h"
     1.8  
     1.9  
    1.10  @interface CoroTest1 : MYCoroutine
    1.11 @@ -45,6 +46,17 @@
    1.12  
    1.13  @synthesize value;
    1.14  
    1.15 +- (void) regress: (int)depth
    1.16 +{
    1.17 +    char useUpSpace[1024];
    1.18 +    useUpSpace[0] = 0;
    1.19 +    NSLog(@"infinite regress:  depth=%i, stack space=%d", depth,self.bytesLeftOnStack);
    1.20 +    if( [[MYCoroutine currentCoroutine] stackSpaceAlmostGone] )
    1.21 +        NSLog(@"infinite regress: bailing out!");
    1.22 +    else
    1.23 +        [self regress: depth+1];
    1.24 +}
    1.25 +
    1.26  - (void) main
    1.27  {
    1.28  	int num = 0;
    1.29 @@ -53,7 +65,6 @@
    1.30  	secondCoro = [[CoroTest2 alloc] init];
    1.31      secondCoro.name = @"second";
    1.32      secondCoro.value = 2;
    1.33 -	[secondCoro start];
    1.34  	
    1.35  	while ( num < 100 ) 
    1.36  	{
    1.37 @@ -63,26 +74,78 @@
    1.38      
    1.39      [secondCoro release];
    1.40      
    1.41 +    NSLog(@"*** TESTING STACK LIMITS ***");
    1.42 +    [self regress: 1];
    1.43 +    
    1.44      [[MYCoroutine mainCoroutine] resume];
    1.45  }
    1.46  
    1.47 ++ (void) test
    1.48 +{
    1.49 +    NSLog(@"*** TESTING COROUTINES ***");
    1.50 +	firstCoro = [[CoroTest1 alloc] init];
    1.51 +    firstCoro.name = @"first";
    1.52 +    firstCoro.value = 1;
    1.53 +    [firstCoro resume];
    1.54 +    
    1.55 +    NSLog(@"Returned from coroutines; exiting");
    1.56 +    
    1.57 +    [firstCoro release];
    1.58 +}
    1.59 +
    1.60  @end
    1.61  
    1.62  
    1.63 +
    1.64 +
    1.65 +@interface Generator : MYCoroutine
    1.66 +{ int _count; }
    1.67 +- (id) initWithCount: (int)count;
    1.68 +@end
    1.69 +
    1.70 +@implementation Generator
    1.71 +
    1.72 +- (id) initWithCount: (int)count
    1.73 +{
    1.74 +    self = [super init];
    1.75 +    if (self != nil) {
    1.76 +        _count = count;
    1.77 +    }
    1.78 +    return self;
    1.79 +}
    1.80 +
    1.81 +
    1.82 +- (void) main
    1.83 +{
    1.84 +    for( int i=1; i<=_count; i++ )
    1.85 +        [MYCoroutine yieldToCaller: [NSNumber numberWithInt: i]];
    1.86 +}
    1.87 +
    1.88 ++ (void) test
    1.89 +{
    1.90 +    NSLog(@"*** TESTING GENERATOR ***");
    1.91 +    Generator *g = [[Generator alloc] initWithCount: 10];
    1.92 +    id value;
    1.93 +    while( nil != (value = [g call]) )
    1.94 +        NSLog(@"Generator yielded %@",value);
    1.95 +    NSLog(@"Generator returned nil");
    1.96 +}
    1.97 +
    1.98 +@end
    1.99 +
   1.100 +
   1.101 +
   1.102  int main()
   1.103  {
   1.104      NSAutoreleasePool *pool = [NSAutoreleasePool new];
   1.105 +    NSLog(@"Starting test...");
   1.106      
   1.107 -    NSLog(@"Starting test...");
   1.108 -    //[[[NSThread alloc] init] start];
   1.109 -	firstCoro = [[CoroTest1 alloc] init];
   1.110 -    firstCoro.name = @"first";
   1.111 -    firstCoro.value = 1;
   1.112 -    [firstCoro start];
   1.113 +    [MYCoroutine setDefaultStackSize: kCoroX_minStackSize];
   1.114      
   1.115 -    NSLog(@"Returned from coroutines; exiting");
   1.116 +    [CoroTest1 test];
   1.117      
   1.118 -    [firstCoro release];
   1.119 +    [Generator test];
   1.120      
   1.121 +    NSLog(@"FINISHED");
   1.122      [pool drain];
   1.123  }