2005-09-04 David Waite <mass@akuma.org>
[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                 LinkedList <int> intlist;
15                 LinkedList <string> strings;
16
17                 [SetUp]
18                 public void Setup ()
19                 {
20                         intlist = new LinkedList <int> ();
21                         
22                         // 2 3 4
23                         intlist.AddLast (3);
24                         intlist.AddLast (4);
25                         intlist.AddFirst (2);
26
27                         string [] tmpStrings = new string [] { "foo", "bar", "baz" };
28                         // FIXME workaround for 74953
29                         
30                         List <string> workaround = new List <string> ();
31                         foreach (string s in tmpStrings)
32                                 workaround.Add (s);
33
34                         // strings = new LinkedList <string> (tmpStrings);
35                         strings = new LinkedList <string> (workaround);
36                 }
37
38                 [Test]
39                 public void AddedTest ()
40                 {
41                         int i = 2;
42                         foreach (int current in intlist)
43                         {
44                                 Assert.AreEqual (i, current);
45                                 i++;
46                         }
47                         Assert.AreEqual (5, i);
48                 }
49
50                 [Test]
51                 public void CreatedTest ()
52                 {
53                         string [] values = new string [] { "foo", "bar", "baz" };
54                         int i = 0;
55                         foreach (string current in strings)
56                         {
57                                 Assert.AreEqual (values [i], current);
58                                 i++;
59                         }
60                         Assert.AreEqual (3, i);
61                 }
62
63                 [Test]
64                 public void NonCircularNodeTest ()
65                 {
66                         LinkedListNode <int> node = intlist.First;
67                         Assert.AreEqual (2, node.Value);
68                         LinkedListNode <int> previous = node.Previous;
69                         Assert.IsNull (previous);
70
71                         node = node.Next;
72                         Assert.IsNotNull (node);
73                         Assert.AreEqual (3, node.Value);
74
75                         node = node.Next;
76                         Assert.IsNotNull (node);
77                         Assert.AreEqual (4, node.Value);
78
79                         node = node.Next;
80                         Assert.IsNull (node);
81                 }
82
83                 [Test]
84                 public void ClearTest ()
85                 {
86                         intlist.Clear ();
87                         Assert.AreEqual (0, intlist.Count);
88                 }
89
90                 [Test]
91                 public void ContainsTest ()
92                 {
93                         Assert.IsTrue (intlist.Contains (3));
94                         Assert.IsFalse (intlist.Contains (5));
95                 }
96
97                 [Test]
98                 public void AddBeforeAndAfterTest ()
99                 {
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));
105
106                         Assert.AreEqual (6, sixNode.Value);
107                         Assert.AreEqual (7, sevenNode.Value);
108
109                         // 2 7 8 3 6 5 4
110                         int [] values = new int [] { 2, 7, 8, 3, 6, 5, 4 };
111                         int i = 0;
112                         foreach (int current in intlist)
113                         {
114                                 Assert.AreEqual (values [i], current);
115                                 i++;
116                         }
117                         for (LinkedListNode <int> current = intlist.First; current != null; current = current.Next )
118                                 Assert.AreSame (intlist, current.List);
119                 }
120
121                 [Test]
122                 public void CopyToTest ()
123                 {
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]);
129                 }
130
131                 [Test]
132                 public void FindTest ()
133                 {
134                         intlist.AddFirst (4);
135
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);
141                 }
142
143                 [Test]
144                 public void RemoveTest ()
145                 {
146                         Assert.IsTrue (intlist.Remove (3));
147                         Assert.AreEqual (2, intlist.Count);
148
149                         int [] values = { 2, 4 };
150                         int i = 0;
151                         foreach (int current in intlist)
152                         {
153                                 Assert.AreEqual (values [i], current);
154                                 i++;
155                         }
156                         Assert.IsFalse (intlist.Remove (5));
157
158                         LinkedListNode <string> node = strings.Find ("baz");
159                         strings.Remove (node);
160
161                         Assert.IsNull (node.List);
162                         Assert.IsNull (node.Previous);
163                         Assert.IsNull (node.Next);
164
165                         string [] values2 = { "foo", "bar" };
166                         i = 0;
167                         foreach (string current in strings)
168                         {
169                                 Assert.AreEqual (values2 [i], current);
170                                 i++;
171                         }
172                 }
173
174                 [Test, ExpectedException (typeof (ArgumentNullException))]
175                 public void RemoveNullNodeTest ()
176                 {
177                         intlist.Remove (null);
178                 }
179
180                 [Test, ExpectedException (typeof (InvalidOperationException))]
181                 public void RemoveInvalidNodeTest ()
182                 {
183                         intlist.Remove (new LinkedListNode <int> (4));
184                 }
185
186                 [Test]
187                 public void RemoveFirstLastTest ()
188                 {
189                         strings.RemoveFirst ();
190                         strings.RemoveLast ();
191                         Assert.AreEqual (1, strings.Count);
192                         Assert.AreEqual ("bar", strings.First.Value);
193                 }
194
195                 [Test]
196                 public void ListSerializationTest ()
197                 {
198                         BinaryFormatter formatter = new BinaryFormatter();
199                         MemoryStream stream = new MemoryStream();
200                         formatter.Serialize(stream, intlist);
201
202                         stream.Position = 0;
203                         object deserialized = formatter.Deserialize(stream);
204
205                         Assert.IsTrue(deserialized is LinkedList <int>);
206
207                         LinkedList <int> dlist = deserialized as LinkedList <int>;
208
209                         int [] values = { 2, 3, 4 };
210                         int i = 0;
211                         foreach (int value in dlist)
212                         {
213                                 Assert.AreEqual (values [i], value);
214                                 i++;
215                         }
216                         Assert.AreEqual(3, i);
217                 }
218
219                 /* FIXME: disabled pending fix for #75299
220                 [Test]
221                 */
222                 public void EnumeratorSerializationTest ()
223                 {
224                         BinaryFormatter formatter = new BinaryFormatter ();
225                         MemoryStream stream = new MemoryStream ();
226                         LinkedList<int>.Enumerator e = intlist.GetEnumerator ();
227                         formatter.Serialize (stream, e);
228
229                         stream.Position = 0;
230                         object deserialized = formatter.Deserialize(stream);
231
232                         Assert.IsTrue(deserialized is LinkedList <int>.Enumerator);
233
234                         LinkedList <int>.Enumerator d = (LinkedList <int>.Enumerator) deserialized;
235
236                         int [] values = { 2, 3, 4 };
237                         int i = 0;
238                         while (d.MoveNext ())
239                         {
240                                 Assert.AreEqual (values [i], d.Current);
241                                 i++;
242                         }
243                         Assert.AreEqual(3, i);
244                 }
245         }
246 }