diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index 8a98172ed..acb0ef2e3 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -44,6 +44,10 @@ CPP_TEST_CASES = \ java_typemaps_proxy \ java_typemaps_typewrapper \ li_std_list \ + li_std_map \ + li_std_set \ + li_std_unordered_map \ + li_std_unordered_set \ # li_boost_intrusive_ptr CPP11_TEST_CASES = \ diff --git a/Examples/test-suite/java/li_std_map_runme.java b/Examples/test-suite/java/li_std_map_runme.java new file mode 100644 index 000000000..3cc794ced --- /dev/null +++ b/Examples/test-suite/java/li_std_map_runme.java @@ -0,0 +1,101 @@ +import li_std_map.*; + +public class li_std_map_runme { + + static { + try { + System.loadLibrary("li_std_map"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringIntMap sim = new StringIntMap(); + IntIntMap iim = new IntIntMap(); + + if (!sim.empty()) throw new RuntimeException("Test (1) failed"); + if (!iim.empty()) throw new RuntimeException("Test (2) failed"); + + if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); + + try { + sim.get("key"); + throw new RuntimeException("Test (5) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.get(1); + throw new RuntimeException("Test (6) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + + sim.del("key"); + iim.del(1); + + if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (11) failed"); + if (!iim.empty()) throw new RuntimeException("Test (12) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + + try { + sim.del("key"); + throw new RuntimeException("Test (15) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.del(1); + throw new RuntimeException("Test (16) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + + sim.clear(); + iim.clear(); + + if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (21) failed"); + if (!iim.empty()) throw new RuntimeException("Test (22) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + + sim.set("key", 1); + sim.set("key2", 2); + iim.set(1, 1); + iim.set(2, 2); + + if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); + if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + + sim.set("key", 3); + iim.set(1, 3); + + if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); + if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + } +} diff --git a/Examples/test-suite/java/li_std_set_runme.java b/Examples/test-suite/java/li_std_set_runme.java new file mode 100644 index 000000000..61edd93dc --- /dev/null +++ b/Examples/test-suite/java/li_std_set_runme.java @@ -0,0 +1,40 @@ +import li_std_set.*; + +public class li_std_set_runme { + + static { + try { + System.loadLibrary("li_std_set"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringSet ss = new StringSet(); + + if (!ss.empty()) throw new RuntimeException("Test (1) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (2) failed"); + if (ss.has("key")) throw new RuntimeException("Test (3) failed"); + if (ss.erase("key")) throw new RuntimeException("Test (4) failed"); + + if (!ss.insert("key")) throw new RuntimeException("Test (5) failed"); + if (ss.insert("key")) throw new RuntimeException("Test (6) failed"); + if (!ss.has("key")) throw new RuntimeException("Test (7) failed"); + + if (!ss.erase("key")) throw new RuntimeException("Test (8) failed"); + if (!ss.empty()) throw new RuntimeException("Test (9) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (10) failed"); + + if (!ss.insert("key1")) throw new RuntimeException("Test (11) failed"); + if (!ss.insert("key2")) throw new RuntimeException("Test (12) failed"); + if (!ss.insert("key3")) throw new RuntimeException("Test (13) failed"); + if (ss.size() != 3) throw new RuntimeException("Test (14) failed"); + + ss.clear(); + if (!ss.empty()) throw new RuntimeException("Test (15) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (16) failed"); + } +} diff --git a/Examples/test-suite/java/li_std_unordered_map_runme.java b/Examples/test-suite/java/li_std_unordered_map_runme.java new file mode 100644 index 000000000..8ab6f23e4 --- /dev/null +++ b/Examples/test-suite/java/li_std_unordered_map_runme.java @@ -0,0 +1,101 @@ +import li_std_unordered_map.*; + +public class li_std_unordered_map_runme { + + static { + try { + System.loadLibrary("li_std_unordered_map"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringIntUnorderedMap sim = new StringIntUnorderedMap(); + IntIntUnorderedMap iim = new IntIntUnorderedMap(); + + if (!sim.empty()) throw new RuntimeException("Test (1) failed"); + if (!iim.empty()) throw new RuntimeException("Test (2) failed"); + + if (sim.size() != 0) throw new RuntimeException("Test (3) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (4) failed"); + + try { + sim.get("key"); + throw new RuntimeException("Test (5) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.get(1); + throw new RuntimeException("Test (6) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (7) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (8) failed"); + + sim.del("key"); + iim.del(1); + + if (sim.has_key("key")) throw new RuntimeException("Test (9) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (10) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (11) failed"); + if (!iim.empty()) throw new RuntimeException("Test (12) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (13) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (14) failed"); + + try { + sim.del("key"); + throw new RuntimeException("Test (15) failed"); + } catch (IndexOutOfBoundsException e) { + } + + try { + iim.del(1); + throw new RuntimeException("Test (16) failed"); + } catch (IndexOutOfBoundsException e) { + } + + sim.set("key", 1); + iim.set(1, 1); + + if (sim.size() != 1) throw new RuntimeException("Test (17) failed"); + if (iim.size() != 1) throw new RuntimeException("Test (18) failed"); + + sim.clear(); + iim.clear(); + + if (sim.has_key("key")) throw new RuntimeException("Test (19) failed"); + if (iim.has_key(1)) throw new RuntimeException("Test (20) failed"); + + if (!sim.empty()) throw new RuntimeException("Test (21) failed"); + if (!iim.empty()) throw new RuntimeException("Test (22) failed"); + if (sim.size() != 0) throw new RuntimeException("Test (23) failed"); + if (iim.size() != 0) throw new RuntimeException("Test (24) failed"); + + sim.set("key", 1); + sim.set("key2", 2); + iim.set(1, 1); + iim.set(2, 2); + + if (sim.get("key") != 1) throw new RuntimeException("Test (25) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (26) failed"); + if (iim.get(1) != 1) throw new RuntimeException("Test (27) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (28) failed"); + + sim.set("key", 3); + iim.set(1, 3); + + if (sim.get("key") != 3) throw new RuntimeException("Test (29) failed"); + if (sim.get("key2") != 2) throw new RuntimeException("Test (30) failed"); + if (iim.get(1) != 3) throw new RuntimeException("Test (31) failed"); + if (iim.get(2) != 2) throw new RuntimeException("Test (32) failed"); + } +} diff --git a/Examples/test-suite/java/li_std_unordered_set_runme.java b/Examples/test-suite/java/li_std_unordered_set_runme.java new file mode 100644 index 000000000..ce94f8fe4 --- /dev/null +++ b/Examples/test-suite/java/li_std_unordered_set_runme.java @@ -0,0 +1,40 @@ +import li_std_unordered_set.*; + +public class li_std_unordered_set_runme { + + static { + try { + System.loadLibrary("li_std_unordered_set"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) throws Throwable + { + StringUnorderedSet ss = new StringUnorderedSet(); + + if (!ss.empty()) throw new RuntimeException("Test (1) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (2) failed"); + if (ss.has("key")) throw new RuntimeException("Test (3) failed"); + if (ss.erase("key")) throw new RuntimeException("Test (4) failed"); + + if (!ss.insert("key")) throw new RuntimeException("Test (5) failed"); + if (ss.insert("key")) throw new RuntimeException("Test (6) failed"); + if (!ss.has("key")) throw new RuntimeException("Test (7) failed"); + + if (!ss.erase("key")) throw new RuntimeException("Test (8) failed"); + if (!ss.empty()) throw new RuntimeException("Test (9) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (10) failed"); + + if (!ss.insert("key1")) throw new RuntimeException("Test (11) failed"); + if (!ss.insert("key2")) throw new RuntimeException("Test (12) failed"); + if (!ss.insert("key3")) throw new RuntimeException("Test (13) failed"); + if (ss.size() != 3) throw new RuntimeException("Test (14) failed"); + + ss.clear(); + if (!ss.empty()) throw new RuntimeException("Test (15) failed"); + if (ss.size() != 0) throw new RuntimeException("Test (16) failed"); + } +} diff --git a/Examples/test-suite/li_std_set.i b/Examples/test-suite/li_std_set.i index 2dcc2f17c..fc9db42a9 100644 --- a/Examples/test-suite/li_std_set.i +++ b/Examples/test-suite/li_std_set.i @@ -1,5 +1,5 @@ /* - * a test of set containers. + * a test of set containers. * Languages should define swig::LANGUAGE_OBJ to be * an entity of their native pointer type which can be * included in a STL container. @@ -13,22 +13,23 @@ %include %include -%include %include -%template(set_string) std::set; -%template(set_int) std::multiset; - - -%template(v_int) std::vector; - - - +// Use language macros since Java doesn't have multiset support (yet) +// and uses different naming conventions. +#if defined(SWIGRUBY) || defined(SWIGPYTHON) + %include + %template(set_int) std::multiset; + %template(v_int) std::vector; + %template(set_string) std::set; +#elif defined(SWIGJAVA) + %template(StringSet) std::set; +#endif #if defined(SWIGRUBY) -%template(LanguageSet) std::set; +%template(LanguageSet) std::set; #endif #if defined(SWIGPYTHON) -%template(pyset) std::set; +%template(pyset) std::set; #endif diff --git a/Examples/test-suite/li_std_unordered_map.i b/Examples/test-suite/li_std_unordered_map.i new file mode 100644 index 000000000..a6869cc0b --- /dev/null +++ b/Examples/test-suite/li_std_unordered_map.i @@ -0,0 +1,12 @@ +/* + * A test of unordered_map containers. + */ + +%module li_std_unordered_map + +%include +%include + +%template(IntIntUnorderedMap) std::unordered_map; +%template(StringIntUnorderedMap) std::unordered_map; + diff --git a/Examples/test-suite/li_std_unordered_set.i b/Examples/test-suite/li_std_unordered_set.i new file mode 100644 index 000000000..a051e9389 --- /dev/null +++ b/Examples/test-suite/li_std_unordered_set.i @@ -0,0 +1,11 @@ +/* + * A test of unordered_set containers. + */ + +%module li_std_unordered_set + +%include +%include + +%template(StringUnorderedSet) std::unordered_set; + diff --git a/Lib/java/std_set.i b/Lib/java/std_set.i new file mode 100644 index 000000000..8c4bb7f17 --- /dev/null +++ b/Lib/java/std_set.i @@ -0,0 +1,47 @@ +/* ----------------------------------------------------------------------------- + * std_set.i + * + * SWIG typemaps for std::set + * ----------------------------------------------------------------------------- */ + +%include + +// ------------------------------------------------------------------------ +// std::set +// ------------------------------------------------------------------------ + +%{ +#include +%} + +namespace std { + +template +class set { + public: + set(); + set(const set&); + + unsigned int size() const; + bool empty() const; + void clear(); + + %extend { + // Returns whether item was inserted. + bool insert(const KeyType& key) { + return self->insert(key).second; + } + + // Returns whether set contains key. + bool has(const KeyType& key) { + return (self->count(key) > 0); + } + + // Returns whether the item was erased. + bool erase(const KeyType& key) { + return (self->erase(key) > 0); + } + } +}; + +} // namespace std \ No newline at end of file diff --git a/Lib/java/std_unordered_map.i b/Lib/java/std_unordered_map.i new file mode 100644 index 000000000..434292b07 --- /dev/null +++ b/Lib/java/std_unordered_map.i @@ -0,0 +1,65 @@ +/* ----------------------------------------------------------------------------- + * std_unordered_map.i + * + * SWIG typemaps for std::unordered_map + * ----------------------------------------------------------------------------- */ + +%include + +// ------------------------------------------------------------------------ +// std::unordered_map +// ------------------------------------------------------------------------ + +%{ +#include +#include +%} + +namespace std { + +template class unordered_map { + public: + typedef size_t size_type; + typedef ptrdiff_t difference_type; + typedef KeyType key_type; + typedef MappedType mapped_type; + unordered_map(); + unordered_map(const unordered_map&); + + unsigned int size() const; + bool empty() const; + void clear(); + + %extend { + const MappedType& get(const KeyType& key) throw (std::out_of_range) { + std::unordered_map::iterator i = self->find(key); + + if (i != self->end()) { + return i->second; + } else { + throw std::out_of_range("key not found"); + } + } + + void set(const KeyType& key, const MappedType& x) { + (*self)[key] = x; + } + + void del(const KeyType& key) throw (std::out_of_range) { + std::unordered_map::iterator i = self->find(key); + + if (i != self->end()) { + self->erase(i); + } else { + throw std::out_of_range("key not found"); + } + } + + bool has_key(const KeyType& key) { + std::unordered_map::iterator i = self->find(key); + return i != self->end(); + } + } +}; + +} // namespace std diff --git a/Lib/java/std_unordered_set.i b/Lib/java/std_unordered_set.i new file mode 100644 index 000000000..a9fac0f82 --- /dev/null +++ b/Lib/java/std_unordered_set.i @@ -0,0 +1,47 @@ +/* ----------------------------------------------------------------------------- + * std_unordered_set.i + * + * SWIG typemaps for std::unordered_set + * ----------------------------------------------------------------------------- */ + +%include + +// ------------------------------------------------------------------------ +// std::unordered_set +// ------------------------------------------------------------------------ + +%{ +#include +%} + +namespace std { + +template +class unordered_set { + public: + unordered_set(); + unordered_set(const unordered_set&); + + unsigned int size() const; + bool empty() const; + void clear(); + + %extend { + // Returns whether item was inserted. + bool insert(const KeyType& key) { + return self->insert(key).second; + } + + // Returns whether set contains key. + bool has(const KeyType& key) { + return (self->count(key) > 0); + } + + // Returns whether the item was erased. + bool erase(const KeyType& key) { + return (self->erase(key) > 0); + } + } +}; + +} // namespace std \ No newline at end of file