-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from dn-m/ordered
Migrate ordered(_:_:) here from Math
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// Ordered.swift | ||
// Algorithms | ||
// | ||
// Created by James Bean on 8/8/18. | ||
// | ||
|
||
/// let (lower,higher) = ordered(7,3) // => (3,7) | ||
/// | ||
/// - Note: If both values are equal, they are returned in the order in which they were given | ||
/// | ||
/// - Returns: 2-tuple of two given values, in order. | ||
/// | ||
public func ordered <T: Comparable> (_ a: T, _ b: T) -> (T, T) { | ||
return a <= b ? (a,b) : (b,a) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// OrderedTests.swift | ||
// AlgorithmsTests | ||
// | ||
// Created by James Bean on 8/8/18. | ||
// | ||
|
||
import XCTest | ||
@testable import Algorithms | ||
|
||
class OrderedTests: XCTestCase { | ||
|
||
func testOrderedEqual() { | ||
let a = 4 | ||
let b = 4 | ||
let (newA, newB) = ordered(a, b) | ||
XCTAssertEqual(newA, a) | ||
XCTAssertEqual(newB, b) | ||
} | ||
|
||
func testOrderInOrder() { | ||
let a = 4 | ||
let b = 5 | ||
let (newA, newB) = ordered(a, b) | ||
XCTAssertEqual(newA, a) | ||
XCTAssertEqual(newB, b) | ||
} | ||
|
||
func testOrderNeedsOrdering() { | ||
let a = 5 | ||
let b = 4 | ||
let (newB, newA) = ordered(a, b) | ||
XCTAssertEqual(newA, a) | ||
XCTAssertEqual(newB, b) | ||
} | ||
} |