efl

Форк
0
/
Structs.cs 
514 строк · 14.8 Кб
1
/*
2
 * Copyright 2019 by its authors. See AUTHORS.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
using System;
17
using System.Runtime.InteropServices;
18
using System.Linq;
19

20
using static EinaTestData.BaseData;
21
using static TestSuite.StructHelpers;
22

23
namespace TestSuite
24
{
25

26
internal class TestStructs
27
{
28
    // Test cases //
29

30
    // Default initialization (C# side)
31

32
    private static void simple_default_instantiation()
33
    {
34
        var simple = new Dummy.StructSimple();
35
        checkZeroedStructSimple(simple);
36
    }
37

38
#if EFL_BETA
39
    private static void complex_default_instantiation()
40
    {
41
        var complex = new Dummy.StructComplex();
42
        checkZeroedStructComplex(complex);
43
    }
44
#endif
45

46
    public static void parameter_initialization()
47
    {
48
        var simple = new Dummy.StructSimple(0x1, 0x2, (char)0x3, 0x4, 0x5);
49
        Test.AssertEquals(0x1, simple.Fbyte);
50
        Test.AssertEquals(0x2, simple.Fubyte);
51
        Test.AssertEquals(0x3, simple.Fchar);
52
        Test.AssertEquals(0x4, simple.Fshort);
53
        Test.AssertEquals(0x5, simple.Fushort);
54
        Test.AssertEquals(0, simple.Fint);
55
    }
56

57
    // As parameters
58

59
    public static void simple_in()
60
    {
61
        var simple = structSimpleWithValues();
62
        var t = new Dummy.TestObject();
63
        bool r = t.StructSimpleIn(simple);
64
        Test.Assert(r, "Function returned false");
65
        t.Dispose();
66
    }
67

68
    /*
69
    public static void simple_ptr_in()
70
    {
71
        var simple = structSimpleWithValues();
72
        int original = simple.Fint;
73
        simple.Fmstring = "Struct Ptr In";
74
        var t = new Dummy.TestObject();
75
        Test.Assert(t.StructSimplePtrIn(ref simple));
76
        Test.AssertEquals(-original, simple.Fint);
77
        Test.AssertEquals("nI rtP tcurtS", simple.Fmstring);
78
    }
79

80
    public static void simple_ptr_in_own()
81
    {
82
        var simple = structSimpleWithValues();
83
        int original = simple.Fint;
84
        simple.Fmstring = "Struct Ptr In Own";
85
        var t = new Dummy.TestObject();
86
        Dummy.StructSimple result = t.StructSimplePtrInOwn(ref simple);
87
        Test.AssertEquals(-original, result.Fint);
88
        Test.AssertEquals("nwO nI rtP tcurtS", result.Fmstring);
89
    }
90
    */
91

92
    public static void simple_out()
93
    {
94
        var simple = new Dummy.StructSimple();
95
        var t = new Dummy.TestObject();
96
        bool r = t.StructSimpleOut(out simple);
97
        Test.Assert(r, "Function returned false");
98
        checkStructSimple(simple);
99
        t.Dispose();
100
    }
101

102
    /*
103
    public static void simple_ptr_out()
104
    {
105
        Dummy.StructSimple simple;
106
        var t = new Dummy.TestObject();
107
        Dummy.StructSimple result = t.StructSimplePtrOut(out simple);
108
        Test.AssertEquals(result.Fint, simple.Fint);
109
        Test.AssertEquals(result.Fstring, simple.Fstring);
110
    }
111

112
    public static void simple_ptr_out_own()
113
    {
114
        Dummy.StructSimple simple;
115
        var t = new Dummy.TestObject();
116
        Dummy.StructSimple result = t.StructSimplePtrOutOwn(out simple);
117
        Test.AssertEquals(result.Fint, simple.Fint);
118
        Test.AssertEquals(simple.Fstring, "Ptr Out Own");
119
    }
120
    */
121

122
    public static void simple_return()
123
    {
124
        var t = new Dummy.TestObject();
125
        var simple = t.StructSimpleReturn();
126
        checkStructSimple(simple);
127
        t.Dispose();
128
    }
129

130
    /*
131
    public static void simple_ptr_return()
132
    {
133
        var t = new Dummy.TestObject();
134
        var simple = t.StructSimplePtrReturn();
135
        Test.AssertEquals(simple.Fstring, "Ret Ptr");
136
    }
137

138
    public static void simple_ptr_return_own()
139
    {
140
        var t = new Dummy.TestObject();
141
        var simple = t.StructSimplePtrReturnOwn();
142
        Test.AssertEquals(simple.Fstring, "Ret Ptr Own");
143
    }
144
    */
145

146
    public class StructReturner : Dummy.TestObject
147
    {
148
        public Dummy.StructSimple received;
149
        public bool called;
150

151
        public StructReturner() : base(null)
152
        {
153
            called = false;
154
            received = default(Dummy.StructSimple);
155
        }
156

157
        public override bool StructSimpleIn(Dummy.StructSimple simple)
158
        {
159
            called = true;
160
            received = simple;
161

162
            return true;
163
        }
164

165
        /*
166
        public override bool StructSimplePtrIn(ref Dummy.StructSimple simple)
167
        {
168
            called = true;
169
            simple.Fstring = "Virtual Struct Ptr In";
170
            return true;
171
        }
172

173
        public override Dummy.StructSimple StructSimplePtrInOwn(ref Dummy.StructSimple simple)
174
        {
175
            called = true;
176
            received = simple;
177
            return received;
178
        }
179
        */
180

181
        public override bool StructSimpleOut(out Dummy.StructSimple simple) {
182
            called = true;
183
            simple = new Dummy.StructSimple(fstring: "Virtual Struct Out");
184
            return true;
185
        }
186

187
        /*
188
        public override Dummy.StructSimple StructSimplePtrOut(out Dummy.StructSimple simple) {
189
            called = true;
190
            // No way to explicitly define the ownership of the parameter.
191
            simple = new Dummy.StructSimple();
192
            simple.Fstring = "Virtual Struct Ptr Out";
193
            return simple;
194
        }
195

196
        public override Dummy.StructSimple StructSimplePtrOutOwn(out Dummy.StructSimple simple) {
197
            called = true;
198
            // No way to explicitly define the ownership of the parameter.
199
            simple = new Dummy.StructSimple();
200
            simple.Fstring = "Virtual Struct Ptr Out Own";
201
            return simple;
202
        }
203
        */
204

205
        public override Dummy.StructSimple StructSimpleReturn()
206
        {
207
            called = true;
208
            var simple = new Dummy.StructSimple(fstring: "Virtual Struct Return");
209
            return simple;
210
        }
211

212
        /*
213
        public override Dummy.StructSimple StructSimplePtrReturn()
214
        {
215
            called = true;
216
            var simple = new Dummy.StructSimple();
217
            simple.Fstring = "Virtual Struct Ptr Return";
218
            return simple;
219
        }
220

221
        public override Dummy.StructSimple StructSimplePtrReturnOwn()
222
        {
223
            called = true;
224
            var simple = new Dummy.StructSimple();
225
            simple.Fstring = "Virtual Struct Ptr Return Own";
226
            return simple;
227
        }
228
        */
229
    }
230

231
    public static void simple_in_virtual()
232
    {
233
        StructReturner t = new StructReturner();
234
        var simple = structSimpleWithValues();
235

236
        t.CallStructSimpleIn(simple);
237
        Test.Assert(t.called);
238
        Test.AssertEquals(simple.Fstring, t.received.Fstring);
239
        t.Dispose();
240
    }
241

242
    /*
243
    public static void simple_ptr_in_virtual()
244
    {
245
        StructReturner t = new StructReturner();
246
        var simple = structSimpleWithValues();
247
        string reference = "Virtual Struct Ptr In";
248

249
        t.CallStructSimplePtrIn(ref simple);
250
        Test.Assert(t.called);
251
        Test.AssertEquals(simple.Fstring, reference);
252
    }
253

254
    public static void simple_ptr_in_own_virtual()
255
    {
256
        StructReturner t = new StructReturner();
257
        var simple = structSimpleWithValues();
258
        simple.Fstring = "Virtual Struct Ptr In Own";
259

260
        t.CallStructSimplePtrInOwn(ref simple);
261
        Test.Assert(t.called);
262
        Test.AssertEquals(t.received.Fstring, simple.Fstring);
263
    }
264
    */
265

266
    public static void simple_out_virtual()
267
    {
268
        StructReturner t = new StructReturner();
269
        Dummy.StructSimple simple;
270
        t.CallStructSimpleOut(out simple);
271
        Test.Assert(t.called, "override was not called");
272
        Test.AssertEquals("Virtual Struct Out", simple.Fstring);
273
        t.Dispose();
274
    }
275

276
    /*
277
    public static void simple_ptr_out_virtual()
278
    {
279
        StructReturner t = new StructReturner();
280
        Dummy.StructSimple simple;
281
        t.CallStructSimplePtrOut(out simple);
282
        Test.Assert(t.called, "override was not called");
283
        Test.AssertEquals("Virtual Struct Ptr Out", simple.Fstring);
284
    }
285

286
    public static void simple_ptr_out_own_virtual()
287
    {
288
        StructReturner t = new StructReturner();
289
        Dummy.StructSimple simple;
290
        t.CallStructSimplePtrOutOwn(out simple);
291
        Test.Assert(t.called, "override was not called");
292
        Test.AssertEquals("Virtual Struct Ptr Out Own", simple.Fstring);
293
    }
294
    */
295

296
    public static void simple_return_virtual()
297
    {
298
        StructReturner t = new StructReturner();
299
        Dummy.StructSimple simple = t.CallStructSimpleReturn();
300
        Test.Assert(t.called, "override was not called");
301
        Test.AssertEquals("Virtual Struct Return", simple.Fstring);
302
        t.Dispose();
303
    }
304

305
    /*
306
    public static void simple_ptr_return_virtual()
307
    {
308
        StructReturner t = new StructReturner();
309
        Dummy.StructSimple simple = t.CallStructSimplePtrReturn();
310
        Test.Assert(t.called, "override was not called");
311
        Test.AssertEquals("Virtual Struct Ptr Return", simple.Fstring);
312
    }
313

314
    public static void simple_ptr_return_own_virtual()
315
    {
316
        StructReturner t = new StructReturner();
317
        Dummy.StructSimple simple = t.CallStructSimplePtrReturnOwn();
318
        Test.Assert(t.called, "override was not called");
319
        Test.AssertEquals("Virtual Struct Ptr Return Own", simple.Fstring);
320
    }
321
    */
322

323
#if EFL_BETA
324
    // Complex Structs
325
    public static void complex_in()
326
    {
327
        var complex = structComplexWithValues();
328
        var t = new Dummy.TestObject();
329
        bool r = t.StructComplexIn(complex);
330
        Test.Assert(r, "Function returned false");
331
        t.Dispose();
332
    }
333

334
    // public static void complex_ptr_in()
335
    // {
336
    // }
337

338
    // public static void complex_ptr_in_own()
339
    // {
340
    // }
341

342
    public static void complex_out()
343
    {
344
        var complex = new Dummy.StructComplex();
345
        var t = new Dummy.TestObject();
346
        bool r = t.StructComplexOut(out complex);
347
        Test.Assert(r, "Function returned false");
348
        checkStructComplex(complex);
349
        t.Dispose();
350
    }
351

352
    public static void complex_iterator_retrieves_list_correctly()
353
    {
354
        var complex = structComplexWithValues();
355

356
        var i = 0;
357
        foreach (var elm in complex.Fiterator) {
358
            Test.AssertEquals(elm, complex.Flist[i]);
359
            i++;
360
        }
361
    }
362

363
    public static void complex_iterator_retrieves_array_correctly()
364
    {
365
        var complex = structComplexWithValues();
366

367
        var i = 0;
368
        foreach (var elm in complex.Fiterator) {
369
            Test.AssertEquals(elm, complex.Farray[i]);
370
            i++;
371
        }
372
    }
373

374
    public static void complex_accessor_retrieves_list_correctly()
375
    {
376
        var complex = structComplexWithValues();
377

378
        var i = 0;
379
        foreach (var elm in complex.Faccessor) {
380
            Test.AssertEquals(elm, complex.Flist[i]);
381
            i++;
382
        }
383
    }
384

385
    public static void complex_accessor_retrieves_array_correctly()
386
    {
387
        var complex = structComplexWithValues();
388

389
        var i = 0;
390
        foreach (var elm in complex.Faccessor) {
391
            Test.AssertEquals(elm, complex.Farray[i]);
392
            i++;
393
        }
394
    }
395

396
    // public static void complex_ptr_out()
397
    // {
398
    // }
399

400
    // public static void complex_ptr_out_own()
