forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_protocol_add_requirements.swift
147 lines (114 loc) · 2.99 KB
/
test_protocol_add_requirements.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
// RUN: %target-resilience-test --no-backward-deployment
// REQUIRES: executable_test
import StdlibUnittest
import protocol_add_requirements
var ProtocolAddRequirementsTest = TestSuite("ProtocolAddRequirements")
struct Halogen : ElementProtocol {
var x: Int
func increment() -> Halogen {
return Halogen(x: x + 1)
}
}
func ==(h1: Halogen, h2: Halogen) -> Bool {
return h1.x == h2.x
}
struct AddMethods : AddMethodsProtocol {
func importantOperation() -> Halogen {
return Halogen(x: 0)
}
#if AFTER
func unimportantOperation() -> Halogen {
return Halogen(x: 10)
}
#endif
}
ProtocolAddRequirementsTest.test("AddMethodRequirements") {
let result = doSomething(AddMethods())
#if BEFORE
let expected = [0, 1, 2].map(Halogen.init)
#else
let expected = [0, 10, 11].map(Halogen.init)
#endif
expectEqual(result, expected)
}
struct AddConstructors : AddConstructorsProtocol, Equatable {
var name: String
init(name: String) {
self.name = name
}
}
func ==(a1: AddConstructors, a2: AddConstructors) -> Bool {
return a1.name == a2.name
}
ProtocolAddRequirementsTest.test("AddConstructorsProtocol") {
// Would be nice if [T?] was Equatable...
if getVersion() == 0 {
let result = testConstructorProtocol(AddConstructors.self)
expectEqual(result.count, 1)
expectEqual(result[0], AddConstructors(name: "Puff"))
} else {
let result = testConstructorProtocol(AddConstructors.self)
expectEqual(result.count, 3)
expectEqual(result[0], AddConstructors(name: "Meow meow"))
expectEqual(result[1], nil)
expectEqual(result[2], AddConstructors(name: "Robster the Lobster"))
}
}
class Box<T> {
var value: T
init(value: T) {
self.value = value
}
}
struct AddProperties : AddPropertiesProtocol {
var speedBox: Box<Int> = Box<Int>(value: 160)
var gearBox: Box<Int> = Box<Int>(value: 8000)
var topSpeed: Int {
get {
return speedBox.value
}
nonmutating set {
speedBox.value = newValue
}
}
var maxRPM: Int {
get {
return gearBox.value
}
set {
gearBox.value = newValue
}
}
}
ProtocolAddRequirementsTest.test("AddPropertyRequirements") {
var x = AddProperties()
do {
let expected = (getVersion() == 0
? [160, 8000]
: [160, 8000, 80, 40, 6000])
expectEqual(getProperties(&x), expected)
}
setProperties(&x)
do {
let expected = (getVersion() == 0
? [320, 15000]
: [320, 15000, 160, 80, 13000])
expectEqual(getProperties(&x), expected)
}
}
struct AddSubscript<Key : Hashable, Value> : AddSubscriptProtocol {
var dict: [Key : Value] = [:]
func get(key key: Key) -> Value {
return dict[key]!
}
mutating func set(key key: Key, value: Value) {
dict[key] = value
}
}
ProtocolAddRequirementsTest.test("AddSubscriptRequirements") {
var t = AddSubscript<String, Int>()
t.set(key: "B", value: 20)
doSomething(&t, k1: "A", k2: "B")
expectEqual(t.get(key: "A"), 20)
}
runAllTests()