暫無描述
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.

060-TestEnums.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using NUnit.Framework;
  3. using UnityBenchShared;
  4. namespace Burst.Compiler.IL.Tests
  5. {
  6. /// <summary>
  7. /// Test with enums.
  8. /// </summary>
  9. internal partial class TestEnums
  10. {
  11. [System.Flags]
  12. public enum MyEnum
  13. {
  14. Hello = 5,
  15. Something = 10
  16. }
  17. [TestCompiler]
  18. public static int test_enum_cast_to_int()
  19. {
  20. MyEnum value = MyEnum.Hello;
  21. return (int)value;
  22. }
  23. [TestCompiler(MyEnum.Hello)]
  24. [TestCompiler(MyEnum.Something)]
  25. public static int test_enum_compare(MyEnum value)
  26. {
  27. if (value == MyEnum.Hello)
  28. return 0;
  29. else
  30. return 1;
  31. }
  32. //[TestCompiler(typeof(StructContainingEnumProvider))]
  33. //public static int test_enum_in_struct(ref StructContainingEnum myStruct)
  34. //{
  35. // return myStruct.intValue + (int)myStruct.value;
  36. //}
  37. [TestCompiler(MyEnum.Hello)]
  38. [TestCompiler(MyEnum.Something)]
  39. [Ignore("Failure")]
  40. public static int test_enum_has_flag(MyEnum value)
  41. {
  42. return value.HasFlag(MyEnum.Hello) ? 3 : 4;
  43. }
  44. [TestCompiler(MyEnum.Hello)]
  45. [TestCompiler(MyEnum.Something)]
  46. public static int test_enum_and_mask(MyEnum value)
  47. {
  48. return (value & MyEnum.Hello) != 0 ? 3 : 4;
  49. }
  50. [TestCompiler(1)]
  51. [TestCompiler(2)]
  52. public static int TestEnumSwitchCase(IntPtr value)
  53. {
  54. var enumValue = (SmallEnum) value.ToInt32();
  55. // Need at least 3 cases to generate a proper switch
  56. // otherwise Roslyn will generate an if/else
  57. switch (enumValue)
  58. {
  59. case SmallEnum.One:
  60. return 7;
  61. case SmallEnum.Two:
  62. return 8;
  63. case SmallEnum.Three:
  64. return 9;
  65. default:
  66. return 10;
  67. }
  68. }
  69. private static int GetToInt32(int value)
  70. {
  71. return value;
  72. }
  73. [TestCompiler]
  74. public static int test_enum_sizeof_small_enum()
  75. {
  76. return sizeof(SmallEnum);
  77. }
  78. [TestCompiler(SmallEnum.Three)]
  79. public static int test_enum_sizeof_small_enum_in_struct_access(SmallEnum value)
  80. {
  81. var s = new MySmallEnumStruct
  82. {
  83. a = value,
  84. b = value,
  85. c = value
  86. };
  87. return (int)s.a + (int)s.b + (int)s.c;
  88. }
  89. public struct StructContainingEnum
  90. {
  91. public MyEnum value;
  92. public int intValue;
  93. }
  94. public enum SmallEnum : byte
  95. {
  96. One,
  97. Two,
  98. Three
  99. }
  100. public struct MySmallEnumStruct
  101. {
  102. public SmallEnum a;
  103. public SmallEnum b;
  104. public SmallEnum c;
  105. public SmallEnum d;
  106. }
  107. public enum SomeByteEnum : byte
  108. {
  109. First = 0,
  110. Last = 255
  111. }
  112. public unsafe struct FixedByte4Struct
  113. {
  114. fixed byte bytes[4];
  115. public SomeByteEnum this[SomeByteEnum index]
  116. {
  117. get { return (SomeByteEnum)bytes[(int)index]; }
  118. }
  119. public struct Provider : IArgumentProvider
  120. {
  121. public object Value => new FixedByte4Struct { };
  122. }
  123. }
  124. [TestCompiler(typeof(FixedByte4Struct.Provider))]
  125. public static SomeByteEnum test_enum_indexer(ref FixedByte4Struct bytes)
  126. {
  127. return bytes[0];
  128. }
  129. }
  130. }