Skip to content

Commit 038f9a2

Browse files
committed
Swift: Split type inference tests into multiple files
1 parent 4042bbe commit 038f9a2

7 files changed

Lines changed: 1359 additions & 1372 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var topLevelDecl : Int = 0
2+
0
3+
topLevelDecl + 1 // $ type=topLevelDecl:Int
4+
5+
class C {
6+
var myInt : Int
7+
// C.init
8+
init(n: Int) {
9+
myInt = n // $ type=n:Int
10+
}
11+
12+
// C.getMyInt
13+
func getMyInt() -> Int {
14+
return myInt // $ type=.myInt:Int
15+
}
16+
}
17+
18+
class Derived : C {
19+
// Derived.init
20+
init() {
21+
super.init(n: 0) // $ type=super:C target=C.init
22+
}
23+
24+
// Derived.callGetMyInt
25+
func callGetMyInt() -> Int {
26+
let x = getMyInt(); // $ type=x:Int target=C.getMyInt
27+
return x
28+
}
29+
}
30+
31+
class Generic<T> {
32+
var value : T
33+
// Generic.init
34+
init(v: T) {
35+
value = v // $ type=v:T
36+
}
37+
38+
// Generic.getValue
39+
func getValue() -> T {
40+
return value // $ type=.value:T
41+
}
42+
}
43+
44+
class GenericDerived : Generic<Int> {
45+
// GenericDerived.init
46+
init() {
47+
super.init(v: 0) // $ type=super@Generic<T>:Int target=Generic.init
48+
}
49+
}
50+
51+
func testGeneric() {
52+
let g = Generic(v: 42) // $ type=g@Generic<T>:Int target=Generic.init
53+
let x = g.getValue() // $ type=x:Int target=Generic.getValue
54+
55+
let gd = GenericDerived() // $ type=gd:GenericDerived target=GenericDerived.init
56+
let y = gd.getValue() // $ type=y:Int target=Generic.getValue
57+
}
58+
59+
// --- Extensions ---
60+
61+
extension C {
62+
// C.doubled
63+
func doubled() -> Int {
64+
return myInt * 2 // $ type=.myInt:Int
65+
}
66+
}
67+
68+
func testExtension() {
69+
let obj = C(n: 10) // $ target=C.init
70+
let d = obj.doubled() // $ type=d:Int target=C.doubled
71+
}
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// --- Static methods ---
2+
3+
class MathUtils {
4+
// MathUtils.square
5+
static func square(x: Int) -> Int {
6+
return x * x // $ type=x:Int
7+
}
8+
9+
// MathUtils.cube
10+
class func cube(x: Int) -> Int {
11+
return x * x * x // $ type=x:Int
12+
}
13+
}
14+
15+
func testStaticMethods() {
16+
let s = MathUtils.square(x: 4) // $ type=s:Int target=MathUtils.square
17+
let cu = MathUtils.cube(x: 3) // $ type=cu:Int target=MathUtils.cube
18+
}
19+
20+
// --- Simple overloading ---
21+
22+
class Overloaded {
23+
// Overloaded.process(_:Int)
24+
func process(_ x: Int) -> Int {
25+
return x + 1 // $ type=x:Int
26+
}
27+
28+
// Overloaded.process(_:String)
29+
func process(_ s: String) -> String {
30+
return s // $ type=s:String
31+
}
32+
}
33+
34+
func testOverloading() {
35+
let o = Overloaded() // $ target=init()
36+
let r1 = o.process(42) // $ type=r1:Int target=Overloaded.process(_:Int)
37+
let r2 = o.process("hello") // $ type=r2:String target=Overloaded.process(_:String)
38+
}
39+
40+
// --- Structs and methods ---
41+
42+
struct Matrix {
43+
var data : [[Int]]
44+
45+
// Matrix.init
46+
init(data: [[Int]]) {
47+
self.data = data
48+
}
49+
50+
// Matrix.rowCount
51+
func rowCount() -> Int {
52+
return data.count
53+
}
54+
}
55+
56+
func testSubscripts() {
57+
let m = Matrix(data: [[1, 2], [3, 4]]) // $ target=Matrix.init
58+
let rc = m.rowCount() // $ type=rc:Int target=Matrix.rowCount
59+
}
60+
61+
// --- Nested types ---
62+
63+
class Outer {
64+
class Inner {
65+
var value : Int
66+
67+
// Outer.Inner.init
68+
init(value: Int) {
69+
self.value = value // $ type=value:Int
70+
}
71+
72+
// Outer.Inner.getValue
73+
func getValue() -> Int {
74+
return self.value // $ type=.value:Int
75+
}
76+
}
77+
}
78+
79+
func testNestedTypes() {
80+
let inner = Outer.Inner(value: 99) // $ type=inner:Inner target=Outer.Inner.init
81+
let v = inner.getValue() // $ type=v:Int target=Outer.Inner.getValue
82+
}
83+
84+
// --- Method chaining ---
85+
86+
class Builder {
87+
var value : Int = 0
88+
89+
// Builder.init
90+
init() {}
91+
92+
// Builder.set
93+
func set(_ v: Int) -> Builder {
94+
value = v
95+
return self
96+
}
97+
98+
// Builder.add
99+
func add(_ v: Int) -> Builder {
100+
value += v
101+
return self
102+
}
103+
104+
// Builder.build
105+
func build() -> Int {
106+
return value // $ type=.value:Int
107+
}
108+
}
109+
110+
func testChaining() {
111+
let b = Builder() // $ type=b:Builder target=Builder.init
112+
let result = b.set(10).add(5).build() // $ type=result:Int target=Builder.set target=Builder.add target=Builder.build
113+
}
114+
115+
// --- Default parameter values ---
116+
117+
class Config {
118+
// Config.init
119+
init() {}
120+
121+
// Config.setup
122+
func setup(retries: Int = 3, timeout: Double = 30.0) -> Int {
123+
return retries // $ type=retries:Int
124+
}
125+
}
126+
127+
func testDefaultParams() {
128+
let cfg = Config() // $ type=cfg:Config target=Config.init
129+
let r1 = cfg.setup() // $ type=r1:Int target=Config.setup
130+
let r2 = cfg.setup(retries: 5) // $ type=r2:Int target=Config.setup
131+
let r3 = cfg.setup(retries: 2, timeout: 60.0) // $ type=r3:Int target=Config.setup
132+
}
133+
134+
// --- Computed properties accessed via methods ---
135+
136+
class Temperature {
137+
var celsius : Double
138+
139+
// Temperature.init
140+
init(celsius: Double) {
141+
self.celsius = celsius // $ type=celsius:Double
142+
}
143+
144+
// Temperature.toCelsius
145+
func toCelsius() -> Double {
146+
return celsius // $ type=.celsius:Double
147+
}
148+
149+
// Temperature.toFahrenheit
150+
func toFahrenheit() -> Double {
151+
return celsius * 9.0 / 5.0 + 32.0 // $ type=.celsius:Double
152+
}
153+
}
154+
155+
func testTemperature() {
156+
let t = Temperature(celsius: 100.0) // $ type=t:Temperature target=Temperature.init
157+
let c = t.toCelsius() // $ type=c:Double target=Temperature.toCelsius
158+
let f = t.toFahrenheit() // $ type=f:Double target=Temperature.toFahrenheit
159+
}
160+
161+
// --- Inheritance with overriding ---
162+
163+
class Animal {
164+
// Animal.init
165+
init() {}
166+
167+
// Animal.speak
168+
func speak() -> String {
169+
return "..."
170+
}
171+
}
172+
173+
class Dog : Animal {
174+
// Dog.init
175+
override init() {
176+
super.init() // $ target=Animal.init
177+
}
178+
179+
// Dog.speak
180+
override func speak() -> String {
181+
return "Woof"
182+
}
183+
184+
// Dog.fetch
185+
func fetch() -> String {
186+
return "ball"
187+
}
188+
}
189+
190+
class Cat : Animal {
191+
// Cat.init
192+
override init() {
193+
super.init() // $ target=Animal.init
194+
}
195+
196+
// Cat.speak
197+
override func speak() -> String {
198+
return "Meow"
199+
}
200+
}
201+
202+
func testOverriding() {
203+
let d = Dog() // $ type=d:Dog target=Dog.init
204+
let ds = d.speak() // $ type=ds:String target=Dog.speak
205+
let df = d.fetch() // $ type=df:String target=Dog.fetch
206+
207+
let ct = Cat() // $ type=ct:Cat target=Cat.init
208+
let cs = ct.speak() // $ type=cs:String target=Cat.speak
209+
}
210+
211+
// --- Mutating methods on structs ---
212+
213+
struct Counter {
214+
var count : Int = 0
215+
216+
// Counter.increment
217+
mutating func increment() {
218+
count += 1
219+
}
220+
221+
// Counter.getCount
222+
func getCount() -> Int {
223+
return count // $ type=.count:Int
224+
}
225+
}
226+
227+
func testMutating() {
228+
var ctr = Counter() // $ type=ctr:Counter target=init()
229+
ctr.increment() // $ target=Counter.increment
230+
let val = ctr.getCount() // $ type=val:Int target=Counter.getCount
231+
}

0 commit comments

Comments
 (0)