forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailable_initializers_root_class.swift
176 lines (126 loc) · 2.53 KB
/
failable_initializers_root_class.swift
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
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
var FailableInitTestSuite = TestSuite("FailableInit")
var deinitCalled = 0
func mustFail<T>(f: () -> T?) {
if f() != nil {
preconditionFailure("Didn't fail")
}
}
func mustSucceed<T>(f: () -> T?) {
if f() == nil {
preconditionFailure("Didn't succeed")
}
}
class FirstClass {
var x: LifetimeTracked
init?(n: Int) {
if n == 0 {
return nil
}
x = LifetimeTracked(0)
if n == 1 {
return nil
}
}
deinit {
deinitCalled += 1
}
}
FailableInitTestSuite.test("FirstClass") {
deinitCalled = 0
mustFail { FirstClass(n: 0) }
expectEqual(0, deinitCalled)
mustFail { FirstClass(n: 1) }
expectEqual(1, deinitCalled)
mustSucceed { FirstClass(n: 2) }
expectEqual(2, deinitCalled)
}
class FirstClassTrivial {
var x: Int
init?(n: Int) {
if n == 0 {
return nil
}
x = 0
if n == 1 {
return nil
}
}
deinit {
deinitCalled += 1
}
}
FailableInitTestSuite.test("FirstClassTrivial") {
deinitCalled = 0
mustFail { FirstClassTrivial(n: 0) }
expectEqual(0, deinitCalled)
mustFail { FirstClassTrivial(n: 1) }
expectEqual(1, deinitCalled)
mustSucceed { FirstClassTrivial(n: 2) }
expectEqual(2, deinitCalled)
}
class SecondClass {
var x: LifetimeTracked
var y: LifetimeTracked
init?(n: Int) {
if n == 0 {
return nil
}
x = LifetimeTracked(0)
if n == 1 {
return nil
}
y = LifetimeTracked(0)
if n == 2 {
return nil
}
}
deinit {
deinitCalled += 1
}
}
FailableInitTestSuite.test("SecondClass") {
deinitCalled = 0
mustFail { SecondClass(n: 0) }
expectEqual(0, deinitCalled)
mustFail { SecondClass(n: 1) }
expectEqual(0, deinitCalled)
mustFail { SecondClass(n: 2) }
expectEqual(1, deinitCalled)
mustSucceed { SecondClass(n: 3) }
expectEqual(2, deinitCalled)
}
class SecondClassTrivial {
var x: Int
var y: Int
init?(n: Int) {
if n == 0 {
return nil
}
x = 0
if n == 1 {
return nil
}
y = 0
if n == 2 {
return nil
}
}
deinit {
deinitCalled += 1
}
}
FailableInitTestSuite.test("SecondClassTrivial") {
deinitCalled = 0
mustFail { SecondClassTrivial(n: 0) }
expectEqual(0, deinitCalled)
mustFail { SecondClassTrivial(n: 1) }
expectEqual(0, deinitCalled)
mustFail { SecondClassTrivial(n: 2) }
expectEqual(1, deinitCalled)
mustSucceed { SecondClassTrivial(n: 3) }
expectEqual(2, deinitCalled)
}
runAllTests()