Skip to content
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

fix Int32 random method generation with passed range into it #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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