File: class_resilience.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (334 lines) | stat: -rw-r--r-- 10,493 bytes parent folder | download
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// RUN: %empty-directory(%t)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_struct)) -enable-library-evolution %S/../Inputs/resilient_struct.swift -emit-module -emit-module-path %t/resilient_struct.swiftmodule -module-name resilient_struct
// RUN: %target-codesign %t/%target-library-name(resilient_struct)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_class)) -enable-library-evolution %S/../Inputs/resilient_class.swift -emit-module -emit-module-path %t/resilient_class.swiftmodule -module-name resilient_class -I%t -L%t -lresilient_struct
// RUN: %target-codesign %t/%target-library-name(resilient_class)

// RUN: %target-build-swift-dylib(%t/%target-library-name(fixed_layout_class)) -enable-library-evolution %S/../Inputs/fixed_layout_class.swift -emit-module -emit-module-path %t/fixed_layout_class.swiftmodule -module-name fixed_layout_class -I%t -L%t -lresilient_struct
// RUN: %target-codesign %t/%target-library-name(fixed_layout_class)

// RUN: %target-build-swift %s -L %t -I %t -lresilient_struct -lresilient_class -lfixed_layout_class -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main

// RUN: %target-run %t/main %t/%target-library-name(resilient_struct) %t/%target-library-name(resilient_class) %t/%target-library-name(fixed_layout_class)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_struct_wmo)) -enable-library-evolution %S/../Inputs/resilient_struct.swift -emit-module -emit-module-path %t/resilient_struct.swiftmodule -module-name resilient_struct -whole-module-optimization
// RUN: %target-codesign %t/%target-library-name(resilient_struct_wmo)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_class_wmo)) -enable-library-evolution %S/../Inputs/resilient_class.swift -emit-module -emit-module-path %t/resilient_class.swiftmodule -module-name resilient_class -I%t -L%t -lresilient_struct_wmo -whole-module-optimization
// RUN: %target-codesign %t/%target-library-name(resilient_class_wmo)

// RUN: %target-build-swift-dylib(%t/%target-library-name(fixed_layout_class_wmo)) -enable-library-evolution %S/../Inputs/fixed_layout_class.swift -emit-module -emit-module-path %t/fixed_layout_class.swiftmodule -module-name fixed_layout_class -I%t -L%t -lresilient_struct_wmo -whole-module-optimization
// RUN: %target-codesign %t/%target-library-name(fixed_layout_class_wmo)

// RUN: %target-build-swift %s -L %t -I %t -lresilient_struct_wmo -lresilient_class_wmo -lfixed_layout_class_wmo -o %t/main2 %target-rpath(%t) -module-name main
// RUN: %target-codesign %t/main2

// RUN: %target-run %t/main2 %t/%target-library-name(resilient_struct_wmo) %t/%target-library-name(resilient_class_wmo) %t/%target-library-name(fixed_layout_class_wmo)

// REQUIRES: executable_test

import StdlibUnittest

import fixed_layout_class
import resilient_class
import resilient_struct

var ResilientClassTestSuite = TestSuite("ResilientClass")

// Concrete class with resilient stored property

protocol ProtocolWithResilientProperty {
  var s: Size { get }
}

public class ClassWithResilientProperty : ProtocolWithResilientProperty {
  public let p: Point
  public let s: Size
  public let color: Int32

  public init(p: Point, s: Size, color: Int32) {
    self.p = p
    self.s = s
    self.color = color
  }
}

@inline(never) func getS(_ p: ProtocolWithResilientProperty) -> Size {
  return p.s
}

ResilientClassTestSuite.test("ClassWithResilientProperty") {
  let c = ClassWithResilientProperty(
      p: Point(x: 10, y: 20),
      s: Size(w: 30, h: 40),
      color: 50)

  expectEqual(c.p.x, 10)
  expectEqual(c.p.y, 20)
  expectEqual(c.s.w, 30)
  expectEqual(c.s.h, 40)
  expectEqual(c.color, 50)

  // Make sure the conformance works
  expectEqual(getS(c).w, 30)
  expectEqual(getS(c).h, 40)
}

