Skip to content

Commit 6591730

Browse files
Start on resource-related tests
1 parent 35f9a93 commit 6591730

8 files changed

Lines changed: 312 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import wit.test.resource_borrow.runner;
2+
import wit.common;
3+
4+
@witExport("$root", "run")
5+
void run() {
6+
auto thing = Thing.makeNew(42);
7+
scope(exit) thing.witDrop;
8+
9+
assert(foo(thing) == 42 + 1 + 2);
10+
}
11+
12+
alias Exports = wit.test.resource_borrow.runner.Exports!(
13+
run
14+
);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import wit.test.resource_borrow.test;
2+
import wit.common;
3+
4+
@witExport("test:resource-borrow/to-test", "thing")
5+
struct ThingImpl {
6+
uint val;
7+
8+
@witExport("test:resource-borrow/to-test", "[constructor]thing")
9+
static Thing constructor(uint v) {
10+
return Thing.makeNew((out typeof(this) self) {
11+
self.val = v + 1;
12+
});
13+
}
14+
}
15+
16+
@witExport("test:resource-borrow/to-test", "foo")
17+
uint foo(Thing.Borrow v) {
18+
return v.rep!ThingImpl.val + 2;
19+
}
20+
21+
alias Exports = wit.test.resource_borrow.test.Exports!(
22+
ThingImpl,
23+
foo
24+
);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import wit.test.resource_import_and_export.intermediate;
2+
import wit.test.resource_import_and_export.test.imports : ThingImport = Thing;
3+
import wit.test.resource_import_and_export.test.exports : ThingExport = Thing;
4+
5+
import wit.common;
6+
7+
@witExport("test:resource-import-and-export/test", "thing")
8+
struct ThingImpl {
9+
ThingImport thing;
10+
11+
@witExport("test:resource-import-and-export/test", "[constructor]thing")
12+
static ThingExport constructor(uint v) {
13+
return ThingExport.makeNew((out typeof(this) self) {
14+
self.thing = ThingImport.makeNew(v + 1);
15+
});
16+
}
17+
18+
@witExport("test:resource-import-and-export/test", "[method]thing.foo")
19+
uint foo() {
20+
return thing.foo + 2;
21+
}
22+
23+
@witExport("test:resource-import-and-export/test", "[method]thing.bar")
24+
void bar(uint v) {
25+
thing.bar(v + 3);
26+
}
27+
28+
@witExport("test:resource-import-and-export/test", "[static]thing.baz")
29+
static ThingExport baz(ThingExport a, ThingExport b) {
30+
scope(exit) {
31+
a.witDrop;
32+
b.witDrop;
33+
}
34+
35+
auto aRep = a.rep!ThingImpl;
36+
auto bRep = b.rep!ThingImpl;
37+
38+
auto result = ThingImport.baz(
39+
aRep.thing,
40+
bRep.thing
41+
);
42+
aRep.thing = Thing.init; // consumed by `baz`
43+
bRep.thing = Thing.init; // consumed by `baz`
44+
scope(exit) result.witDrop;
45+
46+
return ThingImpl.constructor(result.foo + 4);
47+
}
48+
}
49+
50+
@witExport("$root", "toplevel-export")
51+
ThingImport toplevelExport(ThingImport input) {
52+
// `input` not dropped b/c ownership transferred
53+
// to `toplevelImport`
54+
55+
return toplevelImport(input);
56+
}
57+
58+
alias Exports = wit.test.resource_import_and_export.intermediate.Exports!(
59+
ThingImpl,
60+
toplevelExport
61+
);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module leaf_thing;
2+
3+
import wit.test.resource_import_and_export.leaf_thing;
4+
5+
import wit.common;
6+
7+
@witExport("test:resource-import-and-export/test", "thing")
8+
struct ThingImpl {
9+
uint val;
10+
11+
@witExport("test:resource-import-and-export/test", "[constructor]thing")
12+
static Thing constructor(uint v) {
13+
return Thing.makeNew((out typeof(this) self) {
14+
self.val = v + 1;
15+
});
16+
}
17+
18+
@witExport("test:resource-import-and-export/test", "[method]thing.foo")
19+
uint foo() {
20+
return val + 2;
21+
}
22+
23+
@witExport("test:resource-import-and-export/test", "[method]thing.bar")
24+
void bar(uint v) {
25+
val = v + 3;
26+
}
27+
28+
@witExport("test:resource-import-and-export/test", "[static]thing.baz")
29+
static Thing baz(Thing a, Thing b) {
30+
return ThingImpl.constructor(
31+
a.rep!ThingImpl.foo + b.rep!ThingImpl.foo + 4
32+
);
33+
}
34+
}
35+
36+
alias Exports = wit.test.resource_import_and_export.leaf_thing.Exports!(
37+
ThingImpl
38+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module leaf_toplevel;
2+
3+
import wit.test.resource_import_and_export.leaf_toplevel;
4+
5+
import wit.common;
6+
7+
@witExport("$root", "toplevel-export")
8+
Thing toplevelExport(Thing input) {
9+
// `input` not dropped b/c ownership transferred
10+
// via return
11+
12+
return input;
13+
}
14+
15+
alias Exports = wit.test.resource_import_and_export.leaf_toplevel.Exports!(
16+
toplevelExport
17+
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import wit.test.resource_import_and_export.runner;
2+
import wit.common;
3+
4+
@witExport("$root", "run")
5+
void run() {
6+
auto thing1 = Thing.makeNew(42);
7+
scope(exit) thing1.witDrop;
8+
9+
// 42 + 1 (constructor) + 1 (constructor) + 2 (foo) + 2 (foo)
10+
assert(thing1.foo == 48);
11+
12+
// 33 + 3 (bar) + 3 (bar) + 2 (foo) + 2 (foo)
13+
thing1.bar(33);
14+
assert(thing1.foo() == 43);
15+
16+
auto thing2 = Thing.makeNew(81);
17+
scope(exit) thing2.witDrop;
18+
19+
auto thing3 = Thing.baz(thing1, thing2);
20+
thing1 = Thing.init; // thing1 consumed by `baz`
21+
thing2 = Thing.init; // thing2 consumed by `baz`
22+
scope(exit) thing3.witDrop;
23+
}
24+
25+
alias Exports = wit.test.resource_import_and_export.runner.Exports!(
26+
run
27+
);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import wit.test.resource_aggregates.runner;
2+
import wit.common;
3+
4+
@witExport("$root", "run")
5+
void run() {
6+
auto r2Thing = Thing.makeNew(1);
7+
scope(exit) r2Thing.witDrop;
8+
9+
auto r3Thing1 = Thing.makeNew(2);
10+
scope(exit) r3Thing1.witDrop;
11+
12+
auto t2Thing = Thing.makeNew(6);
13+
scope(exit) t2Thing.witDrop;
14+
15+
auto v2Thing = Thing.makeNew(8);
16+
scope(exit) v2Thing.witDrop;
17+
18+
auto l2Thing1 = Thing.makeNew(11);
19+
scope(exit) l2Thing1.witDrop;
20+
21+
auto l2Thing2 = Thing.makeNew(12);
22+
scope(exit) l2Thing2.witDrop;
23+
24+
auto o2Thing = Thing.makeNew(14);
25+
scope(exit) o2Thing.witDrop;
26+
27+
auto result2Thing = Thing.makeNew(16);
28+
scope(exit) result2Thing.witDrop;
29+
30+
immutable Thing[2] l1Elems = [
31+
Thing.makeNew(9),
32+
Thing.makeNew(10),
33+
];
34+
35+
immutable Thing.Borrow[2] l2Elems = [
36+
l2Thing1,
37+
l2Thing2,
38+
];
39+
40+
assert(foo(
41+
R1(thing: Thing.makeNew(0)),
42+
R2(thing: r2Thing),
43+
R3(
44+
thing1: r3Thing1,
45+
thing2: Thing.makeNew(3)
46+
),
47+
tuple(
48+
Thing.makeNew(4),
49+
R1(thing: Thing.makeNew(5))
50+
),
51+
tuple(t2Thing.borrow),
52+
V1.thing(Thing.makeNew(7)),
53+
V2.thing(v2Thing),
54+
l1Elems.witList,
55+
l2Elems.witList,
56+
Thing.makeNew(13).some,
57+
o2Thing.borrow.some,
58+
Thing.makeNew(15).ok!void,
59+
result2Thing.borrow.ok!void
60+
) == 156);
61+
}
62+
63+
alias Exports = wit.test.resource_aggregates.runner.Exports!(
64+
run
65+
);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import wit.test.resource_aggregates.test;
2+
3+
import wit.common;
4+
import std.algorithm.iteration : sum, map;
5+
6+
@witExport("test:resource-aggregates/to-test", "thing")
7+
struct ThingImpl {
8+
uint val;
9+
10+
@witExport("test:resource-aggregates/to-test", "[constructor]thing")
11+
static Thing constructor(uint v) {
12+
return Thing.makeNew((out typeof(this) self) {
13+
self.val = v + 1;
14+
});
15+
}
16+
}
17+
18+
@witExport("test:resource-aggregates/to-test", "foo")
19+
uint foo(
20+
scope ref R1 r1, scope ref R2 r2, scope ref R3 r3,
21+
scope ref T1 t1, scope ref T2 t2,
22+
scope ref V1 v1, scope ref V2 v2,
23+
scope ref L1 l1, scope ref L2 l2,
24+
scope ref Option!Thing o1, scope ref Option!(Thing.Borrow) o2,
25+
scope ref Result!(Thing, void) result1, scope ref Result!(Thing.Borrow, void) result2
26+
) {
27+
scope(exit) {
28+
r1.witDrop;
29+
r2.witDrop;
30+
r3.witDrop;
31+
t1.witDrop;
32+
t2.witDrop;
33+
v1.witDrop;
34+
v2.witDrop;
35+
l1.witDrop;
36+
l2.witDrop;
37+
o1.witDrop;
38+
o2.witDrop;
39+
result1.witDrop;
40+
result2.witDrop;
41+
}
42+
43+
return (
44+
r1.thing.rep!ThingImpl.val
45+
+ r2.thing.rep!ThingImpl.val
46+
+ r3.thing1.rep!ThingImpl.val
47+
+ r3.thing2.rep!ThingImpl.val
48+
+ t1[0].rep!ThingImpl.val
49+
+ t1[1].thing.rep!ThingImpl.val
50+
+ t2[0].rep!ThingImpl.val
51+
+ v1.getThing.rep!ThingImpl.val
52+
+ v2.getThing.rep!ThingImpl.val
53+
+ l1[].map!((a) => a.rep!ThingImpl.val).sum
54+
+ l2[].map!((a) => a.rep!ThingImpl.val).sum
55+
+ o1.unwrap.rep!ThingImpl.val
56+
+ o2.unwrap.rep!ThingImpl.val
57+
+ result1.unwrap.rep!ThingImpl.val
58+
+ result2.unwrap.rep!ThingImpl.val
59+
+ 3
60+
);
61+
}
62+
63+
alias Exports = wit.test.resource_aggregates.test.Exports!(
64+
ThingImpl,
65+
foo
66+
);

0 commit comments

Comments
 (0)