1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -Xfrontend -disable-access-control -o %t/Assert_Debug -Onone
// RUN: %target-build-swift %s -Xfrontend -disable-access-control -o %t/Assert_Release -O
// RUN: %target-build-swift %s -Xfrontend -disable-access-control -o %t/Assert_Unchecked -Ounchecked
// RUN: %target-codesign %t/Assert_Debug
// RUN: %target-codesign %t/Assert_Release
// RUN: %target-codesign %t/Assert_Unchecked
//
// RUN: %target-run %t/Assert_Debug
// RUN: %target-run %t/Assert_Release
// RUN: %target-run %t/Assert_Unchecked
// REQUIRES: executable_test
import StdlibUnittest
//===---
// Tests.
//===---
func testTrapsAreNoreturn(i: Int) -> Int {
// Don't need a return statement in 'case' statements because these functions
// never return.
switch i {
case 2:
preconditionFailure("cannot happen")
case 3:
preconditionFailure("cannot happen")
case 4:
_debugPreconditionFailure("cannot happen")
case 5:
_internalInvariantFailure("cannot happen")
default:
return 0
}
}
var Assert = TestSuite("Assert")
Assert.test("assert")
.xfail(.custom(
{ !_isDebugAssertConfiguration() },
reason: "assertions are disabled in Release and Unchecked mode"))
.crashOutputMatches("this should fail")
.code {
var x = 2
assert(x * 21 == 42, "should not fail")
expectCrashLater()
assert(x == 42, "this should fail")
}
Assert.test("assert/StringInterpolation")
.xfail(.custom(
{ !_isDebugAssertConfiguration() },
reason: "assertions are disabled in Release and Unchecked mode"))
.crashOutputMatches("this should fail")
.code {
var should = "should"
var x = 2
assert(x * 21 == 42, "\(should) not fail")
expectCrashLater()
assert(x == 42, "this \(should) fail")
}
Assert.test("assertionFailure")
.skip(.custom(
{ !_isDebugAssertConfiguration() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches("this should fail")
.code {
expectCrashLater()
assertionFailure("this should fail")
}
Assert.test("assertionFailure/StringInterpolation")
.skip(.custom(
{ !_isDebugAssertConfiguration() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches("this should fail")
.code {
var should = "should"
expectCrashLater()
assertionFailure("this \(should) fail")
}
Assert.test("precondition")
.xfail(.custom(
{ _isFastAssertConfiguration() },
reason: "preconditions are disabled in Unchecked mode"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
var x = 2
precondition(x * 21 == 42, "should not fail")
expectCrashLater()
precondition(x == 42, "this should fail")
}
Assert.test("precondition/StringInterpolation")
.xfail(.custom(
{ _isFastAssertConfiguration() },
reason: "preconditions are disabled in Unchecked mode"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
var should = "should"
var x = 2
precondition(x * 21 == 42, "\(should) not fail")
expectCrashLater()
precondition(x == 42, "this \(should) fail")
}
Assert.test("preconditionFailure")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
expectCrashLater()
preconditionFailure("this should fail")
}
Assert.test("preconditionFailure/StringInterpolation")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
var should = "should"
expectCrashLater()
preconditionFailure("this \(should) fail")
}
Assert.test("fatalError")
.crashOutputMatches("this should fail")
.code {
expectCrashLater()
fatalError("this should fail")
}
Assert.test("fatalError/StringInterpolation")
.crashOutputMatches("this should fail")
.code {
var should = "should"
expectCrashLater()
fatalError("this \(should) fail")
}
// FIXME: swift-3-indexing-model: add tests for fatalError() that use non-ASCII
// characters, and that use NSString-backed String.
// We had to rewrite a part of fatalError() in the indexing effort.
Assert.test("precondition")
.xfail(.custom(
{ _isFastAssertConfiguration() },
reason: "preconditions are disabled in Unchecked mode"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
var x = 2
precondition(x * 21 == 42, "should not fail")
expectCrashLater()
precondition(x == 42, "this should fail")
}
Assert.test("preconditionFailure")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
expectCrashLater()
preconditionFailure("this should fail")
}
Assert.test("_precondition")
.xfail(.custom(
{ _isFastAssertConfiguration() },
reason: "preconditions are disabled in Unchecked mode"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
var x = 2
_precondition(x * 21 == 42, "should not fail")
expectCrashLater()
_precondition(x == 42, "this should fail")
}
Assert.test("_preconditionFailure")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
expectCrashLater()
_preconditionFailure("this should fail")
}
Assert.test("_debugPrecondition")
.xfail(.custom(
{ !_isStdlibDebugChecksEnabled() },
reason: "debug preconditions are disabled"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
var x = 2
_debugPrecondition(x * 21 == 42, "should not fail")
expectCrashLater()
_debugPrecondition(x == 42, "this should fail")
}
Assert.test("_debugPreconditionFailure")
.skip(.custom(
{ !_isStdlibDebugChecksEnabled() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches(_isDebugAssertConfiguration() ? "this should fail" : "")
.code {
expectCrashLater()
_debugPreconditionFailure("this should fail")
}
Assert.test("_internalInvariant")
.xfail(.custom(
{ !_isStdlibInternalChecksEnabled() },
reason: "internal invariant checks are disabled in this build of stdlib"))
.crashOutputMatches("this should fail")
.code {
var x = 2
Swift._internalInvariant(x * 21 == 42, "should not fail")
expectCrashLater()
Swift._internalInvariant(x == 42, "this should fail")
}
Assert.test("_internalInvariantFailure")
.skip(.custom(
{ !_isStdlibInternalChecksEnabled() },
reason: "optimizer assumes that the code path is unreachable"))
.crashOutputMatches("this should fail")
.code {
expectCrashLater()
_internalInvariantFailure("this should fail")
}
runAllTests()
|