DictionaryBaseTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. //
  2. // System.Collections.DictionaryBase
  3. // Test suite for System.Collections.DictionaryBase
  4. //
  5. // Authors:
  6. // Carlos Alberto Barcenilla ([email protected])
  7. //
  8. using System;
  9. using System.Collections;
  10. using NUnit.Framework;
  11. namespace MonoTests.System.Collections
  12. {
  13. [TestFixture]
  14. public class DictionaryBaseTest: Assertion
  15. {
  16. static void Main(string[] args)
  17. {
  18. }
  19. public class ConcreteDictionary : DictionaryBase
  20. {
  21. public bool onInsertFired;
  22. public bool onInsertCompleteFired;
  23. public bool onValidateFired;
  24. public bool onRemoveFired;
  25. public bool onRemoveCompleteFired;
  26. public bool onClearFired;
  27. public bool onClearCompleteFired;
  28. public bool onSetFired;
  29. public bool onSetCompleteFired;
  30. public bool onGetFired;
  31. public bool onInsertMustThrowException;
  32. public bool onInsertCompleteMustThrowException;
  33. public bool onValidateMustThrowException;
  34. public bool onRemoveMustThrowException;
  35. public bool onRemoveCompleteMustThrowException;
  36. public bool onClearMustThrowException;
  37. public bool onClearCompleteMustThrowException;
  38. public bool onSetMustThrowException;
  39. public bool onSetCompleteMustThrowException;
  40. public bool onGetMustThrowException;
  41. public ConcreteDictionary()
  42. {
  43. }
  44. public ConcreteDictionary(int i)
  45. {
  46. for (int j = 0; j < i; j++)
  47. {
  48. ((IDictionary) this).Add(j, j*2);
  49. }
  50. ClearFlags();
  51. }
  52. public IDictionary BaseDictionary
  53. {
  54. get { return this.Dictionary; }
  55. }
  56. public void ClearFlags()
  57. {
  58. onInsertFired = false;
  59. onInsertCompleteFired = false;
  60. onValidateFired = false;
  61. onRemoveFired = false;
  62. onRemoveCompleteFired = false;
  63. onClearFired = false;
  64. onClearCompleteFired = false;
  65. onSetFired = false;
  66. onSetCompleteFired = false;
  67. onGetFired = false;
  68. }
  69. protected override void OnValidate(object key, object value)
  70. {
  71. onValidateFired = true;
  72. if (onValidateMustThrowException)
  73. throw new Exception();
  74. base.OnValidate (key, value);
  75. }
  76. protected override void OnInsert(object key, object value)
  77. {
  78. onInsertFired = true;
  79. if (onInsertMustThrowException)
  80. throw new Exception();
  81. base.OnInsert (key, value);
  82. }
  83. protected override void OnInsertComplete(object key, object value)
  84. {
  85. onInsertCompleteFired = true;
  86. if (onInsertCompleteMustThrowException)
  87. throw new Exception();
  88. base.OnInsertComplete (key, value);
  89. }
  90. protected override void OnRemove(object key, object value)
  91. {
  92. onRemoveFired = true;
  93. if (onRemoveMustThrowException)
  94. throw new Exception();
  95. base.OnRemove (key, value);
  96. }
  97. protected override void OnRemoveComplete(object key, object value)
  98. {
  99. onRemoveCompleteFired = true;
  100. if (onRemoveCompleteMustThrowException)
  101. throw new Exception();
  102. base.OnRemoveComplete (key, value);
  103. }
  104. protected override void OnClear()
  105. {
  106. onClearFired = true;
  107. if (onClearMustThrowException)
  108. throw new Exception();
  109. base.OnClear ();
  110. }
  111. protected override void OnClearComplete()
  112. {
  113. onClearCompleteFired = true;
  114. if (onClearCompleteMustThrowException)
  115. throw new Exception();
  116. base.OnClearComplete ();
  117. }
  118. protected override object OnGet(object key, object currentValue)
  119. {
  120. onGetFired = true;
  121. if (onGetMustThrowException)
  122. throw new Exception();
  123. return base.OnGet (key, currentValue);
  124. }
  125. protected override void OnSet(object key, object oldValue, object newValue)
  126. {
  127. onSetFired = true;
  128. if (onSetMustThrowException)
  129. throw new Exception();
  130. base.OnSet (key, oldValue, newValue);
  131. }
  132. protected override void OnSetComplete(object key, object oldValue, object newValue)
  133. {
  134. onSetCompleteFired = true;
  135. if (onSetCompleteMustThrowException)
  136. throw new Exception();
  137. base.OnSetComplete (key, oldValue, newValue);
  138. }
  139. }
  140. [Test]
  141. public void Add()
  142. {
  143. ConcreteDictionary myDictionary = new ConcreteDictionary(10);
  144. myDictionary.BaseDictionary.Add(100, 1);
  145. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  146. Assert("OnInsert must be fired", myDictionary.onInsertFired);
  147. Assert("OnInsertComplete must be fired", myDictionary.onInsertCompleteFired);
  148. AssertEquals("Count", 11, myDictionary.Count);
  149. AssertEquals(1, myDictionary.BaseDictionary[100]);
  150. }
  151. [Test]
  152. public void AddOnValidateExcept()
  153. {
  154. bool exceptionThrown = false;
  155. ConcreteDictionary myDictionary = new ConcreteDictionary(30);
  156. myDictionary.onValidateMustThrowException = true;
  157. try {
  158. myDictionary.BaseDictionary.Add(111,222);
  159. } catch {
  160. exceptionThrown = true;
  161. } finally {
  162. Assert("Exception must be thrown", exceptionThrown);
  163. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  164. Assert("OnInsert must not be fired", !myDictionary.onInsertFired);
  165. Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
  166. AssertEquals("Count", 30, myDictionary.Count);
  167. }
  168. }
  169. [Test]
  170. public void AddOnInsertExcept()
  171. {
  172. bool exceptionThrown = false;
  173. ConcreteDictionary myDictionary = new ConcreteDictionary(30);
  174. myDictionary.onInsertMustThrowException = true;
  175. try {
  176. myDictionary.BaseDictionary.Add(666,222);
  177. } catch {
  178. exceptionThrown = true;
  179. } finally {
  180. Assert("Exception must be thrown", exceptionThrown);
  181. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  182. Assert("OnInsert must be fired", myDictionary.onInsertFired);
  183. Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
  184. AssertEquals("Count", 30, myDictionary.Count);
  185. }
  186. }
  187. [Test]
  188. public void AddOnInsertCompleteExcept()
  189. {
  190. bool exceptionThrown = false;
  191. ConcreteDictionary myDictionary = new ConcreteDictionary(5);
  192. myDictionary.onInsertCompleteMustThrowException = true;
  193. try {
  194. myDictionary.BaseDictionary.Add(888,999);
  195. } catch {
  196. exceptionThrown = true;
  197. } finally {
  198. Assert("Exception must be thrown", exceptionThrown);
  199. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  200. Assert("OnInsert must be fired", myDictionary.onInsertFired);
  201. Assert("OnInsertComplete must be fired", myDictionary.onInsertCompleteFired);
  202. AssertEquals("Count", 5, myDictionary.Count);
  203. }
  204. }
  205. [Test]
  206. public void AddNullKey()
  207. {
  208. bool exceptionThrown = false;
  209. ConcreteDictionary myDictionary = new ConcreteDictionary();
  210. try {
  211. myDictionary.BaseDictionary.Add(null, 11);
  212. } catch (ArgumentNullException) {
  213. exceptionThrown = true;
  214. } finally {
  215. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  216. Assert("OnInsert must be fired", myDictionary.onInsertFired);
  217. Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
  218. Assert("ArgumentNullException must be thrown", exceptionThrown);
  219. }
  220. }
  221. [Test]
  222. public void Clear()
  223. {
  224. ConcreteDictionary myDictionary = new ConcreteDictionary(30);
  225. myDictionary.Clear();
  226. Assert("OnClear must be fired", myDictionary.onClearFired);
  227. Assert("OnClearComplete must be fired", myDictionary.onClearCompleteFired);
  228. AssertEquals("Count", 0, myDictionary.Count);
  229. }
  230. [Test]
  231. public void ClearOnClearExcept()
  232. {
  233. bool exceptionThrown = false;
  234. ConcreteDictionary myDictionary = new ConcreteDictionary(30);
  235. myDictionary.onClearMustThrowException = true;
  236. try {
  237. myDictionary.Clear();
  238. } catch {
  239. exceptionThrown = true;
  240. } finally {
  241. Assert("Exception must be thrown", exceptionThrown);
  242. Assert("OnClear must be fired", myDictionary.onClearFired);
  243. Assert("OnClearComplete must not be fired", !myDictionary.onClearCompleteFired);
  244. AssertEquals("Count", 30, myDictionary.Count);
  245. }
  246. }
  247. [Test]
  248. public void ClearOnClearCompleteExcept()
  249. {
  250. bool exceptionThrown = false;
  251. ConcreteDictionary myDictionary = new ConcreteDictionary(30);
  252. myDictionary.onClearCompleteMustThrowException = true;
  253. try {
  254. myDictionary.Clear();
  255. } catch {
  256. exceptionThrown = true;
  257. } finally {
  258. Assert("Exception must be thrown", exceptionThrown);
  259. Assert("OnClear must be fired", myDictionary.onClearFired);
  260. Assert("OnClearComplete must be fired", myDictionary.onClearCompleteFired);
  261. AssertEquals("Count", 0, myDictionary.Count);
  262. }
  263. }
  264. [Test]
  265. public void Count()
  266. {
  267. ConcreteDictionary myDictionary = new ConcreteDictionary(19);
  268. AssertEquals(19, myDictionary.Count);
  269. }
  270. [Test]
  271. public void Remove()
  272. {
  273. ConcreteDictionary myDictionary = new ConcreteDictionary(8);
  274. myDictionary.BaseDictionary.Remove(5);
  275. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  276. Assert("OnRemove must be fired", myDictionary.onRemoveFired);
  277. Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
  278. AssertEquals("Count", 7, myDictionary.Count);
  279. AssertEquals(null, myDictionary.BaseDictionary[5]);
  280. }
  281. [Test]
  282. public void RemoveOnValidateExcept()
  283. {
  284. bool exceptionThrown = false;
  285. ConcreteDictionary myDictionary = new ConcreteDictionary(28);
  286. myDictionary.onValidateMustThrowException = true;
  287. try {
  288. myDictionary.BaseDictionary.Remove(11);
  289. } catch {
  290. exceptionThrown = true;
  291. } finally {
  292. Assert("Exception must be thrown in this test", exceptionThrown);
  293. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  294. Assert("OnRemove must not be fired", !myDictionary.onRemoveFired);
  295. Assert("OnRemoveComplete must not be fired", !myDictionary.onRemoveCompleteFired);
  296. AssertEquals("Count", 28, myDictionary.Count);
  297. AssertEquals(22, myDictionary.BaseDictionary[11]);
  298. }
  299. }
  300. [Test]
  301. public void RemoveOnRemoveExcept()
  302. {
  303. bool exceptionThrown = false;
  304. ConcreteDictionary myDictionary = new ConcreteDictionary(28);
  305. myDictionary.onRemoveMustThrowException = true;
  306. try {
  307. myDictionary.BaseDictionary.Remove(11);
  308. } catch {
  309. exceptionThrown = true;
  310. } finally {
  311. Assert("Exception must be thrown", exceptionThrown);
  312. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  313. Assert("OnRemove must be fired", myDictionary.onRemoveFired);
  314. Assert("OnRemoveComplete must not be fired", !myDictionary.onRemoveCompleteFired);
  315. AssertEquals("Count", 28, myDictionary.Count);
  316. AssertEquals(22, myDictionary.BaseDictionary[11]);
  317. }
  318. }
  319. [Test]
  320. public void RemoveOnRemoveCompleteExcept()
  321. {
  322. bool exceptionThrown = false;
  323. ConcreteDictionary myDictionary = new ConcreteDictionary(28);
  324. myDictionary.onRemoveCompleteMustThrowException = true;
  325. try
  326. {
  327. myDictionary.BaseDictionary.Remove(11);
  328. }
  329. catch
  330. {
  331. exceptionThrown = true;
  332. }
  333. finally
  334. {
  335. Assert("Exception must be thrown", exceptionThrown);
  336. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  337. Assert("OnRemove must be fired", myDictionary.onRemoveFired);
  338. Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
  339. AssertEquals("Count", 27, myDictionary.Count);
  340. AssertEquals(null, myDictionary.BaseDictionary[11]);
  341. }
  342. }
  343. [Test]
  344. public void RemoveKeyNotInDictionary()
  345. {
  346. ConcreteDictionary myDictionary = new ConcreteDictionary(28);
  347. myDictionary.BaseDictionary.Remove(80);
  348. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  349. Assert("OnRemove must be fired", myDictionary.onRemoveFired);
  350. Assert("OnRemoveComplete must be fired", myDictionary.onRemoveCompleteFired);
  351. }
  352. [Test]
  353. public void Items()
  354. {
  355. ConcreteDictionary myDictionary = new ConcreteDictionary(19);
  356. for (int i = 0; i < 19; i++)
  357. {
  358. AssertEquals(i*2, (int) myDictionary.BaseDictionary[i]);
  359. }
  360. }
  361. [Test]
  362. public void Contains()
  363. {
  364. ConcreteDictionary myDictionary = new ConcreteDictionary(14);
  365. for (int i = 0; i < 14; i++)
  366. {
  367. Assert("Must contain " + i, myDictionary.BaseDictionary.Contains(i));
  368. }
  369. for (int i = 14; i < 34; i++)
  370. {
  371. Assert("Must not contain " + i, !myDictionary.BaseDictionary.Contains(i));
  372. }
  373. }
  374. [Test]
  375. public void GetEnumerator()
  376. {
  377. ConcreteDictionary myDictionary = new ConcreteDictionary(4);
  378. AssertNotNull(myDictionary.GetEnumerator());
  379. }
  380. [Test]
  381. public void Keys()
  382. {
  383. ConcreteDictionary myDictionary = new ConcreteDictionary(5);
  384. ICollection keys = myDictionary.BaseDictionary.Keys;
  385. int total = 0;
  386. foreach (int i in keys)
  387. total += i;
  388. AssertEquals(10, total);
  389. AssertEquals(5, keys.Count);
  390. }
  391. [Test]
  392. public void Values()
  393. {
  394. ConcreteDictionary myDictionary = new ConcreteDictionary(5);
  395. ICollection values = myDictionary.BaseDictionary.Values;
  396. int total = 0;
  397. foreach (int i in values)
  398. total += i;
  399. AssertEquals(20, total);
  400. AssertEquals(5, values.Count);
  401. }
  402. [Test]
  403. public void Get()
  404. {
  405. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  406. int v = (int) myDictionary.BaseDictionary[10];
  407. Assert("OnGet must be fired", myDictionary.onGetFired);
  408. AssertEquals(v, 20);
  409. }
  410. [Test]
  411. public void GetOnGetExcept()
  412. {
  413. bool exceptionThrown = false;
  414. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  415. myDictionary.onGetMustThrowException = true;
  416. try {
  417. int v = (int) myDictionary.BaseDictionary[10];
  418. } catch {
  419. exceptionThrown = true;
  420. } finally {
  421. Assert("Exception must be thrown", exceptionThrown);
  422. Assert("OnGet must be fired", myDictionary.onGetFired);
  423. }
  424. }
  425. [Test]
  426. public void GetNoKey()
  427. {
  428. bool exceptionThrown = false;
  429. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  430. AssertNull(myDictionary.BaseDictionary[100]);
  431. }
  432. [Test]
  433. public void Set()
  434. {
  435. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  436. myDictionary.BaseDictionary[10] = 50;
  437. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  438. Assert("OnSet must be fired", myDictionary.onSetFired);
  439. Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
  440. AssertEquals(50, myDictionary.BaseDictionary[10]);
  441. }
  442. [Test]
  443. public void SetNewKey()
  444. {
  445. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  446. myDictionary.BaseDictionary[111] = 222;
  447. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  448. Assert("OnSet must be fired", myDictionary.onSetFired);
  449. Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
  450. Assert("OnInsert must not be fired", !myDictionary.onInsertFired);
  451. Assert("OnInsertComplete must not be fired", !myDictionary.onInsertCompleteFired);
  452. AssertEquals(222, myDictionary.BaseDictionary[111]);
  453. AssertEquals(19, myDictionary.Count);
  454. }
  455. [Test]
  456. public void SetOnValidateExcept()
  457. {
  458. bool exceptionThrown = false;
  459. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  460. myDictionary.onValidateMustThrowException = true;
  461. try {
  462. myDictionary.BaseDictionary[10] = 50;
  463. } catch {
  464. exceptionThrown = true;
  465. } finally {
  466. Assert("Exception must be thrown", exceptionThrown);
  467. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  468. Assert("OnSet must not be fired", !myDictionary.onSetFired);
  469. Assert("OnSetComplete not must be fired", !myDictionary.onSetCompleteFired);
  470. AssertEquals(20, myDictionary.BaseDictionary[10]);
  471. }
  472. }
  473. [Test]
  474. public void SetOnSetExcept()
  475. {
  476. bool exceptionThrown = false;
  477. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  478. myDictionary.onSetMustThrowException = true;
  479. try {
  480. myDictionary.BaseDictionary[10] = 50;
  481. } catch {
  482. exceptionThrown = true;
  483. } finally {
  484. Assert("Exception must be thrown", exceptionThrown);
  485. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  486. Assert("OnSet must be fired", myDictionary.onSetFired);
  487. Assert("OnSetComplete must not be fired", !myDictionary.onSetCompleteFired);
  488. AssertEquals(20, myDictionary.BaseDictionary[10]);
  489. }
  490. }
  491. [Test]
  492. public void SetOnSetCompleteExcept()
  493. {
  494. bool exceptionThrown = false;
  495. ConcreteDictionary myDictionary = new ConcreteDictionary(18);
  496. myDictionary.onSetCompleteMustThrowException = true;
  497. try {
  498. myDictionary.BaseDictionary[10] = 50;
  499. } catch {
  500. exceptionThrown = true;
  501. } finally {
  502. Assert("Exception must be thrown", exceptionThrown);
  503. Assert("OnValidate must be fired", myDictionary.onValidateFired);
  504. Assert("OnSet must be fired", myDictionary.onSetFired);
  505. Assert("OnSetComplete must be fired", myDictionary.onSetCompleteFired);
  506. AssertEquals(20, myDictionary.BaseDictionary[10]);
  507. }
  508. }
  509. [Test]
  510. public void IsReadOnly()
  511. {
  512. ConcreteDictionary myDictionary = new ConcreteDictionary(1);
  513. Assert(!myDictionary.BaseDictionary.IsReadOnly);
  514. }
  515. [Test]
  516. public void IsFixedSize()
  517. {
  518. ConcreteDictionary myDictionary = new ConcreteDictionary(1);
  519. Assert(!myDictionary.BaseDictionary.IsFixedSize);
  520. }
  521. [Test]
  522. public void DictionaryProperty()
  523. {
  524. ConcreteDictionary myDictionary = new ConcreteDictionary(1);
  525. AssertEquals(myDictionary, myDictionary.BaseDictionary);
  526. }
  527. }
  528. }