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 LinkedList <int> intlist;
15 LinkedList <string> strings;
20 intlist = new LinkedList <int> ();
27 string [] tmpStrings = new string [] { "foo", "bar", "baz" };
28 // FIXME workaround for 74953
30 List <string> workaround = new List <string> ();
31 foreach (string s in tmpStrings)
34 // strings = new LinkedList <string> (tmpStrings);
35 strings = new LinkedList <string> (workaround);
39 public void AddedTest ()
42 foreach (int current in intlist)
44 Assert.AreEqual (i, current);
47 Assert.AreEqual (5, i);
51 public void CreatedTest ()
53 string [] values = new string [] { "foo", "bar", "baz" };
55 foreach (string current in strings)
57 Assert.AreEqual (values [i], current);
60 Assert.AreEqual (3, i);
64 public void NonCircularNodeTest ()
66 LinkedListNode <int> node = intlist.First;
67 Assert.AreEqual (2, node.Value);
68 LinkedListNode <int> previous = node.Previous;
69 Assert.IsNull (previous);
72 Assert.IsNotNull (node);
73 Assert.AreEqual (3, node.Value);
76 Assert.IsNotNull (node);
77 Assert.AreEqual (4, node.Value);
84 public void ClearTest ()
87 Assert.AreEqual (0, intlist.Count);
91 public void ContainsTest ()
93 Assert.IsTrue (intlist.Contains (3));
94 Assert.IsFalse (intlist.Contains (5));
98 public void AddBeforeAndAfterTest ()
100 LinkedListNode <int> node = intlist.Find (3);
101 intlist.AddAfter (node, new LinkedListNode <int> (5));
102 LinkedListNode <int> sixNode = intlist.AddAfter (node, 6);
103 LinkedListNode <int> sevenNode = intlist.AddBefore (node, 7);
104 intlist.AddBefore (node, new LinkedListNode <int> (8));
106 Assert.AreEqual (6, sixNode.Value);
107 Assert.AreEqual (7, sevenNode.Value);
110 int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
112 foreach (int current in intlist)
114 Assert.AreEqual (values [i], current);
117 for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
118 Assert.AreSame (intlist, current.List);
122 public void CopyToTest ()
124 int [] values = new int [] { 2, 3, 4 };
125 int [] output = new int [3];
126 intlist.CopyTo (output, 0);
127 for (int i = 0; i < 3; i++)
128 Assert.AreEqual (values [i], output [i]);
132 public void FindTest ()
134 intlist.AddFirst (4);
136 LinkedListNode <int> head, tail;
137 head = intlist.Find (4);
138 tail = intlist.FindLast (4);
139 Assert.AreEqual (intlist.First, head);
140 Assert.AreEqual (intlist.Last, tail);
144 public void RemoveTest ()
146 Assert.IsTrue (intlist.Remove (3));
147 Assert.AreEqual (2, intlist.Count);
149 int [] values = { 2, 4 };
151 foreach (int current in intlist)
153 Assert.AreEqual (values [i], current);
156 Assert.IsFalse (intlist.Remove (5));
158 LinkedListNode <string> node = strings.Find ("baz");
159 strings.Remove (node);
161 Assert.IsNull (node.List);
162 Assert.IsNull (node.Previous);
163 Assert.IsNull (node.Next);
165 string [] values2 = { "foo", "bar" };
167 foreach (string current in strings)
169 Assert.AreEqual (values2 [i], current);
174 [Test, ExpectedException (typeof (ArgumentNullException))]
175 public void RemoveNullNodeTest ()
177 intlist.Remove (null);
180 [Test, ExpectedException (typeof (InvalidOperationException))]
181 public void RemoveInvalidNodeTest ()
183 intlist.Remove (new LinkedListNode <int> (4));
187 public void RemoveFirstLastTest ()
189 strings.RemoveFirst ();
190 strings.RemoveLast ();
191 Assert.AreEqual (1, strings.Count);
192 Assert.AreEqual ("bar", strings.First.Value);
196 public void ListSerializationTest ()
198 BinaryFormatter formatter = new BinaryFormatter();
199 MemoryStream stream = new MemoryStream();
200 formatter.Serialize(stream, intlist);
203 object deserialized = formatter.Deserialize(stream);
205 Assert.IsTrue(deserialized is LinkedList <int>);
207 LinkedList <int> dlist = deserialized as LinkedList <int>;
209 int [] values = { 2, 3, 4 };
211 foreach (int value in dlist)
213 Assert.AreEqual (values [i], value);
216 Assert.AreEqual(3, i);
219 /* FIXME: disabled pending fix for #75299
222 public void EnumeratorSerializationTest ()
224 BinaryFormatter formatter = new BinaryFormatter ();
225 MemoryStream stream = new MemoryStream ();
226 LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
227 formatter.Serialize (stream, e);
230 object deserialized = formatter.Deserialize(stream);
232 Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
234 LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
236 int [] values = { 2, 3, 4 };
238 while (d.MoveNext ())
240 Assert.AreEqual (values [i], d.Current);
243 Assert.AreEqual(3, i);