Improve C# std::map tests to do the bulk of the testing on a map where the key and value types are different
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11232 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
07ed699ef3
commit
f3a70a1a03
2 changed files with 53 additions and 41 deletions
|
|
@ -24,55 +24,55 @@ public class li_std_map_runme {
|
|||
public static void Main()
|
||||
{
|
||||
// Set up an int int map
|
||||
IntIntMap iimap = new IntIntMap();
|
||||
StringIntMap simap = new StringIntMap();
|
||||
for (int i = 0; i < collectionSize; i++)
|
||||
{
|
||||
int val = i * 18;
|
||||
iimap.Add(i, val);
|
||||
simap.Add(i.ToString(), val);
|
||||
}
|
||||
|
||||
// Count property test
|
||||
if (iimap.Count != collectionSize)
|
||||
if (simap.Count != collectionSize)
|
||||
throw new Exception("Count test failed");
|
||||
|
||||
// IsReadOnly property test
|
||||
if (iimap.IsReadOnly)
|
||||
if (simap.IsReadOnly)
|
||||
throw new Exception("IsReadOnly test failed");
|
||||
|
||||
// Item indexing test
|
||||
iimap[0] = 200;
|
||||
if (iimap[0] != 200)
|
||||
simap["0"] = 200;
|
||||
if (simap["0"] != 200)
|
||||
throw new Exception("Item property test failed");
|
||||
iimap[0] = 0 * 18;
|
||||
simap["0"] = 0 * 18;
|
||||
|
||||
// ContainsKey() test
|
||||
for (int i = 0; i < collectionSize; i++)
|
||||
{
|
||||
if (!iimap.ContainsKey(i))
|
||||
if (!simap.ContainsKey(i.ToString()))
|
||||
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)))
|
||||
if (!simap.Contains(new KeyValuePair<string, int>(i.ToString(), i * 18)))
|
||||
throw new Exception("Contains test " + i + " failed");
|
||||
}
|
||||
|
||||
// TryGetValue() test
|
||||
int value;
|
||||
bool rc = iimap.TryGetValue(3, out value);
|
||||
bool rc = simap.TryGetValue("3", out value);
|
||||
if (rc != true || value != (3 * 18))
|
||||
throw new Exception("TryGetValue test 1 failed");
|
||||
|
||||
rc = iimap.TryGetValue(-1, out value);
|
||||
rc = simap.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);
|
||||
IList<string> keys = new List<string>(simap.Keys);
|
||||
IList<int> values = new List<int>(simap.Values);
|
||||
if (keys.Count != collectionSize)
|
||||
throw new Exception("Keys count test failed");
|
||||
|
||||
|
|
@ -81,7 +81,7 @@ public class li_std_map_runme {
|
|||
|
||||
for (int i = 0; i < keys.Count; i++)
|
||||
{
|
||||
if (iimap[keys[i]] != values[i])
|
||||
if (simap[keys[i]] != values[i])
|
||||
throw new Exception("Keys and values test failed for index " + i);
|
||||
}
|
||||
}
|
||||
|
|
@ -89,30 +89,30 @@ public class li_std_map_runme {
|
|||
// Add and Remove test
|
||||
for (int i = 100; i < 103; i++)
|
||||
{
|
||||
iimap.Add(i, i * 18);
|
||||
if (!iimap.ContainsKey(i) || iimap[i] != (i * 18))
|
||||
simap.Add(i.ToString(), i * 18);
|
||||
if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
|
||||
throw new Exception("Add test failed for index " + i);
|
||||
|
||||
iimap.Remove(i);
|
||||
if (iimap.ContainsKey(i))
|
||||
simap.Remove(i.ToString());
|
||||
if (simap.ContainsKey(i.ToString()))
|
||||
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))
|
||||
simap.Add(new KeyValuePair<string, int>(i.ToString(), i * 18));
|
||||
if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (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))
|
||||
simap.Remove(new KeyValuePair<string, int>(i.ToString(), i * 18));
|
||||
if (simap.ContainsKey(i.ToString()))
|
||||
throw new Exception("Remove explicit test failed for index " + i);
|
||||
}
|
||||
|
||||
// Duplicate key test
|
||||
try
|
||||
{
|
||||
iimap.Add(3, 0);
|
||||
simap.Add("3", 0);
|
||||
throw new Exception("Adding duplicate key test failed");
|
||||
}
|
||||
catch (ArgumentException)
|
||||
|
|
@ -121,29 +121,29 @@ public class li_std_map_runme {
|
|||
|
||||
// CopyTo() test
|
||||
{
|
||||
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[collectionSize];
|
||||
iimap.CopyTo(outputarray);
|
||||
foreach (KeyValuePair<int, int> val in outputarray)
|
||||
KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize];
|
||||
simap.CopyTo(outputarray);
|
||||
foreach (KeyValuePair<string, int> val in outputarray)
|
||||
{
|
||||
if (iimap[val.Key] != val.Value)
|
||||
if (simap[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);
|
||||
KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[midCollection + collectionSize];
|
||||
simap.CopyTo(outputarray, midCollection);
|
||||
for (int i = midCollection; i < midCollection + collectionSize; i++)
|
||||
{
|
||||
KeyValuePair<int, int> val = outputarray[i];
|
||||
if (iimap[val.Key] != val.Value)
|
||||
KeyValuePair<string, int> val = outputarray[i];
|
||||
if (simap[val.Key] != val.Value)
|
||||
throw new Exception("CopyTo (2) test failed, index:" + val.Key);
|
||||
}
|
||||
}
|
||||
{
|
||||
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[collectionSize - 1];
|
||||
KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize - 1];
|
||||
try
|
||||
{
|
||||
iimap.CopyTo(outputarray);
|
||||
simap.CopyTo(outputarray);
|
||||
throw new Exception("CopyTo (4) test failed");
|
||||
}
|
||||
catch (ArgumentException)
|
||||
|
|
@ -152,18 +152,22 @@ public class li_std_map_runme {
|
|||
}
|
||||
|
||||
// Clear test
|
||||
iimap.Clear();
|
||||
if (iimap.Count != 0)
|
||||
simap.Clear();
|
||||
if (simap.Count != 0)
|
||||
throw new Exception("Clear test failed");
|
||||
|
||||
// Test wrapped methods
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
iimap[i] = i;
|
||||
simap[i.ToString()] = i;
|
||||
}
|
||||
double avg = li_std_map.keyAverage(iimap);
|
||||
double avg = li_std_map.valueAverage(simap);
|
||||
if (avg != 3.0)
|
||||
throw new Exception("Wrapped method keyAverage test failed. Got " + avg);
|
||||
throw new Exception("Wrapped method valueAverage test failed. Got " + avg);
|
||||
|
||||
string keyStringified = li_std_map.stringifyKeys(simap);
|
||||
if (keyStringified != " 1 2 3 4 5")
|
||||
throw new Exception("Wrapped method stringifyKeys test failed. Got " + keyStringified);
|
||||
|
||||
// Test a map with a new specialized type (Struct)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -15,19 +15,27 @@
|
|||
// Add an inline function to test
|
||||
%inline %{
|
||||
|
||||
double keyAverage(std::map<int, int> m) {
|
||||
double valueAverage(std::map<std::string, 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;
|
||||
for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
|
||||
a += i->second;
|
||||
}
|
||||
|
||||
return a / m.size();
|
||||
}
|
||||
|
||||
std::string stringifyKeys(std::map<std::string, int> m) {
|
||||
std::string a;
|
||||
for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
|
||||
a += " " + i->first;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
struct Struct {
|
||||
double num;
|
||||
Struct() : num(0.0) {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue