DictionaryTest.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. //
  2. // MonoTests.System.Collections.Generic.Test.DictionaryTest
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. // Ankit Jain ([email protected])
  7. // David Waite ([email protected])
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. // Copyright (C) 2005 David Waite ([email protected])
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.Runtime.Serialization.Formatters.Binary;
  37. using System.Text;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.Collections.Generic {
  40. [TestFixture]
  41. public class DictionaryTest {
  42. class MyClass {
  43. int a;
  44. int b;
  45. public MyClass (int a, int b)
  46. {
  47. this.a = a;
  48. this.b = b;
  49. }
  50. public override int GetHashCode ()
  51. {
  52. return a + b;
  53. }
  54. public override bool Equals (object obj)
  55. {
  56. if (!(obj is MyClass))
  57. return false;
  58. return ((MyClass)obj).Value == a;
  59. }
  60. public int Value {
  61. get { return a; }
  62. }
  63. }
  64. Dictionary <string, object> _dictionary = null;
  65. Dictionary <MyClass, MyClass> _dictionary2 = null;
  66. Dictionary <int, int> _dictionary3 = null;
  67. [SetUp]
  68. public void SetUp ()
  69. {
  70. _dictionary = new Dictionary <string, object> ();
  71. _dictionary2 = new Dictionary <MyClass, MyClass> ();
  72. _dictionary3 = new Dictionary <int, int>();
  73. }
  74. [Test]
  75. public void AddTest ()
  76. {
  77. _dictionary.Add ("key1", "value");
  78. Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
  79. }
  80. [Test]
  81. public void AddTest2 ()
  82. {
  83. MyClass m1 = new MyClass (10,5);
  84. MyClass m2 = new MyClass (20,5);
  85. MyClass m3 = new MyClass (12,3);
  86. _dictionary2.Add (m1,m1);
  87. _dictionary2.Add (m2, m2);
  88. _dictionary2.Add (m3, m3);
  89. Assert.AreEqual (20, _dictionary2 [m2].Value, "#1");
  90. Assert.AreEqual (10, _dictionary2 [m1].Value, "#2");
  91. Assert.AreEqual (12, _dictionary2 [m3].Value, "#3");
  92. }
  93. [Test]
  94. public void AddTest3 ()
  95. {
  96. _dictionary3.Add (1, 2);
  97. _dictionary3.Add (2, 3);
  98. _dictionary3.Add (3, 4);
  99. Assert.AreEqual (2, _dictionary3[1], "#1");
  100. Assert.AreEqual (3, _dictionary3[2], "#2");
  101. Assert.AreEqual (4, _dictionary3[3], "#3");
  102. }
  103. [Test, ExpectedException(typeof(ArgumentNullException))]
  104. public void AddNullTest ()
  105. {
  106. _dictionary.Add (null, "");
  107. }
  108. [Test, ExpectedException(typeof(ArgumentException))]
  109. public void AddDuplicateTest ()
  110. {
  111. _dictionary.Add("foo", "bar");
  112. _dictionary.Add("foo", "bar");
  113. }
  114. //Tests Add when resize takes place
  115. [Test]
  116. public void AddLargeTest ()
  117. {
  118. int i, numElems = 50;
  119. for (i = 0; i < numElems; i++)
  120. {
  121. _dictionary3.Add (i, i);
  122. }
  123. i = 0;
  124. foreach (KeyValuePair <int, int> entry in _dictionary3)
  125. {
  126. i++;
  127. }
  128. Assert.AreEqual (i, numElems, "Add with resize failed!");
  129. }
  130. [Test]
  131. public void IndexerGetExistingTest ()
  132. {
  133. _dictionary.Add ("key1", "value");
  134. Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
  135. }
  136. [Test, ExpectedException(typeof(KeyNotFoundException))]
  137. public void IndexerGetNonExistingTest ()
  138. {
  139. object foo = _dictionary ["foo"];
  140. }
  141. [Test, ExpectedException(typeof(ArgumentNullException))]
  142. public void IndexerGetNullTest()
  143. {
  144. object s = _dictionary[null];
  145. }
  146. [Test]
  147. public void IndexerSetExistingTest ()
  148. {
  149. _dictionary.Add ("key1", "value1");
  150. _dictionary ["key1"] = "value2";
  151. Assert.AreEqual (1, _dictionary.Count);
  152. Assert.AreEqual ("value2", _dictionary ["key1"]);
  153. }
  154. [Test]
  155. public void IndexerSetNonExistingTest ()
  156. {
  157. _dictionary ["key1"] = "value1";
  158. Assert.AreEqual (1, _dictionary.Count);
  159. Assert.AreEqual ("value1", _dictionary ["key1"]);
  160. }
  161. [Test]
  162. public void RemoveTest ()
  163. {
  164. _dictionary.Add ("key1", "value1");
  165. _dictionary.Add ("key2", "value2");
  166. _dictionary.Add ("key3", "value3");
  167. _dictionary.Add ("key4", "value4");
  168. Assert.IsTrue (_dictionary.Remove ("key3"));
  169. Assert.IsFalse (_dictionary.Remove ("foo"));
  170. Assert.AreEqual (3, _dictionary.Count);
  171. Assert.IsFalse (_dictionary.ContainsKey ("key3"));
  172. }
  173. [Test]
  174. public void RemoveTest2 ()
  175. {
  176. MyClass m1 = new MyClass (10, 5);
  177. MyClass m2 = new MyClass (20, 5);
  178. MyClass m3 = new MyClass (12, 3);
  179. _dictionary2.Add (m1, m1);
  180. _dictionary2.Add (m2, m2);
  181. _dictionary2.Add (m3, m3);
  182. _dictionary2.Remove (m1); // m2 is in rehash path
  183. Assert.AreEqual (20, _dictionary2 [m2].Value, "#4");
  184. }
  185. [Test, ExpectedException(typeof(ArgumentNullException))]
  186. public void IndexerSetNullTest()
  187. {
  188. _dictionary[null] = "bar";
  189. }
  190. [Test]
  191. public void ClearTest ()
  192. {
  193. _dictionary.Add ("key1", "value1");
  194. _dictionary.Add ("key2", "value2");
  195. _dictionary.Add ("key3", "value3");
  196. _dictionary.Add ("key4", "value4");
  197. _dictionary.Clear ();
  198. Assert.AreEqual (0, _dictionary.Count, "Clear method failed!");
  199. Assert.IsFalse (_dictionary.ContainsKey ("key2"));
  200. }
  201. [Test]
  202. public void ContainsKeyTest ()
  203. {
  204. _dictionary.Add ("key1", "value1");
  205. _dictionary.Add ("key2", "value2");
  206. _dictionary.Add ("key3", "value3");
  207. _dictionary.Add ("key4", "value4");
  208. bool contains = _dictionary.ContainsKey ("key4");
  209. Assert.IsTrue (contains, "ContainsKey does not return correct value!");
  210. contains = _dictionary.ContainsKey ("key5");
  211. Assert.IsFalse (contains, "ContainsKey for non existant does not return correct value!");
  212. }
  213. [Test]
  214. public void ContainsValueTest ()
  215. {
  216. _dictionary.Add ("key1", "value1");
  217. _dictionary.Add ("key2", "value2");
  218. _dictionary.Add ("key3", "value3");
  219. _dictionary.Add ("key4", "value4");
  220. bool contains = _dictionary.ContainsValue ("value2");
  221. Assert.IsTrue(contains, "ContainsValue does not return correct value!");
  222. contains = _dictionary.ContainsValue ("@@daisofja@@");
  223. Assert.IsFalse (contains, "ContainsValue for non existant does not return correct value!");
  224. }
  225. [Test]
  226. public void TryGetValueTest()
  227. {
  228. _dictionary.Add ("key1", "value1");
  229. _dictionary.Add ("key2", "value2");
  230. _dictionary.Add ("key3", "value3");
  231. _dictionary.Add ("key4", "value4");
  232. object value = "";
  233. bool retrieved = _dictionary.TryGetValue ("key4", out value);
  234. Assert.IsTrue (retrieved);
  235. Assert.AreEqual ("value4", (string)value, "TryGetValue does not return value!");
  236. retrieved = _dictionary.TryGetValue ("key7", out value);
  237. Assert.IsFalse (retrieved);
  238. Assert.IsNull (value, "value for non existant value should be null!");
  239. }
  240. [Test]
  241. public void ValueTypeTest ()
  242. {
  243. Dictionary <int, float> dict = new Dictionary <int, float> ();
  244. dict.Add (10, 10.3f);
  245. dict.Add (11, 10.4f);
  246. dict.Add (12, 10.5f);
  247. Assert.AreEqual (10.4f, dict [11], "#5");
  248. }
  249. private class MyTest
  250. {
  251. public string Name;
  252. public int RollNo;
  253. public MyTest (string name, int number)
  254. {
  255. Name = name;
  256. RollNo = number;
  257. }
  258. public override int GetHashCode ()
  259. {
  260. return Name.GetHashCode () ^ RollNo;
  261. }
  262. public override bool Equals (object obj)
  263. {
  264. MyTest myt = obj as MyTest;
  265. return myt.Name.Equals (this.Name) &&
  266. myt.RollNo.Equals (this.RollNo);
  267. }
  268. }
  269. [Test]
  270. public void ObjectAsKeyTest ()
  271. {
  272. Dictionary <object, object> dict = new Dictionary <object, object> ();
  273. MyTest key1, key2, key3;
  274. dict.Add ( (key1 = new MyTest ("key1", 234)), "value1");
  275. dict.Add ( (key2 = new MyTest ("key2", 444)), "value2");
  276. dict.Add ( (key3 = new MyTest ("key3", 5655)), "value3");
  277. Assert.AreEqual ("value2", dict [key2], "value is not returned!");
  278. Assert.AreEqual ("value3", dict [key3], "neg: exception should not be thrown!");
  279. }
  280. [Test, ExpectedException (typeof (ArgumentException))]
  281. public void IDictionaryAddTest ()
  282. {
  283. IDictionary iDict = _dictionary as IDictionary;
  284. iDict.Add ("key1", "value1");
  285. iDict.Add ("key2", "value3");
  286. Assert.AreEqual (2, iDict.Count, "IDictioanry interface add is not working!");
  287. //Negative test case
  288. iDict.Add (12, "value");
  289. iDict.Add ("key", 34);
  290. }
  291. [Test]
  292. public void IEnumeratorTest ()
  293. {
  294. _dictionary.Add ("key1", "value1");
  295. _dictionary.Add ("key2", "value2");
  296. _dictionary.Add ("key3", "value3");
  297. _dictionary.Add ("key4", "value4");
  298. IEnumerator itr = ((IEnumerable)_dictionary).GetEnumerator ();
  299. while (itr.MoveNext ()) {
  300. object o = itr.Current;
  301. Assert.AreEqual (typeof (KeyValuePair<string,object>), o.GetType (), "Current should return a type of KeyValuePair");
  302. KeyValuePair<string,object> entry = (KeyValuePair<string,object>) itr.Current;
  303. }
  304. Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
  305. }
  306. [Test]
  307. public void IEnumeratorGenericTest ()
  308. {
  309. _dictionary.Add ("key1", "value1");
  310. _dictionary.Add ("key2", "value2");
  311. _dictionary.Add ("key3", "value3");
  312. _dictionary.Add ("key4", "value4");
  313. IEnumerator <KeyValuePair <string, object>> itr = ((IEnumerable <KeyValuePair <string, object>>)_dictionary).GetEnumerator ();
  314. while (itr.MoveNext ()) {
  315. object o = itr.Current;
  316. Assert.AreEqual (typeof (KeyValuePair <string, object>), o.GetType (), "Current should return a type of KeyValuePair<object,string>");
  317. KeyValuePair <string, object> entry = (KeyValuePair <string, object>)itr.Current;
  318. }
  319. Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
  320. }
  321. [Test]
  322. public void IDictionaryEnumeratorTest ()
  323. {
  324. _dictionary.Add ("key1", "value1");
  325. _dictionary.Add ("key2", "value2");
  326. _dictionary.Add ("key3", "value3");
  327. _dictionary.Add ("key4", "value4");
  328. IDictionaryEnumerator itr = ((IDictionary)_dictionary).GetEnumerator ();
  329. while (itr.MoveNext ()) {
  330. object o = itr.Current;
  331. Assert.AreEqual (typeof (DictionaryEntry), o.GetType (), "Current should return a type of DictionaryEntry");
  332. DictionaryEntry entry = (DictionaryEntry) itr.Current;
  333. }
  334. Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
  335. }
  336. [Test]
  337. public void ForEachTest ()
  338. {
  339. _dictionary.Add ("key1", "value1");
  340. _dictionary.Add ("key2", "value2");
  341. _dictionary.Add ("key3", "value3");
  342. _dictionary.Add ("key4", "value4");
  343. int i = 0;
  344. foreach (KeyValuePair <string, object> entry in _dictionary)
  345. i++;
  346. Assert.AreEqual(4, i, "fail1: foreach entry failed!");
  347. i = 0;
  348. foreach (KeyValuePair <string, object> entry in ((IEnumerable)_dictionary))
  349. i++;
  350. Assert.AreEqual(4, i, "fail2: foreach entry failed!");
  351. i = 0;
  352. foreach (DictionaryEntry entry in ((IDictionary)_dictionary))
  353. i++;
  354. Assert.AreEqual (4, i, "fail3: foreach entry failed!");
  355. }
  356. [Test]
  357. public void ResizeTest ()
  358. {
  359. Dictionary <string, object> dictionary = new Dictionary <string, object> (3);
  360. dictionary.Add ("key1", "value1");
  361. dictionary.Add ("key2", "value2");
  362. dictionary.Add ("key3", "value3");
  363. Assert.AreEqual (3, dictionary.Count);
  364. dictionary.Add ("key4", "value4");
  365. Assert.AreEqual (4, dictionary.Count);
  366. Assert.AreEqual ("value1", dictionary ["key1"].ToString (), "");
  367. Assert.AreEqual ("value2", dictionary ["key2"].ToString (), "");
  368. Assert.AreEqual ("value4", dictionary ["key4"].ToString (), "");
  369. Assert.AreEqual ("value3", dictionary ["key3"].ToString (), "");
  370. }
  371. [Test]
  372. public void KeyCollectionTest ()
  373. {
  374. _dictionary.Add ("key1", "value1");
  375. _dictionary.Add ("key2", "value2");
  376. _dictionary.Add ("key3", "value3");
  377. _dictionary.Add ("key4", "value4");
  378. ICollection <string> keys = ((IDictionary <string, object>)_dictionary).Keys;
  379. Assert.AreEqual (4, keys.Count);
  380. int i = 0;
  381. foreach (string key in keys)
  382. {
  383. i++;
  384. }
  385. Assert.AreEqual(4, i);
  386. }
  387. [Test]
  388. public void KeyValueEnumeratorTest ()
  389. {
  390. IDictionary<int, int> d = new Dictionary<int, int>();
  391. // Values are chosen such that two keys map to the same bucket.
  392. // Default dictionary table size == 10
  393. d [9] = 1;
  394. d [10] = 2;
  395. d [19] = 3;
  396. Assert.AreEqual (d.Count, d.Keys.Count, "d and d.Keys don't appear to match");
  397. Assert.AreEqual (d.Values.Count, d.Keys.Count, "d.Keys and d.Values don't appear to match");
  398. int count = 0;
  399. foreach (int i in d.Values)
  400. ++count;
  401. Assert.AreEqual (count, d.Values.Count, "d.Values doesn't have the correct number of elements");
  402. count = 0;
  403. foreach (int i in d.Keys)
  404. ++count;
  405. Assert.AreEqual (count, d.Keys.Count, "d.Keys doesn't have the correct number of elements");
  406. int nkeys = count;
  407. count = 0;
  408. foreach (int i in d.Keys) {
  409. int foo = d [i];
  410. if (count++ >= nkeys)
  411. Assert.Fail ("Reading a value appears to trash enumerator state");
  412. }
  413. }
  414. [Test] // bug 75073
  415. public void SliceCollectionsEnumeratorTest ()
  416. {
  417. Dictionary<string, int> values = new Dictionary<string, int> ();
  418. IEnumerator <string> ke = values.Keys.GetEnumerator ();
  419. IEnumerator <int> ve = values.Values.GetEnumerator ();
  420. Assert.IsTrue (ke is Dictionary<string, int>.KeyCollection.Enumerator);
  421. Assert.IsTrue (ve is Dictionary<string, int>.ValueCollection.Enumerator);
  422. }
  423. [Test]
  424. public void PlainEnumeratorReturnTest ()
  425. {
  426. // Test that we return a KeyValuePair even for non-generic dictionary iteration
  427. _dictionary["foo"] = "bar";
  428. IEnumerator<KeyValuePair<string, object>> enumerator = _dictionary.GetEnumerator();
  429. Assert.IsTrue(enumerator.MoveNext(), "#1");
  430. Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IEnumerator)enumerator).Current.GetType (), "#2");
  431. Assert.AreEqual (typeof (DictionaryEntry), ((IDictionaryEnumerator)enumerator).Entry.GetType (), "#3");
  432. Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IDictionaryEnumerator)enumerator).Current.GetType (), "#4");
  433. Assert.AreEqual (typeof (KeyValuePair<string,object>), ((object) enumerator.Current).GetType (), "#5");
  434. }
  435. [Test, ExpectedException (typeof (InvalidOperationException))]
  436. public void FailFastTest1 ()
  437. {
  438. Dictionary<int, int> d = new Dictionary<int, int> ();
  439. d [1] = 1;
  440. int count = 0;
  441. foreach (KeyValuePair<int, int> kv in d) {
  442. d [kv.Key + 1] = kv.Value + 1;
  443. if (count++ != 0)
  444. Assert.Fail ("Should not be reached");
  445. }
  446. Assert.Fail ("Should not be reached");
  447. }
  448. [Test, ExpectedException (typeof (InvalidOperationException))]
  449. public void FailFastTest2 ()
  450. {
  451. Dictionary<int, int> d = new Dictionary<int, int> ();
  452. d [1] = 1;
  453. int count = 0;
  454. foreach (int i in d.Keys) {
  455. d [i + 1] = i + 1;
  456. if (count++ != 0)
  457. Assert.Fail ("Should not be reached");
  458. }
  459. Assert.Fail ("Should not be reached");
  460. }
  461. [Test, ExpectedException (typeof (InvalidOperationException))]
  462. public void FailFastTest3 ()
  463. {
  464. Dictionary<int, int> d = new Dictionary<int, int> ();
  465. d [1] = 1;
  466. int count = 0;
  467. foreach (int i in d.Keys) {
  468. d [i] = i;
  469. if (count++ != 0)
  470. Assert.Fail ("Should not be reached");
  471. }
  472. Assert.Fail ("Should not be reached");
  473. }
  474. [Test]
  475. public void SerializationTest()
  476. {
  477. for (int i = 0; i < 50; i++)
  478. {
  479. _dictionary3.Add(i, i);
  480. }
  481. BinaryFormatter formatter = new BinaryFormatter();
  482. MemoryStream stream = new MemoryStream();
  483. formatter.Serialize(stream, _dictionary3);
  484. stream.Position = 0;
  485. object deserialized = formatter.Deserialize(stream);
  486. Assert.IsNotNull(deserialized);
  487. Assert.IsFalse(deserialized == _dictionary3);
  488. Assert.IsTrue(deserialized is Dictionary<int, int>);
  489. Dictionary<int, int> d3 = deserialized as Dictionary<int, int>;
  490. Assert.AreEqual(50, d3.Count);
  491. for (int i = 0; i < 50; i++)
  492. {
  493. Assert.AreEqual(i, d3[i]);
  494. }
  495. }
  496. [Test]
  497. public void ZeroCapacity ()
  498. {
  499. Dictionary<int, int> x = new Dictionary <int, int> (0);
  500. x.Add (1, 2);
  501. x = new Dictionary <int, int> (0);
  502. x.Clear ();
  503. x = new Dictionary <int, int> (0);
  504. int aa = x.Count;
  505. x = new Dictionary <int, int> (0);
  506. try {
  507. int j = x [1];
  508. } catch (KeyNotFoundException){
  509. }
  510. bool b;
  511. b = x.ContainsKey (10);
  512. b = x.ContainsValue (10);
  513. x = new Dictionary <int, int> (0);
  514. x.Remove (10);
  515. x = new Dictionary <int, int> (0);
  516. int intv;
  517. x.TryGetValue (1, out intv);
  518. object oa = x.Keys;
  519. object ob = x.Values;
  520. foreach (KeyValuePair<int,int> a in x){
  521. }
  522. }
  523. [Test]
  524. public void Empty_KeysValues_CopyTo ()
  525. {
  526. Dictionary<int, int> d = new Dictionary<int, int> ();
  527. int[] array = new int[1];
  528. d.Keys.CopyTo (array, array.Length);
  529. d.Values.CopyTo (array, array.Length);
  530. }
  531. [Test]
  532. public void Empty_CopyTo ()
  533. {
  534. Dictionary<int, int> d = new Dictionary<int, int> ();
  535. ICollection c = (ICollection) d;
  536. DictionaryEntry [] array = new DictionaryEntry [1];
  537. c.CopyTo (array, array.Length);
  538. ICollection<KeyValuePair<int,int>> c2 = d;
  539. KeyValuePair<int,int> [] array2 = new KeyValuePair<int,int> [1];
  540. c2.CopyTo (array2, array2.Length);
  541. }
  542. [Test]
  543. public void IDictionary_Contains ()
  544. {
  545. IDictionary d = new Dictionary<int, int> ();
  546. d.Add (1, 2);
  547. Assert.IsTrue (d.Contains (1));
  548. Assert.IsFalse (d.Contains (2));
  549. Assert.IsFalse (d.Contains ("x"));
  550. }
  551. [Test, ExpectedException (typeof (ArgumentNullException))]
  552. public void IDictionary_Contains2 ()
  553. {
  554. IDictionary d = new Dictionary<int, int> ();
  555. d.Contains (null);
  556. }
  557. [Test, ExpectedException (typeof (ArgumentNullException))]
  558. public void IDictionary_Add1 ()
  559. {
  560. IDictionary d = new Dictionary<int, int> ();
  561. d.Add (null, 1);
  562. }
  563. [Test, ExpectedException (typeof (ArgumentException))]
  564. public void IDictionary_Add2 ()
  565. {
  566. IDictionary d = new Dictionary<int, int> ();
  567. d.Add ("bar", 1);
  568. }
  569. [Test, ExpectedException (typeof (ArgumentException))]
  570. public void IDictionary_Add3 ()
  571. {
  572. IDictionary d = new Dictionary<int, int> ();
  573. d.Add (1, "bar");
  574. }
  575. [Test]
  576. public void IDictionary_Remove1 ()
  577. {
  578. IDictionary d = new Dictionary<int, int> ();
  579. d.Add (1, 2);
  580. d.Remove (1);
  581. d.Remove (5);
  582. d.Remove ("foo");
  583. }
  584. [Test, ExpectedException (typeof (ArgumentNullException))]
  585. public void IDictionary_Remove2 ()
  586. {
  587. IDictionary d = new Dictionary<int, int> ();
  588. d.Remove (null);
  589. }
  590. [Test]
  591. public void IDictionary_IndexerGetNonExistingTest ()
  592. {
  593. IDictionary d = new Dictionary<int, int> ();
  594. d.Add(1, 2);
  595. Assert.IsNull(d[2]);
  596. Assert.IsNull(d["foo"]);
  597. }
  598. }
  599. }
  600. #endif // NET_2_0