This commit is contained in:
2026-08-17 01:21:55 +02:00
parent 5761d6e67a
commit 4bd1e24e75
265 changed files with 20617 additions and 24457 deletions
@@ -14,49 +14,44 @@
* limitations under the License.
*/
import Testing
import XCTest
@testable import Common
@testable import FlatBuffers
struct FlatBuffersTests {
final class FlatBuffersTests: XCTestCase {
let country = "Norway"
@Test
func testEndian() { #expect(isLitteEndian == true) }
func testEndian() { XCTAssertEqual(isLitteEndian, true) }
@Test
func testOffset() {
let o = Offset()
let b = Offset(offset: 1)
#expect(o.isEmpty == true)
#expect(b.isEmpty == false)
XCTAssertEqual(o.isEmpty, true)
XCTAssertEqual(b.isEmpty, false)
}
@Test
func testCreateString() {
let helloWorld = "Hello, world!"
var b = FlatBufferBuilder(initialSize: 16)
#expect(b.create(string: country).o == 12)
#expect(b.create(string: helloWorld).o == 32)
XCTAssertEqual(b.create(string: country).o, 12)
XCTAssertEqual(b.create(string: helloWorld).o, 32)
b.clear()
#expect(b.create(string: helloWorld).o == 20)
#expect(b.create(string: country).o == 32)
XCTAssertEqual(b.create(string: helloWorld).o, 20)
XCTAssertEqual(b.create(string: country).o, 32)
b.clear()
#expect(b.create(string: String(repeating: "a", count: 257)).o == 264)
XCTAssertEqual(b.create(string: String(repeating: "a", count: 257)).o, 264)
}
@Test
func testStartTable() {
var b = FlatBufferBuilder(initialSize: 16)
_ = b.startTable(with: 0)
XCTAssertNoThrow(b.startTable(with: 0))
b.clear()
#expect(b.create(string: country).o == 12)
#expect(b.startTable(with: 0) == 12)
XCTAssertEqual(b.create(string: country).o, 12)
XCTAssertEqual(b.startTable(with: 0), 12)
}
@Test
func testCreateFinish() {
var b = FlatBufferBuilder(initialSize: 16)
let countryOff = Country.createCountry(
@@ -71,10 +66,9 @@ struct FlatBuffersTests {
200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0,
]
// swiftformat:enable all
#expect(b.sizedByteArray == v)
XCTAssertEqual(b.sizedByteArray, v)
}
@Test
func testCreateFinishWithPrefix() {
var b = FlatBufferBuilder(initialSize: 16)
let countryOff = Country.createCountry(
@@ -89,10 +83,9 @@ struct FlatBuffersTests {
100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0,
]
// swiftformat:enable all
#expect(b.sizedByteArray == v)
XCTAssertEqual(b.sizedByteArray, v)
}
@Test
func testReadCountry() {
// swiftformat:disable all
let v: [UInt8] = [
@@ -102,20 +95,18 @@ struct FlatBuffersTests {
// swiftformat:enable all
let buffer = ByteBuffer(bytes: v)
let c = Country.getRootAsCountry(buffer)
#expect(c.lan == 100)
#expect(c.log == 200)
#expect(c.nameVector == [78, 111, 114, 119, 97, 121])
#expect(c.name == country)
XCTAssertEqual(c.lan, 100)
XCTAssertEqual(c.log, 200)
XCTAssertEqual(c.nameVector, [78, 111, 114, 119, 97, 121])
XCTAssertEqual(c.name, country)
}
@Test
func testWriteNullableStrings() {
var b = FlatBufferBuilder()
#expect(b.create(string: nil).isEmpty)
#expect(b.createShared(string: nil).isEmpty)
XCTAssertTrue(b.create(string: nil).isEmpty)
XCTAssertTrue(b.createShared(string: nil).isEmpty)
}
@Test
func testWriteOptionalValues() {
var b = FlatBufferBuilder()
let root = optional_scalars_ScalarStuff.createScalarStuff(
@@ -130,17 +121,16 @@ struct FlatBuffersTests {
b.finish(offset: root)
var buffer = b.sizedBuffer
let scalarTable: optional_scalars_ScalarStuff = getRoot(byteBuffer: &buffer)
#expect(scalarTable.justI8 == 80)
#expect(scalarTable.maybeI8 == nil)
#expect(scalarTable.maybeBool == true)
#expect(scalarTable.defaultI8 == 42)
#expect(scalarTable.justU8 == 100)
#expect(scalarTable.maybeU8 == 10)
#expect(scalarTable.justEnum == .one)
#expect(scalarTable.maybeEnum == nil)
XCTAssertEqual(scalarTable.justI8, 80)
XCTAssertNil(scalarTable.maybeI8)
XCTAssertEqual(scalarTable.maybeBool, true)
XCTAssertEqual(scalarTable.defaultI8, 42)
XCTAssertEqual(scalarTable.justU8, 100)
XCTAssertEqual(scalarTable.maybeU8, 10)
XCTAssertEqual(scalarTable.justEnum, .one)
XCTAssertNil(scalarTable.maybeEnum)
}
@Test
func testAlignmentCrash() {
var builder = FlatBufferBuilder(initialSize: 256)