CollectionTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // Collection.cs - NUnit Test Cases for Microsoft.VisualBasic.Collection
  2. //
  3. // Author:
  4. // Chris J. Breisch ([email protected])
  5. //
  6. // (C) Chris J. Breisch
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using Microsoft.VisualBasic;
  11. using System.Collections;
  12. namespace MonoTests.Microsoft.VisualBasic
  13. {
  14. public class CollectionTest : TestCase
  15. {
  16. public CollectionTest() : base ("Microsoft.VisualBasic.Collection") {}
  17. public CollectionTest(string name) : base(name) {}
  18. protected override void SetUp() {}
  19. protected override void TearDown() {}
  20. public static ITest Suite {
  21. get {
  22. return new TestSuite(typeof(CollectionTest));
  23. }
  24. }
  25. // Test Constructor
  26. public void TestNew ()
  27. {
  28. Collection c;
  29. c = new Collection();
  30. AssertNotNull("#N01", c);
  31. AssertEquals("#N02", 0, c.Count);
  32. }
  33. // Test Add method with Key == null
  34. public void TestAddNoKey ()
  35. {
  36. Collection c;
  37. c = new Collection();
  38. c.Add(typeof(int), null, null, null);
  39. c.Add(typeof(double), null, null, null);
  40. c.Add(typeof(string), null, null, null);
  41. AssertEquals("#ANK01", 3, c.Count);
  42. // Collection class is 1-based
  43. AssertEquals("#ANK02", typeof(string), c[3]);
  44. }
  45. // Test Add method with Key specified
  46. public void TestAddKey ()
  47. {
  48. Collection c;
  49. c = new Collection();
  50. c.Add("Baseball", "Base", null, null);
  51. c.Add("Football", "Foot", null, null);
  52. c.Add("Basketball", "Basket", null, null);
  53. c.Add("Volleyball", "Volley", null, null);
  54. AssertEquals("#AK01", 4, c.Count);
  55. // Collection class is 1-based
  56. AssertEquals("#AK02", "Baseball", c[1]);
  57. AssertEquals("#AK03", "Volleyball", c["Volley"]);
  58. }
  59. // Test Add method with Before specified and Key == null
  60. public void TestAddBeforeNoKey ()
  61. {
  62. Collection c;
  63. c = new Collection();
  64. c.Add(typeof(int), null, null, null);
  65. c.Add(typeof(double), null, 1, null);
  66. c.Add(typeof(string), null, 2, null);
  67. c.Add(typeof(object), null, 2, null);
  68. AssertEquals("#ABNK01", 4, c.Count);
  69. // Collection class is 1-based
  70. AssertEquals("#ABNK02", typeof(int), c[4]);
  71. AssertEquals("#ABNK03", typeof(double), c[1]);
  72. AssertEquals("#ABNK04", typeof(object), c[2]);
  73. }
  74. // Test Add method with Before and Key
  75. public void TestAddBeforeKey ()
  76. {
  77. Collection c;
  78. c = new Collection();
  79. c.Add("Baseball", "Base", null, null);
  80. c.Add("Football", "Foot", 1, null);
  81. c.Add("Basketball", "Basket", 1, null);
  82. c.Add("Volleyball", "Volley", 3, null);
  83. AssertEquals("#ABK01", 4, c.Count);
  84. AssertEquals("#ABK02", "Basketball", c[1]);
  85. AssertEquals("#ABK03", "Baseball", c[4]);
  86. AssertEquals("#ABK04", "Volleyball", c["Volley"]);
  87. AssertEquals("#ABK05", "Football", c["Foot"]);
  88. }
  89. // Test Add method with After specified and Key == null
  90. public void TestAddAfterNoKey ()
  91. {
  92. Collection c;
  93. c = new Collection();
  94. c.Add(typeof(int), null, null, 0);
  95. c.Add(typeof(double), null, null, 1);
  96. c.Add(typeof(string), null, null, 1);
  97. c.Add(typeof(object), null, null, 3);
  98. AssertEquals("#AANK01", 4, c.Count);
  99. // Collection class is 1-based
  100. AssertEquals("#AANK02", typeof(object), c[4]);
  101. AssertEquals("#AANK03", typeof(int), c[1]);
  102. AssertEquals("#AANK04", typeof(string), c[2]);
  103. }
  104. // Test Add method with After and Key
  105. public void TestAddAfterKey ()
  106. {
  107. Collection c;
  108. c = new Collection();
  109. c.Add("Baseball", "Base", null, 0);
  110. c.Add("Football", "Foot", null, 1);
  111. c.Add("Basketball", "Basket", null, 1);
  112. c.Add("Volleyball", "Volley", null, 2);
  113. AssertEquals("#AAK01", 4, c.Count);
  114. // Collection class is 1-based
  115. AssertEquals("#AAK02", "Baseball", c[1]);
  116. AssertEquals("#AAK03", "Football", c[4]);
  117. AssertEquals("#AAK04", "Basketball", c["Basket"]);
  118. AssertEquals("#AAK05", "Volleyball", c["Volley"]);
  119. }
  120. // Test GetEnumerator method
  121. public void TestGetEnumerator ()
  122. {
  123. Collection c;
  124. IEnumerator e;
  125. object[] o = new object[4] {typeof(int),
  126. typeof(double), typeof(string), typeof(object)};
  127. int i = 0;
  128. c = new Collection();
  129. c.Add(typeof(int), null, null, null);
  130. c.Add(typeof(double), null, null, null);
  131. c.Add(typeof(string), null, null, null);
  132. c.Add(typeof(object), null, null, null);
  133. e = c.GetEnumerator();
  134. AssertNotNull("#GE01", e);
  135. while (e.MoveNext()) {
  136. AssertEquals("#GE02." + i.ToString(), o[i], e.Current);
  137. i++;
  138. }
  139. e.Reset();
  140. e.MoveNext();
  141. AssertEquals("#GE03", o[0], e.Current);
  142. }
  143. // Test GetEnumerator method again, this time using foreach
  144. public void Testforeach ()
  145. {
  146. Collection c;
  147. object[] o = new object[4] {typeof(int),
  148. typeof(double), typeof(string), typeof(object)};
  149. int i = 0;
  150. c = new Collection();
  151. c.Add(typeof(int), null, null, null);
  152. c.Add(typeof(double), null, null, null);
  153. c.Add(typeof(string), null, null, null);
  154. c.Add(typeof(object), null, null, null);
  155. foreach (object item in c) {
  156. AssertEquals("#fe01." + i.ToString(), o[i], item);
  157. i++;
  158. }
  159. }
  160. // Test Remove method with Index
  161. public void TestRemoveNoKey ()
  162. {
  163. Collection c;
  164. c = new Collection();
  165. c.Add(typeof(int), null, null, null);
  166. c.Add(typeof(double), null, null, null);
  167. c.Add(typeof(string), null, null, null);
  168. c.Add(typeof(object), null, null, null);
  169. AssertEquals("#RNK01", 4, c.Count);
  170. c.Remove(3);
  171. AssertEquals("#RNK02", 3, c.Count);
  172. // Collection class is 1-based
  173. AssertEquals("#RNK03", typeof(object), c[3]);
  174. c.Remove(1);
  175. AssertEquals("#RNK04", 2, c.Count);
  176. AssertEquals("#RNK05", typeof(double), c[1]);
  177. AssertEquals("#RNK06", typeof(object), c[2]);
  178. c.Remove(2);
  179. AssertEquals("#RNK07", 1, c.Count);
  180. AssertEquals("#RNK08", typeof(double), c[1]);
  181. c.Remove(1);
  182. AssertEquals("#RNK09", 0, c.Count);
  183. }
  184. // Test Remove method with Key
  185. public void TestRemoveKey ()
  186. {
  187. Collection c;
  188. c = new Collection();
  189. c.Add("Baseball", "Base", null, null);
  190. c.Add("Football", "Foot", null, null);
  191. c.Add("Basketball", "Basket", null, null);
  192. c.Add("Volleyball", "Volley", null, null);
  193. AssertEquals("#RK01", 4, c.Count);
  194. c.Remove("Foot");
  195. AssertEquals("#RK02", 3, c.Count);
  196. AssertEquals("#RK03", "Basketball", c["Basket"]);
  197. // Collection class is 1-based
  198. AssertEquals("#RK04", "Volleyball", c[3]);
  199. c.Remove("Base");
  200. AssertEquals("#RK05", 2, c.Count);
  201. AssertEquals("#RK06", "Basketball", c[1]);
  202. AssertEquals("#RK07", "Volleyball", c["Volley"]);
  203. c.Remove(2);
  204. AssertEquals("#RK08", 1, c.Count);
  205. AssertEquals("#RK09", "Basketball", c[1]);
  206. AssertEquals("#RK10", "Basketball", c["Basket"]);
  207. c.Remove(1);
  208. AssertEquals("#RK11", 0, c.Count);
  209. }
  210. // Test all the Exceptions we're supposed to throw
  211. public void TestException ()
  212. {
  213. Collection c;
  214. bool caughtException = false;
  215. c = new Collection();
  216. try {
  217. // nothing in Collection yet
  218. object o = c[0];
  219. }
  220. catch (Exception e) {
  221. AssertEquals("#E01", typeof(IndexOutOfRangeException), e.GetType());
  222. caughtException = true;
  223. }
  224. AssertEquals("#E02", true, caughtException);
  225. c.Add("Baseball", "Base", null, null);
  226. c.Add("Football", "Foot", null, null);
  227. c.Add("Basketball", "Basket", null, null);
  228. c.Add("Volleyball", "Volley", null, null);
  229. caughtException = false;
  230. try {
  231. // only 4 elements
  232. object o = c[5];
  233. }
  234. catch (Exception e) {
  235. AssertEquals("#E03", typeof(IndexOutOfRangeException), e.GetType());
  236. caughtException = true;
  237. }
  238. AssertEquals("#E04", true, caughtException);
  239. caughtException = false;
  240. try {
  241. // Collection class is 1-based
  242. object o = c[0];
  243. }
  244. catch (Exception e) {
  245. AssertEquals("#E05", typeof(IndexOutOfRangeException), e.GetType());
  246. caughtException = true;
  247. }
  248. AssertEquals("#E06", true, caughtException);
  249. caughtException = false;
  250. try {
  251. // no member with Key == "Kick"
  252. object o = c["Kick"];
  253. }
  254. catch (Exception e) {
  255. // FIXME
  256. // VB Language Reference says IndexOutOfRangeException
  257. // here, but MS throws ArgumentException
  258. // AssertEquals("#E07", typeof(IndexOutOfRangeException), e.GetType());
  259. AssertEquals("#E07", typeof(ArgumentException), e.GetType());
  260. caughtException = true;
  261. }
  262. AssertEquals("#E08", true, caughtException);
  263. caughtException = false;
  264. try {
  265. // Even though Indexer is an object, really it's a string
  266. object o = c[typeof(int)];
  267. }
  268. catch (Exception e) {
  269. AssertEquals("#E09", typeof(ArgumentException), e.GetType());
  270. caughtException = true;
  271. }
  272. AssertEquals("#E10", true, caughtException);
  273. caughtException = false;
  274. try {
  275. // can't specify both Before and After
  276. c.Add("Kickball", "Kick", "Volley", "Foot");
  277. }
  278. catch (Exception e) {
  279. AssertEquals("#E11", typeof(ArgumentException), e.GetType());
  280. caughtException = true;
  281. }
  282. AssertEquals("#E12", true, caughtException);
  283. caughtException = false;
  284. try {
  285. // Key "Foot" already exists
  286. c.Add("Kickball", "Foot", null, null);
  287. }
  288. catch (Exception e) {
  289. AssertEquals("#E13", typeof(ArgumentException), e.GetType());
  290. caughtException = true;
  291. }
  292. AssertEquals("#E14", true, caughtException);
  293. caughtException = false;
  294. try {
  295. // Even though Before is object, it's really a string
  296. c.Add("Dodgeball", "Dodge", typeof(int), null);
  297. }
  298. catch (Exception e) {
  299. AssertEquals("#E15", typeof(InvalidCastException), e.GetType());
  300. caughtException = true;
  301. }
  302. AssertEquals("#E16", true, caughtException);
  303. caughtException = false;
  304. try {
  305. // Even though After is object, it's really a string
  306. c.Add("Wallyball", "Wally", null, typeof(int));
  307. }
  308. catch (Exception e) {
  309. AssertEquals("#E17", typeof(InvalidCastException), e.GetType());
  310. caughtException = true;
  311. }
  312. AssertEquals("#E18", true, caughtException);
  313. caughtException = false;
  314. try {
  315. // have to pass a legitimate value to remove
  316. c.Remove(null);
  317. }
  318. catch (Exception e) {
  319. AssertEquals("#E19", typeof(ArgumentException), e.GetType());
  320. caughtException = true;
  321. }
  322. AssertEquals("#E20", true, caughtException);
  323. caughtException = false;
  324. try {
  325. // no Key "Golf" exists
  326. c.Remove("Golf");
  327. }
  328. catch (Exception e) {
  329. AssertEquals("#E21", typeof(ArgumentException), e.GetType());
  330. caughtException = true;
  331. }
  332. AssertEquals("#E22", true, caughtException);
  333. caughtException = false;
  334. try {
  335. // no Index 10 exists
  336. c.Remove(10);
  337. }
  338. catch (Exception e) {
  339. AssertEquals("#E23", typeof(IndexOutOfRangeException), e.GetType());
  340. caughtException = true;
  341. }
  342. AssertEquals("#E24", true, caughtException);
  343. caughtException = false;
  344. try {
  345. IEnumerator e = c.GetEnumerator();
  346. // Must MoveNext before Current
  347. object item = e.Current;
  348. }
  349. catch (Exception e) {
  350. // FIXME
  351. // On-line help says InvalidOperationException here,
  352. // but MS throws IndexOutOfRangeException
  353. // AssertEquals("#E25", typeof(InvalidOperationException), e.GetType());
  354. AssertEquals("#E25", typeof(IndexOutOfRangeException), e.GetType());
  355. caughtException = true;
  356. }
  357. AssertEquals("#E26", true, caughtException);
  358. caughtException = false;
  359. try {
  360. IEnumerator e = c.GetEnumerator();
  361. e.MoveNext();
  362. c.Add("Paintball", "Paint", null, null);
  363. // Can't MoveNext if Collection has been modified
  364. e.MoveNext();
  365. }
  366. catch (Exception e) {
  367. // FIXME
  368. // On-line help says this should throw an error. MS doesn't.
  369. AssertEquals("#E27", typeof(InvalidOperationException), e.GetType());
  370. caughtException = true;
  371. }
  372. // FIXME
  373. // What to do about this? MS doesn't throw an error
  374. // AssertEquals("#E28", true, caughtException);
  375. AssertEquals("#E28", false, caughtException);
  376. caughtException = false;
  377. try {
  378. IEnumerator e = c.GetEnumerator();
  379. e.MoveNext();
  380. c.Add("Racketball", "Racket", null, null);
  381. // Can't Reset if Collection has been modified
  382. e.Reset();
  383. }
  384. catch (Exception e) {
  385. // FIXME
  386. // On-line help says this should throw an error. MS doesn't.
  387. AssertEquals("#E29", typeof(InvalidOperationException), e.GetType());
  388. caughtException = true;
  389. }
  390. // FIXME
  391. // What to do about this? MS doesn't throw an error
  392. // AssertEquals("#E30", true, caughtException);
  393. AssertEquals("#E30", false, caughtException);
  394. caughtException = false;
  395. }
  396. }
  397. }