Skip to content

Instantly share code, notes, and snippets.

@josephlord
Created July 15, 2015 21:46
Show Gist options
  • Save josephlord/bf0b8ccd6f2b0f935c0f to your computer and use it in GitHub Desktop.
Save josephlord/bf0b8ccd6f2b0f935c0f to your computer and use it in GitHub Desktop.
Demonstration of assertion behaviour in different build modes
import Foundation
func testAssert(x:Int)->Int {
var res = 0
if x > 0 {
res += 3
assertionFailure("testAssert")
res += 5
return res
} else {
res += 7
}
res += 2
return res
}
let i:Int = Int(arc4random())
print(i)
let result = testAssert(i)
print("\n")
print(result)
print("\n")
print("End")
// Result is:
// 1661870581 -- (random result will vary but will be +ve
// 9 -- (result of false case as assertion assumed valid in -Ounchecked in 6.4 or DISABLE_SAFETY_CHECKS in XC7
// End -- Reaches end of execution
// Program ended with exit code: 0 -- (Exits without error).
// For -O /safety checks on builds the 9 is replaced by 8 showing the test remains in place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment