File: PropertyTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (132 lines) | stat: -rw-r--r-- 4,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//===--- 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<T: Real>(_ type: T.Type) {
    // The real and imaginary parts of a non-finite value should be nan.
    XCTAssertTrue(Complex<T>.infinity.real.isNaN)
    XCTAssertTrue(Complex<T>.infinity.imaginary.isNaN)
    XCTAssertTrue(Complex<T>(.infinity, .nan).real.isNaN)
    XCTAssertTrue(Complex<T>(.nan, 0).imaginary.isNaN)
    // The length of a non-finite value should be infinity.
    XCTAssertEqual(Complex<T>.infinity.length, .infinity)
    XCTAssertEqual(Complex<T>(.infinity, .nan).length, .infinity)
    XCTAssertEqual(Complex<T>(.nan, 0).length, .infinity)
    // The phase of a non-finite value should be nan.
    XCTAssertTrue(Complex<T>.infinity.phase.isNaN)
    XCTAssertTrue(Complex<T>(.infinity, .nan).phase.isNaN)
    XCTAssertTrue(Complex<T>(.nan, 0).phase.isNaN)
    // The length of a zero value should be zero.
    XCTAssertEqual(Complex<T>.zero.length, .zero)
    XCTAssertEqual(Complex<T>(.zero, -.zero).length, .zero)
    XCTAssertEqual(Complex<T>(-.zero,-.zero).length, .zero)
    // The phase of a zero value should be nan.
    XCTAssertTrue(Complex<T>.zero.phase.isNaN)
    XCTAssertTrue(Complex<T>(.zero, -.zero).phase.isNaN)
    XCTAssertTrue(Complex<T>(-.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<T: Real>(_ type: T.Type) {
    // Validate that all zeros compare and hash equal, and all non-finites
    // do too.
    let zeros = [
      Complex<T>( .zero, .zero),
      Complex<T>(-.zero, .zero),
      Complex<T>(-.zero,-.zero),
      Complex<T>( .zero,-.zero)
    ]
    for z in zeros[1...] {
      XCTAssertEqual(zeros[0], z)
      XCTAssertEqual(zeros[0].hashValue, z.hashValue)
    }
    let infs = [
      Complex<T>( .nan,      .nan),
      Complex<T>(-.infinity, .nan),
      Complex<T>(-.ulpOfOne, .nan),
      Complex<T>( .zero,     .nan),
      Complex<T>( .pi,       .nan),
      Complex<T>( .infinity, .nan),
      Complex<T>( .nan,     -.infinity),
      Complex<T>(-.infinity,-.infinity),
      Complex<T>(-.ulpOfOne,-.infinity),
      Complex<T>( .zero,    -.infinity),
      Complex<T>( .pi,      -.infinity),
      Complex<T>( .infinity,-.infinity),
      Complex<T>( .nan,     -.ulpOfOne),
      Complex<T>(-.infinity,-.ulpOfOne),
      Complex<T>( .infinity,-.ulpOfOne),
      Complex<T>( .nan,      .zero),
      Complex<T>(-.infinity, .zero),
      Complex<T>( .infinity, .zero),
      Complex<T>( .nan,      .pi),
      Complex<T>(-.infinity, .pi),
      Complex<T>( .infinity, .pi),
      Complex<T>( .nan,      .infinity),
      Complex<T>(-.infinity, .infinity),
      Complex<T>(-.ulpOfOne, .infinity),
      Complex<T>( .zero,     .infinity),
      Complex<T>( .pi,       .infinity),
      Complex<T>( .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<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.
  }
}