Coroutines/MYCoroutineTest.m
author Jens Alfke <jens@mooseyard.com>
Wed Apr 30 14:18:49 2008 -0700 (2008-04-30)
changeset 1 2475f871c218
parent 0 deb0ee0c5b21
permissions -rw-r--r--
Rewrote CoroX. Simplified APIs. Improved stack size checks.
     1 //
     2 //  MYCoroutineTest.m
     3 //  Coroutines
     4 //
     5 //  Created by Jens Alfke on 4/29/08.
     6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
     7 //
     8 
     9 #import "MYCoroutine.h"
    10 #import "CoroX.h"
    11 
    12 
    13 @interface CoroTest1 : MYCoroutine
    14 {
    15     int value;
    16 }
    17 @property int value;
    18 @end
    19 
    20 @interface CoroTest2 : CoroTest1
    21 @end
    22 
    23 
    24 CoroTest1 *firstCoro, *secondCoro;
    25 
    26 
    27 @implementation CoroTest2
    28 
    29 - (void) main
    30 {
    31     int num = 0;
    32     
    33 	NSLog(@"secondTask created with value %d", self.value);
    34 	
    35 	while (1) 
    36 	{
    37 		NSLog(@"secondTask: %d %d", self.bytesLeftOnStack, num++);
    38 		[firstCoro resume];
    39 	}
    40 }
    41 
    42 @end
    43 
    44 
    45 @implementation CoroTest1
    46 
    47 @synthesize value;
    48 
    49 - (void) regress: (int)depth
    50 {
    51     char useUpSpace[1024];
    52     useUpSpace[0] = 0;
    53     NSLog(@"infinite regress:  depth=%i, stack space=%d", depth,self.bytesLeftOnStack);
    54     if( [[MYCoroutine currentCoroutine] stackSpaceAlmostGone] )
    55         NSLog(@"infinite regress: bailing out!");
    56     else
    57         [self regress: depth+1];
    58 }
    59 
    60 - (void) main
    61 {
    62 	int num = 0;
    63 	
    64 	NSLog(@"firstTask created with value %d", self.value);
    65 	secondCoro = [[CoroTest2 alloc] init];
    66     secondCoro.name = @"second";
    67     secondCoro.value = 2;
    68 	
    69 	while ( num < 100 ) 
    70 	{
    71 		NSLog(@"firstTask:  %d %d", self.bytesLeftOnStack, num++);
    72         [secondCoro resume];
    73 	}
    74     
    75     [secondCoro release];
    76     
    77     NSLog(@"*** TESTING STACK LIMITS ***");
    78     [self regress: 1];
    79     
    80     [[MYCoroutine mainCoroutine] resume];
    81 }
    82 
    83 + (void) test
    84 {
    85     NSLog(@"*** TESTING COROUTINES ***");
    86 	firstCoro = [[CoroTest1 alloc] init];
    87     firstCoro.name = @"first";
    88     firstCoro.value = 1;
    89     [firstCoro resume];
    90     
    91     NSLog(@"Returned from coroutines; exiting");
    92     
    93     [firstCoro release];
    94 }
    95 
    96 @end
    97 
    98 
    99 
   100 
   101 @interface Generator : MYCoroutine
   102 { int _count; }
   103 - (id) initWithCount: (int)count;
   104 @end
   105 
   106 @implementation Generator
   107 
   108 - (id) initWithCount: (int)count
   109 {
   110     self = [super init];
   111     if (self != nil) {
   112         _count = count;
   113     }
   114     return self;
   115 }
   116 
   117 
   118 - (void) main
   119 {
   120     for( int i=1; i<=_count; i++ )
   121         [MYCoroutine yieldToCaller: [NSNumber numberWithInt: i]];
   122 }
   123 
   124 + (void) test
   125 {
   126     NSLog(@"*** TESTING GENERATOR ***");
   127     Generator *g = [[Generator alloc] initWithCount: 10];
   128     id value;
   129     while( nil != (value = [g call]) )
   130         NSLog(@"Generator yielded %@",value);
   131     NSLog(@"Generator returned nil");
   132 }
   133 
   134 @end
   135 
   136 
   137 
   138 int main()
   139 {
   140     NSAutoreleasePool *pool = [NSAutoreleasePool new];
   141     NSLog(@"Starting test...");
   142     
   143     [MYCoroutine setDefaultStackSize: kCoroX_minStackSize];
   144     
   145     [CoroTest1 test];
   146     
   147     [Generator test];
   148     
   149     NSLog(@"FINISHED");
   150     [pool drain];
   151 }