Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 146 lines (122 sloc) 4.131 kb
4918658 Mike Ash initial checkin (compiles, completely untested)
authored
1 #import <Foundation/Foundation.h>
2
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
3 #import "MAAsyncReader.h"
4
5
829ab0d Mike Ash better testing framework
authored
6 static void WithPool(void (^block)(void))
7 {
8 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
9 block();
10 [pool release];
11 }
12
13 static int gFailureCount;
14
15 static void Test(void (*func)(void), const char *name)
16 {
17 WithPool(^{
18 int failureCount = gFailureCount;
19 NSLog(@"Testing %s", name);
20 func();
21 NSLog(@"%s: %s", name, failureCount == gFailureCount ? "SUCCESS" : "FAILED");
22 });
23 }
24
25 #define TEST(func) Test(func, #func)
26
27 #define TEST_ASSERT(cond, ...) do { \
28 if(!(cond)) { \
29 gFailureCount++; \
30 NSString *message = [NSString stringWithFormat: @"" __VA_ARGS__]; \
31 NSLog(@"%s:%d: assertion failed: %s %@", __func__, __LINE__, #cond, message); \
32 } \
33 } while(0)
34
35 static BOOL WaitFor(int (^block)(void))
4918658 Mike Ash initial checkin (compiles, completely untested)
authored
36 {
829ab0d Mike Ash better testing framework
authored
37 NSProcessInfo *pi = [NSProcessInfo processInfo];
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
38
829ab0d Mike Ash better testing framework
authored
39 NSTimeInterval start = [pi systemUptime];
40 __block BOOL stop;
41 do
42 {
43 WithPool(^{
44 stop = block() != 0;
45 });
46 } while(!stop && [pi systemUptime] - start < 10);
47
48 return stop;
49 }
50
0a9ae50 Mike Ash add a real test
authored
51 static int WriteAll(int fd, const void *data, int len)
52 {
53 ssize_t amt = write(fd, data, len);
54 if(amt <= 0 || amt == len)
55 return amt;
56 else
57 return WriteAll(fd, (const char *)data + amt, len - amt);
58 }
59
60 static MAAsyncReader *Reader(int fd)
829ab0d Mike Ash better testing framework
authored
61 {
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
62 MAAsyncReader *reader = [[MAAsyncReader alloc] initWithFileDescriptor: fd];
63 [reader setErrorHandler: ^(int err) {
64 NSLog(@"got error %d (%s)", err, strerror(err));
829ab0d Mike Ash better testing framework
authored
65 abort();
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
66 }];
0a9ae50 Mike Ash add a real test
authored
67 return [reader autorelease];
68 }
69
70 static void TestDevNull(void)
71 {
72 int fd = open("/dev/null", O_RDONLY);
73
74 MAAsyncReader *reader = Reader(fd);
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
75
829ab0d Mike Ash better testing framework
authored
76 __block BOOL didRead = NO;
619491e Mike Ash fix bug when condition is fulfilled at byte 0 in the buffer
authored
77 [reader readUntilCondition: ^NSUInteger (NSData *buffer) { return NSNotFound; }
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
78 callback: ^(NSData *data) {
829ab0d Mike Ash better testing framework
authored
79 TEST_ASSERT(!data);
80 didRead = YES;
a7dbbfe Mike Ash add quickie proof-of-concept test
authored
81 }];
829ab0d Mike Ash better testing framework
authored
82 WaitFor(^int { return didRead; });
83 }
84
0a9ae50 Mike Ash add a real test
authored
85 static void TestPipe(void)
86 {
87 int fds[2];
88 int ret = pipe(fds);
89 TEST_ASSERT(ret == 0);
90
91 int readFD = fds[0];
92 int writeFD = fds[1];
93
94 NSData *d1 = [NSData dataWithBytes: "12345" length: 5];
95 NSData *d2 = [NSData dataWithBytes: "abcdef" length: 6];
96 NSData *d3 = [NSData dataWithBytes: "ghijkl" length: 6];
97
98 __block BOOL done = NO;
99
100 MAAsyncReader *reader = Reader(readFD);
101 [reader readBytes: 5 callback: ^(NSData *data) {
102 TEST_ASSERT([data isEqualToData: d1]);
103 [reader readUntilCString: "\n" callback: ^(NSData *data) {
104 TEST_ASSERT([data isEqualToData: d2]);
105 [reader readBytes: 1 callback: ^(NSData *data) {
106 [reader readUntilCString: "\r\n" callback: ^(NSData *data) {
107 TEST_ASSERT([data isEqualToData: d3]);
619491e Mike Ash fix bug when condition is fulfilled at byte 0 in the buffer
authored
108 [reader readBytes: 2 callback: ^(NSData *data) {
109 [reader readUntilCString: "\r\n" callback: ^(NSData *data) {
110 TEST_ASSERT(data && [data length] == 0);
111 done = YES;
112 }];
113 }];
0a9ae50 Mike Ash add a real test
authored
114 }];
115 }];
116 }];
117 }];
118
119 dispatch_async(dispatch_get_global_queue(0, 0), ^{
120 WriteAll(writeFD, [d1 bytes], [d1 length]);
121 WriteAll(writeFD, [d2 bytes], [d2 length]);
122 WriteAll(writeFD, "\n", 1);
123 WriteAll(writeFD, [d3 bytes], [d3 length]);
124 WriteAll(writeFD, "\r\n", 2);
619491e Mike Ash fix bug when condition is fulfilled at byte 0 in the buffer
authored
125 WriteAll(writeFD, "\r\n", 2);
0a9ae50 Mike Ash add a real test
authored
126 close(writeFD);
127 });
128
129 WaitFor(^int { return done; });
130 }
131
908c839 Mike Ash less annoying main() signature
authored
132 int main(int argc, const char **argv)
829ab0d Mike Ash better testing framework
authored
133 {
134 WithPool(^{
135 TEST(TestDevNull);
0a9ae50 Mike Ash add a real test
authored
136 TEST(TestPipe);
829ab0d Mike Ash better testing framework
authored
137 NSString *message;
138 if(gFailureCount)
139 message = [NSString stringWithFormat: @"FAILED: %d total assertion failure%s", gFailureCount, gFailureCount > 1 ? "s" : ""];
140 else
141 message = @"SUCCESS";
142 NSLog(@"Tests complete: %@", message);
143 });
4918658 Mike Ash initial checkin (compiles, completely untested)
authored
144 return 0;
145 }
Something went wrong with that request. Please try again.