Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System / Test / System.Collections.Generic / LinkedListTest.cs
1 #if NET_2_0
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Text;
6 using System.Runtime.Serialization;
7 using System.Runtime.Serialization.Formatters.Binary;
8 using NUnit.Framework;
9
10 namespace MonoTests.System.Collections.Generic
11 {
12         [TestFixture]
13         public class LinkedListTest
14         {
15                 class EquatableValue : IEquatable<EquatableValue>
16                 {
17                         public readonly string Value;
18
19                         public EquatableValue (string value)
20                         {
21                                 this.Value = value;
22                         }
23
24                         public bool Equals (EquatableValue other)
25                         {
26                                 if (other == null)
27                                         return false;
28
29                                 return string.Equals (Value, other.Value, StringComparison.OrdinalIgnoreCase);
30                         }
31                 }
32
33                 LinkedList <int> intlist;
34                 LinkedList <string> strings;
35
36                 [SetUp]
37                 public void Setup ()
38                 {
39                         intlist = new LinkedList <int> ();
40                         
41                         // 2 3 4
42                         intlist.AddLast (3);
43                         intlist.AddLast (4);
44                         intlist.AddFirst (2);
45
46                         string [] tmpStrings = new string [] { "foo", "bar", "baz" };
47                         // FIXME workaround for 74953
48                         
49                         List <string> workaround = new List <string> ();
50                         foreach (string s in tmpStrings)
51                                 workaround.Add (s);
52
53                         // strings = new LinkedList <string> (tmpStrings);
54                         strings = new LinkedList <string> (workaround);
55                 }
56
57                 [Test]
58                 public void AddedTest ()
59                 {
60                         int i = 2;
61                         foreach (int current in intlist)
62                         {
63                                 Assert.AreEqual (i, current);
64                                 i++;
65                         }
66                         Assert.AreEqual (5, i);
67                 }
68
69                 [Test]
70                 public void CreatedTest ()
71                 {
72                         string [] values = new string [] { "foo", "bar", "baz" };
73                         int i = 0;
74                         foreach (string current in strings)
75                         {
76                                 Assert.AreEqual (values [i], current);
77                                 i++;
78                         }
79                         Assert.AreEqual (3, i);
80                 }
81
82                 [Test]
83                 public void NonCircularNodeTest ()
84                 {
85                         LinkedListNode <int> node = intlist.First;
86                         Assert.AreEqual (2, node.Value);
87                         LinkedListNode <int> previous = node.Previous;
88                         Assert.IsNull (previous);
89
90                         node = node.Next;
91                         Assert.IsNotNull (node);
92                         Assert.AreEqual (3, node.Value);
93
94                         node = node.Next;
95                         Assert.IsNotNull (node);
96                         Assert.AreEqual (4, node.Value);
97
98                         node = node.Next;
99                         Assert.IsNull (node);
100                 }
101
102                 [Test]
103                 public void ClearTest ()
104                 {
105                         LinkedListNode <int> node = intlist.First;
106                         intlist.Clear ();
107
108                         Assert.AreEqual (0, intlist.Count);
109                         Assert.AreEqual (2, node.Value);
110                         Assert.IsNull (node.Next);
111                         Assert.IsNull (node.Previous);
112                 }
113
114                 [Test]
115                 public void ContainsTest ()
116                 {
117                         Assert.IsTrue (intlist.Contains (3));
118                         Assert.IsFalse (intlist.Contains (5));
119                 }
120
121                 [Test]
122                 public void AddBeforeAndAfterTest ()
123                 {
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));
129
130                         Assert.AreEqual (6, sixNode.Value);
131                         Assert.AreEqual (7, sevenNode.Value);
132
133                         // 2 7 8 3 6 5 4
134                         int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
135                         int i = 0;
136                         foreach (int current in intlist)
137                         {
138                                 Assert.AreEqual (values [i], current);
139                                 i++;
140                         }
141                         for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
142                                 Assert.AreSame (intlist, current.List);
143                 }
144
145                 [Test]
146                 public void CopyToTest ()
147                 {
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]);
153                         
154                         LinkedList <int> l = new LinkedList <int> ();
155                         values = new int [l.Count];
156                         l.CopyTo (values, 0);
157                 }
158
159                 [Test]
160                 public void FindTest ()
161                 {
162                         intlist.AddFirst (4);
163
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);
169                 }
170
171                 [Test]
172                 public void RemoveTest ()
173                 {
174                         Assert.IsTrue (intlist.Remove (3));
175                         Assert.AreEqual (2, intlist.Count);
176
177                         int [] values = { 2, 4 };
178                         int i = 0;
179                         foreach (int current in intlist)
180                         {
181                                 Assert.AreEqual (values [i], current);
182                                 i++;
183                         }
184                         Assert.IsFalse (intlist.Remove (5));
185
186                         LinkedListNode <string> node = strings.Find ("baz");
187                         strings.Remove (node);
188
189                         Assert.IsNull (node.List);
190                         Assert.IsNull (node.Previous);
191                         Assert.IsNull (node.Next);
192
193                         string [] values2 = { "foo", "bar" };
194                         i = 0;
195                         foreach (string current in strings)
196                         {
197                                 Assert.AreEqual (values2 [i], current);
198                                 i++;
199                         }
200                 }
201
202                 [Test, ExpectedException (typeof (ArgumentNullException))]
203                 public void RemoveNullNodeTest ()
204                 {
205                         intlist.Remove (null);
206                 }
207                 
208                 [Test, ExpectedException (typeof (InvalidOperationException))]
209                 public void RemoveInvalidNodeTest ()
210                 {
211                         intlist.Remove (new LinkedListNode <int> (4));
212                 }
213
214                 [Test]
215                 public void RemoveFirstLastTest ()
216                 {
217                         strings.RemoveFirst ();
218                         strings.RemoveLast ();
219                         Assert.AreEqual (1, strings.Count);
220                         Assert.AreEqual ("bar", strings.First.Value);
221                 }
222
223                 [Test]
224                 public void ListSerializationTest ()
225                 {
226                         BinaryFormatter formatter = new BinaryFormatter();
227                         MemoryStream stream = new MemoryStream();
228                         formatter.Serialize(stream, intlist);
229
230                         stream.Position = 0;
231                         object deserialized = formatter.Deserialize(stream);
232
233                         Assert.IsTrue(deserialized is LinkedList <int>);
234
235                         LinkedList <int> dlist = deserialized as LinkedList <int>;
236
237                         int [] values = { 2, 3, 4 };
238                         int i = 0;
239                         foreach (int value in dlist)
240                         {
241                                 Assert.AreEqual (values [i], value);
242                                 i++;
243                         }
244                         Assert.AreEqual(3, i);
245                 }
246
247                 /* FIXME: disabled pending fix for #75299
248                 [Test]
249                 */
250                 public void EnumeratorSerializationTest ()
251                 {
252                         BinaryFormatter formatter = new BinaryFormatter ();
253                         MemoryStream stream = new MemoryStream ();
254                         LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
255                         formatter.Serialize (stream, e);
256
257                         stream.Position = 0;
258                         object deserialized = formatter.Deserialize(stream);
259
260                         Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
261
262                         LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
263
264                         int [] values = { 2, 3, 4 };
265                         int i = 0;
266                         while (d.MoveNext ())
267                         {
268                                 Assert.AreEqual (values [i], d.Current);
269                                 i++;
270                         }
271                         Assert.AreEqual(3, i);
272                 }
273
274                 public void EnumeratorAfterEnd ()
275                 {
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");
282                 }
283                 
284                 [Test] //bug 481621
285                 public void PlayWithNullValues ()
286                 {
287                         LinkedList <string> li = new LinkedList <string> ();
288                         li.AddLast ((string)null);
289                         li.AddLast ("abcd");
290                         li.AddLast ((string)null);
291                         li.AddLast ("efgh");
292                         Assert.AreEqual (4, li.Count);
293                         Assert.AreEqual ("efgh", li.Last.Value);
294                         Assert.IsNull (li.First.Value);
295
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);
300
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);
305
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);
310                 }
311
312                 [Test]
313                 public void EqualityComparer ()
314                 {
315                         var list = new LinkedList<EquatableValue> ();
316                         var mv  = new EquatableValue ("first");
317                         list.AddFirst (mv);
318
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");
323                 }
324
325                 [Test]
326                 public void RemoveFromEmptyList ()
327                 {
328                         var linkedList = new LinkedList<string> ();
329                         try {
330                                 linkedList.RemoveFirst ();
331                                 Assert.Fail ("#1");
332                         } catch (InvalidOperationException) {
333                         }
334
335                         try {
336                                 linkedList.RemoveLast ();
337                                 Assert.Fail ("#1");
338                         } catch (InvalidOperationException) {
339                         }
340                 }
341         }
342 }
343
344 #endif