-
Notifications
You must be signed in to change notification settings - Fork 34
/
GCDObjCTests.m
167 lines (129 loc) · 4.27 KB
/
GCDObjCTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// GCDQueueTests.m
// GCDObjC
//
// Copyright (c) 2012 Mark Smith. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <dispatch/dispatch.h>
#import <libkern/OSAtomic.h>
#import "GCDObjC.h"
@interface GCDObjCTests : XCTestCase
@end
@implementation GCDObjCTests
- (void)testMainQueue {
XCTAssertEqual([GCDQueue mainQueue].dispatchQueue, dispatch_get_main_queue());
XCTAssertTrue([GCDQueue isMainQueue]);
[[GCDQueue new] queueAndAwaitBlock:^{ XCTAssertFalse([GCDQueue isMainQueue]); }];
}
- (void)testGlobalQueues {
XCTAssertEqual([GCDQueue globalQueue].dispatchQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
XCTAssertEqual([GCDQueue highPriorityGlobalQueue].dispatchQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
XCTAssertEqual([GCDQueue lowPriorityGlobalQueue].dispatchQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0));
XCTAssertEqual([GCDQueue backgroundPriorityGlobalQueue].dispatchQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
}
- (void)testQueueBlock {
GCDSemaphore *semaphore = [GCDSemaphore new];
GCDQueue *queue = [GCDQueue new];
__block int32_t val = 0;
[queue queueBlock:^{
OSAtomicIncrement32(&val);
[semaphore signal];
}];
[semaphore wait];
XCTAssertEqual(val, 1);
}
- (void)testQueueBlockAfterDelay {
GCDSemaphore *semaphore = [GCDSemaphore new];
GCDQueue *queue = [GCDQueue new];
NSDate *then = [NSDate new];
__block int32_t val = 0;
[queue queueBlock:^{
OSAtomicIncrement32(&val);
[semaphore signal];
} afterDelay:0.5];
XCTAssertEqual(val, 0);
[semaphore wait];
XCTAssertEqual(val, 1);
NSDate *now = [NSDate new];
XCTAssertTrue([now timeIntervalSinceDate:then] > 0.4);
XCTAssertTrue([now timeIntervalSinceDate:then] < 0.6);
}
- (void)testQueueAndAwaitBlock {
GCDQueue *queue = [GCDQueue new];
__block int32_t val = 0;
[queue queueAndAwaitBlock:^{ OSAtomicIncrement32(&val); }];
XCTAssertEqual(val, 1);
}
- (void)testQueueAndAwaitBlockIterationCount {
GCDQueue *queue = [[GCDQueue alloc] initConcurrent];
__block int32_t val = 0;
[queue queueAndAwaitBlock:^(size_t i){ OSAtomicIncrement32(&val); } iterationCount:100];
XCTAssertEqual(val, 100);
}
- (void)testQueueBlockInGroup {
GCDQueue *queue = [[GCDQueue alloc] initConcurrent];
GCDGroup *group = [GCDGroup new];
__block int32_t val = 0;
for (int i = 0; i < 100; ++i) {
[queue queueBlock:^{ OSAtomicIncrement32(&val); } inGroup:group];
}
[group wait];
XCTAssertEqual(val, 100);
}
- (void)testQueueNotifyBlockForGroup {
GCDQueue *queue = [[GCDQueue alloc] initConcurrent];
GCDSemaphore *semaphore = [GCDSemaphore new];
GCDGroup *group = [GCDGroup new];
__block int32_t val = 0;
__block int32_t notifyVal = 0;
for (int i = 0; i < 100; ++i) {
[queue queueBlock:^{ OSAtomicIncrement32(&val); } inGroup:group];
}
[queue queueNotifyBlock:^{ notifyVal = val; [semaphore signal]; } inGroup:group];
[semaphore wait];
XCTAssertEqual(notifyVal, 100);
}
- (void)testQueueBarrierBlock {
GCDQueue *queue = [[GCDQueue alloc] initConcurrent];
GCDSemaphore *semaphore = [GCDSemaphore new];
__block int32_t val = 0;
__block int32_t barrierVal = 0;
for (int i = 0; i < 100; ++i) {
[queue queueBlock:^{ OSAtomicIncrement32(&val); }];
}
[queue queueBarrierBlock:^{ barrierVal = val; [semaphore signal]; }];
for (int i = 0; i < 100; ++i) {
[queue queueBlock:^{ OSAtomicIncrement32(&val); }];
}
[semaphore wait];
XCTAssertEqual(barrierVal, 100);
}
- (void)testQueueAndAwaitBarrierBlock {
GCDQueue *queue = [[GCDQueue alloc] initConcurrent];
__block int32_t val = 0;
for (int i = 0; i < 100; ++i) {
[queue queueBlock:^{ OSAtomicIncrement32(&val); }];
}
[queue queueAndAwaitBarrierBlock:^{}];
XCTAssertEqual(val, 100);
}
static int onceVal;
- (void)onceBlock {
GCDExecOnce(^{ ++onceVal; });
}
- (void)testExecOnce {
onceVal = 0;
for (int i = 0; i < 100; ++i) {
[self onceBlock];
}
XCTAssertEqual(onceVal, 1);
}
+ (instancetype)theTestInstance {
GCDSharedInstance(^{ return [self new]; });
}
- (void)testSharedInstance {
XCTAssertTrue([[GCDObjCTests theTestInstance] class] == [self class]);
XCTAssertEqual([GCDObjCTests theTestInstance], [GCDObjCTests theTestInstance]);
}
@end