3 using System.Collections.Generic;
6 using System.Runtime.Serialization;
7 using System.Runtime.Serialization.Formatters.Binary;
10 namespace MonoTests.System.Collections.Generic
13 public class LinkedListTest
15 LinkedList <int> intlist;
16 LinkedList <string> strings;
21 intlist = new LinkedList <int> ();
28 string [] tmpStrings = new string [] { "foo", "bar", "baz" };
29 // FIXME workaround for 74953
31 List <string> workaround = new List <string> ();
32 foreach (string s in tmpStrings)
35 // strings = new LinkedList <string> (tmpStrings);
36 strings = new LinkedList <string> (workaround);
40 public void AddedTest ()
43 foreach (int current in intlist)
45 Assert.AreEqual (i, current);
48 Assert.AreEqual (5, i);
52 public void CreatedTest ()
54 string [] values = new string [] { "foo", "bar", "baz" };
56 foreach (string current in strings)
58 Assert.AreEqual (values [i], current);
61 Assert.AreEqual (3, i);
65 public void NonCircularNodeTest ()
67 LinkedListNode <int> node = intlist.First;
68 Assert.AreEqual (2, node.Value);
69 LinkedListNode <int> previous = node.Previous;
70 Assert.IsNull (previous);
73 Assert.IsNotNull (node);
74 Assert.AreEqual (3, node.Value);
77 Assert.IsNotNull (node);
78 Assert.AreEqual (4, node.Value);
85 public void ClearTest ()
87 LinkedListNode <int> node = intlist.First;
90 Assert.AreEqual (0, intlist.Count);
91 Assert.AreEqual (2, node.Value);
92 Assert.IsNull (node.Next);
93 Assert.IsNull (node.Previous);
97 public void ContainsTest ()
99 Assert.IsTrue (intlist.Contains (3));
100 Assert.IsFalse (intlist.Contains (5));
104 public void AddBeforeAndAfterTest ()
106 LinkedListNode <int> node = intlist.Find (3);
107 intlist.AddAfter (node, new LinkedListNode <int> (5));
108 LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
109 LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
110 intlist.AddBefore (node, new LinkedListNode <int> (8));
112 Assert.AreEqual (6, sixNode.Value);
113 Assert.AreEqual (7, sevenNode.Value);
116 int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
118 foreach (int current in intlist)
120 Assert.AreEqual (values [i], current);
123 for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
124 Assert.AreSame (intlist, current.List);
128 public void CopyToTest ()
130 int [] values = new int [] { 2, 3, 4 };
131 int [] output = new int [3];
132 intlist.CopyTo (output, 0);
133 for (int i = 0; i < 3; i++)
134 Assert.AreEqual (values [i], output [i]);
136 LinkedList <int> l = new LinkedList <int> ();
137 values = new int [l.Count];
138 l.CopyTo (values, 0);
142 public void FindTest ()
144 intlist.AddFirst (4);
146 LinkedListNode <int> head, tail;
147 head = intlist.Find (4);
148 tail = intlist.FindLast (4);
149 Assert.AreEqual (intlist.First, head);
150 Assert.AreEqual (intlist.Last, tail);
154 public void RemoveTest ()
156 Assert.IsTrue (intlist.Remove (3));
157 Assert.AreEqual (2, intlist.Count);
159 int [] values = { 2, 4 };
161 foreach (int current in intlist)
163 Assert.AreEqual (values [i], current);
166 Assert.IsFalse (intlist.Remove (5));
168 LinkedListNode <string> node = strings.Find ("baz");
169 strings.Remove (node);
171 Assert.IsNull (node.List);
172 Assert.IsNull (node.Previous);
173 Assert.IsNull (node.Next);
175 string [] values2 = { "foo", "bar" };
177 foreach (string current in strings)
179 Assert.AreEqual (values2 [i], current);
184 [Test, ExpectedException (typeof (ArgumentNullException))]
185 public void RemoveNullNodeTest ()
187 intlist.Remove (null);
190 [Test, ExpectedException (typeof (InvalidOperationException))]
191 public void RemoveInvalidNodeTest ()
193 intlist.Remove (new LinkedListNode <int> (4));
197 public void RemoveFirstLastTest ()
199 strings.RemoveFirst ();
200 strings.RemoveLast ();
201 Assert.AreEqual (1, strings.Count);
202 Assert.AreEqual ("bar", strings.First.Value);
206 public void ListSerializationTest ()
208 BinaryFormatter formatter = new BinaryFormatter();
209 MemoryStream stream = new MemoryStream();
210 formatter.Serialize(stream, intlist);
213 object deserialized = formatter.Deserialize(stream);
215 Assert.IsTrue(deserialized is LinkedList <int>);
217 LinkedList <int> dlist = deserialized as LinkedList <int>;
219 int [] values = { 2, 3, 4 };
221 foreach (int value in dlist)
223 Assert.AreEqual (values [i], value);
226 Assert.AreEqual(3, i);
229 /* FIXME: disabled pending fix for #75299
232 public void EnumeratorSerializationTest ()
234 BinaryFormatter formatter = new BinaryFormatter ();
235 MemoryStream stream = new MemoryStream ();
236 LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
237 formatter.Serialize (stream, e);
240 object deserialized = formatter.Deserialize(stream);
242 Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
244 LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
246 int [] values = { 2, 3, 4 };
248 while (d.MoveNext ())
250 Assert.AreEqual (values [i], d.Current);
253 Assert.AreEqual(3, i);
257 public void PlayWithNullValues ()
259 LinkedList <string> li = new LinkedList <string> ();
260 li.AddLast ((string)null);
262 li.AddLast ((string)null);
264 Assert.AreEqual (4, li.Count);
265 Assert.AreEqual ("efgh", li.Last.Value);
266 Assert.IsNull (li.First.Value);
268 Assert.IsTrue (li.Remove ((string)null));
269 Assert.AreEqual (3, li.Count);
270 Assert.AreEqual ("efgh", li.Last.Value);
271 Assert.AreEqual ("abcd", li.First.Value);
273 Assert.IsTrue (li.Remove ((string)null));
274 Assert.AreEqual (2, li.Count);
275 Assert.AreEqual ("efgh", li.Last.Value);
276 Assert.AreEqual ("abcd", li.First.Value);
278 Assert.IsFalse (li.Remove ((string)null));
279 Assert.AreEqual (2, li.Count);
280 Assert.AreEqual ("efgh", li.Last.Value);
281 Assert.AreEqual ("abcd", li.First.Value);