Skip to content

Commit a5abb9d

Browse files
committed
Evolution: Add some tests for @_fixed_layout classes
@_fixed_layout classes have resilient vtables now, so we can add and re-order methods. Removing overrides is not legal though because they are subject to devirtualization.
1 parent ff09603 commit a5abb9d

6 files changed

+229
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
public func getVersion() -> Int {
3+
#if BEFORE
4+
return 0
5+
#else
6+
return 1
7+
#endif
8+
}
9+
10+
#if BEFORE
11+
12+
@_fixed_layout
13+
public class AddVirtualMethod {
14+
public init() {}
15+
16+
public func firstMethod() -> Int {
17+
return 1
18+
}
19+
20+
public func secondMethod() -> Int {
21+
return 2
22+
}
23+
}
24+
25+
#else
26+
27+
@_fixed_layout
28+
public class AddVirtualMethod {
29+
// Note: methods were re-ordered, new method added in the middle
30+
public func secondMethod() -> Int {
31+
return 2
32+
}
33+
34+
public func thirdMethod() -> Int {
35+
return 3
36+
}
37+
38+
public func firstMethod() -> Int {
39+
return 1
40+
}
41+
42+
public init() {}
43+
}
44+
45+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#if BEFORE
3+
4+
@_fixed_layout
5+
open class AddVirtualMethod {
6+
public init() {}
7+
8+
open func f1() -> Int {
9+
return 1
10+
}
11+
}
12+
13+
#else
14+
15+
@_fixed_layout
16+
open class AddVirtualMethod {
17+
public init() {}
18+
19+
open func f1() -> Int {
20+
return f2() + 1
21+
}
22+
23+
open func f2() -> Int {
24+
return 0
25+
}
26+
}
27+
28+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public func getVersion() -> Int {
2+
#if BEFORE
3+
return 0
4+
#else
5+
return 1
6+
#endif
7+
}
8+
9+
#if BEFORE
10+
@_fixed_layout
11+
open class Base {
12+
public init() {}
13+
open func firstMethod() -> Int {
14+
return 1
15+
}
16+
open func secondMethod() -> Int {
17+
return 2
18+
}
19+
open func callOverriddenMethods() -> Int {
20+
return firstMethod() * 10 + secondMethod()
21+
}
22+
}
23+
#else
24+
@_fixed_layout
25+
open class Base {
26+
public init() {}
27+
open func secondMethod() -> Int {
28+
return 2
29+
}
30+
open func firstMethod() -> Int {
31+
return 1
32+
}
33+
open func callOverriddenMethods() -> Int {
34+
return firstMethod() * 10 + secondMethod()
35+
}
36+
}
37+
#endif
38+
39+
@_fixed_layout
40+
public class Derived : Base {
41+
public override func firstMethod() -> Int {
42+
return 10
43+
}
44+
45+
public override func secondMethod() -> Int {
46+
return 20
47+
}
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-resilience-test
2+
// REQUIRES: executable_test
3+
4+
import StdlibUnittest
5+
import class_fixed_layout_add_virtual_method
6+
7+
8+
var ClassAddVirtualMethodTest = TestSuite("ClassAddVirtualMethod")
9+
10+
ClassAddVirtualMethodTest.test("ClassAddVirtualMethod") {
11+
let c = AddVirtualMethod()
12+
13+
do {
14+
expectEqual(1, c.firstMethod())
15+
expectEqual(2, c.secondMethod())
16+
}
17+
}
18+
19+
runAllTests()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %target-resilience-test
2+
// REQUIRES: executable_test
3+
4+
import StdlibUnittest
5+
import class_fixed_layout_add_virtual_method_subclass
6+
7+
8+
var ClassAddVirtualMethodSubclassTest = TestSuite("ClassAddVirtualMethodSubclass")
9+
10+
class AddVirtualMethodSubclass : AddVirtualMethod {
11+
func f3() -> Int {
12+
return f1() + 1
13+
}
14+
}
15+
16+
ClassAddVirtualMethodSubclassTest.test("AddVirtualMethod") {
17+
let t = AddVirtualMethodSubclass()
18+
19+
expectEqual(1, t.f1())
20+
expectEqual(2, t.f3())
21+
}
22+
23+
class AddVirtualMethodGenericSubclass<T> : AddVirtualMethod {
24+
func f3(_ t: T) -> [Int : T] {
25+
return [f1() : t]
26+
}
27+
}
28+
29+
ClassAddVirtualMethodSubclassTest.test("AddVirtualMethodGeneric") {
30+
let t = AddVirtualMethodGenericSubclass<String>()
31+
32+
expectEqual(1, t.f1())
33+
expectEqual([1 : "hi"], t.f3("hi"))
34+
}
35+
36+
runAllTests()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %target-resilience-test
2+
// REQUIRES: executable_test
3+
4+
import StdlibUnittest
5+
import class_fixed_layout_superclass_reorder_methods
6+
7+
8+
var SuperclassReorderMethodsTest = TestSuite("SuperclassReorderMethods")
9+
10+
SuperclassReorderMethodsTest.test("TestOverrides") {
11+
class MyDerived : Base {
12+
override func firstMethod() -> Int {
13+
return 3
14+
}
15+
override func secondMethod() -> Int {
16+
return 4
17+
}
18+
}
19+
20+
expectEqual(MyDerived().callOverriddenMethods(), 34)
21+
}
22+
23+
SuperclassReorderMethodsTest.test("TestSuper") {
24+
class MyDerived : Base {
25+
override func firstMethod() -> Int {
26+
return super.firstMethod() + 3
27+
}
28+
override func secondMethod() -> Int {
29+
return super.secondMethod() + 3
30+
}
31+
}
32+
33+
expectEqual(MyDerived().callOverriddenMethods(), 45)
34+
}
35+
36+
extension Derived {
37+
public func firstMethodExt() -> Int {
38+
return firstMethod() + super.firstMethod()
39+
}
40+
41+
public func secondMethodExt() -> Int {
42+
return secondMethod() + super.secondMethod()
43+
}
44+
}
45+
46+
SuperclassReorderMethodsTest.test("TestSuperExtension") {
47+
let obj = Derived()
48+
expectEqual(obj.firstMethodExt(), 11)
49+
expectEqual(obj.secondMethodExt(), 22)
50+
}
51+
52+
runAllTests()
53+

0 commit comments

Comments
 (0)