Fixed bugs found for the bitmap mask, JSON reader and wifi wrappers in the library.
This commit is contained in:
@@ -27,6 +27,7 @@ enum Mock {
|
||||
nonisolated(unsafe) static let tilemapAPI = UnsafeMutablePointer<playdate_tilemap>.allocate(capacity: 1)
|
||||
nonisolated(unsafe) static let fileAPI = UnsafeMutablePointer<playdate_file>.allocate(capacity: 1)
|
||||
nonisolated(unsafe) static let jsonAPI = UnsafeMutablePointer<playdate_json>.allocate(capacity: 1)
|
||||
nonisolated(unsafe) static let networkAPI = UnsafeMutablePointer<playdate_network>.allocate(capacity: 1)
|
||||
nonisolated(unsafe) static let apiStruct = UnsafeMutablePointer<PlaydateAPI>.allocate(capacity: 1)
|
||||
|
||||
// MARK: - Recordings
|
||||
@@ -41,6 +42,10 @@ enum Mock {
|
||||
/// Caps the bytes a file read returns (0 = end of file, negative =
|
||||
/// error); `nil` fills the whole request.
|
||||
nonisolated(unsafe) static var fileReadLimit: Int32?
|
||||
/// Bytes left to read before 0 (end of file); `nil` never ends.
|
||||
nonisolated(unsafe) static var fileBytesRemaining: Int32?
|
||||
/// The last `network->setEnabled` callback.
|
||||
nonisolated(unsafe) static var networkEnabledCallback: (@convention(c) (PDNetErr) -> Void)?
|
||||
/// The index buffer and count last handed to `setTiles`.
|
||||
nonisolated(unsafe) static var tilesPointer: UnsafeMutablePointer<UInt16>?
|
||||
nonisolated(unsafe) static var tilesCount: Int32 = 0
|
||||
@@ -94,6 +99,8 @@ enum Mock {
|
||||
patternBytes = []
|
||||
stencilRows = []
|
||||
fileReadLimit = nil
|
||||
fileBytesRemaining = nil
|
||||
networkEnabledCallback = nil
|
||||
tilesPointer = nil
|
||||
tilesCount = 0
|
||||
spriteUserdata = [:]
|
||||
@@ -115,6 +122,7 @@ enum Mock {
|
||||
installSound()
|
||||
installFile()
|
||||
installJSON()
|
||||
installNetwork()
|
||||
apiStruct.initialize(to: PlaydateAPI(
|
||||
system: UnsafePointer(sysAPI),
|
||||
file: UnsafePointer(fileAPI),
|
||||
@@ -125,7 +133,7 @@ enum Mock {
|
||||
lua: nil,
|
||||
json: UnsafePointer(jsonAPI),
|
||||
scoreboards: nil,
|
||||
network: nil))
|
||||
network: UnsafePointer(networkAPI)))
|
||||
Playdate.initialize(with: UnsafeMutableRawPointer(apiStruct))
|
||||
}
|
||||
|
||||
@@ -220,6 +228,10 @@ enum Mock {
|
||||
gfxAPI.pointee.freeBitmap = { _ in
|
||||
Mock.record("freeBitmap")
|
||||
}
|
||||
gfxAPI.pointee.getBitmapMask = { _ in
|
||||
Mock.record("getBitmapMask")
|
||||
return Mock.fakePointer()
|
||||
}
|
||||
|
||||
gfxAPI.pointee.newBitmapTable = { count, width, height in
|
||||
Mock.record("newBitmapTable(\(count))")
|
||||
@@ -430,7 +442,11 @@ enum Mock {
|
||||
}
|
||||
fileAPI.pointee.read = { _, buffer, length in
|
||||
Mock.record("read(\(length))")
|
||||
let count = min(Int32(length), Mock.fileReadLimit ?? Int32(length))
|
||||
var count = min(Int32(length), Mock.fileReadLimit ?? Int32(length))
|
||||
if let remaining = Mock.fileBytesRemaining {
|
||||
count = min(count, remaining)
|
||||
Mock.fileBytesRemaining = remaining - max(count, 0)
|
||||
}
|
||||
if count > 0 { memset(buffer, 0xAB, Int(count)) }
|
||||
return count
|
||||
}
|
||||
@@ -440,6 +456,16 @@ enum Mock {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Network
|
||||
|
||||
private static func installNetwork() {
|
||||
networkAPI.initialize(to: playdate_network())
|
||||
networkAPI.pointee.setEnabled = { flag, callback in
|
||||
Mock.record("setEnabled(\(flag),\(callback == nil ? "nil" : "callback"))")
|
||||
Mock.networkEnabledCallback = callback
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - JSON
|
||||
|
||||
/// Simulates the OS parser's callback sequence for the document
|
||||
@@ -447,14 +473,18 @@ enum Mock {
|
||||
private static func installJSON() {
|
||||
jsonAPI.initialize(to: playdate_json())
|
||||
|
||||
// Pulls one chunk through the reader, as the OS would, and decodes
|
||||
// it as `null`.
|
||||
// Reads until the reader returns 0 or less, then decodes `null`.
|
||||
jsonAPI.pointee.decode = { _, reader, outval in
|
||||
var buffer = [UInt8](repeating: 0, count: 16)
|
||||
let count = buffer.withUnsafeMutableBufferPointer { buffer in
|
||||
reader.read?(reader.userdata, buffer.baseAddress, Int32(buffer.count)) ?? -1
|
||||
var total: Int32 = 0, last: Int32 = 0
|
||||
for _ in 0..<64 {
|
||||
last = buffer.withUnsafeMutableBufferPointer { buffer in
|
||||
reader.read?(reader.userdata, buffer.baseAddress, Int32(buffer.count)) ?? -1
|
||||
}
|
||||
guard last > 0 else { break }
|
||||
total += last
|
||||
}
|
||||
Mock.record("decode(\(count))")
|
||||
Mock.record("decode(total:\(total),end:\(last))")
|
||||
outval?.pointee = json_value()
|
||||
outval?.pointee.type = CChar(kJSONNull.rawValue)
|
||||
return 1
|
||||
|
||||
@@ -127,6 +127,23 @@ struct WrapperTests {
|
||||
#expect(UnsafePointer(Mock.tilesPointer) == storage)
|
||||
}
|
||||
|
||||
@Test func bitmapMaskIsFreedAndKeepsItsBitmapAlive() {
|
||||
weak var weakBitmap: Graphics.Bitmap?
|
||||
var mask: Graphics.Bitmap?
|
||||
do {
|
||||
let bitmap = Graphics.Bitmap(width: 8, height: 8)
|
||||
weakBitmap = bitmap
|
||||
mask = bitmap.mask
|
||||
}
|
||||
#expect(mask != nil)
|
||||
#expect(weakBitmap != nil) // the mask keeps it alive
|
||||
#expect(Mock.eventCount("freeBitmap") == 0)
|
||||
|
||||
mask = nil
|
||||
#expect(weakBitmap == nil)
|
||||
#expect(Mock.eventCount("freeBitmap") == 2) // the mask, then the bitmap
|
||||
}
|
||||
|
||||
// MARK: Sprite
|
||||
|
||||
@Test func spriteUserdataRecoversWrapperInCallbacks() {
|
||||
@@ -385,13 +402,14 @@ struct WrapperTests {
|
||||
}
|
||||
|
||||
@Test func jsonDecodeFileReadsThroughTheHandleAndClosesIt() throws {
|
||||
Mock.fileReadLimit = 5
|
||||
Mock.fileBytesRemaining = 20
|
||||
let value = try JSON.decodeFile(path: "save.json")
|
||||
guard case .null = value else {
|
||||
Issue.record("expected .null, got \(value)")
|
||||
return
|
||||
}
|
||||
#expect(Mock.events.contains("decode(5)"))
|
||||
// End of file reaches the decoder as 0, not -1.
|
||||
#expect(Mock.events.contains("decode(total:20,end:0)"))
|
||||
#expect(Mock.eventCount("close") == 1)
|
||||
}
|
||||
|
||||
@@ -417,6 +435,20 @@ struct WrapperTests {
|
||||
#expect(throws: PlaydateError.self) { try handle.read(length: 8) }
|
||||
}
|
||||
|
||||
// MARK: Network
|
||||
|
||||
@Test func networkDisableRegistersNoCallbackSoEnableGetsItsOwnResult() {
|
||||
var results: [String] = []
|
||||
Network.disable()
|
||||
#expect(Mock.events.last == "setEnabled(false,nil)")
|
||||
|
||||
Network.enable { error in results.append(error == nil ? "ok" : "failed") }
|
||||
#expect(Mock.events.last == "setEnabled(true,callback)")
|
||||
|
||||
Mock.networkEnabledCallback?(NET_OK)
|
||||
#expect(results == ["ok"])
|
||||
}
|
||||
|
||||
// MARK: JSON
|
||||
|
||||
@Test func jsonDecodeBuildsTheValueTree() throws {
|
||||
|
||||
Reference in New Issue
Block a user