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

Tutorial Playground #91

Merged
merged 28 commits into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions SwiftCheck/Gen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ public struct Gen<A> {
/// only that value.
///
/// The input collection is required to be non-empty.
public static func fromElementsOf<S : CollectionType where S.Generator.Element == A, S.Index : protocol<RandomType, BidirectionalIndexType>>(xs : S) -> Gen<A> {
assert(!xs.isEmpty, "Gen.fromElementsOf used with empty sequence")

return choose((xs.startIndex, xs.endIndex.predecessor())).fmap { i in
public static func fromElementsOf<S : Indexable where S.Index : protocol<Comparable, RandomType>>(xs : S) -> Gen<S._Element> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This evil is only temporary.

return Gen.fromElementsIn(xs.startIndex..<xs.endIndex.advancedBy(-1)).fmap { i in
return xs[i]
}
}
Expand Down
122 changes: 0 additions & 122 deletions SwiftCheck/SwiftCheck.playground/Contents.swift

This file was deleted.

3 changes: 0 additions & 3 deletions SwiftCheck/SwiftCheck.playground/Sources/SupportCode.swift

This file was deleted.

4 changes: 0 additions & 4 deletions SwiftCheck/SwiftCheck.playground/contents.xcplayground

This file was deleted.

4 changes: 2 additions & 2 deletions SwiftCheck/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ internal func reportMinimumCaseFound(st : CheckerState, res : TestResult) -> (In
let shrinkMsg = st.successfulShrinkCount > 1 ? (" and \(st.successfulShrinkCount) shrink") : ""

print("Proposition: " + st.name)
print(res.reason + pluralize(testMsg, i: st.successfulTestCount.successor()) + pluralize(shrinkMsg, i: st.successfulShrinkCount) + "):")
print(res.reason + pluralize(testMsg, i: st.successfulTestCount.successor()) + (st.successfulShrinkCount > 1 ? pluralize(shrinkMsg, i: st.successfulShrinkCount) : "") + "):")
dispatchAfterFinalFailureCallbacks(st, res: res)
return (st.successfulShrinkCount, st.failedShrinkStepCount - st.failedShrinkStepDistance, st.failedShrinkStepDistance)
}
Expand Down Expand Up @@ -631,7 +631,7 @@ internal func cons<T>(lhs : T, var _ rhs : [T]) -> [T] {
}

private func pluralize(s : String, i : Int) -> String {
if i == 0 {
if i == 1 {
return s
}
return s + "s"
Expand Down
2 changes: 1 addition & 1 deletion SwiftCheckTests/ComplexSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftCheck
let upper : Gen<Character>= Gen<Character>.fromElementsIn("A"..."Z")
let lower : Gen<Character> = Gen<Character>.fromElementsIn("a"..."z")
let numeric : Gen<Character> = Gen<Character>.fromElementsIn("0"..."9")
let special = Gen<Character>.fromElementsOf(["!", "#", "$", "%", "&", "'", "*", "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~", "."])
let special : Gen<Character> = Gen<Character>.fromElementsOf(["!", "#", "$", "%", "&", "'", "*", "+", "-", "/", "=", "?", "^", "_", "`", "{", "|", "}", "~", "."])
let hexDigits = Gen<Character>.oneOf([
Gen<Character>.fromElementsIn("A"..."F"),
numeric,
Expand Down
25 changes: 13 additions & 12 deletions SwiftCheckTests/GenSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ class GenSpec : XCTestCase {
return forAll(g) { $0 == 0 }
}

property("Gen.fromElementsOf only generates the elements of the given array") <- forAll { (xss : Array<Int>) in
if xss.isEmpty {
return Discard()
}
let l = Set(xss)
return forAll(Gen.fromElementsOf(xss)) { l.contains($0) }
}

property("Gen.fromElementsOf only generates the elements of the given array") <- forAll { (n1 : Int, n2 : Int) in
return forAll(Gen.fromElementsOf([n1, n2])) { $0 == n1 || $0 == n2 }
}
// This crashes swiftc.
// property("Gen.fromElementsOf only generates the elements of the given array") <- forAll { (xss : Array<Int>) in
// if xss.isEmpty {
// return Discard()
// }
// let l = Set(xss)
// return forAll(Gen<Int>.fromElementsOf(xss)) { l.contains($0) }
// }
//
// property("Gen.fromElementsOf only generates the elements of the given array") <- forAll { (n1 : Int, n2 : Int) in
// return forAll(Gen.fromElementsOf([n1, n2])) { $0 == n1 || $0 == n2 }
// }

property("Gen.fromElementsOf only generates the elements of the given interval") <- forAll { (n1 : Int, n2 : Int) in
return (n1 < n2) ==> {
let interval = n1..<n2
let interval = n1...n2
return forAll(Gen<Int>.fromElementsOf(interval)) { interval.contains($0) }
}
}
Expand Down