Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

032-Patterns.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // These tests require 2020.2 because that enabled C# 8.0.
  2. #if BURST_TESTS_ONLY || UNITY_2020_2_OR_NEWER
  3. namespace Burst.Compiler.IL.Tests.Shared
  4. {
  5. public class Patterns
  6. {
  7. [TestCompiler(2)]
  8. [TestCompiler(1)]
  9. [TestCompiler(0)]
  10. public static int PropertyPattern(int x)
  11. {
  12. var point = new Point { X = x, Y = 5 };
  13. return point switch
  14. {
  15. { X: 2 } => 10,
  16. { X: 1 } => 5,
  17. _ => 0
  18. };
  19. }
  20. private struct Point
  21. {
  22. public int X;
  23. public int Y;
  24. }
  25. [TestCompiler(1, 2)]
  26. [TestCompiler(2, 4)]
  27. [TestCompiler(0, 0)]
  28. public static int TuplePattern(int x, int y)
  29. {
  30. return (x, y) switch
  31. {
  32. (1, 2) => 10,
  33. (2, 4) => 5,
  34. _ => 0
  35. };
  36. }
  37. private struct DeconstructablePoint
  38. {
  39. public int X;
  40. public int Y;
  41. public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
  42. }
  43. [TestCompiler(1, -1)]
  44. [TestCompiler(-1, 1)]
  45. [TestCompiler(1, 1)]
  46. [TestCompiler(-1, -1)]
  47. public static int PositionalPattern(int pointX, int pointY)
  48. {
  49. var point = new DeconstructablePoint { X = pointX, Y = pointY };
  50. return point switch
  51. {
  52. (0, 0) => 0,
  53. var (x, y) when x > 0 && y > 0 => 1,
  54. var (x, y) when x < 0 && y > 0 => 2,
  55. var (x, y) when x < 0 && y < 0 => 3,
  56. var (x, y) when x > 0 && y < 0 => 4,
  57. var (_, _) => 5
  58. };
  59. }
  60. }
  61. }
  62. #endif