Adopted Array(capacity:) for the read wrappers throughout the library to adapt to Swift 6.4.

This commit is contained in:
2026-09-18 12:10:16 +02:00
parent 4cb0f8f500
commit 95ae01f97a
5 changed files with 47 additions and 24 deletions
+7 -2
View File
@@ -35,6 +35,9 @@ enum Mock {
nonisolated(unsafe) static var buttonState: (current: UInt32, pushed: UInt32, released: UInt32) = (0, 0, 0)
/// The 16 bytes behind the last pattern `LCDColor` seen by a stub.
nonisolated(unsafe) static var patternBytes: [UInt8] = []
/// Caps the bytes a file read returns (0 = end of file, negative =
/// error); `nil` fills the whole request.
nonisolated(unsafe) static var fileReadLimit: Int32?
/// Userdata stored per sprite / menu item, as the OS would keep it.
nonisolated(unsafe) static var spriteUserdata: [OpaquePointer: UnsafeMutableRawPointer] = [:]
nonisolated(unsafe) static var menuUserdata: [OpaquePointer: UnsafeMutableRawPointer] = [:]
@@ -83,6 +86,7 @@ enum Mock {
events = []
buttonState = (0, 0, 0)
patternBytes = []
fileReadLimit = nil
spriteUserdata = [:]
menuUserdata = [:]
menuCallback = nil
@@ -400,9 +404,10 @@ enum Mock {
return 0
}
fileAPI.pointee.read = { _, buffer, length in
memset(buffer, 0xAB, Int(length))
Mock.record("read(\(length))")
return Int32(length)
let count = min(Int32(length), Mock.fileReadLimit ?? Int32(length))
if count > 0 { memset(buffer, 0xAB, Int(count)) }
return count
}
fileAPI.pointee.write = { _, _, length in
Mock.record("write(\(length))")
+13
View File
@@ -345,6 +345,19 @@ struct WrapperTests {
#expect(Mock.eventCount("close") == 1)
}
@Test func fileHandleReadLengthReturnsOnlyTheBytesRead() throws {
let handle = try File.Handle(path: "save.dat", mode: [.read, .readData])
Mock.fileReadLimit = 3
#expect(try handle.read(length: 8) == [0xAB, 0xAB, 0xAB])
Mock.fileReadLimit = 0
#expect(try handle.read(length: 8).isEmpty)
Mock.fileReadLimit = -1
#expect(throws: PlaydateError.self) { try handle.read(length: 8) }
}
// MARK: JSON
@Test func jsonDecodeBuildsTheValueTree() throws {