Skip to content

Commit 13630eb

Browse files
committed
Revert "temporarily remove C# variants runtime test (bytecodealliance#963)"
This reverts commit 3ca7739. As of dotnet/runtimelab#2609, the "return pointer not aligned" issue which forced us to remove the C# `variants` runtime test case _should_ be resolved. 🤞 Signed-off-by: Joel Dice <[email protected]>
1 parent 7af0f81 commit 13630eb

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

tests/runtime/variants/wasm.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Diagnostics;
4+
using VariantsWorld.wit.imports.test.variants;
5+
6+
namespace VariantsWorld
7+
{
8+
9+
public class VariantsWorldImpl : IVariantsWorld
10+
{
11+
public static void TestImports()
12+
{
13+
Debug.Assert(TestInterop.RoundtripOption(new Option<float>(1.0f)).Value == 1);
14+
Debug.Assert(TestInterop.RoundtripOption(Option<float>.None).HasValue == false);
15+
Debug.Assert(TestInterop.RoundtripOption(new Option<float>(2.0f)).Value == 2);
16+
17+
Debug.Assert(TestInterop.RoundtripResult(Result<uint, float>.ok(2)).AsOk == 2.0);
18+
Debug.Assert(TestInterop.RoundtripResult(Result<uint, float>.ok(4)).AsOk == 4.0);
19+
Debug.Assert(TestInterop.RoundtripResult(Result<uint, float>.err(5.3f)).AsErr == 5);
20+
21+
Debug.Assert(TestInterop.RoundtripEnum(ITest.E1.A) == ITest.E1.A);
22+
Debug.Assert(TestInterop.RoundtripEnum(ITest.E1.B) == ITest.E1.B);
23+
24+
Debug.Assert(TestInterop.InvertBool(true) == false);
25+
Debug.Assert(TestInterop.InvertBool(false) == true);
26+
27+
var (a1, a2, a3, a4, a5, a6) =
28+
TestInterop.VariantCasts((ITest.C1.a(1), ITest.C2.a(2), ITest.C3.a(3), ITest.C4.a(4), ITest.C5.a(5), ITest.C6.a(6.0f)));
29+
Debug.Assert(a1.AsA == 1);
30+
Debug.Assert(a2.AsA == 2);
31+
Debug.Assert(a3.AsA == 3);
32+
Debug.Assert(a4.AsA == 4);
33+
Debug.Assert(a5.AsA == 5);
34+
Debug.Assert(a6.AsA == 6.0f);
35+
36+
var (b1, b2, b3, b4, b5, b6) =
37+
TestInterop.VariantCasts((ITest.C1.b(1), ITest.C2.b(2), ITest.C3.b(3), ITest.C4.b(4), ITest.C5.b(5), ITest.C6.b(6.0)));
38+
Debug.Assert(b1.AsB == 1);
39+
Debug.Assert(b2.AsB == 2.0f);
40+
Debug.Assert(b3.AsB == 3.0f);
41+
Debug.Assert(b4.AsB == 4.0f);
42+
Debug.Assert(b5.AsB == 5.0f);
43+
Debug.Assert(b6.AsB == 6.0);
44+
45+
var (za1, za2, za3, za4) =
46+
TestInterop.VariantZeros((ITest.Z1.a(1), ITest.Z2.a(2), ITest.Z3.a(3.0f), ITest.Z4.a(4.0f)));
47+
Debug.Assert(za1.AsA == 1);
48+
Debug.Assert(za2.AsA == 2);
49+
Debug.Assert(za3.AsA == 3.0f);
50+
Debug.Assert(za4.AsA == 4.0f);
51+
52+
var (zb1, zb2, zb3, zb4) =
53+
TestInterop.VariantZeros((ITest.Z1.b(), ITest.Z2.b(), ITest.Z3.b(), ITest.Z4.b()));
54+
//TODO: Add comparison operator to variants and None
55+
//Debug.Assert(zb1.AsB == ITest.Z1.b());
56+
//Debug.Assert(zb2.AsB == ITest.Z2.b());
57+
//Debug.Assert(zb3.AsB == ITest.Z3.b());
58+
//Debug.Assert(zb4.AsB == ITest.Z4.b());
59+
60+
TestInterop.VariantTypedefs(Option<uint>.None, false, Result<uint, None>.err(new None()));
61+
62+
var (a, b, c) = TestInterop.VariantEnums(true, Result<None, None>.ok(new None()), ITest.MyErrno.SUCCESS);
63+
Debug.Assert(a == false);
64+
var test = b.AsErr;
65+
Debug.Assert(c == ITest.MyErrno.A);
66+
}
67+
}
68+
}
69+
70+
namespace VariantsWorld.wit.exports.test.variants
71+
{
72+
public class TestImpl : ITest
73+
{
74+
public static Option<byte> RoundtripOption(Option<float> a)
75+
{
76+
return a.HasValue ? new Option<byte>((byte)a.Value) : Option<byte>.None;
77+
}
78+
79+
public static Result<double, byte> RoundtripResult(Result<uint, float> a)
80+
{
81+
switch (a.Tag)
82+
{
83+
case Result<double, byte>.OK: return Result<double, byte>.ok((double)a.AsOk);
84+
case Result<double, byte>.ERR: return Result<double, byte>.err((byte)a.AsErr);
85+
default: throw new ArgumentException();
86+
}
87+
}
88+
89+
public static ITest.E1 RoundtripEnum(ITest.E1 a)
90+
{
91+
return a;
92+
}
93+
94+
public static bool InvertBool(bool a)
95+
{
96+
return !a;
97+
}
98+
99+
public static (ITest.C1, ITest.C2, ITest.C3, ITest.C4, ITest.C5, ITest.C6)
100+
VariantCasts((ITest.C1, ITest.C2, ITest.C3, ITest.C4, ITest.C5, ITest.C6) a)
101+
{
102+
return a;
103+
}
104+
105+
public static (bool, Result<None, None>, ITest.MyErrno)
106+
VariantEnums(bool a, Result<None, None> b, ITest.MyErrno c)
107+
{
108+
return new(a, b, c);
109+
}
110+
111+
public static void VariantTypedefs(Option<uint> a, bool b, Result<uint, None> c) { }
112+
113+
public static (ITest.Z1, ITest.Z2, ITest.Z3, ITest.Z4) VariantZeros((ITest.Z1, ITest.Z2, ITest.Z3, ITest.Z4) a)
114+
{
115+
return a;
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)