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 class EquatableValue : IEquatable<EquatableValue>
17 public readonly string Value;
19 public EquatableValue (string value)
24 public bool Equals (EquatableValue other)
29 return string.Equals (Value, other.Value, StringComparison.OrdinalIgnoreCase);
33 LinkedList <int> intlist;
34 LinkedList <string> strings;
39 intlist = new LinkedList <int> ();
46 string [] tmpStrings = new string [] { "foo", "bar", "baz" };
47 // FIXME workaround for 74953
49 List <string> workaround = new List <string> ();
50 foreach (string s in tmpStrings)
53 // strings = new LinkedList <string> (tmpStrings);
54 strings = new LinkedList <string> (workaround);
58 public void AddedTest ()
61 foreach (int current in intlist)
63 Assert.AreEqual (i, current);
66 Assert.AreEqual (5, i);
70 public void CreatedTest ()
72 string [] values = new string [] { "foo", "bar", "baz" };
74 foreach (string current in strings)
76 Assert.AreEqual (values [i], current);
79 Assert.AreEqual (3, i);
83 public void NonCircularNodeTest ()
85 LinkedListNode <int> node = intlist.First;
86 Assert.AreEqual (2, node.Value);
87 LinkedListNode <int> previous = node.Previous;
88 Assert.IsNull (previous);
91 Assert.IsNotNull (node);
92 Assert.AreEqual (3, node.Value);
95 Assert.IsNotNull (node);
96 Assert.AreEqual (4, node.Value);
103 public void ClearTest ()
105 LinkedListNode <int> node = intlist.First;
108 Assert.AreEqual (0, intlist.Count);
109 Assert.AreEqual (2, node.Value);
110 Assert.IsNull (node.Next);
111 Assert.IsNull (node.Previous);
115 public void ContainsTest ()
117 Assert.IsTrue (intlist.Contains (3));
118 Assert.IsFalse (intlist.Contains (5));
122 public void AddBeforeAndAfterTest ()
124 LinkedListNode <int> node = intlist.Find (3);
125 intlist.AddAfter (node, new LinkedListNode <int> (5));
126 LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
127 LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
128 intlist.AddBefore (node, new LinkedListNode <int> (8));
130 Assert.AreEqual (6, sixNode.Value);
131 Assert.AreEqual (7, sevenNode.Value);
134 int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
136 foreach (int current in intlist)
138 Assert.AreEqual (values [i], current);
141 for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
142 Assert.AreSame (intlist, current.List);
146 public void CopyToTest ()
148 int [] values = new int [] { 2, 3, 4 };
149 int [] output = new int [3];
150 intlist.CopyTo (output, 0);
151 for (int i = 0; i < 3; i++)
152 Assert.AreEqual (values [i], output [i]);
154 LinkedList <int> l = new LinkedList <int> ();
155 values = new int [l.Count];
156 l.CopyTo (values, 0);
160 public void FindTest ()
162 intlist.AddFirst (4);
164 LinkedListNode <int> head, tail;
165 head = intlist.Find (4);
166 tail = intlist.FindLast (4);
167 Assert.AreEqual (intlist.First, head);
168 Assert.AreEqual (intlist.Last, tail);
172 public void RemoveTest ()
174 Assert.IsTrue (intlist.Remove (3));
175 Assert.AreEqual (2, intlist.Count);
177 int [] values = { 2, 4 };
179 foreach (int current in intlist)
181 Assert.AreEqual (values [i], current);
184 Assert.IsFalse (intlist.Remove (5));
186 LinkedListNode <string> node = strings.Find ("baz");
187 strings.Remove (node);
189 Assert.IsNull (node.List);
190 Assert.IsNull (node.Previous);
191 Assert.IsNull (node.Next);
193 string [] values2 = { "foo", "bar" };
195 foreach (string current in strings)
197 Assert.AreEqual (values2 [i], current);
202 [Test, ExpectedException (typeof (ArgumentNullException))]
203 public void RemoveNullNodeTest ()
205 intlist.Remove (null);
208 [Test, ExpectedException (typeof (InvalidOperationException))]
209 public void RemoveInvalidNodeTest ()
211 intlist.Remove (new LinkedListNode <int> (4));
215 public void RemoveFirstLastTest ()
217 strings.RemoveFirst ();
218 strings.RemoveLast ();
219 Assert.AreEqual (1, strings.Count);
220 Assert.AreEqual ("bar", strings.First.Value);
224 public void ListSerializationTest ()
226 BinaryFormatter formatter = new BinaryFormatter();
227 MemoryStream stream = new MemoryStream();
228 formatter.Serialize(stream, intlist);
231 object deserialized = formatter.Deserialize(stream);
233 Assert.IsTrue(deserialized is LinkedList <int>);
235 LinkedList <int> dlist = deserialized as LinkedList <int>;
237 int [] values = { 2, 3, 4 };
239 foreach (int value in dlist)
241 Assert.AreEqual (values [i], value);
244 Assert.AreEqual(3, i);
247 /* FIXME: disabled pending fix for #75299
250 public void EnumeratorSerializationTest ()
252 BinaryFormatter formatter = new BinaryFormatter ();
253 MemoryStream stream = new MemoryStream ();
254 LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
255 formatter.Serialize (stream, e);
258 object deserialized = formatter.Deserialize(stream);
260 Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
262 LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
264 int [] values = { 2, 3, 4 };
266 while (d.MoveNext ())
268 Assert.AreEqual (values [i], d.Current);
271 Assert.AreEqual(3, i);
274 public void EnumeratorAfterEnd ()
276 var linkedList = new LinkedList<string> ();
277 linkedList.AddLast ("a");
278 var e = linkedList.GetEnumerator ();
279 Assert.IsTrue (e.MoveNext (), "#1");
280 Assert.IsFalse (e.MoveNext (), "#2");
281 Assert.IsFalse (e.MoveNext (), "#3");
285 public void PlayWithNullValues ()
287 LinkedList <string> li = new LinkedList <string> ();
288 li.AddLast ((string)null);
290 li.AddLast ((string)null);
292 Assert.AreEqual (4, li.Count);
293 Assert.AreEqual ("efgh", li.Last.Value);
294 Assert.IsNull (li.First.Value);
296 Assert.IsTrue (li.Remove ((string)null));
297 Assert.AreEqual (3, li.Count);
298 Assert.AreEqual ("efgh", li.Last.Value);
299 Assert.AreEqual ("abcd", li.First.Value);
301 Assert.IsTrue (li.Remove ((string)null));
302 Assert.AreEqual (2, li.Count);
303 Assert.AreEqual ("efgh", li.Last.Value);
304 Assert.AreEqual ("abcd", li.First.Value);
306 Assert.IsFalse (li.Remove ((string)null));
307 Assert.AreEqual (2, li.Count);
308 Assert.AreEqual ("efgh", li.Last.Value);
309 Assert.AreEqual ("abcd", li.First.Value);
313 public void EqualityComparer ()
315 var list = new LinkedList<EquatableValue> ();
316 var mv = new EquatableValue ("first");
319 var test = new EquatableValue ("FIRST");
320 Assert.IsTrue (list.Contains (test), "#1");
321 Assert.AreSame (mv, list.Find (test).Value, "#2");
322 Assert.AreSame (mv, list.FindLast (test).Value, "#3");
326 public void RemoveFromEmptyList ()
328 var linkedList = new LinkedList<string> ();
330 linkedList.RemoveFirst ();
332 } catch (InvalidOperationException) {
336 linkedList.RemoveLast ();
338 } catch (InvalidOperationException) {