mirror of
https://github.com/apple/swift-numerics
synced 2026-11-25 04:47:53 +00:00
[Complex] Encode and decode using unkeyedContainer
This commit is contained in:
1 parent
7bd2909875
commit
eda432142d
2 files changed
+45
-6
No files matched your search
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -344,11 +344,23 @@ extension Complex: Hashable {
|
||||
}
|
||||
|
||||
// MARK: - Conformance to Codable
|
||||
// The synthesized conformance works correction for this protocol, unlike
|
||||
// Hashable and Equatable; all we need to do is specify that we conform.
|
||||
// FloatingPoint does not refine Codable, so this is a conditional conformance.
|
||||
extension Complex: Encodable where RealType: Encodable { }
|
||||
extension Complex: Decodable where RealType: Decodable { }
|
||||
extension Complex: Decodable where RealType: Decodable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
var unkeyedContainer = try decoder.unkeyedContainer()
|
||||
let x = try unkeyedContainer.decode(RealType.self)
|
||||
let y = try unkeyedContainer.decode(RealType.self)
|
||||
self.init(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
extension Complex: Encodable where RealType: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var unkeyedContainer = encoder.unkeyedContainer()
|
||||
try unkeyedContainer.encode(x)
|
||||
try unkeyedContainer.encode(y)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Formatting
|
||||
extension Complex: CustomStringConvertible {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -102,4 +102,31 @@ final class PropertyTests: XCTestCase {
|
||||
testEquatableHashable(Float80.self)
|
||||
#endif
|
||||
}
|
||||
|
||||
func testCodable<T: Codable & Real>(_ type: T.Type) throws {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.nonConformingFloatEncodingStrategy = .convertToString(
|
||||
positiveInfinity: "inf",
|
||||
negativeInfinity: "-inf",
|
||||
nan: "nan")
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
decoder.nonConformingFloatDecodingStrategy = .convertFromString(
|
||||
positiveInfinity: "inf",
|
||||
negativeInfinity: "-inf",
|
||||
nan: "nan")
|
||||
|
||||
for expected: Complex<T> in [.zero, .one, .i, .infinity] {
|
||||
let data = try encoder.encode(expected)
|
||||
// print("*** \(String(decoding: data, as: Unicode.UTF8.self)) ***")
|
||||
let actual = try decoder.decode(Complex<T>.self, from: data)
|
||||
XCTAssertEqual(actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func testCodable() throws {
|
||||
try testCodable(Float32.self)
|
||||
try testCodable(Float64.self)
|
||||
// Float80 doesn't conform to Codable.
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user