Skip to content

Commit 4742ef0

Browse files
Migrate lists test to wit-bindgen test (#1221)
Signed-off-by: James Sturtevant <jsturtevant@gmail.com> Co-authored-by: Alex Crichton <alex@alexcrichton.com>
1 parent 1e2b279 commit 4742ef0

6 files changed

Lines changed: 326 additions & 521 deletions

File tree

tests/runtime-new/lists/runner.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Diagnostics;
4+
using RunnerWorld.wit.imports.test.lists;
5+
using System.Text;
6+
7+
8+
public class Program
9+
{
10+
public static void Main(string[] args)
11+
{
12+
ToTestInterop.EmptyListParam(new byte[0]);
13+
ToTestInterop.EmptyStringParam("");
14+
15+
{
16+
byte[] result = ToTestInterop.EmptyListResult();
17+
Debug.Assert(result.Length == 0);
18+
}
19+
20+
{
21+
string result = ToTestInterop.EmptyStringResult();
22+
Debug.Assert(result.Length == 0);
23+
}
24+
25+
ToTestInterop.ListParam(new byte[] { (byte)1, (byte)2, (byte)3, (byte)4 });
26+
ToTestInterop.ListParam((new byte[] { (byte)1, (byte)2, (byte)3, (byte)4 }).AsSpan());
27+
ToTestInterop.ListParam((new byte[] { (byte)1, (byte)2, (byte)3, (byte)4 }).AsMemory());
28+
ToTestInterop.ListParam2("foo");
29+
ToTestInterop.ListParam3(new List<String>() {
30+
"foo",
31+
"bar",
32+
"baz"
33+
});
34+
35+
ToTestInterop.ListParam4(new List<List<String>>() {
36+
new List<String>() {
37+
"foo",
38+
"bar"
39+
},
40+
new List<String>() {
41+
"baz"
42+
}
43+
});
44+
45+
List<string> randomStrings = new List<string>();
46+
for (int i = 0; i < 1000; i++)
47+
{
48+
randomStrings.Add(Guid.NewGuid().ToString());
49+
}
50+
ToTestInterop.ListParamLarge(randomStrings);
51+
52+
{
53+
byte[] result = ToTestInterop.ListResult();
54+
Debug.Assert(result.Length == 5);
55+
Debug.Assert(result[0] == (byte)1);
56+
Debug.Assert(result[1] == (byte)2);
57+
Debug.Assert(result[2] == (byte)3);
58+
Debug.Assert(result[3] == (byte)4);
59+
Debug.Assert(result[4] == (byte)5);
60+
}
61+
62+
{
63+
string result = ToTestInterop.ListResult2();
64+
Console.WriteLine(result);
65+
Debug.Assert(result == "hello!");
66+
}
67+
68+
{
69+
List<String> result = ToTestInterop.ListResult3();
70+
Debug.Assert(result.Count() == 2);
71+
Console.WriteLine(result[0]);
72+
Console.WriteLine(result[1]);
73+
Debug.Assert(result[0] == "hello,");
74+
Debug.Assert(result[1] == "world!");
75+
}
76+
77+
string[] strings = { "x", "", "hello", "hello ⚑ world" };
78+
foreach (string s in strings)
79+
{
80+
string result = ToTestInterop.StringRoundtrip(s);
81+
Debug.Assert(result == s);
82+
83+
byte[] bytes = Encoding.UTF8.GetBytes(s);
84+
Debug.Assert(bytes.SequenceEqual(ToTestInterop.ListRoundtrip(bytes)));
85+
}
86+
87+
{
88+
var (u, s) = ToTestInterop.ListMinmax8(
89+
new byte[] { byte.MinValue, byte.MaxValue },
90+
new sbyte[] { sbyte.MinValue, sbyte.MaxValue }
91+
);
92+
93+
Debug.Assert(u.Length == 2 && u[0] == byte.MinValue && u[1] == byte.MaxValue);
94+
Debug.Assert(s.Length == 2 && s[0] == sbyte.MinValue && s[1] == sbyte.MaxValue);
95+
}
96+
97+
{
98+
var (u, s) = ToTestInterop.ListMinmax16(
99+
new ushort[] { ushort.MinValue, ushort.MaxValue },
100+
new short[] { short.MinValue, short.MaxValue }
101+
);
102+
103+
Console.WriteLine(u[0]);
104+
Console.WriteLine(u[1]);
105+
Debug.Assert(u.Length == 2, $"u.Length {u.Length}");
106+
Debug.Assert(u[0] == ushort.MinValue, $"u[0] == {u[0]}");
107+
Debug.Assert(u[1] == ushort.MaxValue, $"u[1] == {u[1]}");
108+
109+
Debug.Assert(s.Length == 2);
110+
Console.WriteLine(s[0]);
111+
Console.WriteLine(s[1]);
112+
Debug.Assert(s.Length == 2 && s[0] == short.MinValue && s[1] == short.MaxValue);
113+
}
114+
115+
{
116+
var (u, s) = ToTestInterop.ListMinmax32(
117+
new uint[] { uint.MinValue, uint.MaxValue },
118+
new int[] { int.MinValue, int.MaxValue }
119+
);
120+
121+
Debug.Assert(u.Length == 2 && u[0] == uint.MinValue && u[1] == uint.MaxValue);
122+
Debug.Assert(s.Length == 2 && s[0] == int.MinValue && s[1] == int.MaxValue);
123+
}
124+
125+
{
126+
var (u, s) = ToTestInterop.ListMinmax64(
127+
new ulong[] { ulong.MinValue, ulong.MaxValue },
128+
new long[] { long.MinValue, long.MaxValue }
129+
);
130+
131+
Debug.Assert(u.Length == 2 && u[0] == ulong.MinValue && u[1] == ulong.MaxValue);
132+
133+
Debug.Assert(s.Length == 2 && s[0] == long.MinValue && s[1] == long.MaxValue);
134+
}
135+
136+
{
137+
var (u, s) = ToTestInterop.ListMinmaxFloat(
138+
new float[] {
139+
float.MinValue,
140+
float.MaxValue,
141+
float.NegativeInfinity,
142+
float.PositiveInfinity
143+
},
144+
new double[] {
145+
double.MinValue,
146+
double.MaxValue,
147+
double.NegativeInfinity,
148+
double.PositiveInfinity
149+
});
150+
151+
Debug.Assert(u.Length == 4
152+
&& u[0] == float.MinValue
153+
&& u[1] == float.MaxValue
154+
&& u[2] == float.NegativeInfinity
155+
&& u[3] == float.PositiveInfinity);
156+
157+
Debug.Assert(s.Length == 4
158+
&& s[0] == double.MinValue
159+
&& s[1] == double.MaxValue
160+
&& s[2] == double.NegativeInfinity
161+
&& s[3] == double.PositiveInfinity);
162+
}
163+
}
164+
}

tests/runtime-new/lists/test.cs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Diagnostics;
4+
using System.Text;
5+
6+
namespace TestWorld.wit.exports.test.lists
7+
{
8+
public class ToTestImpl : ITestWorld
9+
{
10+
11+
public static uint AllocatedBytes()
12+
{
13+
return 0;
14+
}
15+
16+
public static void EmptyListParam(byte[] a)
17+
{
18+
Debug.Assert(a.Length == 0);
19+
}
20+
21+
public static void EmptyStringParam(string a)
22+
{
23+
Debug.Assert(a.Length == 0);
24+
}
25+
26+
public static byte[] EmptyListResult()
27+
{
28+
return new byte[0];
29+
}
30+
31+
public static string EmptyStringResult()
32+
{
33+
return "";
34+
}
35+
36+
public static void ListParam(byte[] a)
37+
{
38+
Debug.Assert(a.Length == 4);
39+
Debug.Assert(a[0] == 1);
40+
Debug.Assert(a[1] == 2);
41+
Debug.Assert(a[2] == 3);
42+
Debug.Assert(a[3] == 4);
43+
}
44+
45+
public static void ListParam2(string a)
46+
{
47+
Debug.Assert(a.Equals("foo"));
48+
}
49+
50+
public static void ListParam3(List<String> a)
51+
{
52+
Debug.Assert(a.Count() == 3);
53+
Debug.Assert(a[0].Equals("foo"));
54+
Debug.Assert(a[1].Equals("bar"));
55+
Debug.Assert(a[2].Equals("baz"));
56+
}
57+
58+
public static void ListParam4(List<List<String>> a)
59+
{
60+
Debug.Assert(a.Count() == 2);
61+
Debug.Assert(a[0].Count() == 2);
62+
Debug.Assert(a[1].Count() == 1);
63+
64+
Debug.Assert(a[0][0].Equals("foo"));
65+
Debug.Assert(a[0][1].Equals("bar"));
66+
Debug.Assert(a[1][0].Equals("baz"));
67+
}
68+
69+
public static void ListParam5(List<(byte, uint, byte)> a)
70+
{
71+
Debug.Assert(a.Count() == 2);
72+
Debug.Assert(a[0].Item1 == 1);
73+
Debug.Assert(a[0].Item2 == 2);
74+
Debug.Assert(a[0].Item3 == 3);
75+
Debug.Assert(a[1].Item1 == 4);
76+
Debug.Assert(a[1].Item2 == 5);
77+
Debug.Assert(a[1].Item3 == 6);
78+
}
79+
80+
public static void ListParamLarge(List<String> a)
81+
{
82+
Debug.Assert(a.Count() == 1000);
83+
}
84+
85+
public static byte[] ListResult()
86+
{
87+
return new byte[] { (byte)1, (byte)2, (byte)3, (byte)4, (byte)5 };
88+
}
89+
90+
public static string ListResult2()
91+
{
92+
return "hello!";
93+
}
94+
95+
public static List<string> ListResult3()
96+
{
97+
return new List<string>() {
98+
"hello,",
99+
"world!"
100+
};
101+
}
102+
103+
public static byte[] ListRoundtrip(byte[] a)
104+
{
105+
return a;
106+
}
107+
108+
public static string stringRoundtrip(string a)
109+
{
110+
return a;
111+
}
112+
113+
public static (byte[], byte[]) ListMinmax8(byte[] a, byte[] b)
114+
{
115+
return new(a, b);
116+
}
117+
118+
public static (short[], short[]) ListMinmax16(short[] a, short[] b)
119+
{
120+
return new(a, b);
121+
}
122+
123+
public static (int[], int[]) ListMinmax32(int[] a, int[] b)
124+
{
125+
return new(a, b);
126+
}
127+
128+
public static (long[], long[]) ListMinmax64(long[] a, long[] b)
129+
{
130+
return new(a, b);
131+
}
132+
133+
public static (float[], double[]) ListMinmaxFloat(float[] a, double[] b)
134+
{
135+
return new(a, b);
136+
}
137+
public static (byte[], sbyte[]) ListMinmax8(byte[] a, sbyte[] b)
138+
{
139+
return (a, b);
140+
}
141+
142+
public static (ushort[], short[]) ListMinmax16(ushort[] a, short[] b)
143+
{
144+
return (a, b);
145+
}
146+
147+
public static (uint[], int[]) ListMinmax32(uint[] a, int[] b)
148+
{
149+
return (a, b);
150+
}
151+
152+
public static (ulong[], long[]) ListMinmax64(ulong[] a, long[] b)
153+
{
154+
return (a, b);
155+
}
156+
157+
public static string StringRoundtrip(string a)
158+
{
159+
return a;
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)