1
#import <Foundation/NSOperation.h>
2
#import <Foundation/NSThread.h>
3
#import <Foundation/NSAutoreleasePool.h>
4
#import "ObjectTesting.h"
11
NSMutableArray *testObjs = [[NSMutableArray alloc] init];
12
NSAutoreleasePool *arp = [NSAutoreleasePool new];
14
test_alloc(@"NSOperation");
15
obj1 = [NSOperation new];
16
PASS((obj1 != nil), "can create an operation");
17
[testObjs addObject: obj1];
18
test_NSObject(@"NSOperation", testObjs);
20
PASS(([obj1 isReady] == YES), "operation is ready");
21
PASS(([obj1 isConcurrent] == NO), "operation is not concurrent");
22
PASS(([obj1 isCancelled] == NO), "operation is not cancelled");
23
PASS(([obj1 isExecuting] == NO), "operation is not executing");
24
PASS(([obj1 isFinished] == NO), "operation is not finished");
25
PASS(([[obj1 dependencies] isEqual: [NSArray array]]),
26
"operation has no dependencies");
27
PASS(([obj1 queuePriority] == NSOperationQueuePriorityNormal),
28
"operation has normal priority");
29
[obj1 setQueuePriority: 10000];
30
PASS(([obj1 queuePriority] == NSOperationQueuePriorityVeryHigh),
31
"operation has very high priority");
33
obj2 = [NSOperation new];
34
[obj2 addDependency: obj1];
35
PASS(([[obj2 dependencies] isEqual: testObjs]),
36
"operation has added dependency");
37
PASS(([obj2 isReady] == NO), "operation with dependency is not ready");
39
PASS(([[obj2 dependencies] isEqual: testObjs]),
40
"cancelled dependency continues");
41
PASS(([obj1 isCancelled] == YES), "operation is cancelled");
42
PASS(([[obj2 dependencies] isEqual: testObjs]),
43
"cancelled dependency continues");
44
PASS(([obj2 isReady] == NO), "operation with cancelled dependency not ready");
45
[obj2 removeDependency: obj1];
46
PASS(([[obj2 dependencies] isEqual: [NSArray array]]),
47
"dependency removal works");
48
PASS(([obj2 isReady] == YES), "operation without dependency is ready");
51
obj1 = [NSOperation new];
52
[testObjs replaceObjectAtIndex: 0 withObject: obj1];
53
[obj2 addDependency: obj1];
56
[NSThread sleepForTimeInterval: 1.0];
57
PASS(([obj1 isFinished] == YES), "operation is finished");
58
PASS(([obj1 isReady] == YES), "a finished operation is ready");
59
PASS(([[obj2 dependencies] isEqual: testObjs]),
60
"finished dependency continues");
61
PASS(([obj2 isReady] == YES), "operation with finished dependency is ready");
63
[obj2 removeDependency: obj1];
65
obj1 = [NSOperation new];
66
[testObjs replaceObjectAtIndex: 0 withObject: obj1];
67
[obj2 addDependency: obj1];
69
PASS(([obj2 isReady] == YES),
70
"a cancelled object is ready even with a dependency");
73
PASS(([obj2 isFinished] == YES),
74
"a cancelled object can finish");
76
PASS_EXCEPTION([obj2 start];,
77
NSInvalidArgumentException,
78
"NSOperation cannot be started twice");
80
PASS(([obj2 waitUntilFinished], YES), "wait returns at once");
83
test_alloc(@"NSOperationQueue");
84
obj1 = [NSOperationQueue new];
85
PASS((obj1 != nil), "can create an operation queue");
86
[testObjs removeAllObjects];
87
[testObjs addObject: obj1];
88
test_NSObject(@"NSOperationQueue", testObjs);
90
PASS(([obj1 isSuspended] == NO), "not suspended by default");
91
[obj1 setSuspended: YES];
92
PASS(([obj1 isSuspended] == YES), "set suspended yes");
93
[obj1 setSuspended: NO];
94
PASS(([obj1 isSuspended] == NO), "set suspended no");
96
PASS(([[obj1 name] length] > 0), "name has a default");
97
[obj1 setName: @"mine"];
98
PASS(([[obj1 name] isEqual: @"mine"] == YES), "set name OK");
100
PASS(([[obj1 name] isEqual: @""]), "setting null name gives empty string");
102
PASS(([obj1 maxConcurrentOperationCount] == NSOperationQueueDefaultMaxConcurrentOperationCount), "max concurrent set by default");
103
[obj1 setMaxConcurrentOperationCount: 1];
104
PASS(([obj1 maxConcurrentOperationCount] == 1), "max concurrent set to one");
105
[obj1 setMaxConcurrentOperationCount: 0];
106
PASS(([obj1 maxConcurrentOperationCount] == 0), "max concurrent set to zero");
107
[obj1 setMaxConcurrentOperationCount: 1000000];
108
PASS(([obj1 maxConcurrentOperationCount] == 1000000), "max concurrent set to a million");
109
[obj1 setMaxConcurrentOperationCount: NSOperationQueueDefaultMaxConcurrentOperationCount];
110
PASS(([obj1 maxConcurrentOperationCount] == NSOperationQueueDefaultMaxConcurrentOperationCount), "max concurrent set to default");
111
PASS_EXCEPTION([obj1 setMaxConcurrentOperationCount: -1000000];,
112
NSInvalidArgumentException,
113
"NSOperationQueue cannot be given negative count");
115
obj2 = [NSOperation new];
116
[obj1 addOperation: obj2];
117
[NSThread sleepForTimeInterval: 1.0];
118
PASS(([obj2 isFinished] == YES), "queue ran operation");
120
[arp release]; arp = nil;