PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 284 lines | 217 code | 49 blank | 18 comment | 28 complexity | 3cae96a571a9109cf84261c3be7672ed MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, MIT, MPL-2.0-no-copyleft-exception
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. using System;
  5. using System.Reflection;
  6. using Microsoft.SPOT.Platform.Test;
  7. using System.Collections;
  8. namespace Microsoft.SPOT.Platform.Tests
  9. {
  10. public class InitLocalsTests : IMFTestInterface
  11. {
  12. [SetUp]
  13. public InitializeResult Initialize()
  14. {
  15. Log.Comment("Adding set up for the tests.");
  16. // Add your functionality here.
  17. Log.Comment("These tests test local variable initializations");
  18. return InitializeResult.ReadyToGo;
  19. }
  20. [TearDown]
  21. public void CleanUp()
  22. {
  23. Log.Comment("Cleaning up after the tests");
  24. }
  25. public interface IEmptyInterface
  26. {
  27. }
  28. class TestObj : IEmptyInterface
  29. {
  30. public int Field1;
  31. public void Method1(int i)
  32. {
  33. }
  34. public TestObj()
  35. {
  36. Field1 = 3;
  37. }
  38. public TestObj(int field)
  39. {
  40. Field1 = field;
  41. }
  42. private bool Method2(float f)
  43. {
  44. return true;
  45. }
  46. }
  47. [TestMethod]
  48. public MFTestResults SystemType1_GetType_Test()
  49. {
  50. bool fRes = true;
  51. int i = 0;
  52. Log.Comment("Checking reflection types to assure that they are boxed when used as local variables");
  53. Log.Comment("and when they are assigned to non-valuetype containers (which used to crash)");
  54. try
  55. {
  56. ArrayList list = new ArrayList();
  57. // First lets check all reflection types (Type, AppDomain, Assembly, FieldInfo, MethodInfo,
  58. // ConstructorInfo)
  59. // NOTE: We add the reflection items to the ArrayList to assure that they can be properly
  60. // assigned to a object container (this used to lead to a access violation)
  61. Type type = typeof(int);
  62. list.Add(type);
  63. string name = ((Type)list[i]).Name;
  64. fRes &= name == "Int32";
  65. i++;
  66. AppDomain domain = AppDomain.CurrentDomain;
  67. list.Add(domain);
  68. name = ((AppDomain)list[i]).FriendlyName;
  69. fRes &= name.ToLower() == "default";
  70. i++;
  71. Assembly asm = domain.GetAssemblies()[0];
  72. list.Add(asm);
  73. name = ((Assembly)list[i]).GetName().Name;
  74. fRes &= name.ToLower() == "mscorlib";
  75. i++;
  76. type = Type.GetType("Microsoft.SPOT.Platform.Tests.InitLocalsTests+TestObj");
  77. list.Add(type);
  78. name = ((Type)list[i]).Name;
  79. fRes &= name == "TestObj";
  80. i++;
  81. Type iface = type.GetInterfaces()[0];
  82. list.Add(iface);
  83. name = ((Type)list[i]).Name;
  84. fRes &= name == "IEmptyInterface";
  85. fRes &= iface.Name == "IEmptyInterface";
  86. i++;
  87. FieldInfo fi = type.GetField("Field1");
  88. list.Add(fi);
  89. name = ((FieldInfo)list[i]).Name;
  90. fRes &= name == "Field1";
  91. fRes &= fi.Name == "Field1";
  92. i++;
  93. MethodInfo mi = type.GetMethod("Method1");
  94. list.Add(mi);
  95. name = ((MethodInfo)list[i]).Name;
  96. fRes &= name == "Method1";
  97. fRes &= mi.Name == "Method1";
  98. i++;
  99. ConstructorInfo ci = type.GetConstructor(new Type[] { });
  100. list.Add(ci);
  101. name = ((ConstructorInfo)list[i]).Name;
  102. fRes &= name == ".ctor";
  103. fRes &= ci.Name == ".ctor";
  104. i++;
  105. //
  106. // Now test arrays of reflection types
  107. //
  108. Type[] types = new Type[] { typeof(int), typeof(bool), Type.GetType("Microsoft.SPOT.Platform.Tests.InitLocalsTests+TestObj") };
  109. list.Add(types[2]);
  110. name = ((Type)list[i]).Name;
  111. fRes &= name == "TestObj";
  112. fRes &= types[2].Name == "TestObj";
  113. i++;
  114. AppDomain[] domains = new AppDomain[] { AppDomain.CurrentDomain, AppDomain.CreateDomain("blah") };
  115. list.Add(domains[1]);
  116. name = ((AppDomain)list[i]).FriendlyName;
  117. fRes &= name == "blah";
  118. fRes &= domains[1].FriendlyName == "blah";
  119. AppDomain.Unload(domains[1]);
  120. i++;
  121. Assembly[] asms = new Assembly[] { typeof(InitLocalsTests).Assembly, domains[0].GetAssemblies()[0] };
  122. list.Add(asms[0]);
  123. name = ((Assembly)list[i]).GetName().Name;
  124. fRes &= name == "Microsoft.SPOT.Platform.Tests.Systemlib2";
  125. fRes &= asms[0].GetName().Name == "Microsoft.SPOT.Platform.Tests.Systemlib2";
  126. i++;
  127. FieldInfo[] fis = new FieldInfo[] { types[2].GetField("Field1"), type.GetFields()[0] };
  128. list.Add(fis[0]);
  129. name = ((FieldInfo)list[i]).Name;
  130. fRes &= name == "Field1";
  131. fRes &= fis[0].Name == "Field1";
  132. i++;
  133. MethodInfo[] mis = new MethodInfo[] { type.GetMethods()[2], types[2].GetMethod("Method2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) };
  134. list.Add(mis[1]);
  135. name = ((MethodInfo)list[i]).Name;
  136. fRes &= name == "Method2";
  137. fRes &= mis[1].Name == "Method2";
  138. i++;
  139. ConstructorInfo[] cis = new ConstructorInfo[] { types[2].GetConstructor(new Type[] { }), typeof(TestObj).GetConstructor(new Type[] {typeof(int)}) };
  140. list.Add(cis[0]);
  141. name = ((ConstructorInfo)list[i]).Name;
  142. fRes &= name == ".ctor";
  143. fRes &= cis[0].Name == ".ctor";
  144. i++;
  145. Array ar = Array.CreateInstance(typeof(Type), 3);
  146. ((IList)ar)[0] = typeof(Type);
  147. ((IList)ar)[1] = Type.GetType("System.Collections.ArrayList");
  148. list.Add(ar.GetValue(1));
  149. name = ((Type)list[i]).Name;
  150. fRes &= name == "ArrayList";
  151. fRes &= ((Type)((IList)ar)[0]).Name == "Type";
  152. fRes &= ((Type)ar.GetValue(1)).Name == "ArrayList";
  153. i++;
  154. list.Clear();
  155. Debug.GC(true);
  156. }
  157. catch(Exception e)
  158. {
  159. Log.Exception("Unhandled Exception: ", e);
  160. }
  161. return fRes ? MFTestResults.Pass : MFTestResults.Fail;
  162. }
  163. [TestMethod]
  164. public MFTestResults SystemType1_StructValueTypeReturn_Test()
  165. {
  166. bool fRes = true;
  167. // make sure return values are properly boxed
  168. try
  169. {
  170. Guid g = ReturnGuid();
  171. Debug.Print(g.ToString());
  172. }
  173. catch
  174. {
  175. fRes = false;
  176. }
  177. return fRes ? MFTestResults.Pass : MFTestResults.Fail;
  178. }
  179. [TestMethod]
  180. public MFTestResults SystemType1_StructArrayInit_Test()
  181. {
  182. bool fRes = true;
  183. // make sure arrays are initialized properly for struct value types (Guids in this case).
  184. try
  185. {
  186. Guid[] data = new Guid[] { ReturnGuid(), new Guid(), Guid.NewGuid(), new Guid(344, 45, 24, 24, 4, 42, 42, 4, 44, 22, 4) };
  187. foreach (Guid g2 in data)
  188. {
  189. Debug.Print(g2.ToString());
  190. // test to make sure boxing (to call struct method)
  191. // still works properly
  192. if (Guid.Empty.Equals(g2))
  193. {
  194. fRes &= true;
  195. }
  196. }
  197. }
  198. catch
  199. {
  200. fRes = false;
  201. }
  202. return fRes ? MFTestResults.Pass : MFTestResults.Fail;
  203. }
  204. [TestMethod]
  205. public MFTestResults SystemType1_ArrayListToArrayForStruct_Test()
  206. {
  207. bool fRes = true;
  208. // make sure boxing of struct value type (Guid) is handled properly
  209. // this test was a result of a bug found by a customer.
  210. try
  211. {
  212. Guid ret = new Guid();
  213. ArrayList guidList = new ArrayList();
  214. guidList.Add(Guid.NewGuid());
  215. guidList.Add(Guid.NewGuid());
  216. Guid[] guidArray = (Guid[])guidList.ToArray(typeof(Guid));
  217. foreach (Guid g in guidArray)
  218. {
  219. Debug.Print(g.ToString());
  220. ret = g;
  221. }
  222. }
  223. catch
  224. {
  225. fRes = false;
  226. }
  227. return fRes ? MFTestResults.Pass : MFTestResults.Fail;
  228. }
  229. //--- internal test methods ---//
  230. Guid ReturnGuid()
  231. {
  232. return Guid.NewGuid();
  233. }
  234. }
  235. }