Skip to content

Commit

Permalink
Merge pull request #509 from kstenerud/test-file-utils
Browse files Browse the repository at this point in the history
Test log order in `KSFileUtils`
  • Loading branch information
GLinnik21 authored Jun 21, 2024
2 parents dfdc770 + 0283bd1 commit a90b1eb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<Test
Identifier = "KSDebug_Tests">
</Test>
<Test
Identifier = "KSFileUtils_Tests/testWriteBuffered_FlushAndLargeWriteOrder">
</Test>
<Test
Identifier = "KSFileUtils_Tests/testWriteBuffered_OrderWithLargeData">
</Test>
<Test
Identifier = "KSObjC_Tests/testDateDescription">
</Test>
Expand Down
72 changes: 72 additions & 0 deletions Tests/KSCrashRecordingCoreTests/KSFileUtils_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,76 @@ - (void) testReadLineFromFD
XCTAssertTrue(bytesRead == 0, @"");
}

- (void) testWriteBuffered_OrderOfEntries
{
int writeBufferSize = 10;
NSString* logEntry1 = @"Log1";
NSString* logEntry2 = @"Log2";
NSString* logEntry3 = @"Log3";
char writeBuffer[writeBufferSize];
KSBufferedWriter writer;
NSString* path = [self generateTempFilePath];
XCTAssertTrue(ksfu_openBufferedWriter(&writer, path.UTF8String, writeBuffer, writeBufferSize));

XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry1.UTF8String, (int)logEntry1.length));
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry2.UTF8String, (int)logEntry2.length));
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry3.UTF8String, (int)logEntry3.length));

ksfu_closeBufferedWriter(&writer);
NSError* error = nil;
NSString* actualFileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
XCTAssertNil(error);
NSString* expectedContents = @"Log1Log2Log3";
XCTAssertEqualObjects(actualFileContents, expectedContents);
}

- (void) testWriteBuffered_OrderWithLargeData
{
int writeBufferSize = 10;
NSString* logEntry1 = @"Small1";
NSString* logEntry2 = @"ThisIsLargeData";
NSString* logEntry3 = @"Small3";
char writeBuffer[writeBufferSize];
KSBufferedWriter writer;
NSString* path = [self generateTempFilePath];
XCTAssertTrue(ksfu_openBufferedWriter(&writer, path.UTF8String, writeBuffer, writeBufferSize));

XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry1.UTF8String, (int)logEntry1.length));
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry2.UTF8String, (int)logEntry2.length));
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry3.UTF8String, (int)logEntry3.length));

ksfu_closeBufferedWriter(&writer);
NSError* error = nil;
NSString* actualFileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
XCTAssertNil(error);
NSString* expectedContents = @"Small1ThisIsLargeDataSmall3";
XCTAssertEqualObjects(actualFileContents, expectedContents);
}

- (void) testWriteBuffered_FlushAndLargeWriteOrder
{
int writeBufferSize = 10;
NSString* logEntry1 = @"Buffer1";
NSString* logEntry2 = @"Buffer2";
NSString* largeEntry = @"ThisIsLargeDataThatExceedsBufferSize";
NSString* logEntry3 = @"EndData";
char writeBuffer[writeBufferSize];
KSBufferedWriter writer;
NSString* path = [self generateTempFilePath];
XCTAssertTrue(ksfu_openBufferedWriter(&writer, path.UTF8String, writeBuffer, writeBufferSize));

XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry1.UTF8String, (int)logEntry1.length));
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry2.UTF8String, (int)logEntry2.length));
// This should trigger a flush and write directly
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, largeEntry.UTF8String, (int)largeEntry.length));
XCTAssertTrue(ksfu_writeBufferedWriter(&writer, logEntry3.UTF8String, (int)logEntry3.length));

ksfu_closeBufferedWriter(&writer);
NSError* error = nil;
NSString* actualFileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
XCTAssertNil(error);
NSString* expectedContents = @"Buffer1Buffer2ThisIsLargeDataThatExceedsBufferSizeEndData";
XCTAssertEqualObjects(actualFileContents, expectedContents);
}

@end

0 comments on commit a90b1eb

Please sign in to comment.