Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/ModuleBuilder/ModuleBuilderDefineUninitializedData.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Collections.Generic;
6 using Xunit;
7
8 namespace System.Reflection.Emit.Tests
9 {
10 public class ModuleBuilderDefineUninitializedData
11 {
12 private const int ReservedMaskFieldAttribute = 0x9500; // This constant maps to FieldAttributes.ReservedMask that is not available in the contract.
13
14 public static IEnumerable<object[]> Attributes_TestData()
15 {
16 yield return new object[] { FieldAttributes.Assembly };
17 yield return new object[] { FieldAttributes.FamANDAssem };
18 yield return new object[] { FieldAttributes.Family };
19 yield return new object[] { FieldAttributes.FamORAssem };
20 yield return new object[] { FieldAttributes.FieldAccessMask };
21 yield return new object[] { FieldAttributes.HasDefault };
22 yield return new object[] { FieldAttributes.HasFieldMarshal };
23 yield return new object[] { FieldAttributes.HasFieldRVA };
24 yield return new object[] { FieldAttributes.InitOnly };
25 yield return new object[] { FieldAttributes.Literal };
26 yield return new object[] { FieldAttributes.NotSerialized };
27 yield return new object[] { FieldAttributes.PinvokeImpl };
28 yield return new object[] { FieldAttributes.Private };
29 yield return new object[] { FieldAttributes.PrivateScope };
30 yield return new object[] { FieldAttributes.Public };
31 yield return new object[] { FieldAttributes.RTSpecialName };
32 yield return new object[] { FieldAttributes.SpecialName };
33 yield return new object[] { FieldAttributes.Static };
34 }
35
36 [Theory]
37 [MemberData(nameof(Attributes_TestData))]
38 public void DefineUnitializedData(FieldAttributes attributes)
39 {
40 ModuleBuilder module = Helpers.DynamicModule();
41 foreach (int size in new int[] { 1, 2, 0x003f0000 - 1 })
42 {
43 FieldBuilder field = module.DefineUninitializedData(size.ToString(), size, attributes);
44
45 int expectedAttributes = ((int)attributes | (int)FieldAttributes.Static) & ~ReservedMaskFieldAttribute;
46 Assert.Equal(size.ToString(), field.Name);
47 Assert.Equal((FieldAttributes)expectedAttributes, field.Attributes);
48 }
49 }
50
51 [Theory]
52 [MemberData(nameof(Attributes_TestData))]
53 public void DefineUnitializedData_EmptyName_ThrowsArgumentException(FieldAttributes attributes)
54 {
55 ModuleBuilder module = Helpers.DynamicModule();
56 AssertExtensions.Throws<ArgumentException>("name", () => module.DefineUninitializedData("", 1, attributes));
57 }
58
59 [Theory]
60 [MemberData(nameof(Attributes_TestData))]
61 public void DefineUnitializedData_InvalidSize_ThrowsArgumentException(FieldAttributes attributes)
62 {
63 ModuleBuilder module = Helpers.DynamicModule();
64 foreach (int size in new int[] { -1, 0, 0x003f0000, 0x003f0000 + 1 })
65 {
66 AssertExtensions.Throws<ArgumentException>(null, () => module.DefineUninitializedData("TestField", size, attributes));
67 }
68 }
69
70 [Theory]
71 [MemberData(nameof(Attributes_TestData))]
72 public void DefineUnitializedData_NullName_ThrowsArgumentNullException(FieldAttributes attributes)
73 {
74 ModuleBuilder module = Helpers.DynamicModule();
75 AssertExtensions.Throws<ArgumentNullException>("name", () => module.DefineUninitializedData(null, 1, attributes));
76 }
77
78 [Theory]
79 [MemberData(nameof(Attributes_TestData))]
80 public void DefineUninitalizedData_CreateGlobalFunctionsAlreadyCalled_ThrowsInvalidOperationException(FieldAttributes attributes)
81 {
82 ModuleBuilder module = Helpers.DynamicModule();
83 module.CreateGlobalFunctions();
84
85 Assert.Throws<InvalidOperationException>(() => module.DefineUninitializedData("TestField", 1, attributes));
86 }
87 }
88 }