Skip to content

Commit

Permalink
Merge pull request #131 from dn-m/ordered
Browse files Browse the repository at this point in the history
Migrate ordered(_:_:) here from Math
  • Loading branch information
jsbean authored Aug 9, 2018
2 parents b552efc + 1b5f83a commit 16221b5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/Algorithms/Ordered.swift
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)
}
36 changes: 36 additions & 0 deletions Tests/AlgorithmsTests/OrderedTests.swift
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)
}
}

0 comments on commit 16221b5

Please sign in to comment.