Conform TimeAmount to AdditiveArithmetic (#1691)
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>
This commit is contained in:
parent
5939c78fca
commit
2f9ea47738
|
@ -349,14 +349,27 @@ extension TimeAmount: Comparable {
|
|||
}
|
||||
}
|
||||
|
||||
extension TimeAmount {
|
||||
extension TimeAmount: AdditiveArithmetic {
|
||||
/// The zero value for `TimeAmount`.
|
||||
public static var zero: TimeAmount {
|
||||
return TimeAmount.nanoseconds(0)
|
||||
}
|
||||
|
||||
public static func + (lhs: TimeAmount, rhs: TimeAmount) -> TimeAmount {
|
||||
return TimeAmount(lhs.nanoseconds + rhs.nanoseconds)
|
||||
}
|
||||
|
||||
public static func +=(lhs: inout TimeAmount, rhs: TimeAmount) {
|
||||
lhs = lhs + rhs
|
||||
}
|
||||
|
||||
public static func - (lhs: TimeAmount, rhs: TimeAmount) -> TimeAmount {
|
||||
return TimeAmount(lhs.nanoseconds - rhs.nanoseconds)
|
||||
}
|
||||
|
||||
public static func -=(lhs: inout TimeAmount, rhs: TimeAmount) {
|
||||
lhs = lhs - rhs
|
||||
}
|
||||
|
||||
public static func * <T: BinaryInteger>(lhs: T, rhs: TimeAmount) -> TimeAmount {
|
||||
return TimeAmount(Int64(lhs) * rhs.nanoseconds)
|
||||
|
|
|
@ -29,6 +29,8 @@ extension TimeAmountTests {
|
|||
return [
|
||||
("testTimeAmountConversion", testTimeAmountConversion),
|
||||
("testTimeAmountIsHashable", testTimeAmountIsHashable),
|
||||
("testTimeAmountDoesAddTime", testTimeAmountDoesAddTime),
|
||||
("testTimeAmountDoesSubtractTime", testTimeAmountDoesSubtractTime),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,10 +22,25 @@ class TimeAmountTests: XCTestCase {
|
|||
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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue