Skip to content

Commit 0478bc7

Browse files
runtime tests up through lists
1 parent 381eef1 commit 0478bc7

8 files changed

Lines changed: 763 additions & 8 deletions

File tree

tests/runtime/fixed-length-lists/test.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ void listParam3(in int[20] a) {
1919

2020
@witExport("test:fixed-length-lists/to-test", "list-minmax16")
2121
Tuple!(ushort[4], short[4]) listMinmax16(in ushort[4] a, in short[4] b) {
22-
return Tuple!(ushort[4], short[4])(a, b);
22+
return tuple(a, b);
2323
}
2424

2525

2626
@witExport("test:fixed-length-lists/to-test", "list-minmax-float")
2727
Tuple!(float[2], double[2]) listMinmaxFloat(in float[2] a, in double[2] b) {
28-
return Tuple!(float[2], double[2])(a, b);
28+
return tuple(a, b);
2929
}
3030

3131
@witExport("test:fixed-length-lists/to-test", "list-roundtrip")
@@ -36,12 +36,12 @@ ubyte[8] listResult() => ['0', '1', 'A', 'B', 'a', 'b', 128, 255];
3636

3737
@witExport("test:fixed-length-lists/to-test", "nested-roundtrip")
3838
Tuple!(uint[2][2], int[2][2]) nestedRoundtrip(in uint[2][2] a, in int[2][2] b) {
39-
return Tuple!(uint[2][2], int[2][2])(a, b);
39+
return tuple(a, b);
4040
}
4141

4242
@witExport("test:fixed-length-lists/to-test", "large-roundtrip")
4343
Tuple!(uint[2][2], int[4][4]) largeRoundtrip(in uint[2][2] a, in int[4][4] b) {
44-
return Tuple!(uint[2][2], int[4][4])(a, b);
44+
return tuple(a, b);
4545
}
4646

4747
@witExport("test:fixed-length-lists/to-test", "nightmare-on-cpp")

tests/runtime/flavorful/test.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ Tuple!(ListTypedef2, ListTypedef3) listTypedefs(in ListTypedef a, in ListTypedef
6363
cast(WitString)"typedef4".witList
6464
];
6565

66-
return (const Tuple!(ListTypedef2, ListTypedef3)(
66+
return tuple(
6767
(cast(immutable ubyte[])"typedef3").witList,
6868
strings[].witList
69-
)).witClone;
69+
).witClone;
7070
}
7171

7272

@@ -85,11 +85,11 @@ Tuple!(WitList!bool, WitList!(Result!()), WitList!MyErrno) listOfVariants(in Wit
8585
static immutable bool[] boolsOut = [false, true];
8686
static immutable Result!(void)[] resultsOut = [Result!().err, Result!().ok];
8787
static immutable MyErrno[] enumsOut = [MyErrno.a, MyErrno.b];
88-
return (const Tuple!(WitList!bool, WitList!(Result!()), WitList!MyErrno)(
88+
return tuple(
8989
boolsOut.witList,
9090
resultsOut.witList,
9191
enumsOut.witList
92-
)).witClone;
92+
).witClone;
9393
}
9494

