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