Skip to content

Instantly share code, notes, and snippets.

View truizlop's full-sized avatar

Tomás Ruiz-López truizlop

View GitHub Profile
func concat(first : String, second : String) -> String {
switch (first, second) {
case ("Another ", "example"): return "Another example"
case ("Property-based ", "Testing"): return "Property-based Testing"
case ("Swift ", "rocks"): return "Swift rocks"
case ("One more ", "thing"): return "One more thing"
default: return "Hello, World!"
}
}
func testStringConcat() {
for _ in 0 ..< 100 {
let a = randomString()
let b = randomString()
let expected = a + b
XCTAssertEqual(expected, concat(first: a, second: b))
}
}
func testConcatLengthIsEqualToSumOfLengths() {
for _ in 0 ..< 100 {
let a = randomString()
let b = randomString()
let result = concat(first: a, second: b)
XCTAssertEqual(result.length, a.length + b.length)
}
}
func testConcatIsAssociative() {
for _ in 0 ..< 100 {
let a = randomString()
let b = randomString()
let c = randomString()
let result1 = concat(first: concat(first: a, second: b), second: c)
let result2 = concat(first: a, second: concat(first: b, second: c))
XCTAssertEqual(result1, result2)
}
}
func testConcatHasEmptyStringAsNeutralElement() {
for _ in 0 ..< 100 {
let a = randomString()
let result = concat(first: a, second: "")
XCTAssertEqual(result, a)
}
}
func testConcatLengthIsEqualToSumOfLengths() {
property("Concatenation length is equal to sum of input lengths") <- forAll { (a : String, b : String) in
let output = concat(first: a, second: b)
return output.characters.count == a.characters.count + b.characters.count
}
}
func testConcatIsAssociative() {
property("Concatenation is associative") <- forAll { (a : String, b : String, c : String) in
let output1 = concat(first: concat(first: a, second: b), second: c)
property("Whatever property") <- forAll { (a : String) in
// Test property
}
property("Whatever property") <- forAll(String.arbitrary) { (a : String) in
// Test property
}
let positiveIntsGen = Int.arbitrary.suchThat{ n in n > 0 }
let uppercaseCharacterGen = Gen<Character>.fromElementsIn(Character("A")...Character("Z"))