std::map improvements based on patch from Yuval Baror
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11230 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
6ae7ce1cf1
commit
30335a0198
8 changed files with 946 additions and 206 deletions
239
Examples/test-suite/csharp/li_std_map_runme.cs
Normal file
239
Examples/test-suite/csharp/li_std_map_runme.cs
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* See the LICENSE file for information on copyright, usage and redistribution
|
||||
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
|
||||
*
|
||||
* li_std_map_runme.cs
|
||||
*
|
||||
* SWIG C# tester for std_map.i
|
||||
* Implementation by Yuval Baror (http://yuval.bar-or.org)
|
||||
*
|
||||
* This class tests all the functionality of the std_map.i wrapper.
|
||||
* Upon successful testing, the main function doesn't print out anything.
|
||||
* If any error is found - it will be printed on the screen.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using li_std_mapNamespace;
|
||||
|
||||
public class li_std_map_runme {
|
||||
|
||||
private static readonly int collectionSize = 20;
|
||||
private static readonly int midCollection = collectionSize / 2;
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
// Set up an int int map
|
||||
IntIntMap iimap = new IntIntMap();
|
||||
for (int i = 0; i < collectionSize; i++)
|
||||
{
|
||||
int val = i * 18;
|
||||
iimap.Add(i, val);
|
||||
}
|
||||
|
||||
// Count property test
|
||||
if (iimap.Count != collectionSize)
|
||||
throw new Exception("Count test failed");
|
||||
|
||||
// IsReadOnly property test
|
||||
if (iimap.IsReadOnly)
|
||||
throw new Exception("IsReadOnly test failed");
|
||||
|
||||
// Item indexing test
|
||||
iimap[0] = 200;
|
||||
if (iimap[0] != 200)
|
||||
throw new Exception("Item property test failed");
|
||||
iimap[0] = 0 * 18;
|
||||
|
||||
// ContainsKey() test
|
||||
for (int i = 0; i < collectionSize; i++)
|
||||
{
|
||||
if (!iimap.ContainsKey(i))
|
||||
throw new Exception("ContainsKey test " + i + " failed");
|
||||
}
|
||||
|
||||
// ContainsKey() test
|
||||
for (int i = 0; i < collectionSize; i++)
|
||||
{
|
||||
if (!iimap.Contains(new KeyValuePair<int, int>(i, i * 18)))
|
||||
throw new Exception("Contains test " + i + " failed");
|
||||
}
|
||||
|
||||
// TryGetValue() test
|
||||
int value;
|
||||
bool rc = iimap.TryGetValue(3, out value);
|
||||
if (rc != true || value != (3 * 18))
|
||||
throw new Exception("TryGetValue test 1 failed");
|
||||
|
||||
rc = iimap.TryGetValue(-1, out value);
|
||||
if (rc != false)
|
||||
throw new Exception("TryGetValue test 2 failed");
|
||||
|
||||
// Keys and Values test
|
||||
{
|
||||
IList<int> keys = new List<int>(iimap.Keys);
|
||||
IList<int> values = new List<int>(iimap.Values);
|
||||
if (keys.Count != collectionSize)
|
||||
throw new Exception("Keys count test failed");
|
||||
|
||||
if (values.Count != collectionSize)
|
||||
throw new Exception("Values count test failed");
|
||||
|
||||
for (int i = 0; i < keys.Count; i++)
|
||||
{
|
||||
if (iimap[keys[i]] != values[i])
|
||||
throw new Exception("Keys and values test failed for index " + i);
|
||||
}
|
||||
}
|
||||
|
||||
// Add and Remove test
|
||||
for (int i = 100; i < 103; i++)
|
||||
{
|
||||
iimap.Add(i, i * 18);
|
||||
if (!iimap.ContainsKey(i) || iimap[i] != (i * 18))
|
||||
throw new Exception("Add test failed for index " + i);
|
||||
|
||||
iimap.Remove(i);
|
||||
if (iimap.ContainsKey(i))
|
||||
throw new Exception("Remove test failed for index " + i);
|
||||
}
|
||||
|
||||
for (int i = 200; i < 203; i++)
|
||||
{
|
||||
iimap.Add(new KeyValuePair<int, int>(i, i * 18));
|
||||
if (!iimap.ContainsKey(i) || iimap[i] != (i * 18))
|
||||
throw new Exception("Add explicit test failed for index " + i);
|
||||
|
||||
iimap.Remove(new KeyValuePair<int, int>(i, i * 18));
|
||||
if (iimap.ContainsKey(i))
|
||||
throw new Exception("Remove explicit test failed for index " + i);
|
||||
}
|
||||
|
||||
// Duplicate key test
|
||||
try
|
||||
{
|
||||
iimap.Add(3, 0);
|
||||
throw new Exception("Adding duplicate key test failed");
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
|
||||
// CopyTo() test
|
||||
{
|
||||
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[collectionSize];
|
||||
iimap.CopyTo(outputarray);
|
||||
foreach (KeyValuePair<int, int> val in outputarray)
|
||||
{
|
||||
if (iimap[val.Key] != val.Value)
|
||||
throw new Exception("CopyTo (1) test failed, index:" + val.Key);
|
||||
}
|
||||
}
|
||||
{
|
||||
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[midCollection + collectionSize];
|
||||
iimap.CopyTo(outputarray, midCollection);
|
||||
for (int i = midCollection; i < midCollection + collectionSize; i++)
|
||||
{
|
||||
KeyValuePair<int, int> val = outputarray[i];
|
||||
if (iimap[val.Key] != val.Value)
|
||||
throw new Exception("CopyTo (2) test failed, index:" + val.Key);
|
||||
}
|
||||
}
|
||||
{
|
||||
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[collectionSize - 1];
|
||||
try
|
||||
{
|
||||
iimap.CopyTo(outputarray);
|
||||
throw new Exception("CopyTo (4) test failed");
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// Clear test
|
||||
iimap.Clear();
|
||||
if (iimap.Count != 0)
|
||||
throw new Exception("Clear test failed");
|
||||
|
||||
// Test wrapped methods
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
iimap[i] = i;
|
||||
}
|
||||
double avg = li_std_map.keyAverage(iimap);
|
||||
if (avg != 3.0)
|
||||
throw new Exception("Wrapped method keyAverage test failed. Got " + avg);
|
||||
|
||||
// Test a map with a new specialized type (Struct)
|
||||
{
|
||||
IntStructMap ismap = new IntStructMap();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ismap.Add(i, new Struct(i * 10.1));
|
||||
}
|
||||
|
||||
if (ismap.Count != 10)
|
||||
throw new Exception("Count test on specialized map failed");
|
||||
|
||||
foreach (KeyValuePair<int, Struct> p in ismap)
|
||||
{
|
||||
if ((p.Key * 10.1) != p.Value.num)
|
||||
throw new Exception("Iteration test on specialized map failed for index " + p.Key);
|
||||
}
|
||||
}
|
||||
|
||||
// Test a map of pointers
|
||||
{
|
||||
IntStructPtrMap ispmap = new IntStructPtrMap();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ispmap.Add(i, new Struct(i * 10.1));
|
||||
}
|
||||
|
||||
if (ispmap.Count != 10)
|
||||
throw new Exception("Count test on specialized pointer map failed");
|
||||
|
||||
foreach (KeyValuePair<int, Struct> p in ispmap)
|
||||
{
|
||||
if ((p.Key * 10.1) != p.Value.num)
|
||||
throw new Exception("Iteration test on specialized pointer map failed for index " + p.Key);
|
||||
}
|
||||
}
|
||||
{
|
||||
IntStructConstPtrMap iscpmap = new IntStructConstPtrMap();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
iscpmap.Add(i, new Struct(i * 10.1));
|
||||
}
|
||||
|
||||
if (iscpmap.Count != 10)
|
||||
throw new Exception("Count test on specialized const pointer map failed");
|
||||
|
||||
foreach (KeyValuePair<int, Struct> p in iscpmap)
|
||||
{
|
||||
if ((p.Key * 10.1) != p.Value.num)
|
||||
throw new Exception("Iteration test on specialized const pointer map failed for index " + p.Key);
|
||||
}
|
||||
}
|
||||
|
||||
// Test non-specialized map
|
||||
{
|
||||
StructIntMap limap = new StructIntMap();
|
||||
Struct s7 = new Struct(7);
|
||||
Struct s8 = new Struct(8);
|
||||
limap.set(s7 , 8);
|
||||
if (limap.get(s7) != 8)
|
||||
throw new Exception("Assignment test on non-specialized map failed");
|
||||
|
||||
if (!limap.has_key(s7))
|
||||
throw new Exception("Key test (1) on non-specialized map failed");
|
||||
|
||||
if (limap.has_key(s8))
|
||||
throw new Exception("Key test (2) on non-specialized map failed");
|
||||
}
|
||||
|
||||
// All done
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,40 +1,84 @@
|
|||
/**
|
||||
* @file li_std_map.i
|
||||
* @author gga
|
||||
* @date Mon Apr 30 15:03:58 2007
|
||||
*
|
||||
* @brief a test of map containers.
|
||||
* Languages should define swig::LANGUAGE_OBJ to be
|
||||
* an entity of their native pointer type which can be
|
||||
* included in a STL container.
|
||||
*
|
||||
* For example:
|
||||
* swig::LANGUAGE_OBJ is GC_VALUE in Ruby
|
||||
* swig::LANGUAGE_OBJ is SwigPtr_PyObject in python
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
%module("templatereduce") li_std_map
|
||||
|
||||
%feature("trackobjects");
|
||||
|
||||
%include std_pair.i
|
||||
%include std_map.i
|
||||
%include "std_pair.i"
|
||||
%include "std_map.i"
|
||||
%include "std_string.i"
|
||||
|
||||
// Declare some maps to play around with
|
||||
%template(IntIntMap) std::map<int, int>;
|
||||
%template(StringIntMap) std::map<std::string, int>;
|
||||
|
||||
%ignore Struct::operator<;
|
||||
%ignore Struct::operator==;
|
||||
|
||||
// Add an inline function to test
|
||||
%inline %{
|
||||
struct A{
|
||||
int val;
|
||||
|
||||
A(int v = 0): val(v)
|
||||
{
|
||||
}
|
||||
|
||||
double keyAverage(std::map<int, int> m) {
|
||||
if (m.size() == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
double a = 0.0;
|
||||
for (std::map<int, int>::iterator i = m.begin(); i != m.end(); i++) {
|
||||
a += i->first;
|
||||
}
|
||||
|
||||
return a / m.size();
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
double num;
|
||||
Struct() : num(0.0) {}
|
||||
Struct(double d) : num(d) {}
|
||||
bool operator<(const Struct &other) const { return num < other.num; }
|
||||
bool operator==(const Struct &other) const { return num == other.num; }
|
||||
};
|
||||
|
||||
%}
|
||||
|
||||
namespace std
|
||||
{
|
||||
#if defined(SWIGCSHARP)
|
||||
|
||||
// Specialize some more non-default map types
|
||||
SWIG_STD_MAP_SPECIALIZED(int, int *, int, SWIGTYPE_p_int)
|
||||
SWIG_STD_MAP_SPECIALIZED(int, const int *, int, SWIGTYPE_p_int)
|
||||
SWIG_STD_MAP_SPECIALIZED_SIMPLE(int, Struct)
|
||||
SWIG_STD_MAP_SPECIALIZED(int, Struct *, int, Struct)
|
||||
SWIG_STD_MAP_SPECIALIZED(int, const Struct *, int, Struct)
|
||||
SWIG_STD_MAP_SPECIALIZED(Struct *, int, Struct, int)
|
||||
|
||||
#endif
|
||||
|
||||
//#if !defined(SWIGR)
|
||||
|
||||
// Test out some maps with pointer types
|
||||
%template(IntIntPtrMap) std::map<int, int *>;
|
||||
%template(IntConstIntPtrMap) std::map<int, const int *>;
|
||||
|
||||
//#endif
|
||||
|
||||
|
||||
// Test out some maps with non-basic types and non-basic pointer types
|
||||
%template(IntStructMap) std::map<int, Struct>;
|
||||
%template(IntStructPtrMap) std::map<int, Struct *>;
|
||||
%template(IntStructConstPtrMap) std::map<int, const Struct *>;
|
||||
%template(StructPtrIntMap) std::map<Struct *, int>;
|
||||
|
||||
// Test out a non-specialized map
|
||||
%template(StructIntMap) std::map<Struct, int>;
|
||||
|
||||
// Additional map definitions for Ruby, Python and Octave tests
|
||||
%inline %{
|
||||
struct A{
|
||||
int val;
|
||||
|
||||
A(int v = 0): val(v) {
|
||||
}
|
||||
};
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
%template(pairii) pair<int, int>;
|
||||
%template(pairAA) pair<int, A>;
|
||||
%template(pairA) pair<int, A*>;
|
||||
|
|
@ -58,25 +102,14 @@ namespace std
|
|||
|
||||
}
|
||||
|
||||
%inline {
|
||||
std::pair<int, A*> p_identa(std::pair<int, A*> p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
%inline
|
||||
{
|
||||
std::pair<int, A*>
|
||||
p_identa(std::pair<int, A*> p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
std::map<int,A*> m_identa(const std::map<int,A*>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::map<int, A*> m_identa(const std::map<int,A*>& v) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace std
|
||||
{
|
||||
%template(mapii) map<int,int>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ for k in pm,
|
|||
endif
|
||||
endfor
|
||||
|
||||
mii = li_std_map.mapii();
|
||||
mii = li_std_map.IntIntMap();
|
||||
|
||||
mii{1} = 1;
|
||||
mii{1} = 2;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ for k in pm:
|
|||
|
||||
|
||||
|
||||
mii = li_std_map.mapii()
|
||||
mii = li_std_map.IntIntMap()
|
||||
|
||||
mii[1] = 1
|
||||
mii[1] = 2
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ m.each_key { |k| pm[k] = m[k] }
|
|||
m.each_key { |k| swig_assert_equal("pm[#{k.inspect}]", "m[#{k.inspect}]", binding) }
|
||||
EOF
|
||||
|
||||
mii = Li_std_map::Mapii.new
|
||||
mii = Li_std_map::IntIntMap.new
|
||||
|
||||
mii[1] = 1
|
||||
mii[1] = 2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue