55// Created by Gwen on 2025-11-04.
66//
77
8+ #if compiler(>=6.2)
9+
810import XCTest
911
1012@testable import FlatBuffers
1113
1214@available ( macOS 26 . 0 , iOS 26 . 0 , watchOS 26 . 0 , tvOS 26 . 0 , * )
1315final class FlatBuffersArraysTests : XCTestCase {
14-
16+
1517 func testGoldenData( ) throws {
1618 // swiftformat:disable all
1719 let data : [ UInt8 ] = [
@@ -27,9 +29,9 @@ final class FlatBuffersArraysTests: XCTestCase {
2729 255 , 255 , 255 , 255 , 255 , 255 , 127
2830 ]
2931 // swiftformat:enable all
30-
32+
3133 var builder = FlatBufferBuilder ( initialSize: 1024 )
32-
34+
3335 let nestedStruct1 = MyGame_Example_NestedStruct (
3436 a: [ - 1 , 2 ] ,
3537 b: . a,
@@ -52,22 +54,22 @@ final class FlatBuffersArraysTests: XCTestCase {
5254 e: 1 ,
5355 f: [ - 0x8000000000000000 , 0x7FFFFFFFFFFFFFFF ]
5456 )
55-
57+
5658 let arrayTable = MyGame_Example_ArrayTable . createArrayTable ( & builder, a: arrayStruct)
5759 builder. finish ( offset: arrayTable, fileId: " ARRT " )
58-
60+
5961 let array = builder. sizedByteArray
60-
62+
6163 XCTAssertEqual ( data, array)
62-
64+
6365 var buf = ByteBuffer ( bytes: array)
6466 let table : MyGame_Example_ArrayTable = try getCheckedRoot ( byteBuffer: & buf, fileId: " ARRT " )
65- verify ( a: table. a)
66- mutate ( table : table)
67- verify ( a: table. a)
67+ testNativeStruct ( a: table. a)
68+ testMutations ( in : table)
69+ testNativeStruct ( a: table. a)
6870 }
69-
70- func verify ( a: MyGame_Example_ArrayStruct ? ) {
71+
72+ func testNativeStruct ( a: MyGame_Example_ArrayStruct ? ) {
7173 let a = a!
7274 XCTAssertEqual ( a. a, 12.34 )
7375 XCTAssertEqual ( a. b. count, 15 )
@@ -93,8 +95,8 @@ final class FlatBuffersArraysTests: XCTestCase {
9395 XCTAssertEqual ( a. e, 1 )
9496 XCTAssertEqual ( a. f. count, 2 )
9597 }
96-
97- func mutate ( table: MyGame_Example_ArrayTable ) {
98+
99+ func testMutations ( in table: MyGame_Example_ArrayTable ) {
98100 let a = table. mutableA!
99101 XCTAssertEqual ( a. a, 12.34 )
100102 XCTAssertEqual ( a. b. count, 15 )
@@ -106,7 +108,7 @@ final class FlatBuffersArraysTests: XCTestCase {
106108 XCTAssertEqual ( a. c, - 127 )
107109 XCTAssertEqual ( a. d. count, 2 )
108110 let nestedStruct1 = a. d [ 0 ]
109-
111+
110112 XCTAssertEqual ( nestedStruct1. a. reduce ( 0 ) { $0 + $1 } , 1 )
111113 XCTAssertEqual ( nestedStruct1. b, . a)
112114 XCTAssertEqual ( nestedStruct1. c [ 0 ] , . c)
@@ -119,6 +121,37 @@ final class FlatBuffersArraysTests: XCTestCase {
119121 XCTAssertEqual ( nestedStruct2. c [ 1 ] , . a)
120122 XCTAssertEqual ( nestedStruct2. d [ 0 ] , - 0x1122334455667788 )
121123 XCTAssertEqual ( nestedStruct2. d [ 1 ] , 0x1122334455667788 )
124+
125+ XCTAssertTrue ( a. mutate ( b: 1000 , at: 0 ) )
126+ XCTAssertTrue ( a. mutate ( b: 2000 , at: 1 ) )
127+
128+ XCTAssertTrue ( nestedStruct2. mutate ( c: . a, at: 0 ) )
129+ XCTAssertTrue ( nestedStruct2. mutate ( c: . b, at: 1 ) )
130+
131+ XCTAssertEqual ( nestedStruct2. c [ 0 ] , . a)
132+ XCTAssertEqual ( nestedStruct2. c [ 1 ] , . b)
133+
134+ XCTAssertTrue ( nestedStruct2. mutate ( d: 0 , at: 0 ) )
135+ XCTAssertTrue ( nestedStruct2. mutate ( d: 0 , at: 1 ) )
136+
137+ XCTAssertEqual ( nestedStruct2. d. reduce ( 0 ) { $0 + $1 } , 0 )
138+
139+ let nativeStruct = table. a? . d [ 1 ]
140+
141+ XCTAssertEqual ( nativeStruct? . c [ 0 ] , . a)
142+ XCTAssertEqual ( nativeStruct? . c [ 1 ] , . b)
143+
144+ XCTAssertEqual ( nativeStruct? . d [ 0 ] , 0 )
145+ XCTAssertEqual ( nativeStruct? . d [ 1 ] , 0 )
146+
147+ XCTAssertTrue ( a. mutate ( b: 1 , at: 0 ) )
148+ XCTAssertTrue ( a. mutate ( b: 2 , at: 1 ) )
149+
150+ XCTAssertTrue ( nestedStruct2. mutate ( c: . b, at: 0 ) )
151+ XCTAssertTrue ( nestedStruct2. mutate ( c: . a, at: 1 ) )
152+
153+ XCTAssertTrue ( nestedStruct2. mutate ( d: - 0x1122334455667788 , at: 0 ) )
154+ XCTAssertTrue ( nestedStruct2. mutate ( d: 0x1122334455667788 , at: 1 ) )
122155 }
123156}
124157
@@ -132,3 +165,6 @@ extension InlineArray {
132165 return result
133166 }
134167}
168+
169+ #endif
170+
0 commit comments