ResilientClassTestSuite.test("OutsideClassWithResilientProperty") {
  let c = OutsideParentWithResilientProperty(
      p: Point(x: 10, y: 20),
      s: Size(w: 30, h: 40),
      color: 50)

  expectEqual(c.p.x, 10)
  expectEqual(c.p.y, 20)
  expectEqual(c.s.w, 30)
  expectEqual(c.s.h, 40)
  expectEqual(c.color, 50)

  expectEqual(0, c.laziestNumber)
  c.laziestNumber = 1
  expectEqual(1, c.laziestNumber)
}

// Generic class with resilient stored property

public class GenericClassWithResilientProperty<T> {
  public let s1: Size
  public let t: T
  public let s2: Size

  public init(s1: Size, t: T, s2: Size) {
    self.s1 = s1
    self.t = t
    self.s2 = s2
  }
}

ResilientClassTestSuite.test("GenericClassWithResilientProperty") {
  let c = GenericClassWithResilientProperty<Int32>(
      s1: Size(w: 10, h: 20),
      t: 30,
      s2: Size(w: 40, h: 50))

  expectEqual(c.s1.w, 10)
  expectEqual(c.s1.h, 20)
  expectEqual(c.t, 30)
  expectEqual(c.s2.w, 40)
  expectEqual(c.s2.h, 50)
}


// Concrete class with non-fixed size stored property

public class ClassWithResilientlySizedProperty {
  public let r: Rectangle
  public let color: Int32

  public init(r: Rectangle, color: Int32) {
    self.r = r
    self.color = color
  }
}

ResilientClassTestSuite.test("ClassWithResilientlySizedProperty") {
  let c = ClassWithResilientlySizedProperty(
      r: Rectangle(
          p: Point(x: 10, y: 20),
          s: Size(w: 30, h: 40),
          color: 50),
      color: 60)

  expectEqual(c.r.p.x, 10)
  expectEqual(c.r.p.y, 20)
  expectEqual(c.r.s.w, 30)
  expectEqual(c.r.s.h, 40)
  expectEqual(c.r.color, 50)
  expectEqual(c.color, 60)
}


// Concrete subclass of fixed-layout class with resilient stored property

public class ChildOfParentWithResilientStoredProperty : ClassWithResilientProperty {
  let enabled: Int32

  public init(p: Point, s: Size, color: Int32, enabled: Int32) {
    self.enabled = enabled
    super.init(p: p, s: s, color: color)
  }
}

ResilientClassTestSuite.test("ChildOfParentWithResilientStoredProperty") {
  let c = ChildOfParentWithResilientStoredProperty(
      p: Point(x: 10, y: 20),
      s: Size(w: 30, h: 40),
      color: 50,
      enabled: 60)

  expectEqual(c.p.x, 10)
  expectEqual(c.p.y, 20)
  expectEqual(c.s.w, 30)
  expectEqual(c.s.h, 40)
  expectEqual(c.color, 50)
  expectEqual(c.enabled, 60)
}


// Concrete subclass of fixed-layout class with resilient stored property

public class ChildOfOutsideParentWithResilientStoredProperty : OutsideParentWithResilientProperty {
  let enabled: Int32

  public init(p: Point, s: Size, color: Int32, enabled: Int32) {
    self.enabled = enabled
    super.init(p: p, s: s, color: color)
  }
}

ResilientClassTestSuite.test("ChildOfOutsideParentWithResilientStoredProperty") {
  let c = ChildOfOutsideParentWithResilientStoredProperty(
      p: Point(x: 10, y: 20),
      s: Size(w: 30, h: 40),
      color: 50,
      enabled: 60)

  expectEqual(c.p.x, 10)
  expectEqual(c.p.y, 20)
  expectEqual(c.s.w, 30)
  expectEqual(c.s.h, 40)
  expectEqual(c.color, 50)
  expectEqual(c.enabled, 60)
}


// Resilient class from a different resilience domain

