summaryrefslogtreecommitdiff
path: root/src/tests/efl_mono/Strings.cs
blob: dc5bd08b0eba02b2252166e1849db664c4b762bd (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;

namespace TestSuite {

class TestStrings
{
    /* The managed call is still owner of the sent string */
    public static void in_string()
    {
        {
            var obj = new Dummy.TestObject();
            String sent = "in_string";
            String returned = obj.InString(sent);
            Test.AssertEquals(sent, returned);
        }
        System.GC.Collect();
    }

    /* The managed call must not keep ownership of the C string after the
     * call */
    public static void in_own_string()
    {
        {
            var obj = new Dummy.TestObject();
            String sent = "in_own_string";
            String returned = obj.InOwnString(sent);
            Test.AssertEquals(sent, returned);
        }
        System.GC.Collect();
    }

    /* The managed call must not take ownership of the returned string */
    public static void return_string()
    {
        {
            var obj = new Dummy.TestObject();
            Test.AssertEquals("string", obj.ReturnString());
        }
        System.GC.Collect();
    }

    /* The managed call is free to own the returned string */
    public static void return_own_string()
    {
        {
            var obj = new Dummy.TestObject();
            Test.AssertEquals("own_string", obj.ReturnOwnString());
        }
        System.GC.Collect();
    }

    /* The managed call is *not* the owner of the string put in the out argument */
    public static void out_string()
    {
        {
            String str = String.Empty;
            var obj = new Dummy.TestObject();
            obj.OutString(out str);
            Test.AssertEquals("out_string", str);
        }
        System.GC.Collect();
    }

    /* The managed call is the owner of the string in the out parameter */
    public static void out_own_string()
    {
        {
            String str = String.Empty;
            var obj = new Dummy.TestObject();
            obj.OutOwnString(out str);
            Test.AssertEquals(str.ToString(), "out_own_string");
        }
        System.GC.Collect();
    }

    private class StringReturner : Dummy.TestObject
    {
        public String received_in;
        public String received_in_own;
        public StringReturner() : base(null) {
            received_in = String.Empty;
            received_in_own = String.Empty;
        }

        public override String InString(String str)
        {
            received_in = str;
            return String.Empty;
        }

        public override String InOwnString(String str)
        {
            /* Console.WriteLine("Called my own virtual"); */
            received_in_own = str;
            return String.Empty;
        }

        public override String ReturnString()
        {
            return "inherited";
        }

        public override String ReturnOwnString()
        {
            return "own_inherited";
        }

        public override void OutString(out String str)
        {
            str = "out_inherited";
        }

        public override void OutOwnString(out System.String str)
        {
            str = "out_own_inherited";
        }
    }

    /* The managed wrapper must not take ownership of the in parameter */
    public static void in_string_from_virtual()
    {
        StringReturner obj = new StringReturner();
        /* for (int i = 0; i < 10000; i++) { */
            String sent = "in_inherited";
            obj.CallInString(sent);
            Test.AssertEquals(sent, obj.received_in);
        /* } */
        System.GC.Collect();
    }

    /* The managed wrapper should take ownership of the in parameter */
    public static void in_own_string_from_virtual()
    {
        StringReturner obj = new StringReturner();
        /* for (int i = 0; i < 10000; i++) { */
            String sent = "in_own_inherited";
            obj.CallInOwnString(sent);
            Test.AssertEquals(sent, obj.received_in_own);
        /* } */
        System.GC.Collect();
    }

    /* The managed wrapper still owns the returned C string. We need to cache it until
     * some time in the future */
    public static void return_string_from_virtual()
    {
        var obj = new StringReturner();
        /* for (int i = 0; i < 10000; i ++) // Uncomment this to check for memory leaks. */
        Test.AssertEquals("inherited", obj.CallReturnString());
        System.GC.Collect();
    }

    /* The managed wrapper must surrender the ownership to the C after the virtual call. */
    public static void return_own_string_from_virtual()
    {
        var obj = new StringReturner();
        /* for (int i = 0; i < 10000; i ++) // Uncomment this to check for memory leaks. */
        Test.AssertEquals("own_inherited", obj.CallReturnOwnString());
        System.GC.Collect();
    }

    /* The managed wrapper still owns the C string after the call. Like return without own, we may
     * need to cache it until some time in the future. */
    public static void out_string_from_virtual()
    {
        var obj = new StringReturner();
        /* for (int i = 0; i < 10000; i ++) // Uncomment this to check for memory leaks. */
        Test.AssertEquals("out_inherited", obj.CallOutString());
        System.GC.Collect();
    }

    /* The managed wrapper gives C the ownership of the filled out parameter */
    public static void out_own_string_from_virtual()
    {
        var obj = new StringReturner();
        /* for (int i = 0; i < 10000; i ++) // Uncomment this to check for memory leaks. */
        Test.AssertEquals("out_own_inherited", obj.CallOutOwnString());
        System.GC.Collect();
    }

}

class TestStringshare
{
    public static void in_stringshare()
    {
        {
            var obj = new Dummy.TestObject();
            String sent = "in_stringshare";
            String returned = obj.InStringshare(sent);
            Test.AssertEquals(sent, returned);
        }
        System.GC.Collect();
    }

    public static void in_own_stringshare()
    {
        {
            var obj = new Dummy.TestObject();
            String sent = "in_own_stringshare";
            String returned = obj.InOwnStringshare(sent);
            Test.AssertEquals(sent, returned);
        }
        System.GC.Collect();
    }

    public static void return_stringshare()
    {
        {
            var obj = new Dummy.TestObject();
            Test.AssertEquals("stringshare", obj.ReturnStringshare());
        }
        System.GC.Collect();
    }

    public static void return_own_stringshare()
    {
        {
            var obj = new Dummy.TestObject();
            Test.AssertEquals("own_stringshare", obj.ReturnOwnStringshare());
        }
        System.GC.Collect();
    }

    public static void out_stringshare()
    {
        {
            String str = String.Empty;
            var obj = new Dummy.TestObject();
            obj.OutStringshare(out str);
            Test.AssertEquals("out_stringshare", str);
        }
        System.GC.Collect();
    }

    public static void out_own_stringshare()
    {
        {
            String str = String.Empty;
            var obj = new Dummy.TestObject();
            obj.OutOwnStringshare(out str);
            Test.AssertEquals(str.ToString(), "out_own_stringshare");
        }
        System.GC.Collect();
    }

    private class StringshareReturner : Dummy.TestObject
    {
        public String received_in;
        public String received_in_own;
        public StringshareReturner() : base(null) {
            received_in = String.Empty;
            received_in_own = String.Empty;
        }

        public override String InStringshare(String str)
        {
            received_in = str;
            return String.Empty;
        }

        public override String InOwnStringshare(String str)
        {
            received_in_own = str;
            return String.Empty;
        }

        public override String ReturnStringshare()
        {
            return "inherited";
        }

        public override String ReturnOwnStringshare()
        {
            return "own_inherited";
        }

        public override void OutStringshare(out String str)
        {
            str = "out_inherited";
        }

        public override void OutOwnStringshare(out System.String str)
        {
            str = "out_own_inherited";
        }
    }

    public static void in_stringshare_from_virtual()
    {
        StringshareReturner obj = new StringshareReturner();
        String sent = "in_inherited";
        obj.CallInStringshare(sent);
        Test.AssertEquals(sent, obj.received_in);
    }

    public static void in_own_stringshare_from_virtual()
    {
        StringshareReturner obj = new StringshareReturner();
        String sent = "in_own_inherited";
        obj.CallInOwnStringshare(sent);
        Test.AssertEquals(sent, obj.received_in_own);
    }

    public static void return_stringshare_from_virtual()
    {
        var obj = new StringshareReturner();
        // for (int i = 0; i < 1000000; i ++) // Uncomment this to check for memory leaks.
        Test.AssertEquals("inherited", obj.CallReturnStringshare());
    }

    public static void return_own_stringshare_from_virtual()
    {
        var obj = new StringshareReturner();
        // for (int i = 0; i < 1000000; i ++) // Uncomment this to check for memory leaks.
        Test.AssertEquals("own_inherited", obj.CallReturnOwnStringshare());
    }

    public static void out_stringshare_from_virtual()
    {
        var obj = new StringshareReturner();
        // for (int i = 0; i < 1000000; i ++) // Uncomment this to check for memory leaks.
        Test.AssertEquals("out_inherited", obj.CallOutStringshare());
    }

    public static void out_own_stringshare_from_virtual()
    {
        var obj = new StringshareReturner();
        // for (int i = 0; i < 1000000; i ++) // Uncomment this to check for memory leaks.
        Test.AssertEquals("out_own_inherited", obj.CallOutOwnStringshare());
    }
}

}