Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 1 | // |
| 2 | // TBinaryProtocolTests.swift |
| 3 | // Thrift |
| 4 | // |
| 5 | // Created by Christopher Simpson on 8/18/16. |
| 6 | // |
| 7 | // |
| 8 | |
| 9 | import XCTest |
| 10 | import Foundation |
| 11 | @testable import Thrift |
| 12 | |
| 13 | |
| 14 | /// Testing Binary protocol read/write against itself |
| 15 | /// Uses separate read/write transport/protocols |
| 16 | class TBinaryProtocolTests: XCTestCase { |
| 17 | var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: { |
| 18 | $0.reset(readBuffer: $1) |
| 19 | }) |
| 20 | |
| 21 | var proto: TBinaryProtocol! |
| 22 | |
| 23 | override func setUp() { |
| 24 | super.setUp() |
| 25 | proto = TBinaryProtocol(on: transport) |
| 26 | transport.reset() |
| 27 | } |
| 28 | |
| 29 | override func tearDown() { |
| 30 | super.tearDown() |
| 31 | transport.reset() |
| 32 | } |
| 33 | |
| 34 | func testInt8WriteRead() { |
| 35 | let writeVal: UInt8 = 250 |
| 36 | try? proto.write(writeVal) |
| 37 | try? transport.flush() |
| 38 | |
| 39 | let readVal: UInt8 = (try? proto.read()) ?? 0 |
| 40 | XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)") |
| 41 | } |
| 42 | |
| 43 | func testInt16WriteRead() { |
| 44 | |
| 45 | let writeVal: Int16 = 12312 |
| 46 | try? proto.write(writeVal) |
| 47 | try? transport.flush() |
| 48 | let readVal: Int16 = (try? proto.read()) ?? 0 |
| 49 | XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)") |
| 50 | } |
| 51 | |
| 52 | func testInt32WriteRead() { |
| 53 | let writeVal: Int32 = 2029234 |
| 54 | try? proto.write(writeVal) |
| 55 | try? transport.flush() |
| 56 | |
| 57 | let readVal: Int32 = (try? proto.read()) ?? 0 |
| 58 | XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)") |
| 59 | } |
| 60 | |
| 61 | func testInt64WriteRead() { |
| 62 | let writeVal: Int64 = 234234981374134 |
| 63 | try? proto.write(writeVal) |
| 64 | try? transport.flush() |
| 65 | |
| 66 | let readVal: Int64 = (try? proto.read()) ?? 0 |
| 67 | XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)") |
| 68 | } |
| 69 | |
| 70 | func testDoubleWriteRead() { |
| 71 | let writeVal: Double = 3.1415926 |
| 72 | try? proto.write(writeVal) |
| 73 | try? transport.flush() |
| 74 | |
| 75 | let readVal: Double = (try? proto.read()) ?? 0.0 |
| 76 | XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)") |
| 77 | } |
| 78 | |
| 79 | func testBoolWriteRead() { |
| 80 | let writeVal: Bool = true |
| 81 | try? proto.write(writeVal) |
| 82 | try? transport.flush() |
| 83 | |
| 84 | let readVal: Bool = (try? proto.read()) ?? false |
| 85 | XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)") |
| 86 | } |
| 87 | |
| 88 | func testStringWriteRead() { |
| 89 | let writeVal: String = "Hello World" |
| 90 | try? proto.write(writeVal) |
| 91 | try? transport.flush() |
| 92 | |
| 93 | let readVal: String! |
| 94 | do { |
| 95 | readVal = try proto.read() |
| 96 | } catch let error { |
| 97 | XCTAssertFalse(true, "Error reading \(error)") |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)") |
| 102 | } |
| 103 | |
| 104 | func testDataWriteRead() { |
| 105 | let writeVal: Data = "Data World".data(using: .utf8)! |
| 106 | try? proto.write(writeVal) |
| 107 | try? transport.flush() |
| 108 | |
| 109 | let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)! |
| 110 | XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)") |
| 111 | } |
| 112 | |
| 113 | func testStructWriteRead() { |
| 114 | let msg = "Test Protocol Error" |
| 115 | let writeVal = TApplicationError(error: .protocolError, message: msg) |
| 116 | do { |
| 117 | try writeVal.write(to: proto) |
| 118 | try? transport.flush() |
| 119 | |
| 120 | } catch let error { |
| 121 | XCTAssertFalse(true, "Caught Error attempting to write \(error)") |
| 122 | } |
| 123 | |
| 124 | do { |
| 125 | let readVal = try TApplicationError.read(from: proto) |
| 126 | XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)") |
| 127 | XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)") |
| 128 | } catch let error { |
| 129 | XCTAssertFalse(true, "Caught Error attempting to read \(error)") |
| 130 | } |
| 131 | } |
Chris Simpson | 2566ecd | 2018-08-29 14:40:44 -0400 | [diff] [blame] | 132 | func testUnsafeBitcastUpdate() { |
| 133 | let value: Double = 3.14159 |
| 134 | let val: Int64 = 31415926 |
| 135 | let uval: UInt64 = 31415926 |
| 136 | |
| 137 | let i64 = Int64(bitPattern: value.bitPattern) |
| 138 | let ubc = unsafeBitCast(value, to: Int64.self) |
| 139 | |
| 140 | XCTAssertEqual(i64, ubc, "Bitcast Double-> i64 Values don't match") |
| 141 | |
| 142 | let dbl = Double(bitPattern: UInt64(val)) |
| 143 | let ubdb = unsafeBitCast(val, to: Double.self) |
| 144 | |
| 145 | XCTAssertEqual(dbl, ubdb, "Bitcast i64 -> Double Values don't match") |
| 146 | |
| 147 | let db2 = Double(bitPattern: uval) |
| 148 | let usbc2 = unsafeBitCast(uval, to: Double.self) |
| 149 | |
| 150 | XCTAssertEqual(db2, usbc2, "Bitcast u64 -> Double Values don't match") |
| 151 | |
| 152 | |
| 153 | } |
Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame] | 154 | |
| 155 | static var allTests : [(String, (TBinaryProtocolTests) -> () throws -> Void)] { |
| 156 | return [ |
| 157 | ("testInt8WriteRead", testInt8WriteRead), |
| 158 | ("testInt16WriteRead", testInt16WriteRead), |
| 159 | ("testInt32WriteRead", testInt32WriteRead), |
| 160 | ("testInt64WriteRead", testInt64WriteRead), |
| 161 | ("testDoubleWriteRead", testDoubleWriteRead), |
| 162 | ("testBoolWriteRead", testBoolWriteRead), |
| 163 | ("testStringWriteRead", testStringWriteRead), |
| 164 | ("testDataWriteRead", testDataWriteRead), |
| 165 | ("testStructWriteRead", testStructWriteRead) |
| 166 | ] |
| 167 | } |
| 168 | } |