2 using System.Collections.Generic;
5 using System.Runtime.Serialization;
6 using System.Runtime.Serialization.Formatters.Binary;
9 namespace MonoTests.System.Collections.Generic
12 public class LinkedListTest
14 class EquatableValue : IEquatable<EquatableValue>
16 public readonly string Value;
18 public EquatableValue (string value)
23 public bool Equals (EquatableValue other)
28 return string.Equals (Value, other.Value, StringComparison.OrdinalIgnoreCase);
32 LinkedList <int> intlist;
33 LinkedList <string> strings;
38 intlist = new LinkedList <int> ();
45 string [] tmpStrings = new string [] { "foo", "bar", "baz" };
46 // FIXME workaround for 74953
48 List <string> workaround = new List <string> ();
49 foreach (string s in tmpStrings)
52 // strings = new LinkedList <string> (tmpStrings);
53 strings = new LinkedList <string> (workaround);
57 public void AddedTest ()
60 foreach (int current in intlist)
62 Assert.AreEqual (i, current);
65 Assert.AreEqual (5, i);
69 public void CreatedTest ()
71 string [] values = new string [] { "foo", "bar", "baz" };
73 foreach (string current in strings)
75 Assert.AreEqual (values [i], current);
78 Assert.AreEqual (3, i);
82 public void NonCircularNodeTest ()
84 LinkedListNode <int> node = intlist.First;
85 Assert.AreEqual (2, node.Value);
86 LinkedListNode <int> previous = node.Previous;
87 Assert.IsNull (previous);
90 Assert.IsNotNull (node);
91 Assert.AreEqual (3, node.Value);
94 Assert.IsNotNull (node);
95 Assert.AreEqual (4, node.Value);
102 public void ClearTest ()
104 LinkedListNode <int> node = intlist.First;
107 Assert.AreEqual (0, intlist.Count);
108 Assert.AreEqual (2, node.Value);
109 Assert.IsNull (node.Next);
110 Assert.IsNull (node.Previous);
114 public void ContainsTest ()
116 Assert.IsTrue (intlist.Contains (3));
117 Assert.IsFalse (intlist.Contains (5));
121 public void AddBeforeAndAfterTest ()
123 LinkedListNode <int> node = intlist.Find (3);
124 intlist.AddAfter (node, new LinkedListNode <int> (5));
125 LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
126 LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
127 intlist.AddBefore (node, new LinkedListNode <int> (8));
129 Assert.AreEqual (6, sixNode.Value);
130 Assert.AreEqual (7, sevenNode.Value);
133 int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
135 foreach (int current in intlist)
137 Assert.AreEqual (values [i], current);
140 for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
141 Assert.AreSame (intlist, current.List);
145 public void CopyToTest ()
147 int [] values = new int [] { 2, 3, 4 };
148 int [] output = new int [3];
149 intlist.CopyTo (output, 0);
150 for (int i = 0; i < 3; i++)
151 Assert.AreEqual (values [i], output [i]);
153 LinkedList <int> l = new LinkedList <int> ();
154 values = new int [l.Count];
155 l.CopyTo (values, 0);
159 public void FindTest ()
161 intlist.AddFirst (4);
163 LinkedListNode <int> head, tail;
164 head = intlist.Find (4);
165 tail = intlist.FindLast (4);
166 Assert.AreEqual (intlist.First, head);
167 Assert.AreEqual (intlist.Last, tail);
171 public void RemoveTest ()
173 Assert.IsTrue (intlist.Remove (3));
174 Assert.AreEqual (2, intlist.Count);
176 int [] values = { 2, 4 };
178 foreach (int current in intlist)
180 Assert.AreEqual (values [i], current);
183 Assert.IsFalse (intlist.Remove (5));
185 LinkedListNode <string> node = strings.Find ("baz");
186 strings.Remove (node);
188 Assert.IsNull (node.List);
189 Assert.IsNull (node.Previous);
190 Assert.IsNull (node.Next);
192 string [] values2 = { "foo", "bar" };
194 foreach (string current in strings)
196 Assert.AreEqual (values2 [i], current);
201 [Test, ExpectedException (typeof (ArgumentNullException))]
202 public void RemoveNullNodeTest ()
204 intlist.Remove (null);
207 [Test, ExpectedException (typeof (InvalidOperationException))]
208 public void RemoveInvalidNodeTest ()
210 intlist.Remove (new LinkedListNode <int> (4));
214 public void RemoveFirstLastTest ()
216 strings.RemoveFirst ();
217 strings.RemoveLast ();
218 Assert.AreEqual (1, strings.Count);
219 Assert.AreEqual ("bar", strings.First.Value);
223 public void ListSerializationTest ()
225 BinaryFormatter formatter = new BinaryFormatter();
226 MemoryStream stream = new MemoryStream();
227 formatter.Serialize(stream, intlist);
230 object deserialized = formatter.Deserialize(stream);
232 Assert.IsTrue(deserialized is LinkedList <int>);
234 LinkedList <int> dlist = deserialized as LinkedList <int>;
236 int [] values = { 2, 3, 4 };
238 foreach (int value in dlist)
240 Assert.AreEqual (values [i], value);
243 Assert.AreEqual(3, i);
246 /* FIXME: disabled pending fix for #75299
249 public void EnumeratorSerializationTest ()
251 BinaryFormatter formatter = new BinaryFormatter ();
252 MemoryStream stream = new MemoryStream ();
253 LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
254 formatter.Serialize (stream, e);
257 object deserialized = formatter.Deserialize(stream);
259 Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
261 LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
263 int [] values = { 2, 3, 4 };
265 while (d.MoveNext ())
267 Assert.AreEqual (values [i], d.Current);
270 Assert.AreEqual(3, i);
273 public void EnumeratorAfterEnd ()
275 var linkedList = new LinkedList<string> ();
276 linkedList.AddLast ("a");
277 var e = linkedList.GetEnumerator ();
278 Assert.IsTrue (e.MoveNext (), "#1");
279 Assert.IsFalse (e.MoveNext (), "#2");
280 Assert.IsFalse (e.MoveNext (), "#3");
284 public void PlayWithNullValues ()
286 LinkedList <string> li = new LinkedList <string> ();
287 li.AddLast ((string)null);
289 li.AddLast ((string)null);
291 Assert.AreEqual (4, li.Count);
292 Assert.AreEqual ("efgh", li.Last.Value);
293 Assert.IsNull (li.First.Value);
295 Assert.IsTrue (li.Remove ((string)null));
296 Assert.AreEqual (3, li.Count);
297 Assert.AreEqual ("efgh", li.Last.Value);
298 Assert.AreEqual ("abcd", li.First.Value);
300 Assert.IsTrue (li.Remove ((string)null));
301 Assert.AreEqual (2, li.Count);
302 Assert.AreEqual ("efgh", li.Last.Value);
303 Assert.AreEqual ("abcd", li.First.Value);
305 Assert.IsFalse (li.Remove ((string)null));
306 Assert.AreEqual (2, li.Count);
307 Assert.AreEqual ("efgh", li.Last.Value);
308 Assert.AreEqual ("abcd", li.First.Value);
312 public void EqualityComparer ()
314 var list = new LinkedList<EquatableValue> ();
315 var mv = new EquatableValue ("first");
318 var test = new EquatableValue ("FIRST");
319 Assert.IsTrue (list.Contains (test), "#1");
320 Assert.AreSame (mv, list.Find (test).Value, "#2");
321 Assert.AreSame (mv, list.FindLast (test).Value, "#3");
325 public void RemoveFromEmptyList ()
327 var linkedList = new LinkedList<string> ();
329 linkedList.RemoveFirst ();
331 } catch (InvalidOperationException) {
335 linkedList.RemoveLast ();
337 } catch (InvalidOperationException) {