Created
April 16, 2019 05:27
-
-
Save vdeep/cb49a2f78f3b46ee9bf38b94769f558f to your computer and use it in GitHub Desktop.
Struct to compare version numbers
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
import Foundation | |
struct VersionNumber: Equatable, Comparable { | |
let components: [Int] | |
init(version: String?) { | |
components = version?.split(separator: ".").compactMap({ Int($0) }) ?? [] | |
} | |
static func == (lhs: VersionNumber, rhs: VersionNumber) -> Bool { | |
let (leftComponents, rightComponents) = getNormalizedComponents(left: lhs.components, right: rhs.components) | |
let minCount = min(leftComponents.count, rightComponents.count) | |
for x in 0..<minCount { | |
if leftComponents[x] != rightComponents[x] { | |
return false | |
} | |
} | |
return true | |
} | |
static func < (lhs: VersionNumber, rhs: VersionNumber) -> Bool { | |
let (leftComponents, rightComponents) = getNormalizedComponents(left: lhs.components, right: rhs.components) | |
let minCount = min(leftComponents.count, rightComponents.count) | |
for x in 0..<minCount { | |
if leftComponents[x] > rightComponents[x] { | |
return false | |
} else if leftComponents[x] < rightComponents[x] { | |
return true | |
} | |
} | |
return false | |
} | |
private static func getNormalizedComponents(left: [Int], right: [Int]) -> (left: [Int], right: [Int]) { | |
var leftComponents = left | |
var rightComponents = right | |
if leftComponents.count < rightComponents.count { | |
let diff = rightComponents.count - leftComponents.count | |
for _ in 0..<diff { | |
leftComponents.append(0) | |
} | |
} else if rightComponents.count < leftComponents.count { | |
let diff = leftComponents.count - rightComponents.count | |
for _ in 0..<diff { | |
rightComponents.append(0) | |
} | |
} | |
return (left: leftComponents, right: rightComponents) | |
} | |
} |
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
import XCTest | |
class VersionNumberTests: XCTestCase { | |
func testEqual() { | |
let first = VersionNumber(version: "1.0.0") | |
let second = VersionNumber(version: "1.0.0") | |
XCTAssertFalse(first > second) | |
XCTAssertFalse(first < second) | |
XCTAssertTrue(first == second) | |
} | |
func testLessThan() { | |
let first = VersionNumber(version: "3.0.15") | |
let second = VersionNumber(version: "3.1.12") | |
XCTAssertFalse(first > second) | |
XCTAssertFalse(first == second) | |
XCTAssertTrue(first < second) | |
} | |
func testGreatherThan() { | |
let first = VersionNumber(version: "3.20.500") | |
let second = VersionNumber(version: "3.19.600") | |
XCTAssertFalse(first < second) | |
XCTAssertFalse(first == second) | |
XCTAssertTrue(first > second) | |
} | |
func testDifferentSyntax() { | |
let first = VersionNumber(version: "3") | |
let second = VersionNumber(version: "3.0.0") | |
XCTAssertFalse(first < second) | |
XCTAssertFalse(first > second) | |
XCTAssertTrue(first == second) | |
let third = VersionNumber(version: "3.1") | |
let fourth = VersionNumber(version: "3.2.0") | |
XCTAssertFalse(third > fourth) | |
XCTAssertFalse(third == fourth) | |
XCTAssertTrue(third < fourth) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment