Skip to content

Instantly share code, notes, and snippets.

@josharian
Last active August 29, 2015 13:59
Show Gist options
  • Save josharian/11000893 to your computer and use it in GitHub Desktop.
Save josharian/11000893 to your computer and use it in GitHub Desktop.
CChunkWriter exhaustive test
@property(strong) NSMutableData *received;
@property(assign) BOOL done;
@property(strong) NSString *name;
- (void)testChunkExhaustive {
NSString *chunked = @"24\r\nNow is the winter of our discontent\n\r\n2a\r\nMade glorious summer by this sun of York;\n\r\n2e\r\nAnd all the clouds that lour'd upon our house\n\r\n26\r\nIn the deep bosom of the ocean buried.\r\n0\r\n";
for (NSUInteger i = 1; i < chunked.length; i++) {
for (NSUInteger j = i; j < chunked.length; j++) {
for (NSUInteger k = j; k < chunked.length; k++) {
@autoreleasepool {
self.name = [NSString stringWithFormat:@"Test: %lu %lu %lu", i, j, k];
self.received = [NSMutableData data];
self.done = NO;
BBChunkReader *r = [[BBChunkReader alloc] init];
r.delegate = self;
[r writeData:[[chunked substringWithRange:NSMakeRange(0, i-0)] dataUsingEncoding:NSASCIIStringEncoding]];
[r writeData:[[chunked substringWithRange:NSMakeRange(i, j-i)] dataUsingEncoding:NSASCIIStringEncoding]];
[r writeData:[[chunked substringWithRange:NSMakeRange(j, k-j)] dataUsingEncoding:NSASCIIStringEncoding]];
[r writeData:[[chunked substringWithRange:NSMakeRange(k, chunked.length-k)] dataUsingEncoding:NSASCIIStringEncoding]];
XCTAssertTrue(self.done, @"Chunk reader should have completed: %@", self.name);
}
}
}
}
}
- (void)chunkReader:(BBChunkReader *)reader didReceiveData:(NSData *)data {
[self.received appendData:data];
}
- (void)chunkReaderDidFinishReceivingData:(BBChunkReader *)reader {
NSData *want = [@"Now is the winter of our discontent\nMade glorious summer by this sun of York;\nAnd all the clouds that lour'd upon our house\nIn the deep bosom of the ocean buried." dataUsingEncoding:NSASCIIStringEncoding];
XCTAssertTrue([want isEqualToData:self.received], @"Wrong chunk decoding for %@: got\n---\n%@\n---\n---\nwant\n%@",
self.name,
[[NSString alloc] initWithData:self.received encoding:NSASCIIStringEncoding],
[[NSString alloc] initWithData:want encoding:NSASCIIStringEncoding]
);
self.done = YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment