mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 17:28:19 +00:00
[Swift] Memory usage fix (#8643)
Allows a complete reset for the underlying memory of the _InternalByteBuffers within FlatBuffers and FlexBuffers.
This commit is contained in:
@@ -15,9 +15,10 @@
|
||||
*/
|
||||
|
||||
import Common
|
||||
import FlexBuffers
|
||||
import XCTest
|
||||
|
||||
@testable import FlexBuffers
|
||||
|
||||
final class FlexBuffersReaderTests: XCTestCase {
|
||||
|
||||
func testReadingProperBuffer() throws {
|
||||
@@ -30,6 +31,29 @@ final class FlexBuffersReaderTests: XCTestCase {
|
||||
try validate(buffer: buf)
|
||||
}
|
||||
|
||||
func testReset() throws {
|
||||
var fbx = FlexBuffersWriter(
|
||||
initialSize: 8,
|
||||
flags: .shareKeysAndStrings)
|
||||
write(fbx: &fbx)
|
||||
|
||||
try validate(buffer: ByteBuffer(data: fbx.data))
|
||||
XCTAssertEqual(fbx.capacity, 512)
|
||||
fbx.reset()
|
||||
XCTAssertEqual(fbx.writerIndex, 0)
|
||||
XCTAssertEqual(fbx.capacity, 8)
|
||||
|
||||
write(fbx: &fbx)
|
||||
try validate(buffer: ByteBuffer(data: fbx.data))
|
||||
fbx.reset(keepingCapacity: true)
|
||||
XCTAssertEqual(fbx.writerIndex, 0)
|
||||
XCTAssertEqual(fbx.capacity, 512)
|
||||
|
||||
write(fbx: &fbx)
|
||||
try validate(buffer: ByteBuffer(data: fbx.data))
|
||||
XCTAssertEqual(fbx.capacity, 512)
|
||||
}
|
||||
|
||||
private func validate(buffer buf: ByteBuffer) throws {
|
||||
let reference = try getRoot(buffer: buf)!
|
||||
XCTAssertEqual(reference.type, .map)
|
||||
|
||||
@@ -32,7 +32,11 @@ func createProperBuffer() -> FlexBuffersWriter {
|
||||
var fbx = FlexBuffersWriter(
|
||||
initialSize: 8,
|
||||
flags: .shareKeysAndStrings)
|
||||
write(fbx: &fbx)
|
||||
return fbx
|
||||
}
|
||||
|
||||
func write(fbx: inout FlexBuffersWriter) {
|
||||
fbx.map { map in
|
||||
map.vector(key: "vec") { v in
|
||||
v.add(int64: -100)
|
||||
@@ -57,5 +61,4 @@ func createProperBuffer() -> FlexBuffersWriter {
|
||||
}
|
||||
|
||||
fbx.finish()
|
||||
return fbx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user