diff --git a/Examples/test-suite/javascript/Makefile.in b/Examples/test-suite/javascript/Makefile.in index cce37720a..d04d1831b 100644 --- a/Examples/test-suite/javascript/Makefile.in +++ b/Examples/test-suite/javascript/Makefile.in @@ -23,7 +23,9 @@ CPP_TEST_CASES += \ inplaceadd \ input \ javascript_lib_arrays \ - li_factory + li_factory \ + li_std_containers_int \ + li_std_map_member SWIGEXE = $(top_builddir)/swig SWIG_LIB_DIR = $(top_srcdir)/Lib diff --git a/Examples/test-suite/javascript/li_attribute_template_runme.js b/Examples/test-suite/javascript/li_attribute_template_runme.js new file mode 100644 index 000000000..57a934e80 --- /dev/null +++ b/Examples/test-suite/javascript/li_attribute_template_runme.js @@ -0,0 +1,69 @@ +// Check usage of template attributes + +var li_attribute_template = require("li_attribute_template"); + +chell = new li_attribute_template.Cintint(1, 2, 3); + + +function rassert(what, master) { + if (what != master) { + throw new Error("what: {}".format(what)); + } +} + +// Testing primitive by value attribute +rassert(chell.a, 1); + +chell.a = 3; +rassert(chell.a, 3); + +// Testing primitive by ref attribute + +rassert(chell.b, 2); + +chell.b = 5; +rassert(chell.b, 5); + +// Testing string +chell.str = "abc"; +rassert(chell.str, "abc"); + +// Testing class by value + +rassert(chell.d.value, 1); + +chell.d = new li_attribute_template.Foo(2); +rassert(chell.d.value, 2); + +// Testing class by reference + +rassert(chell.e.value, 2); + +chell.e = new li_attribute_template.Foo(3); +rassert(chell.e.value, 3); + +chell.e.value = 4; +rassert(chell.e.value, 4); + +// Testing moderately complex template by value +rassert(chell.f.first, 1); +rassert(chell.f.second, 2); + +pair = new li_attribute_template.pair_intint(3, 4); +chell.f = pair; +rassert(chell.f.first, 3); +rassert(chell.f.second, 4); + +// Testing moderately complex template by ref +rassert(chell.g.first, 2); +rassert(chell.g.second, 3); + +pair = new li_attribute_template.pair_intint(4, 5); +chell.g = pair; +rassert(chell.g.first, 4); +rassert(chell.g.second, 5); + +chell.g.first = 6; +chell.g.second = 7; +rassert(chell.g.first, 6); +rassert(chell.g.second, 7); diff --git a/Examples/test-suite/javascript/li_std_containers_int_runme.js b/Examples/test-suite/javascript/li_std_containers_int_runme.js new file mode 100644 index 000000000..88688e35d --- /dev/null +++ b/Examples/test-suite/javascript/li_std_containers_int_runme.js @@ -0,0 +1,37 @@ +// In JS std::vector and std::list cannot behave as native containers +// because there is no overloading +var li_std_containers_int = require("li_std_containers_int"); + +function set_check(container, idx, value, size) { + container.set(idx, value); + if (container.get(idx) !== value) + throw new Error( + `Failed setting value at ${idx} in ${container.toString} to ${value}, got ${container.getitem(idx)}`); + if (container.size() != size) + throw new Error(`Expected a size of ${size}, got ${container.size()}`); +} + +function err_check(container, idx, value, size) { + let fail = true; + try { + container.set(idx, value); + } catch { + fail = false; + } + if (fail) throw new Error( + `Expected an exception setting value at ${idx} in ${container.toString} to ${value}, got ${container.get(idx)}`); + if (container.size() != size) + throw new Error(`Expected a size of ${size}, got ${container.size()}`); +} + +var vector; + +vector = new li_std_containers_int.vector_int(); +err_check(vector, 0, 10, 0); + +vector = new li_std_containers_int.vector_int(2); +set_check(vector, 0, 10, 2); +set_check(vector, 1, 0, 2); +err_check(vector, 2, 20, 2); +err_check(vector, 0, 'string', 2); +err_check(vector, 0, {}, 2); diff --git a/Examples/test-suite/javascript/li_std_map_member_runme.js b/Examples/test-suite/javascript/li_std_map_member_runme.js new file mode 100644 index 000000000..98e27454c --- /dev/null +++ b/Examples/test-suite/javascript/li_std_map_member_runme.js @@ -0,0 +1,13 @@ +var li_std_map_member = require("li_std_map_member"); + +a = new li_std_map_member.mapita(); +a.set(1, new li_std_map_member.TestA()); + +if ((a.get(1).i != 1)) { + throw new Error("a[1] != 1"); +} + +a.get(1).i = 2; +if ((a.get(1).i != 2)) { + throw new Error("a[1] != 2"); +} diff --git a/Examples/test-suite/javascript/li_std_map_runme.js b/Examples/test-suite/javascript/li_std_map_runme.js new file mode 100644 index 000000000..5f9b0f646 --- /dev/null +++ b/Examples/test-suite/javascript/li_std_map_runme.js @@ -0,0 +1,49 @@ +// In JavaScript, std::map has very limited capabilities and no iterator at all +var li_std_map = require("li_std_map"); + +function set_check(container, idx, value, size) { + container.set(idx, value); + if (container.get(idx) !== value) + throw new Error( + `Failed setting value at ${idx} in ${container.toString} to ${value}, got ${container.getitem(idx)}`); + if (container.size() != size) + throw new Error(`Expected a size of ${size}, got ${container.size()}`); +} + +function err_check(container, idx, value, size) { + let fail = true; + try { + container.set(idx, value); + } catch { + fail = false; + } + if (fail) throw new Error( + `Expected an exception setting value at ${idx} in ${container.toString} to ${value}, got ${container.get(idx)}`); + if (container.size() != size) + throw new Error(`Expected a size of ${size}, got ${container.size()}`); +} + +var map; + +map = new li_std_map.IntIntMap(); +set_check(map, 0, 10, 1); +set_check(map, 1, 0, 2); +set_check(map, 2, 20, 3); +err_check(map, 0, 'string', 3); + +if (!map.has_key(2)) throw new Error('Expected key to be found'); +if (map.has_key(5)) throw new Error('Expected key to not be found'); +map.del(2); +if (map.has_key(2)) throw new Error('Expected key to not be found'); + + +map = new li_std_map.StringIntMap(); +set_check(map, '0', 10, 1); +set_check(map, '1', 0, 2); +set_check(map, '2', 20, 3); +err_check(map, '0', 'string', 3); + +if (!map.has_key('2')) throw new Error('Expected key to be found'); +if (map.has_key('5')) throw new Error('Expected key to not be found'); +map.del('2'); +if (map.has_key('2')) throw new Error('Expected key to not be found'); diff --git a/Examples/test-suite/javascript/li_std_pair_runme.js b/Examples/test-suite/javascript/li_std_pair_runme.js new file mode 100644 index 000000000..a39b139f1 --- /dev/null +++ b/Examples/test-suite/javascript/li_std_pair_runme.js @@ -0,0 +1,46 @@ +var li_std_pair = require("li_std_pair"); + +function check(flag) { + if (!flag) { + throw new Error("Check failed"); + } +} + +intPair = li_std_pair.makeIntPair(7, 6); +check(typeof intPair === 'object'); +check(Object.keys(intPair).length == 2); +check(intPair.first === 7); +check(intPair.second === 6); + +intPairConstRef = li_std_pair.makeIntPairConstRef(7, 6); +check(typeof intPairConstRef === 'object'); +check(intPairConstRef.first === 7); +check(intPairConstRef.second === 6); + +// +// Each of these should return a reference to a wrapped +// std:: pair < int, int > object(i.e.an intPair instance). +intPairPtr = li_std_pair.makeIntPairPtr(7, 6); +check(typeof intPairPtr === 'object'); +check(intPairPtr.first === 7); +check(intPairPtr.second === 6); + +intPairRef = li_std_pair.makeIntPairRef(7, 6); +check(typeof intPairRef === 'object'); +check(intPairRef.first === 7); +check(intPairRef.second === 6); + +// Now test various input typemaps.Each of the wrapped C++ functions +//(product1, product2 and product3) is expecting an argument of a +// different type(see li_std_pair.i).Typemaps should be in place to +// convert this tuple into the expected argument type. +check(li_std_pair.product1(intPair) === 42); +check(li_std_pair.product2(intPair) === 42); +// check(product3(intPair) == 42) # TODO, if (desirable to match Ruby wrappers behaviour.Requires equivalent to typemap(in) std:: pair * in Lib / ruby / std_pair.i and further fixes to stop recursive calls to swig:) {asptr which this testcase shows.Plus further changes for any type of sequence type(including other STL containers) to be accepted by all methods taking an STL container to match Ruby behaviour. + +// +// Similarly, each of the input typemaps should know what to do +// with an intPair instance. +check(li_std_pair.product1(intPairPtr) === 42); +check(li_std_pair.product2(intPairPtr) === 42); +check(li_std_pair.product3(intPairPtr) === 42); diff --git a/Examples/test-suite/javascript/li_std_pair_using_runme.js b/Examples/test-suite/javascript/li_std_pair_using_runme.js new file mode 100644 index 000000000..5e9ac7283 --- /dev/null +++ b/Examples/test-suite/javascript/li_std_pair_using_runme.js @@ -0,0 +1,9 @@ +var li_std_pair_using = require("li_std_pair_using"); + +one = new li_std_pair_using.StringStringPair("one", "numero uno"); +two = new li_std_pair_using.StringIntPair("two", 2); + +tuple = li_std_pair_using.bounce(one); +if (typeof tuple !== 'object' && tuple.first !== one.first || tuple.second !== one.second) { + throw new Error; +} diff --git a/Examples/test-suite/javascript/li_std_vector_enum_runme.js b/Examples/test-suite/javascript/li_std_vector_enum_runme.js new file mode 100644 index 000000000..cc46cd842 --- /dev/null +++ b/Examples/test-suite/javascript/li_std_vector_enum_runme.js @@ -0,0 +1,21 @@ +var li_std_vector_enum = require("li_std_vector_enum"); + + +function check(a, b) { + if ((a != b)) { + throw new Error("Not equal: ", a, b); + } +} + +ev = new li_std_vector_enum.EnumVector(); + +check(ev.nums.get(0), 10); +check(ev.nums.get(1), 20); +check(ev.nums.get(2), 30); + +expected = 10; +for (let i = 0; i < ev.nums.size(); i++) { + let val = ev.nums.get(i); + check(val, expected); + expected += 10; +} diff --git a/Examples/test-suite/javascript/li_std_vector_runme.js b/Examples/test-suite/javascript/li_std_vector_runme.js new file mode 100644 index 000000000..500a4e59b --- /dev/null +++ b/Examples/test-suite/javascript/li_std_vector_runme.js @@ -0,0 +1,14 @@ +var li_std_vector = require("li_std_vector"); + +if (li_std_vector.typedef_test(101) != 101) { + throw new Error; +} + +let fail = false; +try { + sv = new li_std_vector.StructVector(); + sv.add(null); + fail = true; +} catch { } + +if (fail) throw new Error(); diff --git a/Examples/test-suite/li_std_containers_int.i b/Examples/test-suite/li_std_containers_int.i index d0c76d911..14dbd1e58 100644 --- a/Examples/test-suite/li_std_containers_int.i +++ b/Examples/test-suite/li_std_containers_int.i @@ -5,8 +5,14 @@ // %include std_vector.i + +#ifndef SWIGJAVASCRIPT %include std_list.i +#endif + %template(vector_int) std::vector; -%template(list_int) std::list; +#ifndef SWIGJAVASCRIPT +%template(list_int) std::list; +#endif