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.Linq;
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
20 
21 using Eina;
22 using static EinaTestData.BaseData;
23 
24 namespace TestSuite
25 {
26 
27 internal class StructHelpers
28 {
29     // Auxiliary function //
30 
structSimpleWithValues()31     internal static Dummy.StructSimple structSimpleWithValues()
32     {
33         return  new Dummy.StructSimple(
34            fbyte: (sbyte)-126,
35            fubyte: (byte) 254u,
36            fchar: '~',
37            fshort: (short) -32766,
38            fushort: (ushort) 65534u,
39            fint: -32766,
40            fuint: 65534u,
41            flong: -2147483646,
42            fulong: 4294967294u,
43            fllong: -9223372036854775806,
44            fullong: 18446744073709551614u,
45            fint8: (sbyte) -126,
46            fuint8: (byte) 254u,
47            fint16: (short) -32766,
48            fuint16: (ushort) 65534u,
49            fint32: -2147483646,
50            fuint32: 4294967294u,
51            fint64: -9223372036854775806,
52            fuint64: 18446744073709551614u,
53            fssize: -2147483646,
54            fsize: 4294967294u,
55            fintptr: (IntPtr) 0xFE,
56            fptrdiff: -2147483646,
57            ffloat: -16777216.0f,
58            fdouble: -9007199254740992.0,
59            fbool: true,
60            fenum: Dummy.SampleEnum.V2,
61            fstring: "test/string",
62            fmstring: "test/mstring",
63            fstringshare: "test/stringshare"
64         );
65     }
66 
checkStructSimple(Dummy.StructSimple simple)67     internal static void checkStructSimple(Dummy.StructSimple simple)
68     {
69         Test.Assert(simple.Fbyte == (sbyte) -126);
70         Test.Assert(simple.Fubyte == (byte) 254u);
71         Test.Assert(simple.Fchar == '~');
72         Test.Assert(simple.Fshort == (short) -32766);
73         Test.Assert(simple.Fushort == (ushort) 65534u);
74         Test.Assert(simple.Fint == -32766);
75         Test.Assert(simple.Fuint == 65534u);
76         Test.Assert(simple.Flong == -2147483646);
77         Test.Assert(simple.Fulong == 4294967294u);
78         Test.Assert(simple.Fllong == -9223372036854775806);
79         Test.Assert(simple.Fullong == 18446744073709551614u);
80         Test.Assert(simple.Fint8 == (sbyte) -126);
81         Test.Assert(simple.Fuint8 == (byte) 254u);
82         Test.Assert(simple.Fint16 == (short) -32766);
83         Test.Assert(simple.Fuint16 == (ushort) 65534u);
84         Test.Assert(simple.Fint32 == -2147483646);
85         Test.Assert(simple.Fuint32 == 4294967294u);
86         Test.Assert(simple.Fint64 == -9223372036854775806);
87         Test.Assert(simple.Fuint64 == 18446744073709551614u);
88         Test.Assert(simple.Fssize == -2147483646);
89         Test.Assert(simple.Fsize == 4294967294u);
90         Test.Assert(simple.Fintptr == (IntPtr) 0xFE);
91         Test.Assert(simple.Fptrdiff == -2147483646);
92         Test.Assert(simple.Ffloat == -16777216.0f);
93         Test.Assert(simple.Fdouble == -9007199254740992.0);
94         Test.Assert(simple.Fbool == true);
95         Test.Assert(simple.Fenum == Dummy.SampleEnum.V2);
96         Test.Assert(simple.Fstring == "test/string");
97         Test.Assert(simple.Fmstring == "test/mstring");
98         Test.Assert(simple.Fstringshare == "test/stringshare");
99     }
100 
checkZeroedStructSimple(Dummy.StructSimple simple)101     internal static void checkZeroedStructSimple(Dummy.StructSimple simple)
102     {
103         Test.Assert(simple.Fbyte == 0);
104         Test.Assert(simple.Fubyte == 0);
105         Test.Assert(simple.Fchar == '\0');
106         Test.Assert(simple.Fshort == 0);
107         Test.Assert(simple.Fushort == 0);
108         Test.Assert(simple.Fint == 0);
109         Test.Assert(simple.Fuint == 0);
110         Test.Assert(simple.Flong == 0);
111         Test.Assert(simple.Fulong == 0);
112         Test.Assert(simple.Fllong == 0);
113         Test.Assert(simple.Fullong == 0);
114         Test.Assert(simple.Fint8 == 0);
115         Test.Assert(simple.Fuint8 == 0);
116         Test.Assert(simple.Fint16 == 0);
117         Test.Assert(simple.Fuint16 == 0);
118         Test.Assert(simple.Fint32 == 0);
119         Test.Assert(simple.Fuint32 == 0);
120         Test.Assert(simple.Fint64 == 0);
121         Test.Assert(simple.Fuint64 == 0);
122         Test.Assert(simple.Fssize == 0);
123         Test.Assert(simple.Fsize == 0);
124         Test.Assert(simple.Fintptr == IntPtr.Zero);
125         Test.Assert(simple.Fptrdiff == 0);
126         Test.Assert(simple.Ffloat == 0);
127         Test.Assert(simple.Fdouble == 0);
128         Test.Assert(simple.Fbool == false);
129         Test.Assert(simple.Fenum == Dummy.SampleEnum.V0);
130         Test.Assert(simple.Fstring == null);
131         Test.Assert(simple.Fmstring == null);
132         Test.Assert(simple.Fstringshare == null);
133     }
134 
135 #if EFL_BETA
structComplexWithValues()136     internal static Dummy.StructComplex structComplexWithValues()
137     {
138         var Farray = new Eina.Array<string>();
139         Farray.Add("0x0");
140         Farray.Add("0x2A");
141         Farray.Add("0x42");
142 
143         var Flist = new Eina.List<string>();
144         Flist.Add("0x0");
145         Flist.Add("0x2A");
146         Flist.Add("0x42");
147 
148         var Fhash = new Eina.Hash<string, string>();
149         Fhash["aa"] = "aaa";
150         Fhash["bb"] = "bbb";
151         Fhash["cc"] = "ccc";
152 
153         var Fiterator = ((Eina.Array<string>)Farray).GetIterator();
154         var Faccessor = ((Eina.Array<string>)Farray).GetAccessor();
155 
156         var Fany_value = new Eina.Value(Eina.ValueType.Double);
157         Fany_value.Set(-9007199254740992.0);
158 
159         var Fany_value_ref = new Eina.Value(Eina.ValueType.String);
160         Fany_value_ref.Set("abc");
161 
162         var Fbinbuf = new Eina.Binbuf();
163         Fbinbuf.Append(126);
164 
165         var Fslice = new Eina.Slice(Eina.MemoryNative.Alloc(1), (UIntPtr)1);
166         Marshal.WriteByte(Fslice.Mem, 125);
167 
168         var Fobj = new Dummy.Numberwrapper();
169         Fobj.Number = 42;
170 
171         return new Dummy.StructComplex(
172             farray: Farray,
173             flist: Flist,
174             fhash: Fhash,
175             fiterator: Fiterator,
176             faccessor: Faccessor,
177             fanyValue:Fany_value,
178             fanyValueRef: Fany_value_ref,
179             fbinbuf: Fbinbuf,
180             fslice:Fslice,
181             fobj: Fobj
182         );
183     }
184 
checkStructComplex(Dummy.StructComplex complex)185     internal static void checkStructComplex(Dummy.StructComplex complex)
186     {
187         Test.Assert(complex.Farray.ToArray().SequenceEqual(base_seq_str));
188 
189         Test.Assert(complex.Flist.ToArray().SequenceEqual(base_seq_str));
190 
191         Test.Assert(complex.Fhash["aa"] == "aaa");
192         Test.Assert(complex.Fhash["bb"] == "bbb");
193         Test.Assert(complex.Fhash["cc"] == "ccc");
194 
195         int idx = 0;
196         foreach (string e in complex.Fiterator)
197         {
198             Test.Assert(e == base_seq_str[idx]);
199             ++idx;
200         }
201         Test.AssertEquals(idx, base_seq_str.Length);
202 
203         double double_val = 0;
204         Test.Assert(complex.FanyValue.Get(out double_val));
205         Test.Assert(double_val == -9007199254740992.0);
206 
207         string str_val = null;
208         Test.Assert(complex.FanyValueRef.Get(out str_val));
209         Test.Assert(str_val == "abc");
210 
211         Test.Assert(complex.Fbinbuf.Length == 1);
212         Test.Assert(complex.Fbinbuf.GetBytes()[0] == 126);
213 
214         Test.Assert(complex.Fslice.Length == 1);
215         Test.Assert(complex.Fslice.GetBytes()[0] == 125);
216 
217         Test.Assert(complex.Fobj != null);
218         Test.Assert(complex.Fobj.Number == 42);
219     }
220 
221 
checkZeroedStructComplex(Dummy.StructComplex complex)222     internal static void checkZeroedStructComplex(Dummy.StructComplex complex)
223     {
224         Test.Assert(complex.Farray == null);
225         Test.Assert(complex.Flist == null);
226         Test.Assert(complex.Fhash == null);
227         Test.Assert(complex.Fiterator == null);
228         Test.Assert(complex.FanyValue == null);
229         Test.Assert(complex.FanyValueRef == null);
230         Test.Assert(complex.Fbinbuf == null);
231 
232         Test.Assert(complex.Fslice.Length == 0);
233         Test.Assert(complex.Fslice.Mem == IntPtr.Zero);
234 
235         Test.Assert(complex.Fobj == null);
236     }
237 
238 #endif
239 
240 }
241 
242 }
243