mirror of
https://github.com/apple/swift-nio
synced 2026-11-29 05:24:21 +00:00
Conforms TimeAmount to AdditiveArithmetic. Motivation: TimeAmount does not support -=, +=. Sometimes it is useful to manipulate time amounts when building up a delay and if that iteration fails, we would want to delay += .milliseconds(5) to add 5 milliseconds to our delay and try again. Modifications: Conformed TimeAmount to AdditiveArithmetic: added a static zero property and required operators. Result: TimeAmount conforms to AdditiveArithmetic. Co-authored-by: Josh <jrtkski@icloud.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
47 lines
1.7 KiB
Swift
47 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import NIO
|
|
import XCTest
|
|
|
|
class TimeAmountTests: XCTestCase {
|
|
func testTimeAmountConversion() {
|
|
XCTAssertEqual(TimeAmount.nanoseconds(3), .nanoseconds(3))
|
|
XCTAssertEqual(TimeAmount.microseconds(14), .nanoseconds(14_000))
|
|
XCTAssertEqual(TimeAmount.milliseconds(15), .nanoseconds(15_000_000))
|
|
XCTAssertEqual(TimeAmount.seconds(9), .nanoseconds(9_000_000_000))
|
|
XCTAssertEqual(TimeAmount.minutes(2), .nanoseconds(120_000_000_000))
|
|
XCTAssertEqual(TimeAmount.hours(6), .nanoseconds(21_600_000_000_000))
|
|
XCTAssertEqual(TimeAmount.zero, .nanoseconds(0))
|
|
}
|
|
|
|
func testTimeAmountIsHashable() {
|
|
let amounts: Set<TimeAmount> = [.seconds(1), .milliseconds(4), .seconds(1)]
|
|
XCTAssertEqual(amounts, [.seconds(1), .milliseconds(4)])
|
|
}
|
|
|
|
func testTimeAmountDoesAddTime() {
|
|
var lhs = TimeAmount.nanoseconds(0)
|
|
let rhs = TimeAmount.nanoseconds(5)
|
|
lhs += rhs
|
|
XCTAssertEqual(lhs, .nanoseconds(5))
|
|
}
|
|
|
|
func testTimeAmountDoesSubtractTime() {
|
|
var lhs = TimeAmount.nanoseconds(5)
|
|
let rhs = TimeAmount.nanoseconds(5)
|
|
lhs -= rhs
|
|
XCTAssertEqual(lhs, .nanoseconds(0))
|
|
}
|
|
}
|