[sdks] One more update to get XA PR builds working on Linux (#10853)
[mono-project.git] / sdks / wasm / bindings-test.cs
blobe41cb95f26fbceac639ea838eaa17261d9efd45f
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using System.Net.Http;
6 using System.Reflection;
8 using NUnit.Framework;
9 using WebAssembly;
11 public class TestClass {
12 public static int i32_res;
13 public static void InvokeI32 (int a, int b) {
14 i32_res = a + b;
17 public static float f32_res;
18 public static void InvokeFloat (float f) {
19 f32_res = f;
22 public static double f64_res;
23 public static void InvokeDouble (double d) {
24 f64_res = d;
27 public static long i64_res;
28 public static void InvokeLong (long l) {
29 i64_res = l;
32 public static string string_res;
33 public static void InvokeString (string s) {
34 string_res = s;
37 public static string mkstr;
38 public static string InvokeMkString() {
39 mkstr = "lalalala";
40 return mkstr;
43 public static int int_val;
44 public static void InvokeInt (int i) {
45 int_val = i;
48 public static object obj1;
49 public static object InvokeObj1(object obj)
51 obj1 = obj;
52 return obj;
55 public static object obj2;
56 public static object InvokeObj2(object obj)
58 obj2 = obj;
59 return obj;
62 public static object mkobj;
63 public static object InvokeMkobj()
65 mkobj = new object ();
66 return mkobj;
69 public static int first_val, second_val;
70 public static void PlayWithObj(JSObject obj) {
71 first_val = (int)obj.Invoke ("inc");;
72 second_val = (int)obj.Invoke("add", 20);
75 public static object[] js_objs;
76 public static void PlayWithObjTypes (JSObject obj) {
77 js_objs = new object[4];
78 js_objs [0] = obj.Invoke ("return_int");
79 js_objs [1] = obj.Invoke ("return_double");
80 js_objs [2] = obj.Invoke ("return_string");
81 js_objs [3] = obj.Invoke ("return_bool");
84 public static int do_add;
85 public static void UseFunction (JSObject obj) {
86 do_add = (int)obj.Invoke("call", null, 10, 20);
89 public static int dele_res;
90 public static Func<int, int, int> MkDelegate () {
91 return (a, b) => {
92 dele_res = a + b;
93 return dele_res;
97 public static TaskCompletionSource<int> tcs;
98 public static Task<int> task;
99 public static object MkTask () {
100 tcs = new TaskCompletionSource<int> ();
101 task = tcs.Task;
102 return task;
105 public static TaskCompletionSource<object> tcs3;
106 public static Task task3;
107 public static object MkTaskNull () {
108 tcs3 = new TaskCompletionSource<object> ();
109 task3 = tcs3.Task;
110 return task3;
113 public static Task<object> taskString;
114 public static object MkTaskString () {
115 tcs3 = new TaskCompletionSource<object> ();
116 taskString = tcs3.Task;
117 return taskString;
120 public static Task<object> the_promise;
121 public static void InvokePromise (object obj) {
122 the_promise = (Task<object>)obj;
123 the_promise.ContinueWith((t,o) => {
124 Console.WriteLine ("Promise result is {0}", t.Result);
125 }, null, TaskContinuationOptions.ExecuteSynchronously); //must be sync cuz the mainloop pump is gone
128 public static List<JSObject> js_objs_to_dispose = new List<JSObject>();
129 public static void DisposeObject(JSObject obj)
131 js_objs_to_dispose.Add(obj);
132 obj.Dispose();
135 public static object[] js_props;
136 public static void RetrieveObjectProperties (JSObject obj) {
137 js_props = new object[4];
138 js_props [0] = obj.GetObjectProperty ("myInt");
139 js_props [1] = obj.GetObjectProperty ("myDouble");
140 js_props [2] = obj.GetObjectProperty ("myString");
141 js_props [3] = obj.GetObjectProperty ("myBoolean");
144 public static void PopulateObjectProperties (JSObject obj, bool createIfNotExist) {
145 js_props = new object[4];
146 obj.SetObjectProperty ("myInt", 100, createIfNotExist);
147 obj.SetObjectProperty ("myDouble", 4.5, createIfNotExist);
148 obj.SetObjectProperty ("myString", "qwerty", createIfNotExist);
149 obj.SetObjectProperty ("myBoolean", true, createIfNotExist);
152 public static byte[] byteBuffer;
153 public static void MarshalByteBuffer (byte[] buffer) {
154 byteBuffer = buffer;
157 public static int[] intBuffer;
158 public static void MarshalInt32Array (int[] buffer) {
159 intBuffer = buffer;
162 public static void MarshalByteBufferToInts (byte[] buffer) {
163 intBuffer = new int[buffer.Length / sizeof(int)];
164 for (int i = 0; i < buffer.Length; i += sizeof(int))
165 intBuffer[i / sizeof(int)] = BitConverter.ToInt32(buffer, i);
168 public static float[] floatBuffer;
169 public static void MarshalFloat32Array (float[] buffer) {
170 floatBuffer = buffer;
173 public static void MarshalByteBufferToFloats (byte[] buffer) {
174 floatBuffer = new float[buffer.Length / sizeof(float)];
175 for (int i = 0; i < buffer.Length; i += sizeof(float))
176 floatBuffer[i / sizeof(float)] = BitConverter.ToSingle(buffer, i);
180 public static double[] doubleBuffer;
181 public static void MarshalFloat64Array (double[] buffer) {
182 doubleBuffer = buffer;
185 public static void MarshalByteBufferToDoubles (byte[] buffer) {
186 doubleBuffer = new double[buffer.Length / sizeof(double)];
187 for (int i = 0; i < buffer.Length; i += sizeof(double))
188 doubleBuffer[i / sizeof(double)] = BitConverter.ToDouble(buffer, i);
191 public static void SetTypedArraySByte (JSObject obj) {
192 sbyte[] buffer = Enumerable.Repeat((sbyte)0x20, 11).ToArray();
193 obj.SetObjectProperty ("typedArray", buffer);
196 public static sbyte[] taSByte;
197 public static void GetTypedArraySByte (JSObject obj) {
198 taSByte = (sbyte[])obj.GetObjectProperty ("typedArray");
201 public static void SetTypedArrayByte (JSObject obj) {
202 var dragons = "hic sunt dracones";
203 byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dragons);
204 obj.SetObjectProperty ("dracones", buffer);
207 public static byte[] taByte;
208 public static void GetTypedArrayByte (JSObject obj) {
209 taByte = (byte[])obj.GetObjectProperty ("dracones");
212 public static void SetTypedArrayShort (JSObject obj) {
213 short[] buffer = Enumerable.Repeat((short)0x20, 13).ToArray();
214 obj.SetObjectProperty ("typedArray", buffer);
217 public static short[] taShort;
218 public static void GetTypedArrayShort (JSObject obj) {
219 taShort = (short[])obj.GetObjectProperty ("typedArray");
222 public static void SetTypedArrayUShort (JSObject obj) {
223 ushort[] buffer = Enumerable.Repeat((ushort)0x20, 14).ToArray();
224 obj.SetObjectProperty ("typedArray", buffer);
227 public static ushort[] taUShort;
228 public static void GetTypedArrayUShort (JSObject obj) {
229 taUShort = (ushort[])obj.GetObjectProperty ("typedArray");
233 public static void SetTypedArrayInt (JSObject obj) {
234 int[] buffer = Enumerable.Repeat((int)0x20, 15).ToArray();
235 obj.SetObjectProperty ("typedArray", buffer);
238 public static int[] taInt;
239 public static void GetTypedArrayInt (JSObject obj) {
240 taInt = (int[])obj.GetObjectProperty ("typedArray");
243 public static void SetTypedArrayUInt (JSObject obj) {
244 uint[] buffer = Enumerable.Repeat((uint)0x20, 16).ToArray();
245 obj.SetObjectProperty ("typedArray", buffer);
248 public static uint[] taUInt;
249 public static void GetTypedArrayUInt (JSObject obj) {
250 taUInt = (uint[])obj.GetObjectProperty ("typedArray");
253 public static void SetTypedArrayFloat (JSObject obj) {
254 float[] buffer = Enumerable.Repeat(3.14f, 17).ToArray();
255 obj.SetObjectProperty ("typedArray", buffer);
258 public static float[] taFloat;
259 public static void GetTypedArrayFloat (JSObject obj) {
260 taFloat = (float[])obj.GetObjectProperty ("typedArray");
264 public static void SetTypedArrayDouble (JSObject obj) {
265 double[] buffer = Enumerable.Repeat(3.14d, 18).ToArray();
266 obj.SetObjectProperty ("typedArray", buffer);
269 public static double[] taDouble;
270 public static void GetTypedArrayDouble (JSObject obj) {
271 taDouble = (double[])obj.GetObjectProperty ("typedArray");
274 public static HttpClient client;
275 public static string fakeClientHandlerString;
276 public static HttpClientHandler fakeClientHandler;
277 public static void SetMessageHandler () {
279 var httpMessageHandler = typeof(HttpClient).GetField("GetHttpMessageHandler",
280 BindingFlags.Static |
281 BindingFlags.NonPublic);
283 httpMessageHandler.SetValue(null, (Func<HttpClientHandler>) (() => {
284 return new FakeHttpClientHandler ();
285 }));
287 client = new HttpClient();
293 public class FakeHttpClientHandler : HttpClientHandler
295 public FakeHttpClientHandler () : base()
297 TestClass.fakeClientHandlerString = "Fake HttpClientHandler";
298 TestClass.fakeClientHandler = this;
302 [TestFixture]
303 public class BindingTests {
304 [Test]
305 public static void MarshalPrimitivesToCS ()
307 TestClass.i32_res = 0;
308 Runtime.InvokeJS ("call_test_method(\"InvokeI32\", \"ii\", [10, 20])");
309 Assert.AreEqual (TestClass.i32_res, 30);
311 TestClass.f32_res = 0;
312 Runtime.InvokeJS ("call_test_method(\"InvokeFloat\", \"f\", [1.5])");
313 Assert.AreEqual (TestClass.f32_res, 1.5f);
315 TestClass.f64_res = 0;
316 Runtime.InvokeJS ("call_test_method(\"InvokeDouble\", \"d\", [4.5])");
317 Assert.AreEqual (TestClass.f64_res, 4.5);
319 TestClass.i64_res = 0;
320 Runtime.InvokeJS ("call_test_method(\"InvokeLong\", \"l\", [99])");
321 Assert.AreEqual (TestClass.i64_res, 99);
324 [Test]
325 public static void MarshalStringToCS ()
327 TestClass.string_res = null;
328 Runtime.InvokeJS ("call_test_method(\"InvokeString\", \"s\", [\"hello\"])");
329 Assert.AreEqual (TestClass.string_res, "hello");
332 [Test]
333 public static void MarshalStringToJS ()
335 TestClass.mkstr = TestClass.string_res = null;
336 Runtime.InvokeJS (@"
337 var str = call_test_method (""InvokeMkString"", ""o"", [ ]);
338 call_test_method (""InvokeString"", ""s"", [ str ]);
340 Assert.IsNotNull(TestClass.mkstr);
342 Assert.AreEqual (TestClass.mkstr, TestClass.string_res);
345 [Test]
346 public static void JSObjectKeepIdentityAcrossCalls ()
348 TestClass.obj1 = TestClass.obj2 = null;
349 Runtime.InvokeJS (@"
350 var obj = { foo: 10 };
351 var res = call_test_method (""InvokeObj1"", ""o"", [ obj ]);
352 call_test_method (""InvokeObj2"", ""o"", [ res ]);
355 Assert.IsNotNull(TestClass.obj1);
356 Assert.AreSame(TestClass.obj1, TestClass.obj2);
359 [Test]
360 public static void CSObjectKeepIdentityAcrossCalls ()
362 TestClass.mkobj = TestClass.obj1 = TestClass.obj2 = null;
363 Runtime.InvokeJS (@"
364 var obj = call_test_method (""InvokeMkobj"", """", [ ]);
365 var res = call_test_method (""InvokeObj1"", ""o"", [ obj ]);
366 call_test_method (""InvokeObj2"", ""o"", [ res ]);
369 Assert.IsNotNull(TestClass.obj1);
370 Assert.AreSame(TestClass.mkobj, TestClass.obj1);
371 Assert.AreSame(TestClass.obj1, TestClass.obj2);
374 [Test]
375 public static void JSInvokeInt() {
376 Runtime.InvokeJS (@"
377 var obj = {
378 foo: 10,
379 inc: function() {
380 var c = this.foo;
381 ++this.foo;
382 return c;
384 add: function(val){
385 return this.foo + val;
388 call_test_method (""PlayWithObj"", ""o"", [ obj ]);
391 Assert.AreEqual (TestClass.first_val, 10);
392 Assert.AreEqual (TestClass.second_val, 31);
395 [Test]
396 public static void JSInvokeTypes() {
397 Runtime.InvokeJS (@"
398 var obj = {
399 return_int: function() { return 100; },
400 return_double: function() { return 4.5; },
401 return_string: function() { return 'qwerty'; },
402 return_bool: function() { return true; },
404 call_test_method (""PlayWithObjTypes"", ""o"", [ obj ]);
407 Assert.AreEqual (TestClass.js_objs [0], 100);
408 Assert.AreEqual (TestClass.js_objs [1], 4.5);
409 Assert.AreEqual (TestClass.js_objs [2], "qwerty");
410 Assert.AreEqual (TestClass.js_objs [3], true);
413 [Test]
414 public static void JSObjectApply() {
415 Runtime.InvokeJS (@"
416 var do_add = function(a, b) { return a + b};
417 call_test_method (""UseFunction"", ""o"", [ do_add ]);
419 Assert.AreEqual (TestClass.do_add, 30);
422 [Test]
423 public static void MarshalDelegate() {
424 TestClass.obj1 = null;
425 Runtime.InvokeJS (@"
426 var dele = call_test_method (""MkDelegate"", """", [ ]);
427 var res = dele (10, 20);
428 call_test_method (""InvokeI32"", ""ii"", [ res, res ]);
431 Assert.AreEqual (TestClass.dele_res, 30);
432 Assert.AreEqual (TestClass.i32_res, 60);
435 [Test]
436 public static void PassTaskToJS () {
437 TestClass.int_val = 0;
438 Runtime.InvokeJS (@"
439 var tsk = call_test_method (""MkTask"", """", [ ]);
440 tsk.then (function (value) {
441 Module.print ('PassTaskToJS cont with value ' + value);
444 Assert.AreEqual (0, TestClass.int_val);
445 TestClass.tcs.SetResult (99);
446 //FIXME our test harness doesn't suppport async tests.
447 // So manually verify it for now by checking stdout for `PassTaskToJS cont with value 99`
448 //Assert.AreEqual (99, TestClass.int_val);
452 [Test]
453 public static void PassTaskToJS2 () {
454 TestClass.int_val = 0;
455 Runtime.InvokeJS (@"
456 var tsk = call_test_method (""MkTask"", """", [ ]);
457 tsk.then (function (value) {},
458 function (reason) {
459 Module.print ('PassTaskToJS2 cont failed due to ' + reason);
462 Assert.AreEqual (0, TestClass.int_val);
463 TestClass.tcs.SetException (new Exception ("it failed"));
464 //FIXME our test harness doesn't suppport async tests.
465 // So manually verify it for now by checking stdout for `PassTaskToJS2 cont failed due to System.AggregateException...
466 // Assert.AreEqual (99, TestClass.int_val);
469 [Test]
470 public static void PassTaskToJS3 () {
471 TestClass.int_val = 0;
472 Runtime.InvokeJS (@"
473 var tsk = call_test_method (""MkTaskNull"", """", [ ]);
474 tsk.then( () => {
475 Module.print('PassTaskToJS3 cont without value '); // Success!
476 }, reason => {
477 Module.print('PassTaskToJS3 cont failed due to ' + reason); // Error!
478 } );
480 Assert.AreEqual (0, TestClass.int_val);
481 TestClass.tcs3.SetResult (null);
484 [Test]
485 public static void PassTaskToJS4 () {
486 TestClass.int_val = 0;
487 Runtime.InvokeJS (@"
488 var tsk = call_test_method (""MkTaskNull"", """", [ ]);
489 tsk.then( value => {
490 Module.print(value); // Success!
491 }, reason => {
492 Module.print('PassTaskToJS4 cont failed due to ' + reason); // Error!
493 } );
495 Assert.AreEqual (0, TestClass.int_val);
496 TestClass.tcs3.SetException (new Exception ("it failed"));
499 [Test]
500 public static void PassTaskToJS5 () {
501 TestClass.int_val = 0;
502 Runtime.InvokeJS (@"
503 var tsk = call_test_method (""MkTaskString"", """", [ ]);
504 tsk.then( success => {
505 Module.print('PassTaskToJS5 cont with value ' + success); // Success!
506 }, reason => {
507 Module.print('PassTaskToJS5 cont failed due to ' + reason); // Error!
508 } );
510 Assert.AreEqual (0, TestClass.int_val);
511 TestClass.tcs3.SetResult ("Success");
514 [Test]
515 public static void PassTaskToJS6 () {
516 TestClass.int_val = 0;
517 Runtime.InvokeJS (@"
518 var tsk = call_test_method (""MkTaskString"", """", [ ]);
519 tsk.then( success => {
520 Module.print('PassTaskToJS6 cont with value ' + success); // Success!
521 }, reason => {
522 Module.print('PassTaskToJS6 cont failed due to ' + reason); // Error!
523 } );
525 Assert.AreEqual (0, TestClass.int_val);
526 TestClass.tcs3.SetException (new Exception ("it failed"));
529 [Test]
530 public static void PassPromiseToCS () {
531 TestClass.int_val = 0;
532 Runtime.InvokeJS (@"
533 var resolve_func = null;
534 var promise = new Promise(function (resolve, reject) {
535 resolve_func = resolve;
537 call_test_method (""InvokePromise"", ""o"", [ promise ]);
538 resolve_func (111);
540 //FIXME our test harness doesn't suppport async tests.
541 // So manually verify it for now by checking stdout for `Promise result is 111`
542 // Assert.AreEqual (99, TestClass.int_val);
545 [Test]
546 public static void BindStaticMethod () {
547 TestClass.int_val = 0;
548 Runtime.InvokeJS (@"
549 var invoke_int = Module.mono_bind_static_method (""[binding_tests]TestClass:InvokeInt"");
550 invoke_int (200);
553 Assert.AreEqual (200, TestClass.int_val);
556 [Test]
557 public static void InvokeStaticMethod () {
558 TestClass.int_val = 0;
559 Runtime.InvokeJS (@"
560 Module.mono_call_static_method (""[binding_tests]TestClass:InvokeInt"", [ 300 ]);
563 Assert.AreEqual (300, TestClass.int_val);
566 [Test]
567 public static void ResolveMethod () {
568 TestClass.int_val = 0;
569 Runtime.InvokeJS (@"
570 var invoke_int = Module.mono_method_resolve (""[binding_tests]TestClass:InvokeInt"");
571 call_test_method (""InvokeInt"", ""i"", [ invoke_int ]);
574 Assert.AreNotEqual (0, TestClass.int_val);
577 [Test]
578 public static void DisposeObject () {
579 Runtime.InvokeJS (@"
580 var obj1 = {
582 var obj2 = {
584 var obj3 = {
586 call_test_method (""DisposeObject"", ""o"", [ obj3 ]);
587 call_test_method (""DisposeObject"", ""o"", [ obj2 ]);
588 call_test_method (""DisposeObject"", ""o"", [ obj1 ]);
591 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [0].JSHandle);
592 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [1].JSHandle);
593 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [2].JSHandle);
596 [Test]
597 public static void GetObjectProperties () {
598 Runtime.InvokeJS (@"
599 var obj = {myInt: 100, myDouble: 4.5, myString: ""qwerty"", myBoolean: true};
600 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
603 Assert.AreEqual (100, TestClass.js_props [0]);
604 Assert.AreEqual (4.5, TestClass.js_props [1]);
605 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
606 Assert.AreEqual (true, TestClass.js_props [3]);
609 [Test]
610 public static void SetObjectProperties () {
611 Runtime.InvokeJS (@"
612 var obj = {myInt: 200, myDouble: 0, myString: ""foo"", myBoolean: false};
613 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, false ]);
614 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
617 Assert.AreEqual (100, TestClass.js_props [0]);
618 Assert.AreEqual (4.5, TestClass.js_props [1]);
619 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
620 Assert.AreEqual (true, TestClass.js_props [3]);
623 [Test]
624 public static void SetObjectPropertiesIfNotExistsFalse () {
625 // This test will not create the properties if they do not already exist
626 Runtime.InvokeJS (@"
627 var obj = {myInt: 200};
628 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, false ]);
629 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
632 Assert.AreEqual (100, TestClass.js_props [0]);
633 Assert.AreEqual (null, TestClass.js_props [1]);
634 Assert.AreEqual (null, TestClass.js_props [2]);
635 Assert.AreEqual (null, TestClass.js_props [3]);
638 [Test]
639 public static void SetObjectPropertiesIfNotExistsTrue () {
640 // This test will set the value of the property if it exists and will create and
641 // set the value if it does not exists
642 Runtime.InvokeJS (@"
643 var obj = {myInt: 200};
644 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, true ]);
645 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
648 Assert.AreEqual (100, TestClass.js_props [0]);
649 Assert.AreEqual (4.5, TestClass.js_props [1]);
650 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
651 Assert.AreEqual (true, TestClass.js_props [3]);
655 [Test]
656 public static void MarshalArrayBuffer () {
657 Runtime.InvokeJS (@"
658 var buffer = new ArrayBuffer(16);
659 call_test_method (""MarshalByteBuffer"", ""o"", [ buffer ]);
662 Assert.AreEqual (16, TestClass.byteBuffer.Length);
665 [Test]
666 public static void MarshalArrayBuffer2Int () {
667 // This really does not work to be honest
668 // The length of the marshalled array is 16 ints but
669 // the first 4 ints will be correct and the rest will
670 // probably be trash from memory
671 Runtime.InvokeJS (@"
672 var buffer = new ArrayBuffer(16);
673 var int32View = new Int32Array(buffer);
674 for (var i = 0; i < int32View.length; i++) {
675 int32View[i] = i * 2;
677 call_test_method (""MarshalInt32Array"", ""o"", [ buffer ]);
680 Assert.AreEqual (16, TestClass.intBuffer.Length);
681 Assert.AreEqual (0, TestClass.intBuffer[0]);
682 Assert.AreEqual (2, TestClass.intBuffer[1]);
683 Assert.AreEqual (4, TestClass.intBuffer[2]);
684 Assert.AreEqual (6, TestClass.intBuffer[3]);
687 [Test]
688 public static void MarshalArrayBuffer2Int2 () {
690 Runtime.InvokeJS (@"
691 var buffer = new ArrayBuffer(16);
692 var int32View = new Int32Array(buffer);
693 for (var i = 0; i < int32View.length; i++) {
694 int32View[i] = i * 2;
696 call_test_method (""MarshalByteBufferToInts"", ""o"", [ buffer ]);
699 Assert.AreEqual (4, TestClass.intBuffer.Length);
700 Assert.AreEqual (0, TestClass.intBuffer[0]);
701 Assert.AreEqual (2, TestClass.intBuffer[1]);
702 Assert.AreEqual (4, TestClass.intBuffer[2]);
703 Assert.AreEqual (6, TestClass.intBuffer[3]);
707 [Test]
708 public static void MarshalTypedArray () {
709 Runtime.InvokeJS (@"
710 var buffer = new ArrayBuffer(16);
711 var uint8View = new Uint8Array(buffer);
712 call_test_method (""MarshalByteBuffer"", ""o"", [ uint8View ]);
715 Assert.AreEqual (16, TestClass.byteBuffer.Length);
718 [Test]
719 public static void MarshalTypedArray2Int () {
720 Runtime.InvokeJS (@"
721 var buffer = new ArrayBuffer(16);
722 var int32View = new Int32Array(buffer);
723 for (var i = 0; i < int32View.length; i++) {
724 int32View[i] = i * 2;
726 call_test_method (""MarshalInt32Array"", ""o"", [ int32View ]);
729 Assert.AreEqual (4, TestClass.intBuffer.Length);
730 Assert.AreEqual (0, TestClass.intBuffer[0]);
731 Assert.AreEqual (2, TestClass.intBuffer[1]);
732 Assert.AreEqual (4, TestClass.intBuffer[2]);
733 Assert.AreEqual (6, TestClass.intBuffer[3]);
736 [Test]
737 public static void MarshalTypedArray2Float () {
738 Runtime.InvokeJS (@"
739 var typedArray = new Float32Array([1, 2.1334, 3, 4.2, 5]);
740 call_test_method (""MarshalFloat32Array"", ""o"", [ typedArray ]);
743 Assert.AreEqual (1, TestClass.floatBuffer[0]);
744 Assert.AreEqual (2.1334f, TestClass.floatBuffer[1]);
745 Assert.AreEqual (3, TestClass.floatBuffer[2]);
746 Assert.AreEqual (4.2f, TestClass.floatBuffer[3]);
747 Assert.AreEqual (5, TestClass.floatBuffer[4]);
750 [Test]
751 public static void MarshalArrayBuffer2Float () {
752 Runtime.InvokeJS (@"
753 var buffer = new ArrayBuffer(16);
754 var float32View = new Float32Array(buffer);
755 for (var i = 0; i < float32View.length; i++) {
756 float32View[i] = i * 2.5;
758 call_test_method (""MarshalByteBufferToFloats"", ""o"", [ buffer ]);
761 Assert.AreEqual (4, TestClass.floatBuffer.Length);
762 Assert.AreEqual (0, TestClass.floatBuffer[0]);
763 Assert.AreEqual (2.5f, TestClass.floatBuffer[1]);
764 Assert.AreEqual (5, TestClass.floatBuffer[2]);
765 Assert.AreEqual (7.5f, TestClass.floatBuffer[3]);
768 [Test]
769 public static void MarshalArrayBuffer2Float2 () {
770 // This really does not work to be honest
771 // The length of the marshalled array is 16 floats but
772 // the first 4 floats will be correct and the rest will
773 // probably be trash from memory
774 Runtime.InvokeJS (@"
775 var buffer = new ArrayBuffer(16);
776 var float32View = new Float32Array(buffer);
777 for (var i = 0; i < float32View.length; i++) {
778 float32View[i] = i * 2.5;
780 call_test_method (""MarshalFloat32Array"", ""o"", [ buffer ]);
783 Assert.AreEqual (16, TestClass.floatBuffer.Length);
784 Assert.AreEqual (0, TestClass.floatBuffer[0]);
785 Assert.AreEqual (2.5f, TestClass.floatBuffer[1]);
786 Assert.AreEqual (5, TestClass.floatBuffer[2]);
787 Assert.AreEqual (7.5f, TestClass.floatBuffer[3]);
790 [Test]
791 public static void MarshalTypedArray2Double () {
792 Runtime.InvokeJS (@"
793 var typedArray = new Float64Array([1, 2.1334, 3, 4.2, 5]);
794 call_test_method (""MarshalFloat64Array"", ""o"", [ typedArray ]);
797 Assert.AreEqual (1, TestClass.doubleBuffer[0]);
798 Assert.AreEqual (2.1334d, TestClass.doubleBuffer[1]);
799 Assert.AreEqual (3, TestClass.doubleBuffer[2]);
800 Assert.AreEqual (4.2d, TestClass.doubleBuffer[3]);
801 Assert.AreEqual (5, TestClass.doubleBuffer[4]);
804 [Test]
805 public static void MarshalArrayBuffer2Double () {
806 Runtime.InvokeJS (@"
807 var buffer = new ArrayBuffer(32);
808 var float64View = new Float64Array(buffer);
809 for (var i = 0; i < float64View.length; i++) {
810 float64View[i] = i * 2.5;
812 call_test_method (""MarshalByteBufferToDoubles"", ""o"", [ buffer ]);
815 Assert.AreEqual (4, TestClass.doubleBuffer.Length);
816 Assert.AreEqual (0, TestClass.doubleBuffer[0]);
817 Assert.AreEqual (2.5d, TestClass.doubleBuffer[1]);
818 Assert.AreEqual (5, TestClass.doubleBuffer[2]);
819 Assert.AreEqual (7.5d, TestClass.doubleBuffer[3]);
822 [Test]
823 public static void MarshalArrayBuffer2Double2 () {
824 // This really does not work to be honest
825 // The length of the marshalled array is 32 doubles but
826 // the first 4 doubles will be correct and the rest will
827 // probably be trash from memory
828 Runtime.InvokeJS (@"
829 var buffer = new ArrayBuffer(32);
830 var float64View = new Float64Array(buffer);
831 for (var i = 0; i < float64View.length; i++) {
832 float64View[i] = i * 2.5;
834 call_test_method (""MarshalFloat64Array"", ""o"", [ buffer ]);
837 Assert.AreEqual (32, TestClass.doubleBuffer.Length);
838 Assert.AreEqual (0, TestClass.doubleBuffer[0]);
839 Assert.AreEqual (2.5f, TestClass.doubleBuffer[1]);
840 Assert.AreEqual (5, TestClass.doubleBuffer[2]);
841 Assert.AreEqual (7.5f, TestClass.doubleBuffer[3]);
844 [Test]
845 public static void MarshalTypedArraySByte () {
846 TestClass.int_val = 0;
847 Runtime.InvokeJS (@"
848 var obj = { };
849 call_test_method (""SetTypedArraySByte"", ""o"", [ obj ]);
850 call_test_method (""GetTypedArraySByte"", ""o"", [ obj ]);
852 Assert.AreEqual (11, TestClass.taSByte.Length);
853 Assert.AreEqual (32, TestClass.taSByte[0]);
854 Assert.AreEqual (32, TestClass.taSByte[TestClass.taSByte.Length - 1]);
857 [Test]
858 public static void MarshalTypedArrayByte () {
859 TestClass.int_val = 0;
860 Runtime.InvokeJS (@"
861 var obj = { };
862 call_test_method (""SetTypedArrayByte"", ""o"", [ obj ]);
863 call_test_method (""GetTypedArrayByte"", ""o"", [ obj ]);
865 Assert.AreEqual (17, TestClass.taByte.Length);
866 Assert.AreEqual (104, TestClass.taByte[0]);
867 Assert.AreEqual (115, TestClass.taByte[TestClass.taByte.Length - 1]);
868 Assert.AreEqual ("hic sunt dracones", System.Text.Encoding.Default.GetString(TestClass.taByte));
871 [Test]
872 public static void MarshalTypedArrayShort () {
873 TestClass.int_val = 0;
874 Runtime.InvokeJS (@"
875 var obj = { };
876 call_test_method (""SetTypedArrayShort"", ""o"", [ obj ]);
877 call_test_method (""GetTypedArrayShort"", ""o"", [ obj ]);
879 Assert.AreEqual (13, TestClass.taShort.Length);
880 Assert.AreEqual (32, TestClass.taShort[0]);
881 Assert.AreEqual (32, TestClass.taShort[TestClass.taShort.Length - 1]);
884 [Test]
885 public static void MarshalTypedArrayUShort () {
886 TestClass.int_val = 0;
887 Runtime.InvokeJS (@"
888 var obj = { };
889 call_test_method (""SetTypedArrayUShort"", ""o"", [ obj ]);
890 call_test_method (""GetTypedArrayUShort"", ""o"", [ obj ]);
892 Assert.AreEqual (14, TestClass.taUShort.Length);
893 Assert.AreEqual (32, TestClass.taUShort[0]);
894 Assert.AreEqual (32, TestClass.taUShort[TestClass.taUShort.Length - 1]);
898 [Test]
899 public static void MarshalTypedArrayInt () {
900 TestClass.int_val = 0;
901 Runtime.InvokeJS (@"
902 var obj = { };
903 call_test_method (""SetTypedArrayInt"", ""o"", [ obj ]);
904 call_test_method (""GetTypedArrayInt"", ""o"", [ obj ]);
906 Assert.AreEqual (15, TestClass.taInt.Length);
907 Assert.AreEqual (32, TestClass.taInt[0]);
908 Assert.AreEqual (32, TestClass.taInt[TestClass.taInt.Length - 1]);
911 [Test]
912 public static void MarshalTypedArrayUInt () {
913 TestClass.int_val = 0;
914 Runtime.InvokeJS (@"
915 var obj = { };
916 call_test_method (""SetTypedArrayUInt"", ""o"", [ obj ]);
917 call_test_method (""GetTypedArrayUInt"", ""o"", [ obj ]);
919 Assert.AreEqual (16, TestClass.taUInt.Length);
920 Assert.AreEqual (32, TestClass.taUInt[0]);
921 Assert.AreEqual (32, TestClass.taUInt[TestClass.taUInt.Length - 1]);
924 [Test]
925 public static void MarshalTypedArrayFloat () {
926 TestClass.int_val = 0;
927 Runtime.InvokeJS (@"
928 var obj = { };
929 call_test_method (""SetTypedArrayFloat"", ""o"", [ obj ]);
930 call_test_method (""GetTypedArrayFloat"", ""o"", [ obj ]);
932 Assert.AreEqual (17, TestClass.taFloat.Length);
933 Assert.AreEqual (3.14f, TestClass.taFloat[0]);
934 Assert.AreEqual (3.14f, TestClass.taFloat[TestClass.taFloat.Length - 1]);
938 [Test]
939 public static void MarshalTypedArrayDouble () {
940 TestClass.int_val = 0;
941 Runtime.InvokeJS (@"
942 var obj = { };
943 call_test_method (""SetTypedArrayDouble"", ""o"", [ obj ]);
944 call_test_method (""GetTypedArrayDouble"", ""o"", [ obj ]);
946 Assert.AreEqual (18, TestClass.taDouble.Length);
947 Assert.AreEqual (3.14d, TestClass.taDouble[0]);
948 Assert.AreEqual (3.14d, TestClass.taDouble[TestClass.taDouble.Length - 1]);
952 [Test]
953 public static void HttpMessageHandler () {
954 TestClass.fakeClientHandlerString = string.Empty;
955 TestClass.fakeClientHandler = null;
956 TestClass.client = null;
957 Runtime.InvokeJS (@"
958 call_test_method (""SetMessageHandler"", ""o"", [ ]);
960 Assert.AreEqual ("Fake HttpClientHandler", TestClass.fakeClientHandlerString);
961 Assert.AreNotEqual (null, TestClass.fakeClientHandler);
962 Assert.AreEqual (typeof(FakeHttpClientHandler), TestClass.fakeClientHandler.GetType());
963 Assert.AreNotEqual (null, TestClass.client);