2
* Copyright 2019 by its authors. See AUTHORS.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
using System.Runtime.InteropServices;
20
using static EinaTestData.BaseData;
21
using static TestSuite.StructHelpers;
26
internal class TestStructs
30
// Default initialization (C# side)
32
private static void simple_default_instantiation()
34
var simple = new Dummy.StructSimple();
35
checkZeroedStructSimple(simple);
39
private static void complex_default_instantiation()
41
var complex = new Dummy.StructComplex();
42
checkZeroedStructComplex(complex);
46
public static void parameter_initialization()
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);
59
public static void simple_in()
61
var simple = structSimpleWithValues();
62
var t = new Dummy.TestObject();
63
bool r = t.StructSimpleIn(simple);
64
Test.Assert(r, "Function returned false");
69
public static void simple_ptr_in()
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);
80
public static void simple_ptr_in_own()
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);
92
public static void simple_out()
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);
103
public static void simple_ptr_out()
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);
112
public static void simple_ptr_out_own()
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");
122
public static void simple_return()
124
var t = new Dummy.TestObject();
125
var simple = t.StructSimpleReturn();
126
checkStructSimple(simple);
131
public static void simple_ptr_return()
133
var t = new Dummy.TestObject();
134
var simple = t.StructSimplePtrReturn();
135
Test.AssertEquals(simple.Fstring, "Ret Ptr");
138
public static void simple_ptr_return_own()
140
var t = new Dummy.TestObject();
141
var simple = t.StructSimplePtrReturnOwn();
142
Test.AssertEquals(simple.Fstring, "Ret Ptr Own");
146
public class StructReturner : Dummy.TestObject
148
public Dummy.StructSimple received;
151
public StructReturner() : base(null)
154
received = default(Dummy.StructSimple);
157
public override bool StructSimpleIn(Dummy.StructSimple simple)
166
public override bool StructSimplePtrIn(ref Dummy.StructSimple simple)
169
simple.Fstring = "Virtual Struct Ptr In";
173
public override Dummy.StructSimple StructSimplePtrInOwn(ref Dummy.StructSimple simple)
181
public override bool StructSimpleOut(out Dummy.StructSimple simple) {
183
simple = new Dummy.StructSimple(fstring: "Virtual Struct Out");
188
public override Dummy.StructSimple StructSimplePtrOut(out Dummy.StructSimple simple) {
190
// No way to explicitly define the ownership of the parameter.
191
simple = new Dummy.StructSimple();
192
simple.Fstring = "Virtual Struct Ptr Out";
196
public override Dummy.StructSimple StructSimplePtrOutOwn(out Dummy.StructSimple simple) {
198
// No way to explicitly define the ownership of the parameter.
199
simple = new Dummy.StructSimple();
200
simple.Fstring = "Virtual Struct Ptr Out Own";
205
public override Dummy.StructSimple StructSimpleReturn()
208
var simple = new Dummy.StructSimple(fstring: "Virtual Struct Return");
213
public override Dummy.StructSimple StructSimplePtrReturn()
216
var simple = new Dummy.StructSimple();
217
simple.Fstring = "Virtual Struct Ptr Return";
221
public override Dummy.StructSimple StructSimplePtrReturnOwn()
224
var simple = new Dummy.StructSimple();
225
simple.Fstring = "Virtual Struct Ptr Return Own";
231
public static void simple_in_virtual()
233
StructReturner t = new StructReturner();
234
var simple = structSimpleWithValues();
236
t.CallStructSimpleIn(simple);
237
Test.Assert(t.called);
238
Test.AssertEquals(simple.Fstring, t.received.Fstring);
243
public static void simple_ptr_in_virtual()
245
StructReturner t = new StructReturner();
246
var simple = structSimpleWithValues();
247
string reference = "Virtual Struct Ptr In";
249
t.CallStructSimplePtrIn(ref simple);
250
Test.Assert(t.called);
251
Test.AssertEquals(simple.Fstring, reference);
254
public static void simple_ptr_in_own_virtual()
256
StructReturner t = new StructReturner();
257
var simple = structSimpleWithValues();
258
simple.Fstring = "Virtual Struct Ptr In Own";
260
t.CallStructSimplePtrInOwn(ref simple);
261
Test.Assert(t.called);
262
Test.AssertEquals(t.received.Fstring, simple.Fstring);
266
public static void simple_out_virtual()
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);
277
public static void simple_ptr_out_virtual()
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);
286
public static void simple_ptr_out_own_virtual()
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);
296
public static void simple_return_virtual()
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);
306
public static void simple_ptr_return_virtual()
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);
314
public static void simple_ptr_return_own_virtual()
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);
325
public static void complex_in()
327
var complex = structComplexWithValues();
328
var t = new Dummy.TestObject();
329
bool r = t.StructComplexIn(complex);
330
Test.Assert(r, "Function returned false");
334
// public static void complex_ptr_in()
338
// public static void complex_ptr_in_own()
342
public static void complex_out()
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);
352
public static void complex_iterator_retrieves_list_correctly()
354
var complex = structComplexWithValues();
357
foreach (var elm in complex.Fiterator) {
358
Test.AssertEquals(elm, complex.Flist[i]);
363
public static void complex_iterator_retrieves_array_correctly()
365
var complex = structComplexWithValues();
368
foreach (var elm in complex.Fiterator) {
369
Test.AssertEquals(elm, complex.Farray[i]);
374
public static void complex_accessor_retrieves_list_correctly()
376
var complex = structComplexWithValues();
379
foreach (var elm in complex.Faccessor) {
380
Test.AssertEquals(elm, complex.Flist[i]);
385
public static void complex_accessor_retrieves_array_correctly()
387
var complex = structComplexWithValues();
390
foreach (var elm in complex.Faccessor) {
391
Test.AssertEquals(elm, complex.Farray[i]);
396
// public static void complex_ptr_out()
400
// public static void complex_ptr_out_own()
404
public static void complex_return()
406
var t = new Dummy.TestObject();
407
var complex = t.StructComplexReturn();
408
checkStructComplex(complex);
412
// public static void complex_ptr_return()
416
// public static void complex_ptr_return_own()
421
internal class TestStructEquality
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: "");
426
static Dummy.StructSimple c = new Dummy.StructSimple(4, 3, (char)2, 1, fstring: "", fmstring: "", fstringshare: "");
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: "");
431
public static void test_equals()
433
Test.AssertEquals(a, b);
434
Test.AssertNotEquals(a, c);
435
Test.AssertNotEquals(a, singleDifferentField);
438
public static void test_equals_different_types()
440
Test.Assert(!(a.Equals(new Object())));
443
public static void test_equatable()
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));
450
public static void test_equality_operators()
454
Test.Assert(a != singleDifferentField);
457
public static void test_hash_code()
459
Test.AssertEquals(a.GetHashCode(), b.GetHashCode());
460
Test.AssertNotEquals(a.GetHashCode(), c.GetHashCode());
461
Test.AssertNotEquals(a.GetHashCode(), singleDifferentField.GetHashCode());
465
public static void test_deconstruct() {
466
var p = new Eina.Position2D(1, 2);
469
Test.AssertEquals(x, 1);
470
Test.AssertEquals(y, 2);
475
internal class TestStructTuples
477
private static Eina.Position2D simulate_position_usage(Eina.Position2D p) {
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);
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);
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");
500
public static void test_complex_fields_assign_conversion() {
501
var pos = new Eina.Position2D(1, 2);
503
var types = new System.String[] {"text", "markup"};
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]);