summaryrefslogtreecommitdiff
path: root/src/tests/efl_mono/FunctionPointers.cs
blob: 2514d79b82b7023921289dfbcaeabb7507d575e1 (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
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
using System;
using System.Runtime.InteropServices;

namespace TestSuite
{

class TestFunctionPointers
{

    static bool called = false;

    static int twice(int a)
    {
        called = true;
        return a * 2;
    }

    static int thrice(int a)
    {
        called = true;
        return a * 3;
    }

    static void setup()
    {
        called = false;
    }

    public static void set_callback_basic()
    {
        setup();
        test.Testing obj = new test.TestingConcrete();
        obj.SetCallback(twice);

        Test.Assert(called == false, "set_callback should not call the callback");

        int x = obj.CallCallback(42);

        Test.Assert(called, "call_callback must call a callback");
        Test.AssertEquals(42 * 2, x);
    }

    public static void set_callback_with_lambda()
    {
        setup();

        test.Testing obj = new test.TestingConcrete();
        obj.SetCallback(y => {
                    called = true;
                    return y + 4;
                });

        Test.Assert(called == false, "set_callback should not call the callback");

        int x = obj.CallCallback(37);

        Test.Assert(called, "call_callback must call a callback");
        Test.AssertEquals(37 + 4, x);
    }

    public static void replace_callback()
    {
        setup();

        test.Testing obj = new test.TestingConcrete();
        obj.SetCallback(twice);
        Test.Assert(called == false, "set_callback should not call the callback");

        int x = obj.CallCallback(42);
        Test.Assert(called, "call_callback must call a callback");
        Test.AssertEquals(42 * 2, x);

        bool new_called = false;
        obj.SetCallback(y => {
                    new_called = true;
                    return y * y;
                });

        Test.Assert(new_called == false, "set_callback should not call the callback");

        x = obj.CallCallback(42);
        Test.Assert(new_called, "call_callback must call a callback");
        Test.AssertEquals(42 * 42, x);
    }

    class NoOverride : test.TestingInherit {
    }
    public static void set_callback_inherited_no_override()
    {
        setup();
        NoOverride obj = new NoOverride();
        obj.SetCallback(thrice);

        Test.Assert(!called, "set_callback in virtual should not call the callback");

        int x = obj.CallCallback(42);

        Test.Assert(called, "call_callback must call a callback");
        Test.AssertEquals(42 * 3, x);
    }

    class WithOverride : test.TestingInherit {
        public bool set_called = false;
        public bool invoke_called = false;
        public test.SimpleCb cb = null;

        public WithOverride() : base() {

        }
        public override void SetCallback(test.SimpleCb cb) {
            set_called = true;
            this.cb = cb;
        }
        public override int CallCallback(int a) {
            invoke_called = true;
            if (cb != null)
                return cb(a);
            eina.Log.Error("No callback set upon call_callback invocation");
            return -1;
        }
    }
    public static void set_callback_inherited_with_override()
    {
        setup();
        WithOverride obj = new WithOverride();
        obj.SetCallback(thrice);

        Test.Assert(obj.set_called, "set_callback override must have been called");
        Test.Assert(!obj.invoke_called, "invoke_callback must not have been called");

        obj.set_called = false;
        int x = obj.CallCallback(42);

        Test.Assert(!obj.set_called, "set_callback override must not have been called");
        Test.Assert(obj.invoke_called, "set_callback in virtual should not call the callback");

        Test.Assert(called, "call_callback must call a callback");
        Test.AssertEquals(42 * 3, x);
    }

    // These are needed due to issues calling methods on obj from the GC thread (where the
    // free function is actually called)
    [System.Runtime.InteropServices.DllImport("efl_mono_native_test")] static extern bool free_called_get();
    [System.Runtime.InteropServices.DllImport("efl_mono_native_test")] static extern bool free_called_set(bool val);

     public static void set_callback_inherited_called_from_c()
     {
        setup();
        WithOverride obj = new WithOverride();
        free_called_set(false);
        obj.CallSetCallback();

        Test.Assert(obj.set_called, "set_callback override must have been called");
        Test.Assert(!obj.invoke_called, "invoke_callback must not have been called");
        Test.Assert(!free_called_get(), "call_set_callback must not call the free callback");

        obj.set_called = false;
        int x = obj.CallCallback(42);

        Test.Assert(!obj.set_called, "set_callback override must not have been called");
        Test.Assert(obj.invoke_called, "set_callback in virtual should not call the callback");
        Test.Assert(!free_called_get(), "call_callback must not call the free callback");

        Test.AssertEquals(42 * 3, x);

        setup();
        obj.set_called = false;
        obj.invoke_called = false;
        free_called_set(false);

        // Should release the handle to the wrapper allocated when calling set_callback from C.
        obj.SetCallback(twice);

        GC.Collect();
        GC.WaitForPendingFinalizers();

        Test.Assert(obj.set_called, "set_callback override must have been called");
        Test.Assert(!obj.invoke_called, "invoke_callback must not have been called");
        Test.Assert(free_called_get(), "free callback must have been called");

        obj.set_called = false;
        free_called_set(false);
        x = obj.CallCallback(42);

        Test.Assert(!obj.set_called, "set_callback override must not have been called");
        Test.Assert(obj.invoke_called, "set_callback in virtual should not call the callback");
        Test.Assert(!free_called_get(), "must not call old free_callback on new callback");

        Test.Assert(called, "call_callback must call a callback");
        Test.AssertEquals(42 * 2, x);

    }
}

}