|
| 1 | +//===--- Math.swift.gyb ---------------------------------------*- swift -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// -*- swift -*- |
| 13 | +// RUN: %empty-directory(%t) |
| 14 | +// RUN: %gyb %s -o %t/tgmath.swift |
| 15 | +// RUN: %line-directive %t/tgmath.swift -- %target-build-swift %t/tgmath.swift -o %t/a.out |
| 16 | +// RUN: %target-codesign %t/a.out |
| 17 | +// RUN: %line-directive %t/tgmath.swift -- %target-run %t/a.out |
| 18 | +// REQUIRES: executable_test |
| 19 | + |
| 20 | +#if (arch(i386) || arch(x86_64)) && !os(Windows) |
| 21 | + typealias TestLiteralType = Float80 |
| 22 | +#else |
| 23 | + typealias TestLiteralType = Double |
| 24 | +#endif |
| 25 | + |
| 26 | +import StdlibUnittest |
| 27 | + |
| 28 | +let MathTests = TestSuite("Math") |
| 29 | + |
| 30 | +func expectEqualWithTolerance<T>(_ expected: TestLiteralType, _ actual: T, |
| 31 | + ulps allowed: T = 3, |
| 32 | + file: String = #file, line: UInt = #line) |
| 33 | + where T: BinaryFloatingPoint { |
| 34 | + if actual == T(expected) || actual.isNaN && expected.isNaN { |
| 35 | + return |
| 36 | + } |
| 37 | + // Compute error in ulp, compare to tolerance. |
| 38 | + let absoluteError = T(abs(TestLiteralType(actual) - expected)) |
| 39 | + let ulpError = absoluteError / T(expected).ulp |
| 40 | + expectTrue(ulpError <= allowed, |
| 41 | + "\(actual) != \(expected) as \(T.self)" + |
| 42 | + "\n \(ulpError)-ulp error exceeds \(allowed)-ulp tolerance.", |
| 43 | + file: file, line: line) |
| 44 | +} |
| 45 | + |
| 46 | +%from SwiftMathFunctions import * |
| 47 | + |
| 48 | +internal extension ElementaryFunctions where Self: BinaryFloatingPoint { |
| 49 | + static func elementaryFunctionTests() { |
| 50 | + /* Default tolerance is 3 ulps unless specified otherwise. It's OK to relax |
| 51 | + * this as needed for new platforms, as these tests are *not* intended to |
| 52 | + * validate the math library--they are only intended to check that the |
| 53 | + * Swift bindings are calling the right functions in the math library. */ |
| 54 | + expectEqualWithTolerance(1.1863995522992575361931268186727044683, Self.acos(0.375)) |
| 55 | + expectEqualWithTolerance(0.3843967744956390830381948729670469737, Self.asin(0.375)) |
| 56 | + expectEqualWithTolerance(0.3587706702705722203959200639264604997, Self.atan(0.375)) |
| 57 | + expectEqualWithTolerance(0.9305076219123142911494767922295555080, Self.cos(0.375)) |
| 58 | + expectEqualWithTolerance(0.3662725290860475613729093517162641571, Self.sin(0.375)) |
| 59 | + expectEqualWithTolerance(0.3936265759256327582294137871012180981, Self.tan(0.375)) |
| 60 | + expectEqualWithTolerance(0.4949329230945269058895630995767185785, Self.acosh(1.125)) |
| 61 | + expectEqualWithTolerance(0.9670596312833237113713762009167286709, Self.asinh(1.125)) |
| 62 | + expectEqualWithTolerance(0.7331685343967135223291211023213964500, Self.atanh(0.625)) |
| 63 | + expectEqualWithTolerance(1.0711403467045867672994980155670160493, Self.cosh(0.375)) |
| 64 | + expectEqualWithTolerance(0.3838510679136145687542956764205024589, Self.sinh(0.375)) |
| 65 | + expectEqualWithTolerance(0.3583573983507859463193602315531580424, Self.tanh(0.375)) |
| 66 | + expectEqualWithTolerance(1.4549914146182013360537936919875185083, Self.exp(0.375)) |
| 67 | + expectEqualWithTolerance(1.2968395546510096659337541177924511598, Self.exp2(0.375)) |
| 68 | + expectEqualWithTolerance(2.3713737056616552616517527574788898386, Self.exp10(0.375)) |
| 69 | + expectEqualWithTolerance(0.4549914146182013360537936919875185083, Self.expm1(0.375)) |
| 70 | + expectEqualWithTolerance(-0.980829253011726236856451127452003999, Self.log(0.375)) |
| 71 | + expectEqualWithTolerance(-1.415037499278843818546261056052183491, Self.log2(0.375)) |
| 72 | + expectEqualWithTolerance(0.3184537311185346158102472135905995955, Self.log1p(0.375)) |
| 73 | + expectEqualWithTolerance(-0.425968732272281148346188780918363771, Self.log10(0.375)) |
| 74 | + expectEqualWithTolerance(0.7211247851537041911608191553900547941, Self.root(0.375, 3)) |
| 75 | + expectEqualWithTolerance(0.6123724356957945245493210186764728479, Self.sqrt(0.375)) |
| 76 | + expectEqualWithTolerance(0.54171335479545025876069682133938570, Self.pow(0.375, 0.625)) |
| 77 | + expectEqualWithTolerance(-0.052734375, Self.pow(-0.375, 3)) |
| 78 | + |
| 79 | + expectEqual(Self.acos(0.375), acos(0.375)) |
| 80 | + expectEqual(Self.asin(0.375), asin(0.375)) |
| 81 | + expectEqual(Self.atan(0.375), atan(0.375)) |
| 82 | + expectEqual(Self.cos(0.375), cos(0.375)) |
| 83 | + expectEqual(Self.sin(0.375), sin(0.375)) |
| 84 | + expectEqual(Self.tan(0.375), tan(0.375)) |
| 85 | + expectEqual(Self.acosh(1.125), acosh(1.125)) |
| 86 | + expectEqual(Self.asinh(1.125), asinh(1.125)) |
| 87 | + expectEqual(Self.atanh(0.625), atanh(0.625)) |
| 88 | + expectEqual(Self.cosh(0.375), cosh(0.375)) |
| 89 | + expectEqual(Self.sinh(0.375), sinh(0.375)) |
| 90 | + expectEqual(Self.tanh(0.375), tanh(0.375)) |
| 91 | + expectEqual(Self.exp(0.375), exp(0.375)) |
| 92 | + expectEqual(Self.exp2(0.375), exp2(0.375)) |
| 93 | + expectEqual(Self.exp10(0.375), exp10(0.375)) |
| 94 | + expectEqual(Self.expm1(0.375), expm1(0.375)) |
| 95 | + expectEqual(Self.log(0.375), log(0.375)) |
| 96 | + expectEqual(Self.log2(0.375), log2(0.375)) |
| 97 | + expectEqual(Self.log1p(0.375), log1p(0.375)) |
| 98 | + expectEqual(Self.log10(0.375), log10(0.375)) |
| 99 | + expectEqual(Self.sqrt(0.375), sqrt(0.375)) |
| 100 | + expectEqual(Self.pow(0.375, 0.625), pow(0.375, 0.625)) |
| 101 | + expectEqual(Self.root(0.375, 3), root(0.375, 3)) |
| 102 | + expectEqual(Self.pow(-0.375, 3), pow(-0.375, 3)) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +internal extension Real where Self: BinaryFloatingPoint { |
| 107 | + static func realFunctionTests() { |
| 108 | + expectEqualWithTolerance(0.54041950027058415544357836460859991, Self.atan2(y: 0.375, x: 0.625)) |
| 109 | + expectEqualWithTolerance(0.72886898685566255885926910969319788, Self.hypot(0.375, 0.625)) |
| 110 | + expectEqualWithTolerance(0.4041169094348222983238250859191217675, Self.erf(0.375)) |
| 111 | + expectEqualWithTolerance(0.5958830905651777016761749140808782324, Self.erfc(0.375)) |
| 112 | + expectEqualWithTolerance(2.3704361844166009086464735041766525098, Self.gamma(0.375)) |
| 113 | +#if !os(Windows) |
| 114 | + expectEqualWithTolerance( -0.11775527074107877445136203331798850, Self.logGamma(1.375), ulps: 16) |
| 115 | + expectEqual(.plus, Self.signGamma(1.375)) |
| 116 | + expectEqual(.minus, Self.signGamma(-2.375)) |
| 117 | +#endif |
| 118 | + |
| 119 | + expectEqual(Self.atan2(y: 0.375, x: 0.625), atan2(y: 0.375, x: 0.625)) |
| 120 | + expectEqual(Self.hypot(0.375, 0.625), hypot(0.375, 0.625)) |
| 121 | + expectEqual(Self.erf(0.375), erf(0.375)) |
| 122 | + expectEqual(Self.erfc(0.375), erfc(0.375)) |
| 123 | + expectEqual(Self.gamma(0.375), gamma(0.375)) |
| 124 | +#if !os(Windows) |
| 125 | + expectEqual(Self.logGamma(1.375), logGamma(1.375)) |
| 126 | + expectEqual(Self.signGamma(1.375), signGamma(1.375)) |
| 127 | + expectEqual(Self.signGamma(-2.375), signGamma(-2.375)) |
| 128 | +#endif |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +internal extension BinaryFloatingPoint { |
| 133 | + static func floatingPointFunctionTests() { |
| 134 | + expectEqual(1 as Self, ceil(0.375)) |
| 135 | + expectEqual(0 as Self, floor(0.375)) |
| 136 | + expectEqual(0 as Self, Swift.round(0.375)) |
| 137 | + expectEqual(0 as Self, trunc(0.375)) |
| 138 | + expectEqual(0 as Self, ceil(-0.625)) |
| 139 | + expectEqual(-1 as Self, floor(-0.625)) |
| 140 | + expectEqual(-1 as Self, Swift.round(-0.625)) |
| 141 | + expectEqual(0 as Self, trunc(-0.625)) |
| 142 | + expectEqual(1 as Self, ceil(0.5)) |
| 143 | + expectEqual(0 as Self, floor(0.5)) |
| 144 | + expectEqual(1 as Self, Swift.round(0.5)) |
| 145 | + expectEqual(0 as Self, trunc(0.5)) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +%for T in ['Float', 'Double', 'CGFloat', 'Float80']: |
| 150 | +% if T == 'Float80': |
| 151 | +#if (arch(i386) || arch(x86_64)) && !os(Windows) |
| 152 | +% elif T == 'CGFloat': |
| 153 | +#if canImport(CoreGraphics) |
| 154 | + import CoreGraphics |
| 155 | +% end |
| 156 | + |
| 157 | +MathTests.test("${T}") { |
| 158 | + ${T}.elementaryFunctionTests() |
| 159 | + ${T}.realFunctionTests() |
| 160 | + ${T}.floatingPointFunctionTests() |
| 161 | +} |
| 162 | + |
| 163 | +% if T in ['CGFloat', 'Float80']: |
| 164 | +#endif |
| 165 | +% end |
| 166 | +%end |
| 167 | + |
| 168 | +runAllTests() |
0 commit comments