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 ()
88 Assert.AreEqual (0, intlist.Count);
92 public void ContainsTest ()
94 Assert.IsTrue (intlist.Contains (3));
95 Assert.IsFalse (intlist.Contains (5));
99 public void AddBeforeAndAfterTest ()
101 LinkedListNode <int> node = intlist.Find (3);
102 intlist.AddAfter (node, new LinkedListNode <int> (5));
103 LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
104 LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
105 intlist.AddBefore (node, new LinkedListNode <int> (8));
107 Assert.AreEqual (6, sixNode.Value);
108 Assert.AreEqual (7, sevenNode.Value);
111 int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
113 foreach (int current in intlist)
115 Assert.AreEqual (values [i], current);
118 for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
119 Assert.AreSame (intlist, current.List);
123 public void CopyToTest ()
125 int [] values = new int [] { 2, 3, 4 };
126 int [] output = new int [3];
127 intlist.CopyTo (output, 0);
128 for (int i = 0; i < 3; i++)
129 Assert.AreEqual (values [i], output [i]);
133 public void FindTest ()
135 intlist.AddFirst (4);
137 LinkedListNode <int> head, tail;
138 head = intlist.Find (4);
139 tail = intlist.FindLast (4);
140 Assert.AreEqual (intlist.First, head);
141 Assert.AreEqual (intlist.Last, tail);
145 public void RemoveTest ()
147 Assert.IsTrue (intlist.Remove (3));
148 Assert.AreEqual (2, intlist.Count);
150 int [] values = { 2, 4 };
152 foreach (int current in intlist)
154 Assert.AreEqual (values [i], current);
157 Assert.IsFalse (intlist.Remove (5));
159 LinkedListNode <string> node = strings.Find ("baz");
160 strings.Remove (node);
162 Assert.IsNull (node.List);
163 Assert.IsNull (node.Previous);
164 Assert.IsNull (node.Next);
166 string [] values2 = { "foo", "bar" };
168 foreach (string current in strings)
170 Assert.AreEqual (values2 [i], current);
175 [Test, ExpectedException (typeof (ArgumentNullException))]
176 public void RemoveNullNodeTest ()
178 intlist.Remove (null);
181 [Test, ExpectedException (typeof (InvalidOperationException))]
182 public void RemoveInvalidNodeTest ()
184 intlist.Remove (new LinkedListNode <int> (4));
188 public void RemoveFirstLastTest ()
190 strings.RemoveFirst ();
191 strings.RemoveLast ();
192 Assert.AreEqual (1, strings.Count);
193 Assert.AreEqual ("bar", strings.First.Value);
197 public void ListSerializationTest ()
199 BinaryFormatter formatter = new BinaryFormatter();
200 MemoryStream stream = new MemoryStream();
201 formatter.Serialize(stream, intlist);
204 object deserialized = formatter.Deserialize(stream);
206 Assert.IsTrue(deserialized is LinkedList <int>);
208 LinkedList <int> dlist = deserialized as LinkedList <int>;
210 int [] values = { 2, 3, 4 };
212 foreach (int value in dlist)
214 Assert.AreEqual (values [i], value);
217 Assert.AreEqual(3, i);
220 /* FIXME: disabled pending fix for #75299
223 public void EnumeratorSerializationTest ()
225 BinaryFormatter formatter = new BinaryFormatter ();
226 MemoryStream stream = new MemoryStream ();
227 LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
228 formatter.Serialize (stream, e);
231 object deserialized = formatter.Deserialize(stream);
233 Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
235 LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
237 int [] values = { 2, 3, 4 };
239 while (d.MoveNext ())
241 Assert.AreEqual (values [i], d.Current);
244 Assert.AreEqual(3, i);