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