summaryrefslogtreecommitdiff
path: root/Examples/test-suite/csharp/csharp_prepost_runme.cs
blob: ca3979065ca5646c455c167d3cc6cbc44959cc52 (plain)
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

using System;
using System.Reflection;
using csharp_prepostNamespace;

public class csharp_prepost_runme {

  class PrePost3_Derived : PrePost3
  {
    public PrePost3_Derived(){}
    public override void method(ref double[] vpre, DoubleVector vpost)
    {
      Assert(vpre[0], 1.0);
      vpre[0] = 2.0;
      Assert(vpost.Count, 2);
      vpost.Add(1.0);
    }
    public override int methodint(ref double[] vpre, DoubleVector vpost)
    {
      method(ref vpre, vpost);
      return vpost.Count;
    }
  }
  public static void Main() {
    {
      double[] v;
      csharp_prepost.globalfunction(out v);
      Assert(v.Length, 3);
      Assert(v[0], 0.0);
      Assert(v[1], 1.1);
      Assert(v[2], 2.2);
    }
    {
      double[] v;
      new PrePostTest(out v);
      Assert(v.Length, 2);
      Assert(v[0], 3.3);
      Assert(v[1], 4.4);
    }
    {
      double[] v;
      PrePostTest p = new PrePostTest();
      p.method(out v);
      Assert(v.Length, 2);
      Assert(v[0], 5.5);
      Assert(v[1], 6.6);
    }
    {
      double[] v;
      PrePostTest.staticmethod(out v);
      Assert(v.Length, 2);
      Assert(v[0], 7.7);
      Assert(v[1], 8.8);
    }

    {
      PrePost3_Derived p = new PrePost3_Derived();
      double[] vpre = new double[] { 1.0 };
      DoubleVector vpost = new DoubleVector();
      vpost.Add(3.0);
      vpost.Add(4.0);
      p.method(ref vpre, vpost);
      Assert(vpre[0], 2.0);
      Assert(vpost.Count, 3);
    }
    {
      PrePost3_Derived p = new PrePost3_Derived();
      double[] vpre = new double[] { 1.0 };
      DoubleVector vpost = new DoubleVector();
      vpost.Add(3.0);
      vpost.Add(4.0);
      int size = p.methodint(ref vpre, vpost);
      Assert(vpre[0], 2.0);
      Assert(vpost.Count, 3);
      Assert(size, 3);
    }

    // Check attributes are generated for the constructor helper function
    {
      CsinAttributes c = new CsinAttributes(5);
      Assert(c.getVal(), 500);

      Type type = typeof(CsinAttributes);
      {
        MethodInfo member = (MethodInfo)type.GetMember("SwigConstructCsinAttributes", BindingFlags.NonPublic | BindingFlags.Static)[0];
        if (Attribute.GetCustomAttribute(member, typeof(CustomIntPtrAttribute)) == null)
          throw new Exception("No CustomIntPtr attribute for " + member.Name);
        ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
        if (parameter.Name != "val")
          throw new Exception("Incorrect parameter name");
        Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
        if (attribute.GetType() != typeof(CustomIntAttribute))
          throw new Exception("Expecting CustomInt attribute");
      }
    }
    // Dates
    {
      // pre post typemap attributes example
      System.DateTime dateIn = new System.DateTime(2011, 4, 13);
      System.DateTime dateOut = new System.DateTime();

      // Note in calls below, dateIn remains unchanged and dateOut 
      // is set to a new value by the C++ call
      csharp_prepostNamespace.Action action = new csharp_prepostNamespace.Action(dateIn, out dateOut);
      if (dateOut != dateIn)
        throw new Exception("dates wrong");

      dateIn = new System.DateTime(2012, 7, 14);
      action.doSomething(dateIn, out dateOut);
      if (dateOut != dateIn)
        throw new Exception("dates wrong");

      System.DateTime refDate = new System.DateTime(1999, 12, 31);
      if (csharp_prepost.ImportantDate != refDate)
        throw new Exception("dates wrong");

      refDate = new System.DateTime(1999, 12, 31);
      csharp_prepost.ImportantDate = refDate;
      System.DateTime importantDate = csharp_prepost.ImportantDate;
      if (importantDate != refDate)
        throw new Exception("dates wrong");

      System.DateTime christmasEve = new System.DateTime(2000, 12, 24);
      csharp_prepost.addYears(ref christmasEve, 10);
      if (christmasEve != new System.DateTime(2010, 12, 24))
        throw new Exception("dates wrong");

      Person person = new Person();
      person.Birthday = christmasEve;
      if (person.Birthday != christmasEve)
        throw new Exception("dates wrong");
    }
  }
  private static void Assert(double d1, double d2) {
    if (d1 != d2)
      throw new Exception("assertion failure. " + d1 + " != " + d2);
  }
}

// Custom attribute classes
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public class CustomIntAttribute : Attribute {}

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public class CustomIntPtrAttribute : Attribute {}