Skip to content

Commit

Permalink
Add suggested test
Browse files Browse the repository at this point in the history
  • Loading branch information
alrz committed Dec 9, 2022
1 parent df09a7a commit 238631e
Showing 1 changed file with 133 additions and 3 deletions.
136 changes: 133 additions & 3 deletions src/Compilers/CSharp/Test/Emit2/Semantics/PatternMatchingTests5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3155,13 +3155,27 @@ public void IsExpression_SwitchDispatch_Numeric()
var source = """
class C
{
public static void Main()
{
System.Console.Write(
Test(0) == false
& Test(1)
& Test(2)
& Test(3)
& Test(4)
& Test(5)
& Test(6)
& Test(7)
& Test(8)
);
}
public static bool Test(int a)
{
return (a is 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8);
}
}
""";
var compilation = CompileAndVerify(source);
var compilation = CompileAndVerify(source, expectedOutput: "True");
compilation.VerifyIL("C.Test", """
{
// Code size 14 (0xe)
Expand All @@ -3182,20 +3196,136 @@ .locals init (bool V_0)
}
""");
}


[Fact, WorkItem(45679, "https://github.com/dotnet/roslyn/issues/45679")]
public void IsExpression_SwitchDispatch_SwitchIL()
{
var source = """
class C
{
public static void Main()
{
System.Console.Write(
Test(1, null) == false
& Test(1, default(int))
& Test(2, default(bool))
& Test(3, default(double))
& Test(4, default(long))
& Test(5, default(long)) == false
& Test(100, null) == false
& Test(100, default(uint))
& Test(101, default(ulong))
& Test(102, default(float))
& Test(103, default(short))
& Test(104, default(short)) == false
);
}
public static bool Test(int a, object b)
{
return (a, b) is
(1, int) or
(2, bool) or
(3, double) or
(4, long) or
(100, uint) or
(101, ulong) or
(102, float) or
(103, short);
}
}
""";
var compilation = CompileAndVerify(source, expectedOutput: "True");
compilation.VerifyIL("C.Test", """
{
// Code size 137 (0x89)
.maxstack 2
.locals init (bool V_0)
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: sub
IL_0003: switch (
IL_0033,
IL_003d,
IL_0047,
IL_0051)
IL_0018: ldarg.0
IL_0019: ldc.i4.s 100
IL_001b: sub
IL_001c: switch (
IL_005b,
IL_0065,
IL_006f,
IL_0079)
IL_0031: br.s IL_0085
IL_0033: ldarg.1
IL_0034: isinst "int"
IL_0039: brtrue.s IL_0081
IL_003b: br.s IL_0085
IL_003d: ldarg.1
IL_003e: isinst "bool"
IL_0043: brtrue.s IL_0081
IL_0045: br.s IL_0085
IL_0047: ldarg.1
IL_0048: isinst "double"
IL_004d: brtrue.s IL_0081
IL_004f: br.s IL_0085
IL_0051: ldarg.1
IL_0052: isinst "long"
IL_0057: brtrue.s IL_0081
IL_0059: br.s IL_0085
IL_005b: ldarg.1
IL_005c: isinst "uint"
IL_0061: brtrue.s IL_0081
IL_0063: br.s IL_0085
IL_0065: ldarg.1
IL_0066: isinst "ulong"
IL_006b: brtrue.s IL_0081
IL_006d: br.s IL_0085
IL_006f: ldarg.1
IL_0070: isinst "float"
IL_0075: brtrue.s IL_0081
IL_0077: br.s IL_0085
IL_0079: ldarg.1
IL_007a: isinst "short"
IL_007f: brfalse.s IL_0085
IL_0081: ldc.i4.1
IL_0082: stloc.0
IL_0083: br.s IL_0087
IL_0085: ldc.i4.0
IL_0086: stloc.0
IL_0087: ldloc.0
IL_0088: ret
}
""");
}

[Fact, WorkItem(45679, "https://github.com/dotnet/roslyn/issues/45679")]
public void IsExpression_SwitchDispatch_String()
{
var source = """
class C
{
public static void Main()
{
System.Console.Write(
Test("0") == false
& Test("1")
& Test("2")
& Test("3")
& Test("4")
& Test("5")
& Test("6")
& Test("7")
& Test("8")
);
}
public static bool Test(string a)
{
return (a is "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8");
}
}
""";
var compilation = CompileAndVerify(source);
var compilation = CompileAndVerify(source, expectedOutput: "True");
compilation.VerifyIL("C.Test", """
{
// Code size 244 (0xf4)
Expand Down

0 comments on commit 238631e

Please sign in to comment.