9595
alias Exports = wit.test.flavorful.test.Exports!(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ args = '--features y'
2+
3+
import wit.foo.bar.runner;
4+
import wit.common;
5+
6+
@witExport("$root", "run")
7+
void run() {
8+
y();
9+
z();
10+
}
11+
12+
alias Exports = wit.foo.bar.runner.Exports!(
13+
run
14+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ args = '--features y'
2+
3+
import wit.foo.bar.test;
4+
import wit.common;
5+
6+
@witExport("foo:bar/bindings@1.2.3", "y")
7+
void y() {}
8+
9+
@witExport("foo:bar/bindings@1.2.3", "z")
10+
void z() {}
11+
12+
alias Exports = wit.foo.bar.test.Exports!(
13+
y,
14+
z
15+
);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import wit.test.list_in_variant.runner;
2+
import wit.common;
3+
4+
@witExport("$root", "run")
5+
void run() {
6+
const WitString[2] hw = ["hello".witList, "world".witList];
7+
{
8+
auto result = listInOption(some(hw[].witList));
9+
scope(exit) result.witFree;
10+
11+
assert(result == "hello,world");
12+
}
13+
{
14+
auto result = listInOption(none!(WitList!WitString));
15+
scope(exit) result.witFree;
16+
17+
assert(result == "none");
18+
}
19+
20+
const WitString[3] fbb_data = ["foo".witList, "bar".witList, "baz".witList];
21+
auto fbb = PayloadOrEmpty.withData(fbb_data.witList);
22+
{
23+
auto result = listInVariant(fbb);
24+
scope(exit) result.witFree;
25+
26+
assert(result == "foo,bar,baz");
27+
}
28+
{
29+
auto result = listInVariant(PayloadOrEmpty.empty);
30+
scope(exit) result.witFree;
31+
32+
assert(result == "empty");
33+
}
34+
35+
const WitString[3] abc = ["a".witList, "b".witList, "c".witList];
36+
{
37+
auto result = listInResult(Result!(WitList!WitString, WitString).ok(abc[].witList));
38+
scope(exit) result.witFree;
39+
40+
assert(result == "a,b,c");
41+
}
42+
{
43+
auto result = listInResult(Result!(WitList!WitString, WitString).err("oops".witList));
44+
scope(exit) result.witFree;
45+
46+
assert(result == "err:oops");
47+
}
48+
49+
const WitString[2] hw2 = ["hello".witList, "world".witList];
50+
auto s1 = listInOptionWithReturn(some(hw2.witList));
51+
{
52+
auto result = s1.count;
53+
scope(exit) result.witFree;
54+
55+
assert(result == 2);
56+
}
57+
{
58+
auto result = s1.label;
59+
scope(exit) result.witFree;
60+
61+
assert(result == "hello,world");
62+
}
63+
auto s2 = listInOptionWithReturn(none!(WitList!WitString));
64+
{
65+
auto result = s2.count;
66+
scope(exit) result.witFree;
67+
68+
assert(result == 0);
69+
}
70+
{
71+
auto result = s2.label;
72+
scope(exit) result.witFree;
73+
74+
assert(result == "none");
75+
}
76+
77+
const WitString[3] xyz = ["x".witList, "y".witList, "z".witList];
78+
{
79+
auto result = topLevelList(xyz.witList);
80+
scope(exit) result.witFree;
81+
82+
assert(result == "x,y,z");
83+
}
84+
}
85+
86+
alias Exports = wit.test.list_in_variant.runner.Exports!(
87+
run
88+
);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import wit.test.list_in_variant.test;
2+
import wit.common;
3+
4+
// Allocates directly with `malloc`, so no witClone needed.
5+
extern(C) void* malloc(size_t size);
6+
7+
char[] commaJoin(in WitString[] strs) {
8+
if (strs.length == 0) return null;
9+
10+
size_t total = 0;
11+
foreach (i, str; strs) {
12+
total += str.length;
13+
14+
if (i+1 != strs.length) {
15+
total += 1; // comma
16+
}
17+
}
18+
19+
void* ptr = malloc(total);
20+
assert(ptr);
21+
char[] chars = cast(char[])ptr[0..total];
22+
23+
size_t cursor = 0;
24+
foreach (i, str; strs) {
25+
foreach (chr; str) {
26+
chars[cursor++] = chr;
27+
}
28+
29+
if (i+1 != strs.length) {
30+
chars[cursor++] = ',';
31+
}
32+
}
33+
34+
return chars;
35+
}
36+
37+
@witExport("test:list-in-variant/to-test", "list-in-option")
38+
WitString listInOption(in Option!(WitList!WitString) data) {
39+
if (data.isSome) {
40+
return data.unwrap.commaJoin.witList; // no clone
41+
}
42+
return "none".witList.witClone;
43+
}
44+
45+
@witExport("test:list-in-variant/to-test", "list-in-variant")
46+
WitString listInVariant(in PayloadOrEmpty data) {
47+
if (data.isWithData) {
48+
return data.getWithData.commaJoin.witList; // no clone
49+
}
50+
return "empty".witList.witClone;
51+
}
52+
53+
@witExport("test:list-in-variant/to-test", "list-in-result")
54+
WitString listInResult(in Result!(WitList!WitString, WitString) data) {
55+
if (data.isOk) {
56+
return data.unwrap.commaJoin.witList;
57+
}
58+
59+
60+
auto errStr = data.unwrapErr;
61+
void* ptr = malloc(errStr.length+4);
62+
assert(ptr);
63+
char[] chars = cast(char[])ptr[0..errStr.length+4];
64+
65+
chars[0..4] = "err:";
66+
foreach (i, ref chr; chars[4..$]) {
67+
chr = errStr[i];
68+
}
69+
70+
return chars.witList; // no clone
71+
}
72+
73+
@witExport("test:list-in-variant/to-test", "list-in-option-with-return")
74+
Summary listInOptionWithReturn(in Option!(WitList!WitString) data) {
75+
if (data.isSome) {
76+
auto items = data.unwrap();
77+
return Summary(items.length, items.commaJoin.witList); // no clone
78+
}
79+
80+
return Summary(0, "none".witList.witClone);
81+
}
82+
83+
@witExport("test:list-in-variant/to-test", "top-level-list")
84+
WitString topLevelList(in WitList!WitString data) {
85+
return data.commaJoin.witList; // no clone
86+
}
87+
88+
alias Exports = wit.test.list_in_variant.test.Exports!(
89+
listInOption,
90+
listInVariant,
91+
listInResult,
92+
listInOptionWithReturn,
93+
topLevelList
94+
);

0 commit comments

Comments
 (0)