netmf /client_v4_3_SDKR2/Test/Platform/Tests/CLR/mscorlib/systemlib/systemlib2/InitLocalsTests.cs

Language C# Lines 284
MD5 Hash 3cae96a571a9109cf84261c3be7672ed Estimated Cost $5,458 (why?)
Repository https://netmf.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Microsoft Corporation.  All rights reserved.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Reflection;
using Microsoft.SPOT.Platform.Test;
using System.Collections;


namespace Microsoft.SPOT.Platform.Tests
{
    public class InitLocalsTests : IMFTestInterface
    {
        [SetUp]
        public InitializeResult Initialize()
        {
            Log.Comment("Adding set up for the tests.");
            // Add your functionality here.   

            Log.Comment("These tests test local variable initializations");

            return InitializeResult.ReadyToGo;
        }

        [TearDown]
        public void CleanUp()
        {
            Log.Comment("Cleaning up after the tests");
        }

        public interface IEmptyInterface
        {
        }

        class TestObj : IEmptyInterface
        {
            public int Field1;
            public void Method1(int i)
            {
            }
            public TestObj()
            {
                Field1 = 3;
            }

            public TestObj(int field)
            {
                Field1 = field;
            }

            private bool Method2(float f)
            {
                return true;
            }
        }

        [TestMethod]
        public MFTestResults SystemType1_GetType_Test()
        {
            bool fRes = true;
            int i = 0;

            Log.Comment("Checking reflection types to assure that they are boxed when used as local variables");
            Log.Comment("and when they are assigned to non-valuetype containers (which used to crash)");

            try
            {
                ArrayList list = new ArrayList();

                // First lets check all reflection types (Type, AppDomain, Assembly, FieldInfo, MethodInfo, 
                // ConstructorInfo)
                // NOTE: We add the reflection items to the ArrayList to assure that they can be properly 
                // assigned to a object container (this used to lead to a access violation)
                Type type = typeof(int);
                list.Add(type);
                string name = ((Type)list[i]).Name;
                fRes &= name == "Int32";
                i++;

                AppDomain domain = AppDomain.CurrentDomain;
                list.Add(domain);
                name = ((AppDomain)list[i]).FriendlyName;
                fRes &= name.ToLower() == "default";
                i++;

                Assembly asm = domain.GetAssemblies()[0];
                list.Add(asm);
                name = ((Assembly)list[i]).GetName().Name;
                fRes &= name.ToLower() == "mscorlib";
                i++;

                type = Type.GetType("Microsoft.SPOT.Platform.Tests.InitLocalsTests+TestObj");
                list.Add(type);
                name = ((Type)list[i]).Name;
                fRes &= name == "TestObj";
                i++;

                Type iface = type.GetInterfaces()[0];
                list.Add(iface);
                name = ((Type)list[i]).Name;
                fRes &= name == "IEmptyInterface";
                fRes &= iface.Name == "IEmptyInterface";
                i++;

                FieldInfo fi = type.GetField("Field1");
                list.Add(fi);
                name = ((FieldInfo)list[i]).Name;
                fRes &= name == "Field1";
                fRes &= fi.Name == "Field1";
                i++;

                MethodInfo mi = type.GetMethod("Method1");
                list.Add(mi);
                name = ((MethodInfo)list[i]).Name;
                fRes &= name == "Method1";
                fRes &= mi.Name == "Method1";
                i++;

                ConstructorInfo ci = type.GetConstructor(new Type[] { });
                list.Add(ci);
                name = ((ConstructorInfo)list[i]).Name;
                fRes &= name == ".ctor";
                fRes &= ci.Name == ".ctor";
                i++;

                // 
                // Now test arrays of reflection types
                // 
                Type[] types = new Type[] { typeof(int), typeof(bool), Type.GetType("Microsoft.SPOT.Platform.Tests.InitLocalsTests+TestObj") };
                list.Add(types[2]);
                name = ((Type)list[i]).Name;
                fRes &= name == "TestObj";
                fRes &= types[2].Name == "TestObj";
                i++;

                AppDomain[] domains = new AppDomain[] { AppDomain.CurrentDomain, AppDomain.CreateDomain("blah") };
                list.Add(domains[1]);
                name = ((AppDomain)list[i]).FriendlyName;
                fRes &= name == "blah";
                fRes &= domains[1].FriendlyName == "blah";
                AppDomain.Unload(domains[1]);
                i++;

                Assembly[] asms = new Assembly[] { typeof(InitLocalsTests).Assembly, domains[0].GetAssemblies()[0] };
                list.Add(asms[0]);
                name = ((Assembly)list[i]).GetName().Name;
                fRes &= name == "Microsoft.SPOT.Platform.Tests.Systemlib2";
                fRes &= asms[0].GetName().Name == "Microsoft.SPOT.Platform.Tests.Systemlib2";
                i++;

                FieldInfo[] fis = new FieldInfo[] { types[2].GetField("Field1"), type.GetFields()[0] };
                list.Add(fis[0]);
                name = ((FieldInfo)list[i]).Name;
                fRes &= name == "Field1";
                fRes &= fis[0].Name == "Field1";
                i++;

                MethodInfo[] mis = new MethodInfo[] { type.GetMethods()[2], types[2].GetMethod("Method2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) };
                list.Add(mis[1]);
                name = ((MethodInfo)list[i]).Name;
                fRes &= name == "Method2";
                fRes &= mis[1].Name == "Method2";
                i++;

                ConstructorInfo[] cis = new ConstructorInfo[] { types[2].GetConstructor(new Type[] { }), typeof(TestObj).GetConstructor(new Type[] {typeof(int)}) };
                list.Add(cis[0]);
                name = ((ConstructorInfo)list[i]).Name;
                fRes &= name == ".ctor";
                fRes &= cis[0].Name == ".ctor";
                i++;

                Array ar = Array.CreateInstance(typeof(Type), 3);
                ((IList)ar)[0] = typeof(Type);
                ((IList)ar)[1] = Type.GetType("System.Collections.ArrayList");

                list.Add(ar.GetValue(1));
                name = ((Type)list[i]).Name;
                fRes &= name == "ArrayList";
                fRes &= ((Type)((IList)ar)[0]).Name == "Type";
                fRes &= ((Type)ar.GetValue(1)).Name == "ArrayList";
                i++;

                list.Clear();
                Debug.GC(true);
            }
            catch(Exception e)
            {
                Log.Exception("Unhandled Exception: ", e);
            }


            return fRes ? MFTestResults.Pass : MFTestResults.Fail;
        }

        [TestMethod]
        public MFTestResults SystemType1_StructValueTypeReturn_Test()
        {
            bool fRes = true;

            // make sure return values are properly boxed
            try
            {
                Guid g = ReturnGuid();

                Debug.Print(g.ToString());
            }
            catch
            {
                fRes = false;
            }

            return fRes ? MFTestResults.Pass : MFTestResults.Fail;
        }

        [TestMethod]
        public MFTestResults SystemType1_StructArrayInit_Test()
        {
            bool fRes = true;

            // make sure arrays are initialized properly for struct value types (Guids in this case).
            try
            {
                Guid[] data = new Guid[] { ReturnGuid(), new Guid(), Guid.NewGuid(), new Guid(344, 45, 24, 24, 4, 42, 42, 4, 44, 22, 4) };

                foreach (Guid g2 in data)
                {
                    Debug.Print(g2.ToString());
                    
                    // test to make sure boxing (to call struct method)
                    // still works properly
                    if (Guid.Empty.Equals(g2))
                    {
                        fRes &= true;
                    }
                }
            }
            catch
            {
                fRes = false;
            }

            return fRes ? MFTestResults.Pass : MFTestResults.Fail;
        }

        [TestMethod]
        public MFTestResults SystemType1_ArrayListToArrayForStruct_Test()
        {
            bool fRes = true;

            // make sure boxing of struct value type (Guid) is handled properly
            // this test was a result of a bug found by a customer.
            try
            {
                Guid ret = new Guid();
                ArrayList guidList = new ArrayList();
                guidList.Add(Guid.NewGuid());
                guidList.Add(Guid.NewGuid());
                Guid[] guidArray = (Guid[])guidList.ToArray(typeof(Guid));

                foreach (Guid g in guidArray)
                {
                    Debug.Print(g.ToString());

                    ret = g;
                }
            }
            catch
            {
                fRes = false;
            }

            return fRes ? MFTestResults.Pass : MFTestResults.Fail;
        }

        //--- internal test methods ---//

        Guid ReturnGuid()
        {
            return Guid.NewGuid();
        }

    }
}
Back to Top