]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // |
2 | // TCompactProtocolTests.swift | |
3 | // Thrift | |
4 | // | |
5 | // Created by Christopher Simpson on 8/19/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 TCompactProtocolTests: XCTestCase { | |
17 | var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: { | |
18 | $0.reset(readBuffer: $1) | |
19 | }) | |
20 | var proto: TCompactProtocol! | |
21 | ||
22 | override func setUp() { | |
23 | super.setUp() | |
24 | proto = TCompactProtocol(on: transport) | |
25 | transport.reset() | |
26 | } | |
27 | ||
28 | override func tearDown() { | |
29 | super.tearDown() | |
30 | transport.reset() | |
31 | } | |
32 | ||
33 | func testInt8WriteRead() { | |
34 | let writeVal: UInt8 = 250 | |
35 | try? proto.write(writeVal) | |
36 | try? transport.flush() | |
37 | let readVal: UInt8 = (try? proto.read()) ?? 0 | |
38 | XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)") | |
39 | } | |
40 | ||
41 | func testInt16WriteRead() { | |
42 | let writeVal: Int16 = 12312 | |
43 | try? proto.write(writeVal) | |
44 | try? transport.flush() | |
45 | ||
46 | let readVal: Int16 = (try? proto.read()) ?? 0 | |
47 | XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)") | |
48 | } | |
49 | ||
50 | func testInt32WriteRead() { | |
51 | let writeVal: Int32 = 2029234 | |
52 | try? proto.write(writeVal) | |
53 | try? transport.flush() | |
54 | ||
55 | let readVal: Int32 = (try? proto.read()) ?? 0 | |
56 | XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)") | |
57 | } | |
58 | ||
59 | func testInt64WriteRead() { | |
60 | let writeVal: Int64 = 234234981374134 | |
61 | try? proto.write(writeVal) | |
62 | try? transport.flush() | |
63 | ||
64 | let readVal: Int64 = (try? proto.read()) ?? 0 | |
65 | XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)") | |
66 | } | |
67 | ||
68 | func testDoubleWriteRead() { | |
69 | let writeVal: Double = 3.1415926 | |
70 | try? proto.write(writeVal) | |
71 | try? transport.flush() | |
72 | ||
73 | let readVal: Double = (try? proto.read()) ?? 0.0 | |
74 | XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)") | |
75 | } | |
76 | ||
77 | func testBoolWriteRead() { | |
78 | let writeVal: Bool = true | |
79 | try? proto.write(writeVal) | |
80 | try? transport.flush() | |
81 | ||
82 | let readVal: Bool = (try? proto.read()) ?? false | |
83 | XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)") | |
84 | } | |
85 | ||
86 | func testStringWriteRead() { | |
87 | let writeVal: String = "Hello World" | |
88 | try? proto.write(writeVal) | |
89 | try? transport.flush() | |
90 | ||
91 | let readVal: String! | |
92 | do { | |
93 | readVal = try proto.read() | |
94 | } catch let error { | |
95 | XCTAssertFalse(true, "Error reading \(error)") | |
96 | return | |
97 | } | |
98 | ||
99 | XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)") | |
100 | } | |
101 | ||
102 | func testDataWriteRead() { | |
103 | let writeVal: Data = "Data World".data(using: .utf8)! | |
104 | try? proto.write(writeVal) | |
105 | try? transport.flush() | |
106 | ||
107 | let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)! | |
108 | XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)") | |
109 | } | |
110 | ||
111 | func testStructWriteRead() { | |
112 | let msg = "Test Protocol Error" | |
113 | let writeVal = TApplicationError(error: .protocolError, message: msg) | |
114 | do { | |
115 | try writeVal.write(to: proto) | |
116 | try transport.flush() | |
117 | ||
118 | } catch let error { | |
119 | XCTAssertFalse(true, "Caught Error attempting to write \(error)") | |
120 | } | |
121 | ||
122 | do { | |
123 | let readVal = try TApplicationError.read(from: proto) | |
124 | XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)") | |
125 | XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)") | |
126 | } catch let error { | |
127 | XCTAssertFalse(true, "Caught Error attempting to read \(error)") | |
128 | } | |
129 | } | |
130 | ||
131 | func testInt32ZigZag() { | |
132 | let zero: Int32 = 0 | |
133 | let one: Int32 = 1 | |
134 | let nOne: Int32 = -1 | |
135 | let two: Int32 = 2 | |
136 | let nTwo: Int32 = -2 | |
137 | let max = Int32.max | |
138 | let min = Int32.min | |
139 | ||
140 | XCTAssertEqual(proto.i32ToZigZag(zero), UInt32(0), "Error 32bit zigzag on \(zero)") | |
141 | XCTAssertEqual(proto.zigZagToi32(0), zero, "Error 32bit zigzag on \(zero)") | |
142 | ||
143 | XCTAssertEqual(proto.i32ToZigZag(nOne), UInt32(1), "Error 32bit zigzag on \(nOne)") | |
144 | XCTAssertEqual(proto.zigZagToi32(1), nOne, "Error 32bit zigzag on \(nOne)") | |
145 | ||
146 | XCTAssertEqual(proto.i32ToZigZag(one), UInt32(2), "Error 32bit zigzag on \(one)") | |
147 | XCTAssertEqual(proto.zigZagToi32(2), one, "Error 32bit zigzag on \(one)") | |
148 | ||
149 | XCTAssertEqual(proto.i32ToZigZag(nTwo), UInt32(3), "Error 32bit zigzag on \(nTwo)") | |
150 | XCTAssertEqual(proto.zigZagToi32(3), nTwo, "Error 32bit zigzag on \(nTwo)") | |
151 | ||
152 | XCTAssertEqual(proto.i32ToZigZag(two), UInt32(4), "Error 32bit zigzag on \(two)") | |
153 | XCTAssertEqual(proto.zigZagToi32(4), two, "Error 32bit zigzag on \(two)") | |
154 | ||
155 | let uMaxMinusOne: UInt32 = UInt32.max - 1 | |
156 | XCTAssertEqual(proto.i32ToZigZag(max), uMaxMinusOne, "Error 32bit zigzag on \(max)") | |
157 | XCTAssertEqual(proto.zigZagToi32(uMaxMinusOne), max, "Error 32bit zigzag on \(max)") | |
158 | ||
159 | XCTAssertEqual(proto.i32ToZigZag(min), UInt32.max, "Error 32bit zigzag on \(min)") | |
160 | XCTAssertEqual(proto.zigZagToi32(UInt32.max), min, "Error 32bit zigzag on \(min)") | |
161 | } | |
162 | ||
163 | func testInt64ZigZag() { | |
164 | let zero: Int64 = 0 | |
165 | let one: Int64 = 1 | |
166 | let nOne: Int64 = -1 | |
167 | let two: Int64 = 2 | |
168 | let nTwo: Int64 = -2 | |
169 | let max = Int64.max | |
170 | let min = Int64.min | |
171 | ||
172 | XCTAssertEqual(proto.i64ToZigZag(zero), UInt64(0), "Error 64bit zigzag on \(zero)") | |
173 | XCTAssertEqual(proto.zigZagToi64(0), zero, "Error 64bit zigzag on \(zero)") | |
174 | ||
175 | XCTAssertEqual(proto.i64ToZigZag(nOne), UInt64(1), "Error 64bit zigzag on \(nOne)") | |
176 | XCTAssertEqual(proto.zigZagToi64(1), nOne, "Error 64bit zigzag on \(nOne)") | |
177 | ||
178 | XCTAssertEqual(proto.i64ToZigZag(one), UInt64(2), "Error 64bit zigzag on \(one)") | |
179 | XCTAssertEqual(proto.zigZagToi64(2), one, "Error 64bit zigzag on \(one)") | |
180 | ||
181 | XCTAssertEqual(proto.i64ToZigZag(nTwo), UInt64(3), "Error 64bit zigzag on \(nTwo)") | |
182 | XCTAssertEqual(proto.zigZagToi64(3), nTwo, "Error 64bit zigzag on \(nTwo)") | |
183 | ||
184 | XCTAssertEqual(proto.i64ToZigZag(two), UInt64(4), "Error 64bit zigzag on \(two)") | |
185 | XCTAssertEqual(proto.zigZagToi64(4), two, "Error 64bit zigzag on \(two)") | |
186 | ||
187 | let uMaxMinusOne: UInt64 = UInt64.max - 1 | |
188 | XCTAssertEqual(proto.i64ToZigZag(max), uMaxMinusOne, "Error 64bit zigzag on \(max)") | |
189 | XCTAssertEqual(proto.zigZagToi64(uMaxMinusOne), max, "Error 64bit zigzag on \(max)") | |
190 | ||
191 | XCTAssertEqual(proto.i64ToZigZag(min), UInt64.max, "Error 64bit zigzag on \(min)") | |
192 | XCTAssertEqual(proto.zigZagToi64(UInt64.max), min, "Error 64bit zigzag on \(min)") | |
193 | } | |
194 | ||
195 | static var allTests : [(String, (TCompactProtocolTests) -> () throws -> Void)] { | |
196 | return [ | |
197 | ("testInt8WriteRead", testInt8WriteRead), | |
198 | ("testInt16WriteRead", testInt16WriteRead), | |
199 | ("testInt32WriteRead", testInt32WriteRead), | |
200 | ("testInt64WriteRead", testInt64WriteRead), | |
201 | ("testDoubleWriteRead", testDoubleWriteRead), | |
202 | ("testBoolWriteRead", testBoolWriteRead), | |
203 | ("testStringWriteRead", testStringWriteRead), | |
204 | ("testDataWriteRead", testDataWriteRead), | |
205 | ("testStructWriteRead", testStructWriteRead), | |
206 | ("testInt32ZigZag", testInt32ZigZag), | |
207 | ("testInt64ZigZag", testInt64ZigZag) | |
208 | ] | |
209 | } | |
210 | } |