PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp/csharp_prepost_runme.cs

#
C# | 107 lines | 88 code | 13 blank | 6 comment | 20 complexity | a9db7d364c2a8a71fc5579dc1bcf26fb MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. using System;
  2. using System.Reflection;
  3. using csharp_prepostNamespace;
  4. public class csharp_prepost_runme {
  5. public static void Main() {
  6. {
  7. double[] v;
  8. csharp_prepost.globalfunction(out v);
  9. Assert(v.Length, 3);
  10. Assert(v[0], 0.0);
  11. Assert(v[1], 1.1);
  12. Assert(v[2], 2.2);
  13. }
  14. {
  15. double[] v;
  16. new PrePostTest(out v);
  17. Assert(v.Length, 2);
  18. Assert(v[0], 3.3);
  19. Assert(v[1], 4.4);
  20. }
  21. {
  22. double[] v;
  23. PrePostTest p = new PrePostTest();
  24. p.method(out v);
  25. Assert(v.Length, 2);
  26. Assert(v[0], 5.5);
  27. Assert(v[1], 6.6);
  28. }
  29. {
  30. double[] v;
  31. PrePostTest.staticmethod(out v);
  32. Assert(v.Length, 2);
  33. Assert(v[0], 7.7);
  34. Assert(v[1], 8.8);
  35. }
  36. // Check attributes are generated for the constructor helper function
  37. {
  38. CsinAttributes c = new CsinAttributes(5);
  39. Assert(c.getVal(), 500);
  40. Type type = typeof(CsinAttributes);
  41. {
  42. MethodInfo member = (MethodInfo)type.GetMember("SwigConstructCsinAttributes", BindingFlags.NonPublic | BindingFlags.Static)[0];
  43. if (Attribute.GetCustomAttribute(member, typeof(CustomIntPtrAttribute)) == null)
  44. throw new Exception("No CustomIntPtr attribute for " + member.Name);
  45. ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
  46. if (parameter.Name != "val")
  47. throw new Exception("Incorrect parameter name");
  48. Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
  49. if (attribute.GetType() != typeof(CustomIntAttribute))
  50. throw new Exception("Expecting CustomInt attribute");
  51. }
  52. }
  53. // Dates
  54. {
  55. // pre post typemap attributes example
  56. System.DateTime dateIn = new System.DateTime(2011, 4, 13);
  57. System.DateTime dateOut = new System.DateTime();
  58. // Note in calls below, dateIn remains unchanged and dateOut
  59. // is set to a new value by the C++ call
  60. csharp_prepostNamespace.Action action = new csharp_prepostNamespace.Action(dateIn, out dateOut);
  61. if (dateOut != dateIn)
  62. throw new Exception("dates wrong");
  63. dateIn = new System.DateTime(2012, 7, 14);
  64. action.doSomething(dateIn, out dateOut);
  65. if (dateOut != dateIn)
  66. throw new Exception("dates wrong");
  67. System.DateTime refDate = new System.DateTime(1999, 12, 31);
  68. if (csharp_prepost.ImportantDate != refDate)
  69. throw new Exception("dates wrong");
  70. refDate = new System.DateTime(1999, 12, 31);
  71. csharp_prepost.ImportantDate = refDate;
  72. System.DateTime importantDate = csharp_prepost.ImportantDate;
  73. if (importantDate != refDate)
  74. throw new Exception("dates wrong");
  75. System.DateTime christmasEve = new System.DateTime(2000, 12, 24);
  76. csharp_prepost.addYears(ref christmasEve, 10);
  77. if (christmasEve != new System.DateTime(2010, 12, 24))
  78. throw new Exception("dates wrong");
  79. Person person = new Person();
  80. person.Birthday = christmasEve;
  81. if (person.Birthday != christmasEve)
  82. throw new Exception("dates wrong");
  83. }
  84. }
  85. private static void Assert(double d1, double d2) {
  86. if (d1 != d2)
  87. throw new Exception("assertion failure. " + d1 + " != " + d2);
  88. }
  89. }
  90. // Custom attribute classes
  91. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  92. public class CustomIntAttribute : Attribute {}
  93. [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  94. public class CustomIntPtrAttribute : Attribute {}