PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp/li_std_map_runme.cs

#
C# | 246 lines | 184 code | 37 blank | 25 comment | 66 complexity | db567de21f74461f67d2c6a378a7ef7c MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * li_std_map_runme.cs
  3. *
  4. * SWIG C# tester for std_map.i
  5. * This class tests all the functionality of the std_map.i wrapper.
  6. * Upon successful testing, the main function doesn't print out anything.
  7. * If any error is found - it will be printed on the screen.
  8. * ----------------------------------------------------------------------------- */
  9. using System;
  10. using System.Collections.Generic;
  11. using li_std_mapNamespace;
  12. public class li_std_map_runme {
  13. private static readonly int collectionSize = 20;
  14. private static readonly int midCollection = collectionSize / 2;
  15. public static void Main()
  16. {
  17. // Set up an int int map
  18. StringIntMap simap = new StringIntMap();
  19. for (int i = 0; i < collectionSize; i++)
  20. {
  21. int val = i * 18;
  22. simap.Add(i.ToString(), val);
  23. }
  24. // Count property test
  25. if (simap.Count != collectionSize)
  26. throw new Exception("Count test failed");
  27. // IsReadOnly property test
  28. if (simap.IsReadOnly)
  29. throw new Exception("IsReadOnly test failed");
  30. // Item indexing test
  31. simap["0"] = 200;
  32. if (simap["0"] != 200)
  33. throw new Exception("Item property test failed");
  34. simap["0"] = 0 * 18;
  35. // ContainsKey() test
  36. for (int i = 0; i < collectionSize; i++)
  37. {
  38. if (!simap.ContainsKey(i.ToString()))
  39. throw new Exception("ContainsKey test " + i + " failed");
  40. }
  41. // ContainsKey() test
  42. for (int i = 0; i < collectionSize; i++)
  43. {
  44. if (!simap.Contains(new KeyValuePair<string, int>(i.ToString(), i * 18)))
  45. throw new Exception("Contains test " + i + " failed");
  46. }
  47. // TryGetValue() test
  48. int value;
  49. bool rc = simap.TryGetValue("3", out value);
  50. if (rc != true || value != (3 * 18))
  51. throw new Exception("TryGetValue test 1 failed");
  52. rc = simap.TryGetValue("-1", out value);
  53. if (rc != false)
  54. throw new Exception("TryGetValue test 2 failed");
  55. // Keys and Values test
  56. {
  57. IList<string> keys = new List<string>(simap.Keys);
  58. IList<int> values = new List<int>(simap.Values);
  59. Dictionary<string, int> check = new Dictionary<string, int>();
  60. if (keys.Count != collectionSize)
  61. throw new Exception("Keys count test failed");
  62. if (values.Count != collectionSize)
  63. throw new Exception("Values count test failed");
  64. for (int i = 0; i < keys.Count; i++)
  65. {
  66. if (simap[keys[i]] != values[i])
  67. throw new Exception("Keys and values test failed for index " + i);
  68. check.Add(keys[i], values[i]);
  69. }
  70. for (int i = 0; i < collectionSize; i++)
  71. {
  72. if (!check.ContainsKey(i.ToString()))
  73. throw new Exception("Keys and Values ContainsKey test " + i + " failed");
  74. }
  75. }
  76. // Add and Remove test
  77. for (int i = 100; i < 103; i++)
  78. {
  79. simap.Add(i.ToString(), i * 18);
  80. if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
  81. throw new Exception("Add test failed for index " + i);
  82. simap.Remove(i.ToString());
  83. if (simap.ContainsKey(i.ToString()))
  84. throw new Exception("Remove test failed for index " + i);
  85. }
  86. for (int i = 200; i < 203; i++)
  87. {
  88. simap.Add(new KeyValuePair<string, int>(i.ToString(), i * 18));
  89. if (!simap.ContainsKey(i.ToString()) || simap[i.ToString()] != (i * 18))
  90. throw new Exception("Add explicit test failed for index " + i);
  91. simap.Remove(new KeyValuePair<string, int>(i.ToString(), i * 18));
  92. if (simap.ContainsKey(i.ToString()))
  93. throw new Exception("Remove explicit test failed for index " + i);
  94. }
  95. // Duplicate key test
  96. try
  97. {
  98. simap.Add("3", 0);
  99. throw new Exception("Adding duplicate key test failed");
  100. }
  101. catch (ArgumentException)
  102. {
  103. }
  104. // CopyTo() test
  105. {
  106. KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize];
  107. simap.CopyTo(outputarray);
  108. foreach (KeyValuePair<string, int> val in outputarray)
  109. {
  110. if (simap[val.Key] != val.Value)
  111. throw new Exception("CopyTo (1) test failed, index:" + val.Key);
  112. }
  113. }
  114. {
  115. KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[midCollection + collectionSize];
  116. simap.CopyTo(outputarray, midCollection);
  117. for (int i = midCollection; i < midCollection + collectionSize; i++)
  118. {
  119. KeyValuePair<string, int> val = outputarray[i];
  120. if (simap[val.Key] != val.Value)
  121. throw new Exception("CopyTo (2) test failed, index:" + val.Key);
  122. }
  123. }
  124. {
  125. KeyValuePair<string, int>[] outputarray = new KeyValuePair<string, int>[collectionSize - 1];
  126. try
  127. {
  128. simap.CopyTo(outputarray);
  129. throw new Exception("CopyTo (4) test failed");
  130. }
  131. catch (ArgumentException)
  132. {
  133. }
  134. }
  135. // Clear test
  136. simap.Clear();
  137. if (simap.Count != 0)
  138. throw new Exception("Clear test failed");
  139. // Test wrapped methods
  140. for (int i = 1; i <= 5; i++)
  141. {
  142. simap[i.ToString()] = i;
  143. }
  144. double avg = li_std_map.valueAverage(simap);
  145. if (avg != 3.0)
  146. throw new Exception("Wrapped method valueAverage test failed. Got " + avg);
  147. string keyStringified = li_std_map.stringifyKeys(simap);
  148. if (keyStringified != " 1 2 3 4 5")
  149. throw new Exception("Wrapped method stringifyKeys test failed. Got " + keyStringified);
  150. // Test a map with a new complex type (Struct)
  151. {
  152. IntStructMap ismap = new IntStructMap();
  153. for (int i = 0; i < 10; i++)
  154. {
  155. ismap.Add(i, new Struct(i * 10.1));
  156. }
  157. if (ismap.Count != 10)
  158. throw new Exception("Count test on complex type map failed");
  159. foreach (KeyValuePair<int, Struct> p in ismap)
  160. {
  161. if ((p.Key * 10.1) != p.Value.num)
  162. throw new Exception("Iteration test on complex type map failed for index " + p.Key);
  163. }
  164. }
  165. // Test a map of pointers
  166. {
  167. IntStructPtrMap ispmap = new IntStructPtrMap();
  168. for (int i = 0; i < 10; i++)
  169. {
  170. ispmap.Add(i, new Struct(i * 10.1));
  171. }
  172. if (ispmap.Count != 10)
  173. throw new Exception("Count test on complex type pointer map failed");
  174. foreach (KeyValuePair<int, Struct> p in ispmap)
  175. {
  176. if ((p.Key * 10.1) != p.Value.num)
  177. throw new Exception("Iteration test on complex type pointer map failed for index " + p.Key);
  178. }
  179. }
  180. {
  181. IntStructConstPtrMap iscpmap = new IntStructConstPtrMap();
  182. for (int i = 0; i < 10; i++)
  183. {
  184. iscpmap.Add(i, new Struct(i * 10.1));
  185. }
  186. if (iscpmap.Count != 10)
  187. throw new Exception("Count test on complex type const pointer map failed");
  188. foreach (KeyValuePair<int, Struct> p in iscpmap)
  189. {
  190. if ((p.Key * 10.1) != p.Value.num)
  191. throw new Exception("Iteration test on complex type const pointer map failed for index " + p.Key);
  192. }
  193. }
  194. // Test complex type as key (Struct)
  195. {
  196. StructIntMap limap = new StructIntMap();
  197. Struct s7 = new Struct(7);
  198. Struct s8 = new Struct(8);
  199. limap[s7] = 8;
  200. if (limap[s7] != 8)
  201. throw new Exception("Assignment test on complex key map failed");
  202. if (!limap.ContainsKey(s7))
  203. throw new Exception("Key test (1) on complex key map failed");
  204. if (limap.ContainsKey(s8))
  205. throw new Exception("Key test (2) on complex key map failed");
  206. }
  207. // All done
  208. }
  209. }