[ruby] add tests for unordered containers.

This commit is contained in:
Takashi Tamura 2017-01-29 11:18:53 +09:00
commit ec9b380c0c
3 changed files with 65 additions and 13 deletions

View file

@ -545,6 +545,7 @@ CPP11_TEST_CASES = \
cpp11_explicit_conversion_operators \ cpp11_explicit_conversion_operators \
cpp11_final_override \ cpp11_final_override \
cpp11_function_objects \ cpp11_function_objects \
cpp11_hash_tables \
cpp11_inheriting_constructors \ cpp11_inheriting_constructors \
cpp11_initializer_list \ cpp11_initializer_list \
cpp11_initializer_list_extend \ cpp11_initializer_list_extend \
@ -572,7 +573,6 @@ CPP11_TEST_CASES = \
# Broken C++11 test cases. # Broken C++11 test cases.
CPP11_TEST_BROKEN = \ CPP11_TEST_BROKEN = \
# cpp11_hash_tables \ # not fully implemented yet
# cpp11_variadic_templates \ # Broken for some languages (such as Java) # cpp11_variadic_templates \ # Broken for some languages (such as Java)
# cpp11_reference_wrapper \ # No typemaps # cpp11_reference_wrapper \ # No typemaps

View file

@ -4,19 +4,27 @@
%inline %{ %inline %{
#include <set> #include <set>
//#include <map> #include <map>
#include <unordered_set> #include <unordered_set>
//#include <unordered_map> #include <unordered_map>
%} %}
%include "std_set.i" %include "std_set.i"
//%include "std_map.i" %include "std_multiset.i"
%include "std_map.i"
%include "std_multimap.i"
%include "std_unordered_set.i" %include "std_unordered_set.i"
//%include "std_unordered_map.i" %include "std_unordered_multiset.i"
%include "std_unordered_map.i"
%include "std_unordered_multimap.i"
%template (SetInt) std::set<int>; %template (SetInt) std::set<int>;
//%template (MapIntInt) std::map<int, int>; %template (MultiSetInt) std::multiset<int>;
%template (MapIntInt) std::map<int, int>;
%template (MultiMapIntInt) std::multimap<int, int>;
%template (UnorderedSetInt) std::unordered_set<int>; %template (UnorderedSetInt) std::unordered_set<int>;
//%template (UnorderedMapIntInt) std::unordered_map<int, int>; %template (UnorderedMultiSetInt) std::unordered_multiset<int>;
%template (UnorderedMapIntInt) std::unordered_map<int, int>;
%template (UnorderedMultiMapIntInt) std::unordered_multimap<int, int>;
%inline %{ %inline %{
using namespace std; using namespace std;
@ -25,19 +33,19 @@ class MyClass {
public: public:
set<int> getSet() { return _set; } set<int> getSet() { return _set; }
void addSet(int elt) { _set.insert(_set.begin(), elt); } void addSet(int elt) { _set.insert(_set.begin(), elt); }
// map<int, int> getMap() { return _map; } map<int, int> getMap() { return _map; }
// void addMap(int elt1, int elt2) { _map.insert(make_pair(elt1, elt2)); } void addMap(int elt1, int elt2) { _map.insert(make_pair(elt1, elt2)); }
unordered_set<int> getUnorderedSet() { return _unordered_set; } unordered_set<int> getUnorderedSet() { return _unordered_set; }
void addUnorderedSet(int elt) { _unordered_set.insert(_unordered_set.begin(), elt); } void addUnorderedSet(int elt) { _unordered_set.insert(_unordered_set.begin(), elt); }
// unordered_map<int, int> getUnorderedMap() { return _unordered_map; } unordered_map<int, int> getUnorderedMap() { return _unordered_map; }
// void addUnorderedMap(int elt1, int elt2) { _unordered_map.insert(make_pair(elt1, elt2)); } void addUnorderedMap(int elt1, int elt2) { _unordered_map.insert(make_pair(elt1, elt2)); }
private: private:
set<int> _set; set<int> _set;
// map<int, int> _map; map<int, int> _map;
unordered_set<int> _unordered_set; unordered_set<int> _unordered_set;
// unordered_map<int, int> _unordered_map; unordered_map<int, int> _unordered_map;
}; };
%} %}

View file

@ -0,0 +1,44 @@
require 'swig_assert'
require 'cpp11_hash_tables'
[Cpp11_hash_tables::MapIntInt.new({1=>7}),
Cpp11_hash_tables::MultiMapIntInt.new({1=>7}),
Cpp11_hash_tables::UnorderedMapIntInt.new({1=>7}),
Cpp11_hash_tables::UnorderedMultiMapIntInt.new({1=>7})].each{|x|
swig_assert_equal("x[1]", "7", binding)
swig_assert_equal("x[2]", "nil", binding)
x[2] = 9
swig_assert_equal("x[2]", "9", binding)
x.delete(2)
swig_assert_equal("x[2]", "nil", binding)
swig_assert_equal("x.empty?", "false", binding)
x.delete(1)
swig_assert_equal("x.empty?", "true", binding)
swig_assert_equal("x.include?(1)", "false", binding)
}
[Cpp11_hash_tables::MultiMapIntInt.new({1=>7}),
Cpp11_hash_tables::UnorderedMultiMapIntInt.new({1=>7})].each{|x|
x[1] = 9
swig_assert_equal("x[1].sort", "[7,9]", binding)
}
[Cpp11_hash_tables::SetInt.new([1]),
Cpp11_hash_tables::MultiSetInt.new([1]),
Cpp11_hash_tables::UnorderedSetInt.new([1]),
Cpp11_hash_tables::UnorderedMultiSetInt.new([1])].each{|x|
swig_assert_equal("x.include?(1)", "true", binding)
swig_assert_equal("x.include?(2)", "false", binding)
x << 2
swig_assert_equal("x.include?(2)", "true", binding)
x.erase(2)
swig_assert_equal("x.empty?", "false", binding)
x.erase(1)
swig_assert_equal("x.empty?", "true", binding)
}
[Cpp11_hash_tables::MultiSetInt.new([1]),
Cpp11_hash_tables::UnorderedMultiSetInt.new([1])].each{|x|
x << 1
swig_assert_equal("x.count(1)", "2", binding)
}