//===--- PropertyTests.swift ----------------------------------*- swift -*-===// // // This source file is part of the Swift Numerics open source project // // 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 // //===----------------------------------------------------------------------===// import XCTest import ComplexModule import RealModule final class PropertyTests: XCTestCase { func testProperties(_ type: T.Type) { // The real and imaginary parts of a non-finite value should be nan. XCTAssertTrue(Complex.infinity.real.isNaN) XCTAssertTrue(Complex.infinity.imaginary.isNaN) XCTAssertTrue(Complex(.infinity, .nan).real.isNaN) XCTAssertTrue(Complex(.nan, 0).imaginary.isNaN) // The length of a non-finite value should be infinity. XCTAssertEqual(Complex.infinity.length, .infinity) XCTAssertEqual(Complex(.infinity, .nan).length, .infinity) XCTAssertEqual(Complex(.nan, 0).length, .infinity) // The phase of a non-finite value should be nan. XCTAssertTrue(Complex.infinity.phase.isNaN) XCTAssertTrue(Complex(.infinity, .nan).phase.isNaN) XCTAssertTrue(Complex(.nan, 0).phase.isNaN) // The length of a zero value should be zero. XCTAssertEqual(Complex.zero.length, .zero) XCTAssertEqual(Complex(.zero, -.zero).length, .zero) XCTAssertEqual(Complex(-.zero,-.zero).length, .zero) // The phase of a zero value should be nan. XCTAssertTrue(Complex.zero.phase.isNaN) XCTAssertTrue(Complex(.zero, -.zero).phase.isNaN) XCTAssertTrue(Complex(-.zero,-.zero).phase.isNaN) } func testProperties() { testProperties(Float.self) testProperties(Double.self) #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) testProperties(Float80.self) #endif } func testEquatableHashable(_ type: T.Type) { // Validate that all zeros compare and hash equal, and all non-finites // do too. let zeros = [ Complex( .zero, .zero), Complex(-.zero, .zero), Complex(-.zero,-.zero), Complex( .zero,-.zero) ] for z in zeros[1...] { XCTAssertEqual(zeros[0], z) XCTAssertEqual(zeros[0].hashValue, z.hashValue) } let infs = [ Complex( .nan, .nan), Complex(-.infinity, .nan), Complex(-.ulpOfOne, .nan), Complex( .zero, .nan), Complex( .pi, .nan), Complex( .infinity, .nan), Complex( .nan, -.infinity), Complex(-.infinity,-.infinity), Complex(-.ulpOfOne,-.infinity), Complex( .zero, -.infinity), Complex( .pi, -.infinity), Complex( .infinity,-.infinity), Complex( .nan, -.ulpOfOne), Complex(-.infinity,-.ulpOfOne), Complex( .infinity,-.ulpOfOne), Complex( .nan, .zero), Complex(-.infinity, .zero), Complex( .infinity, .zero), Complex( .nan, .pi), Complex(-.infinity, .pi), Complex( .infinity, .pi), Complex( .nan, .infinity), Complex(-.infinity, .infinity), Complex(-.ulpOfOne, .infinity), Complex( .zero, .infinity), Complex( .pi, .infinity), Complex( .infinity, .infinity), ] for i in infs[1...] { XCTAssertEqual(infs[0], i) XCTAssertEqual(infs[0].hashValue, i.hashValue) } } func testEquatableHashable() { testEquatableHashable(Float.self) testEquatableHashable(Double.self) #if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android) testEquatableHashable(Float80.self) #endif } func testCodable(_ 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 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.self, from: data) XCTAssertEqual(actual, expected) } } func testCodable() throws { try testCodable(Float32.self) try testCodable(Float64.self) // Float80 doesn't conform to Codable. } }