Skip to content

Commit

Permalink
Merge pull request #96 from benrimmington/complex-unkeyed
Browse files Browse the repository at this point in the history
[Complex] Encode and decode using unkeyedContainer
  • Loading branch information
stephentyrone authored Jan 22, 2020
2 parents 7bd2909 + eda4321 commit a2aa24d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
22 changes: 17 additions & 5 deletions Sources/Complex/Complex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 28 additions & 1 deletion Tests/ComplexTests/PropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
}
}

0 comments on commit a2aa24d

Please sign in to comment.