ResilientClassTestSuite.test("ResilientOutsideParent") {
  let c = ResilientOutsideParent()

  expectEqual(c.property, "ResilientOutsideParent.property")
  expectEqual(c.finalProperty, "ResilientOutsideParent.finalProperty")
}


// Concrete subclass of resilient class

public class ChildOfResilientOutsideParent : ResilientOutsideParent {
  let enabled: Int32

  public init(enabled: Int32) {
    self.enabled = enabled
    super.init()
  }
}

ResilientClassTestSuite.test("ChildOfResilientOutsideParent") {
  let c = ChildOfResilientOutsideParent(enabled: 60)

  expectEqual(c.property, "ResilientOutsideParent.property")
  expectEqual(c.finalProperty, "ResilientOutsideParent.finalProperty")
  expectEqual(c.enabled, 60)
}


// Concrete subclass of resilient class

public class ChildOfResilientOutsideParentWithResilientStoredProperty : ResilientOutsideParent {
  public let p: Point
  public let s: Size
  public let color: Int32

  public init(p: Point, s: Size, color: Int32) {
    self.p = p
    self.s = s
    self.color = color
    super.init()
  }
}

ResilientClassTestSuite.test("ChildOfResilientOutsideParentWithResilientStoredProperty") {
  let c = ChildOfResilientOutsideParentWithResilientStoredProperty(
      p: Point(x: 10, y: 20),
      s: Size(w: 30, h: 40),
      color: 50)

  expectEqual(c.property, "ResilientOutsideParent.property")
  expectEqual(c.finalProperty, "ResilientOutsideParent.finalProperty")

  expectEqual(c.p.x, 10)
  expectEqual(c.p.y, 20)
  expectEqual(c.s.w, 30)
  expectEqual(c.s.h, 40)
  expectEqual(c.color, 50)
}

class ChildWithMethodOverride : ResilientOutsideParent {
  override func getValue() -> Int {
    return 1
  }
}

ResilientClassTestSuite.test("ChildWithMethodOverride") {
  let c = ChildWithMethodOverride()
  expectEqual(c.getValue(), 1)
}

ResilientClassTestSuite.test("TypeByName") {
  expectTrue(_typeByName("main.ClassWithResilientProperty")
             == ClassWithResilientProperty.self)
  expectTrue(_typeByName("main.ClassWithResilientlySizedProperty")
             == ClassWithResilientlySizedProperty.self)
  expectTrue(_typeByName("main.ChildOfParentWithResilientStoredProperty")
             == ChildOfParentWithResilientStoredProperty.self)
  expectTrue(_typeByName("main.ChildOfOutsideParentWithResilientStoredProperty")
             == ChildOfOutsideParentWithResilientStoredProperty.self)
}

@frozen
public struct Empty {}

// rdar://48031465
public class ClassWithEmptyThenResilient {
  public let empty: Empty
  public let resilient: ResilientInt

  public init(empty: Empty, resilient: ResilientInt) {
    self.empty = empty
    self.resilient = resilient
  }
}

ResilientClassTestSuite.test("EmptyThenResilient") {
  let c = ClassWithEmptyThenResilient(empty: Empty(),
                                      resilient: ResilientInt(i: 17))
  expectEqual(c.resilient.i, 17)
}

public class ClassWithResilientThenEmpty {
  public let resilient: ResilientInt
  public let empty: Empty

  public init(empty: Empty, resilient: ResilientInt) {
    self.empty = empty
    self.resilient = resilient
  }
}

ResilientClassTestSuite.test("ResilientThenEmpty") {
  let c = ClassWithResilientThenEmpty(empty: Empty(),
                                      resilient: ResilientInt(i: 17))
  expectEqual(c.resilient.i, 17)
}

// This test triggers https://github.com/apple/swift/issues/43427
// (rdar://problem/25318716) on macOS 10.9 and iOS 7. Disable it for now when
// testing on those versions.
if #available(macOS 10.10, iOS 8, *) {
  runAllTests()
} else {
  runNoTests()
}