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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(TestSupport)
import TestSupport
#endif
#if canImport(FoundationInternationalization)
@testable import FoundationEssentials
@testable import FoundationInternationalization
#endif
#if FOUNDATION_FRAMEWORK
@testable import Foundation
#endif
final class DurationExtensionTests : XCTestCase {
func testRoundingMode() {
func verify(_ tests: [Int64], increment: Int64, expected: [FloatingPointRoundingRule: [Int64]], file: StaticString = #filePath, line: UInt = #line) {
let modes: [FloatingPointRoundingRule] = [.down, .up, .towardZero, .awayFromZero, .toNearestOrEven, .toNearestOrAwayFromZero]
for mode in modes {
var actual: [Duration] = []
for test in tests {
actual.append(Duration.seconds(test).rounded(increment: Duration.seconds(increment), rule: mode))
}
XCTAssertEqual(actual, expected[mode]?.map { Duration.seconds($0) }, "\(mode) does not match", file: file, line: line)
}
}
verify([9223372036854775018, 18, 15, 12, 8, 5, 2, 0], increment: 10, expected: [
.down : [9223372036854775010, 10, 10, 10, 0, 0, 0, 0],
.up : [9223372036854775020, 20, 20, 20, 10, 10, 10, 0],
.towardZero: [9223372036854775010, 10, 10, 10, 0, 0, 0, 0],
.awayFromZero: [9223372036854775020, 20, 20, 20, 10, 10, 10, 0],
.toNearestOrEven: [9223372036854775020, 20, 20, 10, 10, 0, 0, 0],
.toNearestOrAwayFromZero: [9223372036854775020, 20, 20, 10, 10, 10, 0, 0]
])
verify([ -2, -5, -8, -12, -15, -18, -9223372036854775018 ], increment: 10, expected: [
.down : [-10, -10, -10, -20, -20, -20, -9223372036854775020],
.up : [ 0, 0, 0, -10, -10, -10, -9223372036854775010],
.towardZero: [ 0, 0, 0, -10, -10, -10, -9223372036854775010],
.awayFromZero: [-10, -10, -10, -20, -20, -20, -9223372036854775020],
.toNearestOrEven: [ 0, 0, -10, -10, -20, -20, -9223372036854775020],
.toNearestOrAwayFromZero: [ 0, -10, -10, -10, -20, -20, -9223372036854775020]
])
verify([9223372036854775018, 18, 15, 12, 8, 5, 2, 0], increment: 5, expected: [
.down : [9223372036854775015, 15, 15, 10, 5, 5, 0, 0],
.up : [9223372036854775020, 20, 15, 15, 10, 5, 5, 0],
.towardZero: [9223372036854775015, 15, 15, 10, 5, 5, 0, 0],
.awayFromZero: [9223372036854775020, 20, 15, 15, 10, 5, 5, 0],
.toNearestOrEven: [9223372036854775020, 20, 15, 10, 10, 5, 0, 0],
.toNearestOrAwayFromZero: [9223372036854775020, 20, 15, 10, 10, 5, 0, 0]
])
verify([ -2, -5, -8, -12, -15, -18, -9223372036854775018 ], increment: 5, expected: [
.down : [ -5, -5, -10, -15, -15, -20, -9223372036854775020],
.up : [ 0, -5, -5, -10, -15, -15, -9223372036854775015],
.towardZero: [ 0, -5, -5, -10, -15, -15, -9223372036854775015],
.awayFromZero: [ -5, -5, -10, -15, -15, -20, -9223372036854775020],
.toNearestOrEven: [ 0, -5, -10, -10, -15, -20, -9223372036854775020],
.toNearestOrAwayFromZero: [ 0, -5, -10, -10, -15, -20, -9223372036854775020]
])
verify([9223372036854775018, 18, 15, 12, 8, 5, 2, 0], increment: -10, expected: [
.down : [9223372036854775010, 10, 10, 10, 0, 0, 0, 0],
.up : [9223372036854775020, 20, 20, 20, 10, 10, 10, 0],
.towardZero: [9223372036854775010, 10, 10, 10, 0, 0, 0, 0],
.awayFromZero: [9223372036854775020, 20, 20, 20, 10, 10, 10, 0],
.toNearestOrEven: [9223372036854775020, 20, 20, 10, 10, 0, 0, 0],
.toNearestOrAwayFromZero: [9223372036854775020, 20, 20, 10, 10, 10, 0, 0]
])
verify([ -2, -5, -8, -12, -15, -18, -9223372036854775018 ], increment: -10, expected: [
.down : [-10, -10, -10, -20, -20, -20, -9223372036854775020],
.up : [ 0, 0, 0, -10, -10, -10, -9223372036854775010],
.towardZero: [ 0, 0, 0, -10, -10, -10, -9223372036854775010],
.awayFromZero: [-10, -10, -10, -20, -20, -20, -9223372036854775020],
.toNearestOrEven: [ 0, 0, -10, -10, -20, -20, -9223372036854775020],
.toNearestOrAwayFromZero: [ 0, -10, -10, -10, -20, -20, -9223372036854775020]
])
verify([9223372036854775018, 18, 15, 12, 8, 5, 2, 0], increment: 9223372036854775807, expected: [
.down : [0, 0, 0, 0, 0, 0, 0, 0],
.up : [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 0],
.towardZero: [0, 0, 0, 0, 0, 0, 0, 0],
.awayFromZero: [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 0],
.toNearestOrEven: [9223372036854775807, 0, 0, 0, 0, 0, 0, 0],
.toNearestOrAwayFromZero: [9223372036854775807, 0, 0, 0, 0, 0, 0, 0]
])
}
}
|