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