Update Swift Library and tests

This commit is contained in:
Chris Simpson 2018-08-29 14:40:44 -04:00 committed by James E. King III
parent 6e29b192a3
commit 2566ecd5d9
12 changed files with 112 additions and 21 deletions

View File

@ -20,6 +20,8 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Brought to you by [FiscalNote, Inc](http://www.fiscalnote.com/)
## Build
@ -92,6 +94,7 @@ func write(_ val: String) throws
| no_strict* | Generates non-strict structs |
| debug_descriptions | Allow use of debugDescription so the app can add description via a cateogory/extension |
| log_unexpected | Log every time an unexpected field ID or type is encountered. |
| safe_enums | Generate enum types with an unknown case to handle unspecified values rather than throw a serialization error |

View File

@ -79,7 +79,7 @@ public class TBinaryProtocol: TProtocol {
messageName = try read()
} else {
if strictRead {
let errorMessage = "Missing message version, old client? Message Name: \(currentMessageName)"
let errorMessage = "Missing message version, old client? Message Name: \(currentMessageName ?? "")"
throw TProtocolError(error: .invalidData,
message: errorMessage)
}
@ -233,7 +233,7 @@ public class TBinaryProtocol: TProtocol {
public func read() throws -> Double {
let val = try read() as Int64
return unsafeBitCast(val, to: Double.self)
return Double(bitPattern: UInt64(bitPattern: val))
}
public func read() throws -> Data {
@ -371,7 +371,7 @@ public class TBinaryProtocol: TProtocol {
public func write(_ value: Double) throws {
// Notably unsafe, since Double and Int64 are the same size, this should work fine
try self.write(unsafeBitCast(value, to: Int64.self))
try self.write(Int64(bitPattern: value.bitPattern))
}
public func write(_ data: Data) throws {

View File

@ -22,12 +22,12 @@ open class TClient {
public let inProtocol: TProtocol
public let outProtocol: TProtocol
public init(inoutProtocol: TProtocol) {
required public init(inoutProtocol: TProtocol) {
self.inProtocol = inoutProtocol
self.outProtocol = inoutProtocol
}
public init(inProtocol: TProtocol, outProtocol: TProtocol) {
required public init(inProtocol: TProtocol, outProtocol: TProtocol) {
self.inProtocol = inProtocol
self.outProtocol = outProtocol
}

View File

@ -222,11 +222,11 @@ public class TCompactProtocol: TProtocol {
///
/// - returns: zigzaged UInt32
func i32ToZigZag(_ n : Int32) -> UInt32 {
return UInt32(n << 1) ^ UInt32(n >> 31)
return UInt32(bitPattern: Int32(n << 1) ^ Int32(n >> 31))
}
func i64ToZigZag(_ n : Int64) -> UInt64 {
return UInt64(n << 1) ^ UInt64(n >> 63)
return UInt64(bitPattern: Int64(n << 1) ^ Int64(n >> 63))
}
func zigZagToi32(_ n: UInt32) -> Int32 {
@ -379,7 +379,7 @@ public class TCompactProtocol: TProtocol {
return UnsafePointer<UInt64>(OpaquePointer(ptr)).pointee
}
let bits = CFSwapInt64LittleToHost(i64)
return unsafeBitCast(bits, to: Double.self)
return Double(bitPattern: bits)
}
public func read() throws -> Data {
@ -557,7 +557,7 @@ public class TCompactProtocol: TProtocol {
}
public func write(_ value: Double) throws {
var bits = CFSwapInt64HostToLittle(unsafeBitCast(value, to: UInt64.self))
var bits = CFSwapInt64HostToLittle(value.bitPattern)
let data = withUnsafePointer(to: &bits) {
return Data(bytes: UnsafePointer<UInt8>(OpaquePointer($0)), count: MemoryLayout<UInt64>.size)
}

View File

@ -18,7 +18,7 @@
*/
public struct TList<Element : TSerializable> : RandomAccessCollection, MutableCollection, ExpressibleByArrayLiteral, TSerializable, Hashable {
typealias Storage = Array<Element>
public typealias Storage = Array<Element>
public typealias Indices = Storage.Indices
internal var storage = Storage()

View File

@ -18,7 +18,7 @@
*/
public struct TMap<Key : TSerializable & Hashable, Value : TSerializable>: Collection, ExpressibleByDictionaryLiteral, Hashable, TSerializable {
typealias Storage = Dictionary<Key, Value>
public typealias Storage = Dictionary<Key, Value>
public typealias Element = Storage.Element
public typealias Index = Storage.Index
public typealias IndexDistance = Storage.IndexDistance

View File

@ -21,7 +21,7 @@ import Foundation
public struct TSet<Element : TSerializable & Hashable> : SetAlgebra, Hashable, Collection, ExpressibleByArrayLiteral, TSerializable {
/// Typealias for Storage type
typealias Storage = Set<Element>
public typealias Storage = Set<Element>
/// Internal Storage used for TSet (Set\<Element\>)

View File

@ -83,8 +83,8 @@ public class TCFSocketTransport: TStreamTransport {
CFWriteStreamSetProperty(writeStream, .shouldCloseNativeSocket, kCFBooleanTrue)
if secure {
CFReadStreamSetProperty(readStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL._rawValue)
CFWriteStreamSetProperty(writeStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL._rawValue)
CFReadStreamSetProperty(readStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL.rawValue as CFString)
CFWriteStreamSetProperty(writeStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL.rawValue as CFString)
}
inputStream = readStream as InputStream

View File

@ -1,3 +1,3 @@
class Thrift {
let version = "0.0.1"
let version = "1.1.0"
}

View File

@ -129,6 +129,28 @@ class TBinaryProtocolTests: XCTestCase {
XCTAssertFalse(true, "Caught Error attempting to read \(error)")
}
}
func testUnsafeBitcastUpdate() {
let value: Double = 3.14159
let val: Int64 = 31415926
let uval: UInt64 = 31415926
let i64 = Int64(bitPattern: value.bitPattern)
let ubc = unsafeBitCast(value, to: Int64.self)
XCTAssertEqual(i64, ubc, "Bitcast Double-> i64 Values don't match")
let dbl = Double(bitPattern: UInt64(val))
let ubdb = unsafeBitCast(val, to: Double.self)
XCTAssertEqual(dbl, ubdb, "Bitcast i64 -> Double Values don't match")
let db2 = Double(bitPattern: uval)
let usbc2 = unsafeBitCast(uval, to: Double.self)
XCTAssertEqual(db2, usbc2, "Bitcast u64 -> Double Values don't match")
}
static var allTests : [(String, (TBinaryProtocolTests) -> () throws -> Void)] {
return [

View File

@ -128,6 +128,70 @@ class TCompactProtocolTests: XCTestCase {
}
}
func testInt32ZigZag() {
let zero: Int32 = 0
let one: Int32 = 1
let nOne: Int32 = -1
let two: Int32 = 2
let nTwo: Int32 = -2
let max = Int32.max
let min = Int32.min
XCTAssertEqual(proto.i32ToZigZag(zero), UInt32(0), "Error 32bit zigzag on \(zero)")
XCTAssertEqual(proto.zigZagToi32(0), zero, "Error 32bit zigzag on \(zero)")
XCTAssertEqual(proto.i32ToZigZag(nOne), UInt32(1), "Error 32bit zigzag on \(nOne)")
XCTAssertEqual(proto.zigZagToi32(1), nOne, "Error 32bit zigzag on \(nOne)")
XCTAssertEqual(proto.i32ToZigZag(one), UInt32(2), "Error 32bit zigzag on \(one)")
XCTAssertEqual(proto.zigZagToi32(2), one, "Error 32bit zigzag on \(one)")
XCTAssertEqual(proto.i32ToZigZag(nTwo), UInt32(3), "Error 32bit zigzag on \(nTwo)")
XCTAssertEqual(proto.zigZagToi32(3), nTwo, "Error 32bit zigzag on \(nTwo)")
XCTAssertEqual(proto.i32ToZigZag(two), UInt32(4), "Error 32bit zigzag on \(two)")
XCTAssertEqual(proto.zigZagToi32(4), two, "Error 32bit zigzag on \(two)")
let uMaxMinusOne: UInt32 = UInt32.max - 1
XCTAssertEqual(proto.i32ToZigZag(max), uMaxMinusOne, "Error 32bit zigzag on \(max)")
XCTAssertEqual(proto.zigZagToi32(uMaxMinusOne), max, "Error 32bit zigzag on \(max)")
XCTAssertEqual(proto.i32ToZigZag(min), UInt32.max, "Error 32bit zigzag on \(min)")
XCTAssertEqual(proto.zigZagToi32(UInt32.max), min, "Error 32bit zigzag on \(min)")
}
func testInt64ZigZag() {
let zero: Int64 = 0
let one: Int64 = 1
let nOne: Int64 = -1
let two: Int64 = 2
let nTwo: Int64 = -2
let max = Int64.max
let min = Int64.min
XCTAssertEqual(proto.i64ToZigZag(zero), UInt64(0), "Error 64bit zigzag on \(zero)")
XCTAssertEqual(proto.zigZagToi64(0), zero, "Error 64bit zigzag on \(zero)")
XCTAssertEqual(proto.i64ToZigZag(nOne), UInt64(1), "Error 64bit zigzag on \(nOne)")
XCTAssertEqual(proto.zigZagToi64(1), nOne, "Error 64bit zigzag on \(nOne)")
XCTAssertEqual(proto.i64ToZigZag(one), UInt64(2), "Error 64bit zigzag on \(one)")
XCTAssertEqual(proto.zigZagToi64(2), one, "Error 64bit zigzag on \(one)")
XCTAssertEqual(proto.i64ToZigZag(nTwo), UInt64(3), "Error 64bit zigzag on \(nTwo)")
XCTAssertEqual(proto.zigZagToi64(3), nTwo, "Error 64bit zigzag on \(nTwo)")
XCTAssertEqual(proto.i64ToZigZag(two), UInt64(4), "Error 64bit zigzag on \(two)")
XCTAssertEqual(proto.zigZagToi64(4), two, "Error 64bit zigzag on \(two)")
let uMaxMinusOne: UInt64 = UInt64.max - 1
XCTAssertEqual(proto.i64ToZigZag(max), uMaxMinusOne, "Error 64bit zigzag on \(max)")
XCTAssertEqual(proto.zigZagToi64(uMaxMinusOne), max, "Error 64bit zigzag on \(max)")
XCTAssertEqual(proto.i64ToZigZag(min), UInt64.max, "Error 64bit zigzag on \(min)")
XCTAssertEqual(proto.zigZagToi64(UInt64.max), min, "Error 64bit zigzag on \(min)")
}
static var allTests : [(String, (TCompactProtocolTests) -> () throws -> Void)] {
return [
("testInt8WriteRead", testInt8WriteRead),
@ -138,7 +202,9 @@ class TCompactProtocolTests: XCTestCase {
("testBoolWriteRead", testBoolWriteRead),
("testStringWriteRead", testStringWriteRead),
("testDataWriteRead", testDataWriteRead),
("testStructWriteRead", testStructWriteRead)
("testStructWriteRead", testStructWriteRead),
("testInt32ZigZag", testInt32ZigZag),
("testInt64ZigZag", testInt64ZigZag)
]
}
}

View File

@ -3,7 +3,7 @@ import XCTest
class ThriftTests: XCTestCase {
func testVersion() {
XCTAssertEqual(Thrift().version, "0.0.1")
XCTAssertEqual(Thrift().version, "1.1.0")
}
func test_in_addr_extension() {