Test non-default compare template argument in std::map

This commit is contained in:
William S Fulton 2018-10-09 22:43:19 +01:00
parent 967776189e
commit dcbccc6f6f
3 changed files with 47 additions and 0 deletions

View File

@ -240,6 +240,20 @@ public class li_std_map_runme {
throw new Exception("Key test (2) on complex key map failed");
}
// Custom compare function
{
StringLengthNumberMap slmap = new StringLengthNumberMap();
li_std_map.populate(slmap);
string keys = string.Join(" ", new List<string>(slmap.Keys));
if (keys != "a aa zzz xxxx aaaaa")
throw new Exception("Keys are wrong or in wrong order: " + keys);
string values = string.Join(" ", new List<int>(slmap.Values));
if (values != "1 2 3 4 5")
throw new Exception("Values are wrong or in wrong order: " + values);
}
// All done
}
}

View File

@ -117,4 +117,25 @@ namespace std {
}
}
%ignore LengthCompare::operator();
%inline %{
struct LengthCompare {
bool operator() (std::string s1, std::string s2) const {
return s1.size() < s2.size();
}
};
%}
// A map sorted by string lengths
%template(StringLengthNumberMap) std::map< std::string, int, LengthCompare >;
%inline %{
std::map< std::string, int, LengthCompare > MyMap;
void populate(std::map< std::string, int, LengthCompare >&m) {
m["aa"] = 2;
m["xxxx"] = 4;
m["a"] = 1;
m["aaaaa"] = 5;
m["zzz"] = 3;
}
%}

View File

@ -67,3 +67,15 @@ if [i for i in mii.itervalues()] != [2]:
raise RuntimeError("itervalues")
if [i for i in mii.iteritems()] != [(1, 2)]:
raise RuntimeError("iteritems")
slmap = li_std_map.StringLengthNumberMap()
li_std_map.populate(slmap)
keys = " ".join([k for k in slmap.keys()])
if keys != "a aa zzz xxxx aaaaa":
raise RuntimeError("Keys are wrong or in wrong order: " + keys)
values = " ".join([str(v) for v in slmap.values()])
if values != "1 2 3 4 5":
raise RuntimeError("Values are wrong or in wrong order: " + values)