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:
William S Fulton 2009-05-22 06:53:20 +00:00
commit f3a70a1a03
2 changed files with 53 additions and 41 deletions

View file

@ -24,55 +24,55 @@ public class li_std_map_runme {
public static void Main() public static void Main()
{ {
// Set up an int int map // Set up an int int map
IntIntMap iimap = new IntIntMap(); StringIntMap simap = new StringIntMap();
for (int i = 0; i < collectionSize; i++) for (int i = 0; i < collectionSize; i++)
{ {
int val = i * 18; int val = i * 18;
iimap.Add(i, val); simap.Add(i.ToString(), val);
} }
// Count property test // Count property test
if (iimap.Count != collectionSize) if (simap.Count != collectionSize)
throw new Exception("Count test failed"); throw new Exception("Count test failed");
// IsReadOnly property test // IsReadOnly property test
if (iimap.IsReadOnly) if (simap.IsReadOnly)
throw new Exception("IsReadOnly test failed"); throw new Exception("IsReadOnly test failed");
// Item indexing test // Item indexing test
iimap[0] = 200; simap["0"] = 200;
if (iimap[0] != 200) if (simap["0"] != 200)
throw new Exception("Item property test failed"); throw new Exception("Item property test failed");
iimap[0] = 0 * 18; simap["0"] = 0 * 18;
// ContainsKey() test // ContainsKey() test
for (int i = 0; i < collectionSize; i++) for (int i = 0; i < collectionSize; i++)
{ {
if (!iimap.ContainsKey(i)) if (!simap.ContainsKey(i.ToString()))
throw new Exception("ContainsKey test " + i + " failed"); throw new Exception("ContainsKey test " + i + " failed");
} }
// ContainsKey() test // ContainsKey() test
for (int i = 0; i < collectionSize; i++) 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"); throw new Exception("Contains test " + i + " failed");
} }
// TryGetValue() test // TryGetValue() test
int value; int value;
bool rc = iimap.TryGetValue(3, out value); bool rc = simap.TryGetValue("3", out value);
if (rc != true || value != (3 * 18)) if (rc != true || value != (3 * 18))
throw new Exception("TryGetValue test 1 failed"); throw new Exception("TryGetValue test 1 failed");
rc = iimap.TryGetValue(-1, out value); rc = simap.TryGetValue("-1", out value);
if (rc != false) if (rc != false)
throw new Exception("TryGetValue test 2 failed"); throw new Exception("TryGetValue test 2 failed");
// Keys and Values test // Keys and Values test
{ {
IList<int> keys = new List<int>(iimap.Keys); IList<string> keys = new List<string>(simap.Keys);
IList<int> values = new List<int>(iimap.Values); IList<int> values = new List<int>(simap.Values);
if (keys.Count != collectionSize) if (keys.Count != collectionSize)
throw new Exception("Keys count test failed"); 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++) 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); 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 // Add and Remove test
for (int i = 100; i < 103; i++) for (int i = 100; i < 103; i++)
{ {
iimap.Add(i, i * 18); simap.Add(i.ToString(), i * 18);
if (!iimap.ContainsKey(i) || iimap[i] != (i * 18)) if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
throw new Exception("Add test failed for index " + i); throw new Exception("Add test failed for index " + i);
iimap.Remove(i); simap.Remove(i.ToString());
if (iimap.ContainsKey(i)) if (simap.ContainsKey(i.ToString()))
throw new Exception("Remove test failed for index " + i); throw new Exception("Remove test failed for index " + i);
} }
for (int i = 200; i < 203; i++) for (int i = 200; i < 203; i++)
{ {
iimap.Add(new KeyValuePair<int, int>(i, i * 18)); simap.Add(new KeyValuePair<string, int>(i.ToString(), i * 18));
if (!iimap.ContainsKey(i) || iimap[i] != (i * 18)) if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
throw new Exception("Add explicit test failed for index " + i); throw new Exception("Add explicit test failed for index " + i);
iimap.Remove(new KeyValuePair<int, int>(i, i * 18)); simap.Remove(new KeyValuePair<string, int>(i.ToString(), i * 18));
if (iimap.ContainsKey(i)) if (simap.ContainsKey(i.ToString()))
throw new Exception("Remove explicit test failed for index " + i); throw new Exception("Remove explicit test failed for index " + i);
} }
// Duplicate key test // Duplicate key test
try try
{ {
iimap.Add(3, 0); simap.Add("3", 0);
throw new Exception("Adding duplicate key test failed"); throw new Exception("Adding duplicate key test failed");
} }
catch (ArgumentException) catch (ArgumentException)
@ -121,29 +121,29 @@ public class li_std_map_runme {
// CopyTo() test // CopyTo() test
{ {
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[collectionSize]; KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize];
iimap.CopyTo(outputarray); simap.CopyTo(outputarray);
foreach (KeyValuePair<int, int> val in 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); throw new Exception("CopyTo (1) test failed, index:" + val.Key);
} }
} }
{ {
KeyValuePair<int, int>[] outputarray = new KeyValuePair<int, int>[midCollection + collectionSize]; KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[midCollection + collectionSize];
iimap.CopyTo(outputarray, midCollection); simap.CopyTo(outputarray, midCollection);
for (int i = midCollection; i < midCollection + collectionSize; i++) for (int i = midCollection; i < midCollection + collectionSize; i++)
{ {
KeyValuePair<int, int> val = outputarray[i]; KeyValuePair<string, int> val = outputarray[i];
if (iimap[val.Key] != val.Value) if (simap[val.Key] != val.Value)
throw new Exception("CopyTo (2) test failed, index:" + val.Key); 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 try
{ {
iimap.CopyTo(outputarray); simap.CopyTo(outputarray);
throw new Exception("CopyTo (4) test failed"); throw new Exception("CopyTo (4) test failed");
} }
catch (ArgumentException) catch (ArgumentException)
@ -152,18 +152,22 @@ public class li_std_map_runme {
} }
// Clear test // Clear test
iimap.Clear(); simap.Clear();
if (iimap.Count != 0) if (simap.Count != 0)
throw new Exception("Clear test failed"); throw new Exception("Clear test failed");
// Test wrapped methods // Test wrapped methods
for (int i = 1; i <= 5; i++) 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) 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) // Test a map with a new specialized type (Struct)
{ {

View file

@ -15,19 +15,27 @@
// Add an inline function to test // Add an inline function to test
%inline %{ %inline %{
double keyAverage(std::map<int, int> m) { double valueAverage(std::map<std::string, int> m) {
if (m.size() == 0) { if (m.size() == 0) {
return 0.0; return 0.0;
} }
double a = 0.0; double a = 0.0;
for (std::map<int, int>::iterator i = m.begin(); i != m.end(); i++) { for (std::map<std::string, int>::iterator i = m.begin(); i != m.end(); i++) {
a += i->first; a += i->second;
} }
return a / m.size(); 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 { struct Struct {
double num; double num;
Struct() : num(0.0) {} Struct() : num(0.0) {}