Skip to content

Commit 2eb7537

Browse files
committed
Add some test for static and virtual calls
1 parent 8472730 commit 2eb7537

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

  • usvm-ts/src/test

usvm-ts/src/test/kotlin/org/usvm/samples/Call.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,65 @@ class Call : TsMethodTestRunner() {
164164
{ r -> r.number == 30.0 }
165165
)
166166
}
167+
168+
@Disabled("Static calls are broken in IR")
169+
@Test
170+
fun `test static`() {
171+
val method = getMethod(className, "callStatic")
172+
discoverProperties<TsValue.TsNumber>(
173+
method = method,
174+
{ r -> r.number == 50.0 }
175+
)
176+
}
177+
178+
@Disabled("Inheritance is broken")
179+
@Test
180+
fun `test virtual call`() {
181+
val method = getMethod(className, "callVirtual")
182+
discoverProperties<TsValue.TsClass, TsValue.TsNumber>(
183+
method = method,
184+
{ obj, r ->
185+
when (obj.name) {
186+
"Parent" -> r.number == 100.0
187+
"Child" -> r.number == 200.0
188+
else -> false
189+
}
190+
},
191+
)
192+
}
193+
194+
@Test
195+
fun `test virtual parent`() {
196+
val method = getMethod(className, "callVirtualParent")
197+
discoverProperties<TsValue.TsNumber>(
198+
method = method,
199+
{ r -> r.number == 100.0 },
200+
)
201+
}
202+
203+
@Disabled("Calls to super are broken in IR")
204+
@Test
205+
fun `test virtual child`() {
206+
val method = getMethod(className, "callVirtualChild")
207+
discoverProperties<TsValue.TsNumber>(
208+
method = method,
209+
{ r -> r.number == 200.0 },
210+
)
211+
}
212+
213+
@Disabled("Too complex")
214+
@Test
215+
fun `test virtual dispatch`() {
216+
val method = getMethod(className, "virtualDispatch")
217+
discoverProperties<TsValue.TsClass, TsValue.TsNumber>(
218+
method = method,
219+
{ obj, r -> obj.name == "Parent" && r.number == 100.0 },
220+
{ obj, r -> obj.name == "Child" && r.number == 200.0 },
221+
invariants = arrayOf(
222+
{ _, r -> r.number != -1.0 },
223+
)
224+
)
225+
}
167226
}
168227

169228
fun fib(n: Double): Double {

usvm-ts/src/test/resources/samples/Call.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,37 @@ class Call {
9595
callNamespace(): number {
9696
return new N1.C().foo();
9797
}
98+
99+
static fifty(): number {
100+
return 50;
101+
}
102+
103+
callStatic(): number {
104+
return Call.fifty();
105+
}
106+
107+
callVirtual(obj: Parent): number {
108+
return obj.virtualMethod(); // 100 or 200
109+
}
110+
111+
callVirtualParent(): number {
112+
let obj: Parent = new Parent();
113+
return obj.virtualMethod(); // 100
114+
}
115+
116+
callVirtualChild(): number {
117+
let obj: Child = new Child();
118+
return obj.virtualMethod(); // 200
119+
}
120+
121+
virtualDispatch(obj: Parent): number {
122+
if (obj instanceof Child) {
123+
return obj.virtualMethod(); // 200
124+
} else if (obj instanceof Parent) {
125+
return obj.virtualMethod(); // 100
126+
}
127+
return -1; // unreachable
128+
}
98129
}
99130

100131
class A {
@@ -126,13 +157,17 @@ namespace N2 {
126157
}
127158

128159
class Parent {
160+
depth = 1;
161+
129162
virtualMethod(): number {
130-
return 1;
163+
return 100;
131164
}
132165
}
133166

134167
class Child extends Parent {
168+
override depth = 2;
169+
135170
override virtualMethod(): number {
136-
return 2;
171+
return 200;
137172
}
138173
}

0 commit comments

Comments
 (0)