1 import XCTest
2 @testable import FlatBuffers
3 
4 final class FlatBuffersVectors: XCTestCase {
5 
testCreatingTwoCountriesnull6     func testCreatingTwoCountries() {
7         let norway = "Norway"
8         let denmark = "Denmark"
9         var b = FlatBufferBuilder(initialSize: 20)
10         let noStr = b.create(string: norway)
11         let deStr = b.create(string: denmark)
12         let n = Country.createCountry(builder: &b, offset: noStr, log: 888, lan: 700)
13         let d = Country.createCountry(builder: &b, offset: deStr, log: 200, lan: 100)
14         let vector = [n, d]
15         let vectorOffset = b.createVector(ofOffsets: vector)
16         b.finish(offset: vectorOffset)
17         XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 2, 0, 0, 0, 48, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 18, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 40, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 24, 0, 0, 0, 188, 2, 0, 0, 120, 3, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0])
18     }
19 
testCreateIntArraynull20     func testCreateIntArray() {
21         let numbers: [Int32] = [1, 2, 3, 4, 5]
22         let b = FlatBufferBuilder(initialSize: 20)
23         let o = b.createVector(numbers, size: numbers.count)
24         b.finish(offset: o)
25         XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0])
26     }
27 
testCreateEmptyIntArraynull28     func testCreateEmptyIntArray() {
29         let numbers: [Int32] = []
30         let b = FlatBufferBuilder(initialSize: 20)
31         let o = b.createVector(numbers, size: numbers.count)
32         b.finish(offset: o)
33         XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 0, 0, 0, 0])
34     }
35 
testCreateVectorOfStringsnull36     func testCreateVectorOfStrings() {
37         let strs = ["Denmark", "Norway"]
38         let b = FlatBufferBuilder(initialSize: 20)
39         let o = b.createVector(ofStrings: strs)
40         b.finish(offset: o)
41         XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 2, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0])
42     }
testCreateSharedStringVectornull43     func testCreateSharedStringVector() {
44         let norway = "Norway"
45         let denmark = "Denmark"
46         let b = FlatBufferBuilder(initialSize: 20)
47         let noStr = b.createShared(string: norway)
48         let deStr = b.createShared(string: denmark)
49         let _noStr = b.createShared(string: norway)
50         let _deStr = b.createShared(string: denmark)
51         let v = [noStr, deStr, _noStr, _deStr]
52         let end = b.createVector(ofOffsets: v)
53         b.finish(offset: end)
54         XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 4, 0, 0, 0, 28, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0])
55     }
56 
testReadInt32Arraynull57     func testReadInt32Array() {
58         let data: [Int32] = [1, 2, 3, 4, 5]
59         let b = FlatBufferBuilder(initialSize: 20)
60         let v = Numbers.createNumbersVector(b: b, array: data)
61         let end = Numbers.createNumbers(b: b, o: v)
62         b.finish(offset: end)
63         let number = Numbers.getRootAsNumbers(ByteBuffer(bytes: b.sizedByteArray))
64         XCTAssertEqual(number.vArrayInt32, [1, 2, 3, 4, 5])
65     }
66 
testReadDoubleArraynull67     func testReadDoubleArray() {
68         let data: [Double] = [1, 2, 3, 4, 5]
69         let b = FlatBufferBuilder(initialSize: 20)
70         let v = Numbers.createNumbersVector(b: b, array: data)
71         let end = Numbers.createNumbers(b: b, o: v)
72         b.finish(offset: end)
73         let number = Numbers.getRootAsNumbers(ByteBuffer(bytes: b.sizedByteArray))
74         XCTAssertEqual(number.vArrayDouble, [1, 2, 3, 4, 5])
75     }
76 }
77 
78 struct Numbers {
79 
80     private var __t: Table
81 
82     private init(_ t: Table) {
83         __t = t
84     }
85 
getRootAsNumbersnull86     @inlinable static func getRootAsNumbers(_ bb: ByteBuffer) -> Numbers {
87         return Numbers(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0))))
88     }
89 
90     var vArrayInt: [Int]? { return __t.getVector(at: 4) }
91     var vArrayInt32: [Int32]? { return __t.getVector(at: 4) }
92     var vArrayDouble: [Double]? { return __t.getVector(at: 4) }
93     var vArrayFloat: [Float32]? { return __t.getVector(at: 4) }
94 
createNumbersVectornull95     static func createNumbersVector(b: FlatBufferBuilder, array: [Int]) -> Offset<UOffset> {
96         return b.createVector(array, size: array.count)
97     }
98 
createNumbersVectornull99     static func createNumbersVector(b: FlatBufferBuilder, array: [Int32]) -> Offset<UOffset> {
100         return b.createVector(array, size: array.count)
101     }
102 
createNumbersVectornull103     static func createNumbersVector(b: FlatBufferBuilder, array: [Double]) -> Offset<UOffset> {
104         return b.createVector(array, size: array.count)
105     }
106 
createNumbersVectornull107     static func createNumbersVector(b: FlatBufferBuilder, array: [Float32]) -> Offset<UOffset> {
108         return b.createVector(array, size: array.count)
109     }
110 
createNumbersnull111     static func createNumbers(b: FlatBufferBuilder, o: Offset<UOffset>) -> Offset<UOffset> {
112         let start = b.startTable(with: 1)
113         b.add(offset: o, at: 0)
114         return Offset(offset: b.endTable(at: start))
115     }
116 }
117