-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests. Fix end of data check on array decode.
- Loading branch information
Showing
10 changed files
with
324 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Tests/CDRCodableTests/CDRCodableDecodingArrayKeyedTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
///// | ||
//// CDRCodableDecodingArrayKeyedTests.swift | ||
/// Copyright © 2024 Dmitriy Borovikov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import CDRCodable | ||
|
||
class CDRCodableDecodingArrayKeyedTests: XCTestCase { | ||
var decoder: CDRDecoder! | ||
|
||
override func setUp() { | ||
self.decoder = CDRDecoder() | ||
} | ||
|
||
func testDecodeData() { | ||
struct TestStruct: Codable, Equatable { | ||
let b: UInt8 | ||
let s: Data | ||
} | ||
let value = TestStruct(b: 0x55, s: "hello".data(using: .utf8)!) | ||
let data = Data([0x55, 0, 0, 0, 5, 0, 0, 0, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0, 0, 0]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
func testDecodeArray8() { | ||
struct TestStruct: Codable, Equatable { | ||
let b: UInt8 | ||
let a: [Int8] | ||
} | ||
let value = TestStruct(b: 0x55, a: [-1, 2, 3]) | ||
let data = Data([0x55, 0, 0, 0, 3, 0, 0, 0, 0xff, 2, 3, 0]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
func testDecodeArray16() { | ||
struct TestStruct: Codable, Equatable { | ||
let b: UInt8 | ||
let a: [Int16] | ||
} | ||
let value = TestStruct(b: 0x55, a: [-1, 2, 3]) | ||
let data = Data([0x55, 0, 0, 0, 3, 0, 0, 0, 0xff, 0xff, 2, 0, 3, 0, 0, 0]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
func testDecodeArray32() { | ||
struct TestStruct: Codable, Equatable { | ||
let b: UInt8 | ||
let a: [Int32] | ||
} | ||
let value = TestStruct(b: 0x55, a: [-1, 2, 3]) | ||
let data = Data([0x55, 0, 0, 0, 3, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 2, 0, 0, 0, 3, 0, 0, 0]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
func testDecodeArray64() { | ||
struct TestStruct: Codable, Equatable { | ||
let a: [Int64] | ||
} | ||
let value = TestStruct(a: [-1, 2, 3]) | ||
let data = Data([3, 0, 0, 0, | ||
0, 0, 0, 0, | ||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | ||
2, 0, 0, 0, 0, 0, 0, 0, | ||
3, 0, 0, 0, 0, 0, 0, 0]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
func testDecodeArrayFloat() { | ||
struct TestStruct: Codable, Equatable { | ||
let b: UInt8 | ||
let a: [Float] | ||
} | ||
let value = TestStruct(b: 0x55, a: [1, 2, 3]) | ||
let data = Data([0x55, 0, 0, 0, | ||
3, 0, 0, 0, | ||
0, 0, 0x80, 0x3f, | ||
0, 0, 0, 0x40, | ||
0, 0, 0x40, 0x40]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
func testDecodeArrayDouble() { | ||
struct TestStruct: Codable, Equatable { | ||
let a: [Double] | ||
} | ||
let value = TestStruct(a: [1, 2, 3]) | ||
let data = Data([3, 0, 0, 0, | ||
0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0xf0, 0x3f, | ||
0, 0, 0, 0, 0, 0, 0, 0x40, | ||
0, 0, 0, 0, 0, 0, 8, 0x40]) | ||
let decoded = try! decoder.decode(TestStruct.self, from: data) | ||
XCTAssertEqual(value, decoded) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
Tests/CDRCodableTests/CDRCodableDecodingErrorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
///// | ||
//// CDRCodableDecodingErrorTests.swift | ||
/// Copyright © 2024 Dmitriy Borovikov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import CDRCodable | ||
|
||
class CDRCodableDecodingErrorTests: XCTestCase { | ||
var decoder: CDRDecoder! | ||
|
||
override func setUp() { | ||
self.decoder = CDRDecoder() | ||
} | ||
|
||
func testDecodeInt32EOD() { | ||
let data = Data([0, 0, 0]) | ||
XCTAssertThrowsError(try decoder.decode(Int32.self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.dataCorrupted = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testDecodeStringCountEOD() { | ||
let data = Data([0, 0, 0]) | ||
XCTAssertThrowsError(try decoder.decode(String.self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.dataCorrupted = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testDecodeStringEOD() { | ||
let data = Data([2, 0, 0, 0, 0x55]) | ||
XCTAssertThrowsError(try decoder.decode(String.self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.dataCorrupted = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testDecodeArrayEOD() { | ||
let data = Data([1, 0, 0, 0, | ||
0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0]) | ||
XCTAssertThrowsError(try decoder.decode([Int64].self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.dataCorrupted = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testDecodeArrayStructEOD() { | ||
let data = Data([1, 0, 0, 0, | ||
0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0]) | ||
struct TestStruct: Decodable { | ||
let a: [Int64] | ||
} | ||
XCTAssertThrowsError(try decoder.decode(TestStruct.self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.dataCorrupted = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testDecodeFixedArrayEOD() { | ||
let data = Data([0, 0, 0, 0, | ||
0, 0, 0]) | ||
struct TestStruct: Decodable { | ||
let b: UInt8 | ||
let a: [Int32] | ||
enum CodingKeys: Int, CodingKey { | ||
case b = 0 | ||
case a = 0x10001 | ||
} | ||
} | ||
XCTAssertThrowsError(try decoder.decode(TestStruct.self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.dataCorrupted = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testDecodeFixedArrayWrongDef() { | ||
let data = Data([0, 0, 0, 0, | ||
0, 0, 0, 0]) | ||
struct TestStruct: Decodable { | ||
let a: [Data] | ||
enum CodingKeys: Int, CodingKey { | ||
case a = 0x10001 | ||
} | ||
} | ||
XCTAssertThrowsError(try decoder.decode(TestStruct.self, from: data), "Unexpected end of data") { | ||
guard case DecodingError.typeMismatch = $0 else { | ||
XCTFail("Wrong error type") | ||
return | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.