401
    // {
402
    // }
403

404
    public static void complex_return()
405
    {
406
        var t = new Dummy.TestObject();
407
        var complex = t.StructComplexReturn();
408
        checkStructComplex(complex);
409
        t.Dispose();
410
    }
411
#endif
412
    // public static void complex_ptr_return()
413
    // {
414
    // }
415

416
    // public static void complex_ptr_return_own()
417
    // {
418
    // }
419
}
420

421
internal class TestStructEquality
422
{
423
    static Dummy.StructSimple a = new Dummy.StructSimple(1, 2, (char)3, 4, fstring: "", fmstring: "", fstringshare: "");
424
    static Dummy.StructSimple b = new Dummy.StructSimple(1, 2, (char)3, 4, fstring: "", fmstring: "", fstringshare: "");
425

426
    static Dummy.StructSimple c = new Dummy.StructSimple(4, 3, (char)2, 1, fstring: "", fmstring: "", fstringshare: "");
427

428
    // to check if we differ on a single struct field
429
    static Dummy.StructSimple singleDifferentField = new Dummy.StructSimple(1, 2, (char)3, 5, fstring: "", fmstring: "", fstringshare: "");
430

431
    public static void test_equals()
432
    {
433
        Test.AssertEquals(a, b);
434
        Test.AssertNotEquals(a, c);
435
        Test.AssertNotEquals(a, singleDifferentField);
436
    }
437

438
    public static void test_equals_different_types()
439
    {
440
        Test.Assert(!(a.Equals(new Object())));
441
    }
442

443
    public static void test_equatable()
444
    {
445
        Test.Assert(((IEquatable<Dummy.StructSimple>)a).Equals(b));
446
        Test.Assert(!((IEquatable<Dummy.StructSimple>)a).Equals(c));
447
        Test.Assert(!((IEquatable<Dummy.StructSimple>)a).Equals(singleDifferentField));
448
    }
449

450
    public static void test_equality_operators()
451
    {
452
        Test.Assert(a == b);
453
        Test.Assert(a != c);
454
        Test.Assert(a != singleDifferentField);
455
    }
456

457
    public static void test_hash_code()
458
    {
459
        Test.AssertEquals(a.GetHashCode(), b.GetHashCode());
460
        Test.AssertNotEquals(a.GetHashCode(), c.GetHashCode());
461
        Test.AssertNotEquals(a.GetHashCode(), singleDifferentField.GetHashCode());
462
    }
463

464
#if !MONO
465
    public static void test_deconstruct() {
466
        var p = new Eina.Position2D(1, 2);
467
        var (x, y) = p;
468

469
        Test.AssertEquals(x, 1);
470
        Test.AssertEquals(y, 2);
471
    }
472
#endif
473
}
474

475
internal class TestStructTuples
476
{
477
    private static Eina.Position2D simulate_position_usage(Eina.Position2D p) {
478
        return p;
479
    }
480

481
    public static void test_same_type_fields_assign_conversion() {
482
        Eina.Position2D p = (1, 2);
483
        Test.AssertEquals(p.X, 1);
484
        Test.AssertEquals(p.Y, 2);
485
    }
486

487
    public static void test_same_type_fields_call_conversion() {
488
        var p = simulate_position_usage((1, 2));
489
        Test.AssertEquals(p.X, 1);
490
        Test.AssertEquals(p.Y, 2);
491
    }
492

493
    public static void test_different_type_fields_assign_conversion() {
494
        Efl.Ui.FormatValue v = (1, "Format");
495
        Test.AssertEquals(v.Value, 1);
496
        Test.AssertEquals(v.Text, "Format");
497
    }
498

499
#if EFL_BETA
500
    public static void test_complex_fields_assign_conversion() {
501
        var pos = new Eina.Position2D(1, 2);
502
        uint seat = 3;
503
        var types = new System.String[] {"text", "markup"};
504

505
        Efl.Ui.DropEvent attr = (pos, seat, types);
506
        Test.AssertEquals(attr.Position, pos);
507
        Test.AssertEquals(attr.Seat, seat);
508
        Test.AssertEquals(attr.AvailableTypes.ElementAt(0), types[0]);
509
        Test.AssertEquals(attr.AvailableTypes.ElementAt(1), types[1]);
510
    }
511
#endif
512
}
513

514
}
515

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.