Skip to content
Open
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
6 changes: 5 additions & 1 deletion SwiftRandom/Randoms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public extension Int {
public extension Int32 {
/// SwiftRandom extension
public static func random(_ range: Range<Int>) -> Int32 {
return random(range.upperBound, range.lowerBound)
#if swift(>=3)
return random(range.lowerBound, range.upperBound)
#else
return random(range.lowerBound, range.upperBound - 1)
#endif
}

/// SwiftRandom extension
Expand Down
13 changes: 12 additions & 1 deletion SwiftRandomTests/SwiftRandomTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SwiftRandomTests: XCTestCase {
/// Tests for extensions applied directly above Swift types.
func testTypeExtensions() {
XCTAssertNotNil(Int.random())
XCTAssertNotNil(Int32.random())
XCTAssertNotNil(Date.random())
XCTAssertNotNil(Date.randomWithinDaysBeforeToday(7))
XCTAssertNotNil(CGFloat.random())
Expand Down Expand Up @@ -66,7 +67,17 @@ class SwiftRandomTests: XCTestCase {
XCTAssertLessThan(randomLessTen, 10)
}
}


func testRandomInt32Range() {
for _ in 0...10 {

let randomLessTen = Int32.random(0..<10)

XCTAssertGreaterThanOrEqual(randomLessTen, Int32(0))
XCTAssertLessThan(randomLessTen, Int32(10))
}
}

/// Tests generating random a `String` of a specified length.
func testRandomStringOfLength() {
let precision = 100
Expand Down