Skip to content

[SR-6785] Fix Decimal conformance to Strideable #3066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Darwin/Foundation-swiftoverlay-Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ class TestDecimal : XCTestCase {
XCTAssertEqual(Decimal(-9), Decimal(1) - Decimal(10))
XCTAssertEqual(Decimal(3), Decimal(2).nextUp)
XCTAssertEqual(Decimal(2), Decimal(3).nextDown)
XCTAssertEqual(Decimal(-476), Decimal(1024).distance(to: Decimal(1500)))
XCTAssertEqual(Decimal(68040), Decimal(386).advanced(by: Decimal(67654)))
XCTAssertEqual(Decimal(1.234), abs(Decimal(1.234)))
XCTAssertEqual(Decimal(1.234), abs(Decimal(-1.234)))
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
Expand Down Expand Up @@ -588,6 +586,17 @@ class TestDecimal : XCTestCase {
}
}
}

func test_Strideable() {
XCTAssertEqual(Decimal(476), Decimal(1024).distance(to: Decimal(1500)))
XCTAssertEqual(Decimal(68040), Decimal(386).advanced(by: Decimal(67654)))

let x = 42 as Decimal
XCTAssertEqual(x.distance(to: 43), 1)
XCTAssertEqual(x.advanced(by: 1), 43)
XCTAssertEqual(x.distance(to: 41), -1)
XCTAssertEqual(x.advanced(by: -1), 41)
}

func test_ULP() {
let x = 0.1 as Decimal
Expand Down
2 changes: 1 addition & 1 deletion Darwin/Foundation-swiftoverlay/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ extension Decimal {

extension Decimal : Strideable {
public func distance(to other: Decimal) -> Decimal {
return self - other
return other - self
}

public func advanced(by n: Decimal) -> Decimal {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ extension Decimal {

extension Decimal : Strideable {
public func distance(to other: Decimal) -> Decimal {
return self - other
return other - self
}
public func advanced(by n: Decimal) -> Decimal {
return self + n
Expand Down
14 changes: 12 additions & 2 deletions Tests/Foundation/Tests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,6 @@ class TestDecimal: XCTestCase {
XCTAssertEqual(Decimal(-9), Decimal(1) - Decimal(10))
XCTAssertEqual(Decimal(3), Decimal(2).nextUp)
XCTAssertEqual(Decimal(2), Decimal(3).nextDown)
XCTAssertEqual(Decimal(-476), Decimal(1024).distance(to: Decimal(1500)))
XCTAssertEqual(Decimal(68040), Decimal(386).advanced(by: Decimal(67654)))
XCTAssertEqual(Decimal(1.234), abs(Decimal(1.234)))
XCTAssertEqual(Decimal(1.234), abs(Decimal(-1.234)))
XCTAssertEqual((0 as Decimal).magnitude, 0 as Decimal)
Expand Down Expand Up @@ -851,6 +849,17 @@ class TestDecimal: XCTestCase {

XCTAssertEqual(100,number.objCType.pointee, "ObjC type for NSDecimalNumber is 'd'")
}

func test_Strideable() {
XCTAssertEqual(Decimal(476), Decimal(1024).distance(to: Decimal(1500)))
XCTAssertEqual(Decimal(68040), Decimal(386).advanced(by: Decimal(67654)))

let x = 42 as Decimal
XCTAssertEqual(x.distance(to: 43), 1)
XCTAssertEqual(x.advanced(by: 1), 43)
XCTAssertEqual(x.distance(to: 41), -1)
XCTAssertEqual(x.advanced(by: -1), 41)
}

func test_ULP() {
let x = 0.1 as Decimal
Expand Down Expand Up @@ -1462,6 +1471,7 @@ class TestDecimal: XCTestCase {
("test_ScanDecimal", test_ScanDecimal),
("test_SimpleMultiplication", test_SimpleMultiplication),
("test_SmallerNumbers", test_SmallerNumbers),
("test_Strideable", test_Strideable),
("test_ULP", test_ULP),
("test_ZeroPower", test_ZeroPower),
("test_parseDouble", test_parseDouble),
Expand Down