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.Runtime.CompilerServices;
20 using System.Diagnostics.Contracts;
21 using Eina;
22 
23 namespace EinaTestData
24 {
25 
26 class BaseSequence
27 {
Values()28     public static byte[] Values() => new byte[3]{0x0, 0x2A, 0x42};
29 }
30 
31 public static class BaseData
32 {
33     public static readonly int[] base_seq_int = {0x0, 0x2A, 0x42};
34     public static readonly int[] append_seq_int = {42, 43, 33};
35     public static readonly int[] modified_seq_int = {0x0, 0x2A, 0x42, 42, 43, 33};
36 
37     public static readonly string[] base_seq_str = {"0x0", "0x2A", "0x42"};
38     public static readonly string[] append_seq_str = {"42", "43", "33"};
39     public static readonly string[] modified_seq_str = {"0x0", "0x2A", "0x42",
40         "42",  "43",   "33"};
41 
42     public static readonly Eina.Stringshare[] base_seq_strshare = {"0x0", "0x2A",
43         "0x42"};
44     public static readonly Eina.Stringshare[] append_seq_strshare = {"42", "43",
45         "33"};
46     public static readonly Eina.Stringshare[] modified_seq_strshare = {
47         "0x0", "0x2A", "0x42", "42", "43", "33"};
48 
NW(int n)49     public static Dummy.Numberwrapper NW(int n)
50     {
51         var nw = new Dummy.Numberwrapper();
52         nw.Number = n;
53         return nw;
54     }
55 
BaseSeqObj()56     public static Dummy.Numberwrapper[] BaseSeqObj()
57     {
58         var a = new Dummy.Numberwrapper();
59         var b = new Dummy.Numberwrapper();
60         var c = new Dummy.Numberwrapper();
61         a.Number = 0x0;
62         b.Number = 0x2A;
63         c.Number = 0x42;
64         return new Dummy.Numberwrapper[]{a, b, c};
65     }
66 
AppendSeqObj()67     public static Dummy.Numberwrapper[] AppendSeqObj()
68     {
69         var a = new Dummy.Numberwrapper();
70         var b = new Dummy.Numberwrapper();
71         var c = new Dummy.Numberwrapper();
72         a.Number = 42;
73         b.Number = 43;
74         c.Number = 33;
75         return new Dummy.Numberwrapper[]{a, b, c};
76     }
77 
ModifiedSeqObj()78     public static Dummy.Numberwrapper[] ModifiedSeqObj()
79     {
80         var a = new Dummy.Numberwrapper();
81         var b = new Dummy.Numberwrapper();
82         var c = new Dummy.Numberwrapper();
83         var d = new Dummy.Numberwrapper();
84         var e = new Dummy.Numberwrapper();
85         var f = new Dummy.Numberwrapper();
86         a.Number = 0x0;
87         b.Number = 0x2A;
88         c.Number = 0x42;
89         d.Number = 42;
90         e.Number = 43;
91         f.Number = 33;
92         return new Dummy.Numberwrapper[]{a, b, c, d, e, f};
93     }
94 
NumberwrapperSequenceAssertEqual( Dummy.Numberwrapper[] a, Dummy.Numberwrapper[] b, [ CallerLineNumber ] int line = 0, [ CallerFilePath ] string file = null, [ CallerMemberName ] string member = null)95     public static void NumberwrapperSequenceAssertEqual(
96         Dummy.Numberwrapper[] a, Dummy.Numberwrapper[] b,
97         [ CallerLineNumber ] int line = 0,
98         [ CallerFilePath ] string file = null,
99         [ CallerMemberName ] string member = null) {
100         Contract.Requires(a != null, nameof(a));
101         Contract.Requires(b != null, nameof(b));
102         Test.Assert(a.Length == b.Length, "Different length", line, file, member);
103         for (int i = 0; i < a.Length; ++i)
104         {
105             int av = a[i].Number;
106             int bv = b[i].Number;
107             Test.Assert(av == bv, $"Different values for element [{i}]: {av} == {bv}", line, file, member);
108         }
109     }
110 }
111 
112 class NativeInheritImpl : Dummy.TestObject
113 {
NativeInheritImpl(Efl.Object parent = null)114     public NativeInheritImpl(Efl.Object parent = null) : base(parent) {}
115 
116     public bool slice_in_flag = false;
117     public bool rw_slice_in_flag = false;
118     public bool slice_out_flag = false;
119     public bool rw_slice_out_flag = false;
120     public bool binbuf_in_flag = false;
121     public bool binbuf_in_own_flag = false;
122     public bool binbuf_out_flag = false;
123     public bool binbuf_out_own_flag = false;
124     public bool binbuf_return_flag = false;
125     public bool binbuf_return_own_flag = false;
126 
EinaSliceIn(Eina.Slice slice)127     override public bool EinaSliceIn(Eina.Slice slice)
128     {
129         slice_in_flag = true;
130         return slice.GetBytes().SequenceEqual(BaseSequence.Values());
131     }
132 
EinaRwSliceIn(Eina.RwSlice slice)133     override public bool EinaRwSliceIn(Eina.RwSlice slice)
134     {
135         rw_slice_in_flag = true;
136         return slice.GetBytes().SequenceEqual(BaseSequence.Values());
137     }
138 
139     private byte[] slice_out_seq = null;
140     private GCHandle slice_out_pinned;
EinaSliceOut(ref Eina.Slice slice)141     override public bool EinaSliceOut(ref Eina.Slice slice)
142     {
143         slice_out_flag = true;
144 
145         slice_out_seq = (byte[]) BaseSequence.Values();
146         slice_out_pinned = GCHandle.Alloc(slice_out_seq, GCHandleType.Pinned);
147         IntPtr ptr = slice_out_pinned.AddrOfPinnedObject();
148 
149         slice.Mem = ptr;
150         slice.Len = (UIntPtr) slice_out_seq.Length;
151 
152         return true;
153     }
154 
155     private byte[] rw_slice_out_seq = null;
156     private GCHandle rw_slice_out_pinned;
EinaRwSliceOut(ref Eina.RwSlice slice)157     override public bool EinaRwSliceOut(ref Eina.RwSlice slice)
158     {
159         rw_slice_out_flag = true;
160 
161         rw_slice_out_seq = (byte[]) BaseSequence.Values();
162         rw_slice_out_pinned = GCHandle.Alloc(rw_slice_out_seq, GCHandleType.Pinned);
163         IntPtr ptr = rw_slice_out_pinned.AddrOfPinnedObject();
164 
165         slice.Mem = ptr;
166         slice.Len = (UIntPtr) rw_slice_out_seq.Length;
167 
168         return true;
169     }
170 
171     // //
172     //
EinaBinbufIn(Eina.Binbuf binbuf)173     override public bool EinaBinbufIn(Eina.Binbuf binbuf)
174     {
175         binbuf_in_flag = true;
176 
177         bool r = binbuf.GetBytes().SequenceEqual(BaseSequence.Values());
178         r = r && !binbuf.Own;
179 
180         binbuf.Insert(42, 0);
181         binbuf.Insert(43, 0);
182         binbuf.Append(33);
183 
184         binbuf.Dispose();
185 
186         return r;
187     }
188 
189     private Eina.Binbuf binbuf_in_own_binbuf = null;
EinaBinbufInOwn(Eina.Binbuf binbuf)190     override public bool EinaBinbufInOwn(Eina.Binbuf binbuf)
191     {
192         binbuf_in_own_flag = true;
193 
194         bool r = binbuf.GetBytes().SequenceEqual(BaseSequence.Values());
195         r = r && binbuf.Own;
196 
197         binbuf.Insert(42, 0);
198         binbuf.Insert(43, 0);
199         binbuf.Append(33);
200 
201         binbuf_in_own_binbuf = binbuf;
202 
203         return r;
204     }
binbuf_in_own_still_usable()205     public bool binbuf_in_own_still_usable()
206     {
207         bool r = binbuf_in_own_binbuf.GetBytes().SequenceEqual(new byte[]{43, 42, 0x0, 0x2A, 0x42, 33});
208         r = r && binbuf_in_own_binbuf.Own;
209 
210         binbuf_in_own_binbuf.Dispose();
211         binbuf_in_own_binbuf = null;
212 
213         return r;
214     }
215 
216     private Eina.Binbuf binbuf_out_binbuf = null;
EinaBinbufOut(out Eina.Binbuf binbuf)217     override public bool EinaBinbufOut(out Eina.Binbuf binbuf)
218     {
219         binbuf_out_flag = true;
220 
221         binbuf = new Eina.Binbuf();
222         binbuf.Append(33);
223 
224         binbuf_out_binbuf = binbuf;
225 
226         return true;
227     }
binbuf_out_still_usable()228     public bool binbuf_out_still_usable()
229     {
230         bool r = binbuf_out_binbuf.GetBytes().SequenceEqual(BaseSequence.Values());
231         r = r && binbuf_out_binbuf.Own;
232 
233         binbuf_out_binbuf.Dispose();
234         binbuf_out_binbuf = null;
235 
236         return r;
237     }
238 
239     private Eina.Binbuf binbuf_out_own_binbuf = null;
EinaBinbufOutOwn(out Eina.Binbuf binbuf)240     override public bool EinaBinbufOutOwn(out Eina.Binbuf binbuf)
241     {
242         binbuf_out_own_flag = true;
243 
244         binbuf = new Eina.Binbuf();
245         binbuf.Append(33);
246 
247         binbuf_out_own_binbuf = binbuf;
248 
249         return true;
250     }
binbuf_out_own_no_longer_own()251     public bool binbuf_out_own_no_longer_own()
252     {
253         bool r = !binbuf_out_own_binbuf.Own;
254         binbuf_out_own_binbuf.Dispose();
255         binbuf_out_own_binbuf = null;
256         return r;
257     }
258 
259     private Eina.Binbuf binbuf_return_binbuf = null;
EinaBinbufReturn()260     override public Eina.Binbuf EinaBinbufReturn()
261     {
262         binbuf_return_flag = true;
263 
264         var binbuf = new Eina.Binbuf();
265         binbuf.Append(33);
266 
267         binbuf_return_binbuf = binbuf;
268 
269         return binbuf;
270     }
binbuf_return_still_usable()271     public bool binbuf_return_still_usable()
272     {
273         bool r = binbuf_return_binbuf.GetBytes().SequenceEqual(BaseSequence.Values());
274         r = r && binbuf_return_binbuf.Own;
275 
276         binbuf_return_binbuf.Dispose();
277         binbuf_return_binbuf = null;
278 
279         return r;
280     }
281 
282     private Eina.Binbuf binbuf_return_own_binbuf = null;
EinaBinbufReturnOwn()283     override public Eina.Binbuf EinaBinbufReturnOwn()
284     {
285         binbuf_return_own_flag = true;
286 
287         var binbuf = new Eina.Binbuf();
288         binbuf.Append(33);
289 
290         binbuf_return_own_binbuf = binbuf;
291 
292         return binbuf;
293     }
binbuf_return_own_no_longer_own()294     public bool binbuf_return_own_no_longer_own()
295     {
296         bool r = !binbuf_return_own_binbuf.Own;
297         binbuf_return_own_binbuf.Dispose();
298         binbuf_return_own_binbuf = null;
299         return r;
300     }
301 }
302 
303 } // EinaTestData
304 
305 
306