LinkedListTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #if NET_2_0
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using NUnit.Framework;
  9. namespace MonoTests.System.Collections.Generic
  10. {
  11. [TestFixture]
  12. public class LinkedListTest
  13. {
  14. LinkedList <int> intlist;
  15. LinkedList <string> strings;
  16. [SetUp]
  17. public void Setup ()
  18. {
  19. intlist = new LinkedList <int> ();
  20. // 2 3 4
  21. intlist.AddLast (3);
  22. intlist.AddLast (4);
  23. intlist.AddFirst (2);
  24. string [] tmpStrings = new string [] { "foo", "bar", "baz" };
  25. // FIXME workaround for 74953
  26. List <string> workaround = new List <string> ();
  27. foreach (string s in tmpStrings)
  28. workaround.Add (s);
  29. // strings = new LinkedList <string> (tmpStrings);
  30. strings = new LinkedList <string> (workaround);
  31. }
  32. [Test]
  33. public void AddedTest ()
  34. {
  35. int i = 2;
  36. foreach (int current in intlist)
  37. {
  38. Assert.AreEqual (i, current);
  39. i++;
  40. }
  41. Assert.AreEqual (5, i);
  42. }
  43. [Test]
  44. public void CreatedTest ()
  45. {
  46. string [] values = new string [] { "foo", "bar", "baz" };
  47. int i = 0;
  48. foreach (string current in strings)
  49. {
  50. Assert.AreEqual (values [i], current);
  51. i++;
  52. }
  53. Assert.AreEqual (3, i);
  54. }
  55. [Test]
  56. public void NonCircularNodeTest ()
  57. {
  58. LinkedListNode <int> node = intlist.First;
  59. Assert.AreEqual (2, node.Value);
  60. LinkedListNode <int> previous = node.Previous;
  61. Assert.IsNull (previous);
  62. node = node.Next;
  63. Assert.IsNotNull (node);
  64. Assert.AreEqual (3, node.Value);
  65. node = node.Next;
  66. Assert.IsNotNull (node);
  67. Assert.AreEqual (4, node.Value);
  68. node = node.Next;
  69. Assert.IsNull (node);
  70. }
  71. [Test]
  72. public void ClearTest ()
  73. {
  74. intlist.Clear ();
  75. Assert.AreEqual (0, intlist.Count);
  76. }
  77. [Test]
  78. public void ContainsTest ()
  79. {
  80. Assert.IsTrue (intlist.Contains (3));
  81. Assert.IsFalse (intlist.Contains (5));
  82. }
  83. [Test]
  84. public void AddBeforeAndAfterTest ()
  85. {
  86. LinkedListNode <int> node = intlist.Find (3);
  87. intlist.AddAfter (node, new LinkedListNode <int> (5));
  88. LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
  89. LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
  90. intlist.AddBefore (node, new LinkedListNode <int> (8));
  91. Assert.AreEqual (6, sixNode.Value);
  92. Assert.AreEqual (7, sevenNode.Value);
  93. // 2 7 8 3 6 5 4
  94. int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
  95. int i = 0;
  96. foreach (int current in intlist)
  97. {
  98. Assert.AreEqual (values [i], current);
  99. i++;
  100. }
  101. for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
  102. Assert.AreSame (intlist, current.List);
  103. }
  104. [Test]
  105. public void CopyToTest ()
  106. {
  107. int [] values = new int [] { 2, 3, 4 };
  108. int [] output = new int [3];
  109. intlist.CopyTo (output, 0);
  110. for (int i = 0; i < 3; i++)
  111. Assert.AreEqual (values [i], output [i]);
  112. }
  113. [Test]
  114. public void FindTest ()
  115. {
  116. intlist.AddFirst (4);
  117. LinkedListNode <int> head, tail;
  118. head = intlist.Find (4);
  119. tail = intlist.FindLast (4);
  120. Assert.AreEqual (intlist.First, head);
  121. Assert.AreEqual (intlist.Last, tail);
  122. }
  123. [Test]
  124. public void RemoveTest ()
  125. {
  126. Assert.IsTrue (intlist.Remove (3));
  127. Assert.AreEqual (2, intlist.Count);
  128. int [] values = { 2, 4 };
  129. int i = 0;
  130. foreach (int current in intlist)
  131. {
  132. Assert.AreEqual (values [i], current);
  133. i++;
  134. }
  135. Assert.IsFalse (intlist.Remove (5));
  136. LinkedListNode <string> node = strings.Find ("baz");
  137. strings.Remove (node);
  138. Assert.IsNull (node.List);
  139. Assert.IsNull (node.Previous);
  140. Assert.IsNull (node.Next);
  141. string [] values2 = { "foo", "bar" };
  142. i = 0;
  143. foreach (string current in strings)
  144. {
  145. Assert.AreEqual (values2 [i], current);
  146. i++;
  147. }
  148. }
  149. [Test, ExpectedException (typeof (ArgumentNullException))]
  150. public void RemoveNullNodeTest ()
  151. {
  152. intlist.Remove (null);
  153. }
  154. [Test, ExpectedException (typeof (InvalidOperationException))]
  155. public void RemoveInvalidNodeTest ()
  156. {
  157. intlist.Remove (new LinkedListNode <int> (4));
  158. }
  159. [Test]
  160. public void RemoveFirstLastTest ()
  161. {
  162. strings.RemoveFirst ();
  163. strings.RemoveLast ();
  164. Assert.AreEqual (1, strings.Count);
  165. Assert.AreEqual ("bar", strings.First.Value);
  166. }
  167. [Test]
  168. public void ListSerializationTest ()
  169. {
  170. BinaryFormatter formatter = new BinaryFormatter();
  171. MemoryStream stream = new MemoryStream();
  172. formatter.Serialize(stream, intlist);
  173. stream.Position = 0;
  174. object deserialized = formatter.Deserialize(stream);
  175. Assert.IsTrue(deserialized is LinkedList <int>);
  176. LinkedList <int> dlist = deserialized as LinkedList <int>;
  177. int [] values = { 2, 3, 4 };
  178. int i = 0;
  179. foreach (int value in dlist)
  180. {
  181. Assert.AreEqual (values [i], value);
  182. i++;
  183. }
  184. Assert.AreEqual(3, i);
  185. }
  186. /* FIXME: disabled pending fix for #75299
  187. [Test]
  188. */
  189. public void EnumeratorSerializationTest ()
  190. {
  191. BinaryFormatter formatter = new BinaryFormatter ();
  192. MemoryStream stream = new MemoryStream ();
  193. LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
  194. formatter.Serialize (stream, e);
  195. stream.Position = 0;
  196. object deserialized = formatter.Deserialize(stream);
  197. Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
  198. LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
  199. int [] values = { 2, 3, 4 };
  200. int i = 0;
  201. while (d.MoveNext ())
  202. {
  203. Assert.AreEqual (values [i], d.Current);
  204. i++;
  205. }
  206. Assert.AreEqual(3, i);
  207. }
  208. }
  209. }
  210. #endif