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