Adds windows swift support (#8622)

Adding support for windows requires the code generations
to add a compiler statement to completely ignore GRPC code
generation on windows

Cleanup the project to use the main Package.swift to run tests
instead of having it separate and includes the imports for GRPC
within it.

Adds windows swift ci
This commit is contained in:
mustiikhalil
2025-06-30 14:45:48 +02:00
committed by GitHub
parent 75556437cc
commit b8db3a9a6a
38 changed files with 64 additions and 68 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Common
import FlexBuffers
import XCTest
final class FlexBuffersJSONTests: XCTestCase {
func testEncodingJSON() throws {
let buf: ByteBuffer = createProperBuffer().sizedByteBuffer
let reference = try getRoot(buffer: buf)!
let json = reference.jsonString()
// swiftformat:disable all
XCTAssertEqual(
json,
"{\"bar\": [1, 2, 3], \"bar3\": [1, 2, 3], \"bool\": true, \"bools\": [true, false, true, false], \"foo\": 100.0, \"mymap\": {\"foo\": \"Fred\"}, \"vec\": [-100, \"Fred\", 4.0, \"M\", false, 4.0]}"
)
// swiftformat:enable all
let data = json.data(using: .utf8)!
let decodedData = try JSONSerialization.jsonObject(
with: data,
options: []) as! [String: Any]
XCTAssertEqual(decodedData["bar"] as! [Int], [1, 2, 3])
XCTAssertEqual(decodedData["bar3"] as! [Int], [1, 2, 3])
let vec: [Any] = decodedData["vec"] as! [Any]
XCTAssertEqual(vec[0] as! Int, -100)
XCTAssertEqual(vec[1] as! String, "Fred")
XCTAssertEqual(vec[2] as! Double, 4.0)
XCTAssertEqual(vec[3] as! String, "M")
XCTAssertEqual(vec[4] as! Bool, false)
XCTAssertEqual(vec[5] as! Double, 4.0)
}
}

View File

@@ -0,0 +1,116 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Common
import FlexBuffers
import XCTest
final class FlexBuffersReaderTests: XCTestCase {
func testReadingProperBuffer() throws {
let buf: ByteBuffer = createProperBuffer().byteBuffer
try validate(buffer: buf)
}
func testReadingSizedBuffer() throws {
let buf: ByteBuffer = createSizedBuffer()
try validate(buffer: buf)
}
private func validate(buffer buf: ByteBuffer) throws {
let reference = try getRoot(buffer: buf)!
XCTAssertEqual(reference.type, .map)
let map = reference.map!
XCTAssertEqual(map.count, 7)
let vecRef = map["vec"]!
XCTAssertEqual(vecRef.type, .vector)
let vec = vecRef.vector!
XCTAssertEqual(vec.count, 6)
XCTAssertEqual(vec[0]?.type, .int)
XCTAssertEqual(vec[0]?.int, -100)
XCTAssertEqual(vec[1]?.type, .string)
XCTAssertEqual(vec[1]?.cString, "Fred")
XCTAssertNil(vec[1]?.int)
XCTAssertEqual(vec[2]?.double, 4.0)
XCTAssertTrue(vec[3]?.type == .blob)
let blob = vec[3]!.blob { pointer in
Array(pointer)
}
XCTAssertEqual(blob?.count, 1)
XCTAssertEqual(blob?[0], 77)
XCTAssertEqual(vec[4]?.type, .bool)
XCTAssertEqual(vec[4]?.bool, false)
XCTAssertEqual(vec[5]?.double, 4.0) // Shared with vec[2]
let barVec = map["bar"]!.typedVector!
XCTAssertEqual(barVec.count, 3)
XCTAssertEqual(barVec[2]?.int, 3)
XCTAssertEqual(barVec[2]?.asInt(), UInt8(3))
let fixedVec = map["bar3"]!.fixedTypedVector!
XCTAssertEqual(fixedVec.count, 3)
XCTAssertEqual(fixedVec[2]?.int, 3)
XCTAssertEqual(fixedVec[2]?.asInt(), UInt8(3))
XCTAssertEqual(map["bool"]?.bool, true)
let boolsVector = map["bools"]!.typedVector!
XCTAssertEqual(boolsVector.type, .bool)
XCTAssertEqual(boolsVector[0]?.bool, true)
XCTAssertEqual(boolsVector[1]?.bool, false)
let bools = [true, false, true, false]
boolsVector.withUnsafeRawBufferPointer { buff in
for i in 0..<boolsVector.count {
XCTAssertEqual(buff.load(fromByteOffset: i, as: Bool.self), bools[i])
}
}
XCTAssertEqual(map["foo"]?.double, 100)
XCTAssertNil(map["unknown"])
let mymap = map["mymap"]?.map
// Check if both addresses used are the same for keys and strings
XCTAssertEqual(mymap?.keys[0]?.cString, map.keys[4]?.cString)
map.keys[4]?.withUnsafeRawPointer { pointer in
mymap?.keys[0]?.withUnsafeRawPointer { mymapPointer in
XCTAssertEqual(pointer, mymapPointer)
}
}
XCTAssertEqual(mymap?.values[0]?.cString, vec[1]?.cString)
vec[1]?.withUnsafeRawPointer { pointer in
mymap?.values[0]?.withUnsafeRawPointer { mymapPointer in
XCTAssertEqual(pointer, mymapPointer)
}
}
}
private var path: String {
#if os(macOS)
// Gets the current path of this test file then
// strips out the nested directories.
let filePath = URL(filePath: #file)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
return filePath.absoluteString
#else
return FileManager.default.currentDirectoryPath
#endif
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Common
import FlexBuffers
import XCTest
final class FlexBuffersStringTests: XCTestCase {
func testEncodingUnicodeString() {
let text = "プ画をみて✋"
let bytes = text.data(using: .unicode, allowLossyConversion: true)
var flx = FlexBuffersWriter()
flx.map { writer in
writer.add(blob: bytes!, key: "text", length: bytes!.count)
}
flx.finish()
let byteBuffer = flx.sizedByteBuffer
let reference = try! getRoot(buffer: byteBuffer)
let root = reference?.map?["text"]
let builtString = root?.blob {
let data = Data(bytes: $0.baseAddress!, count: Int($0.count))
return String(data: data, encoding: .unicode)
}
XCTAssertEqual(builtString, text)
}
}

View File

@@ -0,0 +1,236 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Common
import FlexBuffers
import XCTest
final class FlexBuffersWriterTests: XCTestCase {
func testDeallocation() {
let buf: ByteBuffer = {
var fbx = FlexBuffersWriter()
fbx.add(string: "Hello")
fbx.finish()
return fbx.sizedByteBuffer
}()
buf.withUnsafeBytes {
XCTAssertEqual(
Array($0),
[5, 72, 101, 108, 108, 111, 0, 6, 20, 1])
}
}
func testAddingVectorOfScalars() {
var fbx = FlexBuffersWriter()
fbx.vector {
let arr: [Int32] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
$0.create(vector: arr)
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
[10, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 20, 0, 0, 0, 1, 41, 46, 2, 40, 1])
// swiftformat:enable all
}
}
func testAddingVectorOfUnsignedScalars() {
var fbx = FlexBuffersWriter()
fbx.vector {
let arr: [UInt] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
$0.create(vector: arr)
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
[10, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 1, 81, 51, 2, 40, 1])
// swiftformat:enable all
}
}
func testAddingVectorOfBools() {
var fbx = FlexBuffersWriter()
fbx.vector {
let arr: [Bool] = [true, false, true, false]
$0.create(vector: arr)
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
[4, 1, 0, 1, 0, 1, 5, 144, 2, 40, 1])
// swiftformat:enable all
}
}
func testSortingWithinMap() {
var fbx = FlexBuffersWriter()
fbx.map {
$0.add(bool: false, key: "bool2")
$0.add(bool: true, key: "bool1")
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
[98, 111, 111, 108, 50, 0, 98, 111, 111, 108, 49, 0, 2, 7, 14, 2, 1, 2, 1, 0, 104, 104, 4, 36, 1]
)
// swiftformat:enable all
}
}
func testSharingKeyWithinMap() {
var fbx = FlexBuffersWriter(initialSize: 1000, flags: .shareKeysAndStrings)
fbx.map {
$0.add(string: "welcome", key: "welcome")
$0.add(string: "welcome", key: "welcome")
$0.add(string: "welcome", key: "welcome")
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
[119, 101, 108, 99, 111, 109, 101, 0, 7, 119, 101, 108, 99, 111, 109, 101, 0, 3, 18, 19, 20, 3, 1, 3, 15, 16, 17, 20, 20, 20, 6, 36, 1]
)
// swiftformat:enable all
}
}
func testNestingVectorInMap() {
let buf: ByteBuffer = createSizedBuffer()
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
flexbufferGolden
)
// swiftformat:enable all
}
}
func testAddingNil() {
var fbx = FlexBuffersWriter(
initialSize: 8,
flags: .shareKeysAndStrings)
fbx.map { map in
map.addNil(key: "v")
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
[118, 0, 1, 3, 1, 1, 1, 0, 0, 2, 36, 1]
)
// swiftformat:enable all
}
}
func testAddingManually() {
var fbx = FlexBuffersWriter(
initialSize: 8,
flags: .shareKeysAndStrings)
let outerMap = fbx.startMap()
let vector = fbx.startVector(key: "vec")
fbx.add(int64: -100)
fbx.add(string: "Fred")
fbx.indirect(float32: 4.0)
let lv = fbx.lastValue()
let blob: [UInt8] = [77]
fbx.add(blob: blob, length: blob.count)
fbx.add(bool: false)
fbx.reuse(value: lv!)
fbx.endVector(start: vector)
let ints: [Int32] = [1, 2, 3]
fbx.create(vector: ints, key: "bar")
fbx.createFixed(vector: ints, key: "bar3")
let bools = [true, false, true, false]
fbx.create(vector: bools, key: "bools")
fbx.add(bool: true, key: "bool")
fbx.add(double: 100, key: "foo")
let innerMap = fbx.startMap(key: "mymap")
fbx.add(string: "Fred", key: "foo")
fbx.endMap(start: innerMap)
fbx.endMap(start: outerMap)
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
flexbufferGolden
)
// swiftformat:enable all
}
}
func testEncodingAllTypes() {
var fbx = FlexBuffersWriter()
fbx.vector {
$0.indirect(int64: 9)
$0.indirect(uint64: 9)
$0.indirect(float32: 3)
$0.indirect(double: 3)
$0.addNil()
$0.add(bool: true)
$0.add(int64: 9)
$0.add(int64: -9)
$0.add(uint64: 9)
$0.add(double: 2.4)
$0.add(float32: 2.4)
$0.add(double: -2.4)
$0.add(float32: -2.4)
}
fbx.finish()
let buf: ByteBuffer = fbx.sizedByteBuffer
buf.withUnsafeBytes {
// swiftformat:disable all
XCTAssertEqual(
Array($0),
allTypesGolden)
// swiftformat:enable all
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import FlexBuffers
// swiftformat:disable all
let flexbufferGolden: [UInt8] = [118, 101, 99, 0, 4, 70, 114, 101, 100, 0, 0, 0, 0, 0, 128, 64, 1, 77, 6, 156, 15, 9, 5, 0, 12, 4, 20, 34, 100, 104, 34, 98, 97, 114, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 98, 97, 114, 51, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 98, 111, 111, 108, 115, 0, 4, 1, 0, 1, 0, 98, 111, 111, 108, 0, 102, 111, 111, 0, 109, 121, 109, 97, 112, 0, 1, 11, 1, 1, 1, 98, 20, 7, 75, 55, 25, 37, 22, 19, 112, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 7, 0, 0, 0, 88, 0, 0, 0, 72, 0, 0, 0, 1, 0, 0, 0, 61, 0, 0, 0, 0, 0, 200, 66, 45, 0, 0, 0, 133, 0, 0, 0, 46, 78, 106, 144, 14, 36, 40, 35, 38, 1]
let allTypesGolden: [UInt8] = [9, 9, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 247, 255, 255, 255, 255, 255, 255, 255, 9, 0, 0, 0, 0, 0, 0, 0, 51, 51, 51, 51, 51, 51, 3, 64, 0, 0, 0, 64, 51, 51, 3, 64, 51, 51, 51, 51, 51, 51, 3, 192, 0, 0, 0, 64, 51, 51, 3, 192, 24, 28, 34, 34, 3, 107, 7, 7, 11, 15, 15, 15, 15, 117, 43, 1]
// swiftformat:enable all
@inline(__always)
func createSizedBuffer() -> ByteBuffer {
createProperBuffer().sizedByteBuffer
}
@inline(__always)
func createProperBuffer() -> FlexBuffersWriter {
var fbx = FlexBuffersWriter(
initialSize: 8,
flags: .shareKeysAndStrings)
fbx.map { map in
map.vector(key: "vec") { v in
v.add(int64: -100)
v.add(string: "Fred")
v.indirect(float32: 4.0)
let lv = v.lastValue()
let blob: [UInt8] = [77]
v.add(blob: blob, length: blob.count)
v.add(bool: false)
v.reuse(value: lv!)
}
let ints: [Int32] = [1, 2, 3]
map.create(vector: ints, key: "bar")
map.createFixed(vector: ints, key: "bar3")
let bools = [true, false, true, false]
map.create(vector: bools, key: "bools")
map.add(bool: true, key: "bool")
map.add(double: 100, key: "foo")
map.map(key: "mymap") { m in
m.add(string: "Fred", key: "foo")
}
}
fbx.finish()
return fbx
}