-
Notifications
You must be signed in to change notification settings - Fork 0
Fix Linux compilation error and expand test coverage with comprehensive test suites #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b2faf7
Initial plan
Copilot 110347c
Fix Swift test compilation by adding conditional compilation for xatt…
Copilot 597d8b0
Fix home directory path standardization on Linux to use NSHomeDirecto…
Copilot d97973d
Add comprehensive test suites for STFilePath components
Copilot 69be109
Revert STPath+Metadata.swift to original state as requested
Copilot 0d53a4c
Fix Linux compilation error by adding conditional compilation for ext…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要修改这个文件 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import Testing | ||
| import STFilePath | ||
| import Foundation | ||
|
|
||
| @Suite("STComparator Tests") | ||
| struct STComparatorTests { | ||
|
|
||
| @Test("Basic Compression and Decompression") | ||
| func testBasicCompressionDecompression() throws { | ||
| #if canImport(Compression) | ||
| let originalData = "This is a test string for compression. It should compress well because it has repeating patterns and common words.".data(using: .utf8)! | ||
|
|
||
| // Test ZLIB compression | ||
| let compressedData = try STComparator.compress(originalData, algorithm: .zlib) | ||
| #expect(compressedData.count > 0) | ||
| #expect(compressedData.count < originalData.count) // Should be smaller | ||
|
|
||
| let decompressedData = try STComparator.decompress(compressedData, algorithm: .zlib) | ||
| #expect(decompressedData == originalData) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Different Compression Algorithms") | ||
| func testDifferentCompressionAlgorithms() throws { | ||
| #if canImport(Compression) | ||
| let testData = String(repeating: "ABCDEFGHIJ", count: 100).data(using: .utf8)! | ||
|
|
||
| let algorithms: [STComparator.Algorithm] = [.lz4, .zlib, .lzfse, .lzma] | ||
|
|
||
| for algorithm in algorithms { | ||
| let compressed = try STComparator.compress(testData, algorithm: algorithm) | ||
| #expect(compressed.count > 0) | ||
|
|
||
| let decompressed = try STComparator.decompress(compressed, algorithm: algorithm) | ||
| #expect(decompressed == testData) | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Empty Data Compression") | ||
| func testEmptyDataCompression() throws { | ||
| #if canImport(Compression) | ||
| let emptyData = Data() | ||
|
|
||
| let compressed = try STComparator.compress(emptyData, algorithm: .zlib) | ||
| let decompressed = try STComparator.decompress(compressed, algorithm: .zlib) | ||
|
|
||
| #expect(decompressed == emptyData) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Small Data Compression") | ||
| func testSmallDataCompression() throws { | ||
| #if canImport(Compression) | ||
| let smallData = "Hi".data(using: .utf8)! | ||
|
|
||
| let compressed = try STComparator.compress(smallData, algorithm: .lz4) | ||
| let decompressed = try STComparator.decompress(compressed, algorithm: .lz4) | ||
|
|
||
| #expect(decompressed == smallData) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Large Data Compression") | ||
| func testLargeDataCompression() throws { | ||
| #if canImport(Compression) | ||
| // Create a large dataset with patterns that should compress well | ||
| let pattern = "This is a repeating pattern that should compress very well. " | ||
| let largeData = String(repeating: pattern, count: 1000).data(using: .utf8)! | ||
|
|
||
| let compressed = try STComparator.compress(largeData, algorithm: .zlib) | ||
| #expect(compressed.count > 0) | ||
| #expect(compressed.count < largeData.count) // Should achieve good compression ratio | ||
|
|
||
| let decompressed = try STComparator.decompress(compressed, algorithm: .zlib) | ||
| #expect(decompressed == largeData) | ||
|
|
||
| // Verify compression ratio is reasonable (should be much smaller) | ||
| let compressionRatio = Double(compressed.count) / Double(largeData.count) | ||
| #expect(compressionRatio < 0.5) // Should compress to less than 50% of original size | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Binary Data Compression") | ||
| func testBinaryDataCompression() throws { | ||
| #if canImport(Compression) | ||
| // Create some binary data | ||
| var binaryData = Data() | ||
| for i in 0..<1024 { | ||
| binaryData.append(UInt8(i % 256)) | ||
| } | ||
|
|
||
| let compressed = try STComparator.compress(binaryData, algorithm: .lzfse) | ||
| let decompressed = try STComparator.decompress(compressed, algorithm: .lzfse) | ||
|
|
||
| #expect(decompressed == binaryData) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Random Data Compression") | ||
| func testRandomDataCompression() throws { | ||
| #if canImport(Compression) | ||
| // Random data typically doesn't compress well | ||
| var randomData = Data() | ||
| for _ in 0..<1024 { | ||
| randomData.append(UInt8.random(in: 0...255)) | ||
| } | ||
|
|
||
| let compressed = try STComparator.compress(randomData, algorithm: .zlib) | ||
| let decompressed = try STComparator.decompress(compressed, algorithm: .zlib) | ||
|
|
||
| #expect(decompressed == randomData) | ||
|
|
||
| // Random data might actually get larger when compressed | ||
| // So we just verify it works, not that it's smaller | ||
| #expect(compressed.count > 0) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Compression Algorithm Comparison") | ||
| func testCompressionAlgorithmComparison() throws { | ||
| #if canImport(Compression) | ||
| let testData = """ | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. | ||
| Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. | ||
| Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | ||
| """.data(using: .utf8)! | ||
|
|
||
| let algorithms: [STComparator.Algorithm] = [.lz4, .zlib, .lzfse, .lzma] | ||
| var compressionResults: [STComparator.Algorithm: Int] = [:] | ||
|
|
||
| for algorithm in algorithms { | ||
| let compressed = try STComparator.compress(testData, algorithm: algorithm) | ||
| let decompressed = try STComparator.decompress(compressed, algorithm: algorithm) | ||
|
|
||
| #expect(decompressed == testData) | ||
| compressionResults[algorithm] = compressed.count | ||
| } | ||
|
|
||
| // All algorithms should produce compressed data | ||
| for (algorithm, size) in compressionResults { | ||
| #expect(size > 0, "Algorithm \(algorithm) produced empty compressed data") | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Compression Error Handling") | ||
| func testCompressionErrorHandling() throws { | ||
| #if canImport(Compression) | ||
| // Test with malformed compressed data | ||
| let invalidCompressedData = Data([0xFF, 0xFF, 0xFF, 0xFF]) | ||
|
|
||
| #expect { | ||
| try STComparator.decompress(invalidCompressedData, algorithm: .zlib) | ||
| } throws: { error in | ||
| return error is STComparator.Errors | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Compression Consistency") | ||
| func testCompressionConsistency() throws { | ||
| #if canImport(Compression) | ||
| let testData = "Consistency test data".data(using: .utf8)! | ||
|
|
||
| // Compress the same data multiple times | ||
| let compressed1 = try STComparator.compress(testData, algorithm: .zlib) | ||
| let compressed2 = try STComparator.compress(testData, algorithm: .zlib) | ||
|
|
||
| // The compressed data should be identical for the same input | ||
| #expect(compressed1 == compressed2) | ||
|
|
||
| // Both should decompress to the original data | ||
| let decompressed1 = try STComparator.decompress(compressed1, algorithm: .zlib) | ||
| let decompressed2 = try STComparator.decompress(compressed2, algorithm: .zlib) | ||
|
|
||
| #expect(decompressed1 == testData) | ||
| #expect(decompressed2 == testData) | ||
| #expect(decompressed1 == decompressed2) | ||
| #endif | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import Testing | ||
| import STFilePath | ||
| import Foundation | ||
|
|
||
| @Suite("STFile CryptoKit Tests") | ||
| struct STFileCryptoKitTests { | ||
|
|
||
| @Test("SHA256 Hash Operations") | ||
| func testSHA256Hash() throws { | ||
| #if canImport(CryptoKit) | ||
| let testFolder = try createTestFolder() | ||
| defer { try? testFolder.delete() } | ||
| try testFolder.create() | ||
|
|
||
| let file = testFolder.file("hash_test.txt") | ||
| let testData = "Hello, World!".data(using: .utf8)! | ||
| try file.create(with: testData) | ||
|
|
||
| // Test SHA256 hash | ||
| let hash = try file.hash(with: .sha256) | ||
| #expect(!hash.isEmpty) | ||
| #expect(hash.count == 64) // SHA256 produces 32 bytes = 64 hex characters | ||
|
|
||
| // Test consistency - same content should produce same hash | ||
| let hash2 = try file.hash(with: .sha256) | ||
| #expect(hash == hash2) | ||
|
|
||
| // Test with different content | ||
| let file2 = testFolder.file("hash_test2.txt") | ||
| try file2.create(with: "Different content".data(using: .utf8)!) | ||
| let differentHash = try file2.hash(with: .sha256) | ||
| #expect(hash != differentHash) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Multiple Hash Algorithms") | ||
| func testMultipleHashAlgorithms() throws { | ||
| #if canImport(CryptoKit) | ||
| let testFolder = try createTestFolder() | ||
| defer { try? testFolder.delete() } | ||
| try testFolder.create() | ||
|
|
||
| let file = testFolder.file("multi_hash_test.txt") | ||
| let testData = "Test data for multiple hash algorithms".data(using: .utf8)! | ||
| try file.create(with: testData) | ||
|
|
||
| // Test different algorithms produce different results | ||
| let sha256Hash = try file.hash(with: .sha256) | ||
| let sha384Hash = try file.hash(with: .sha384) | ||
| let sha512Hash = try file.hash(with: .sha512) | ||
| let md5Hash = try file.hash(with: .md5) | ||
|
|
||
| // Verify hash lengths | ||
| #expect(sha256Hash.count == 64) // 32 bytes * 2 hex chars | ||
| #expect(sha384Hash.count == 96) // 48 bytes * 2 hex chars | ||
| #expect(sha512Hash.count == 128) // 64 bytes * 2 hex chars | ||
| #expect(md5Hash.count == 32) // 16 bytes * 2 hex chars | ||
|
|
||
| // Verify they're all different | ||
| #expect(sha256Hash != sha384Hash) | ||
| #expect(sha256Hash != sha512Hash) | ||
| #expect(sha256Hash != md5Hash) | ||
| #expect(sha384Hash != sha512Hash) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Large File Hash Performance") | ||
| func testLargeFileHash() throws { | ||
| #if canImport(CryptoKit) | ||
| let testFolder = try createTestFolder() | ||
| defer { try? testFolder.delete() } | ||
| try testFolder.create() | ||
|
|
||
| let file = testFolder.file("large_file.txt") | ||
|
|
||
| // Create a larger file (1MB of data) | ||
| let chunkSize = 1024 | ||
| let numChunks = 1024 | ||
| let chunk = String(repeating: "A", count: chunkSize).data(using: .utf8)! | ||
|
|
||
| try file.create() | ||
| let handle = try file.handle(.writing) | ||
| defer { handle.closeFile() } | ||
|
|
||
| for _ in 0..<numChunks { | ||
| handle.write(chunk) | ||
| } | ||
| handle.closeFile() | ||
|
|
||
| // Test hashing the large file | ||
| let startTime = Date() | ||
| let hash = try file.hash(with: .sha256) | ||
| let duration = Date().timeIntervalSince(startTime) | ||
|
|
||
| #expect(!hash.isEmpty) | ||
| #expect(hash.count == 64) | ||
| #expect(duration < 5.0) // Should complete within 5 seconds | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Hash Kind Direct Usage") | ||
| func testHashKindDirectUsage() throws { | ||
| #if canImport(CryptoKit) | ||
| let testData = "Direct hash test".data(using: .utf8)! | ||
|
|
||
| // Test hashing data directly through STHasherKind | ||
| let sha256Hash = try STHasherKind.sha256.hash(with: testData) | ||
| let md5Hash = try STHasherKind.md5.hash(with: testData) | ||
|
|
||
| #expect(!sha256Hash.isEmpty) | ||
| #expect(!md5Hash.isEmpty) | ||
| #expect(sha256Hash != md5Hash) | ||
|
|
||
| // Test consistency | ||
| let sha256Hash2 = try STHasherKind.sha256.hash(with: testData) | ||
| #expect(sha256Hash == sha256Hash2) | ||
| #endif | ||
| } | ||
|
|
||
| @Test("Empty File Hash") | ||
| func testEmptyFileHash() throws { | ||
| #if canImport(CryptoKit) | ||
| let testFolder = try createTestFolder() | ||
| defer { try? testFolder.delete() } | ||
| try testFolder.create() | ||
|
|
||
| let file = testFolder.file("empty_file.txt") | ||
| try file.create(with: Data()) | ||
|
|
||
| let hash = try file.hash(with: .sha256) | ||
| #expect(!hash.isEmpty) | ||
| #expect(hash.count == 64) | ||
|
|
||
| // Empty files should have a consistent hash | ||
| let hash2 = try file.hash(with: .sha256) | ||
| #expect(hash == hash2) | ||
| #endif | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要修改这个文件
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
STPath+Metadata.swiftfile has been reverted to its original state as requested. However, this creates a compilation issue on Linux environments because the original file uses extended attribute functions without proper imports. The extended attribute tests are already conditionally compiled for Darwin only. Commit: 69be109