[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System / Test / System.Collections.Generic / StackTest.cs
1 //
2 // StackTest.cs
3 //
4 // Author:
5 //  Ben Maurer (bmaurer@ximian.com)
6 //
7
8 using System;
9 using System.Collections;
10 using System.Collections.Generic;
11 using System.IO;
12 using System.Runtime.Serialization.Formatters.Binary;
13
14 using NUnit.Framework;
15
16 namespace MonoTests.System.Collections.Generic
17 {
18         [TestFixture]
19         public class StackTest
20         {
21                 [Test]
22                 public void TestCtor ()
23                 {
24                         Stack <int> a = new Stack <int> ();
25                         Stack <int> b = new Stack <int> (1);
26                         Stack <object> c = new Stack <object> ();
27                         Stack <object> d = new Stack <object> (1);
28                         Stack <object> e = new Stack <object> (0);
29                 }
30                 
31                 [Test]
32                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
33                 public void TestCtorEx ()
34                 {
35                         new Stack <int> (-1);
36                 }
37                 
38                 [Test]
39                 public void TestCtorEnum ()
40                 {
41                         List <int> l = new List <int> ();
42                         l.Add (1);
43                         l.Add (2);
44                         l.Add (3);
45                         
46                         Stack <int> s = new Stack <int> (l);
47                         
48                         // Things get pop'd in reverse
49                         AssertPop (s, 3);
50                         AssertPop (s, 2);
51                         AssertPop (s, 1);
52                 }
53                 
54                 [Test]
55                 [ExpectedException (typeof (ArgumentNullException))]
56                 public void TestCtorEnumNull ()
57                 {
58                         new Stack <int> (null);
59                 }
60                 
61                 [Test]
62                 public void TestClear()
63                 {
64                         Stack <int> s = new Stack <int> ();
65                         s.Clear ();
66                         
67                         Assert.AreEqual (0, s.Count, "#1");
68                         
69                         s.Push (1);
70                         s.Push (2);
71                         
72                         Assert.AreEqual (2, s.Count, "#2");
73                         
74                         s.Clear ();
75                         
76                         Assert.AreEqual (0, s.Count, "#3");
77                 }
78                 
79                 [Test]
80                 public void TestContains ()
81                 {
82                         Stack <int> s = new Stack <int> ();
83                         
84                         Assert.IsFalse (s.Contains (1), "#1");
85                         
86                         s.Push (1);
87                         
88                         Assert.IsTrue  (s.Contains (1), "#2");
89                         Assert.IsFalse (s.Contains (0), "#3");
90                 }
91
92                 [Test]
93                 public void TestCopyTo ()
94                 {
95                         int [] x = new int [3];
96                         Stack <int> z = new Stack <int> ();
97                         z.Push (1);
98                         z.Push (2);
99                         x [0] = 10;
100                         z.CopyTo (x, 1);
101                         
102                         Assert.AreEqual  (10, x [0], "#1");
103                         Assert.AreEqual (2, x [1], "#2");
104                         Assert.AreEqual (1, x [2], "#3");
105                         
106                         z = new Stack <int> ();
107                         x = new int [z.Count];
108                         z.CopyTo (x, 0);                        
109                         
110                         ICollection c = new Stack <int> ();
111                         x = new int [c.Count];
112                         c.CopyTo (x, 0);
113                 }
114
115                 [Test]
116                 public void TestPeek ()
117                 {
118                         Stack <int> s = new Stack <int> ();
119                         s.Push (1);
120                         
121                         Assert.AreEqual (1, s.Peek (), "#1");
122                         Assert.AreEqual (1, s.Count, "#2");
123
124                         IEnumerator enumerator = s.GetEnumerator();
125                         s.Peek();
126                         enumerator.Reset();
127                 }
128                 
129                 [Test]
130                 [ExpectedException (typeof (InvalidOperationException))]
131                 public void TestPeekEx ()
132                 {
133                         Stack <int> s = new Stack <int> ();
134                         s.Peek ();
135                 }
136                 
137                 [Test]
138                 [ExpectedException (typeof (InvalidOperationException))]
139                 public void TestPeekEx2 ()
140                 {
141                         Stack <int> s = new Stack <int> ();
142                         s.Push (1);
143                         s.Pop ();
144                         s.Peek ();
145                 }
146                 
147                 [Test]
148                 public void TestPop ()
149                 {
150                         Stack <int> s = new Stack <int> ();
151                         s.Push (1);
152                         
153                         Assert.AreEqual (1, s.Pop (), "#1");
154                         Assert.AreEqual (0, s.Count, "#2");
155                 }
156                 
157                 [Test]
158                 [ExpectedException (typeof (InvalidOperationException))]
159                 public void TestPopEx ()
160                 {
161                         Stack <int> s = new Stack <int> ();
162                         s.Pop ();
163                 }
164                 
165                 [Test]
166                 [ExpectedException (typeof (InvalidOperationException))]
167                 public void TestPopEx2 ()
168                 {
169                         Stack <int> s = new Stack <int> ();
170                         s.Push (1);
171                         s.Pop ();
172                         s.Pop ();
173                 }
174                 
175                 [Test]
176                 public void TestPush ()
177                 {
178                         Stack <int> s = new Stack <int> ();
179                         s.Push (1);
180                         Assert.AreEqual (1, s.Count, "#1");
181                         s.Push (2);
182                         Assert.AreEqual (2, s.Count, "#2");
183                         
184                         for (int i = 0; i < 100; i ++)
185                                 s.Push (i);
186                         
187                         Assert.AreEqual (102, s.Count, "#3");
188                 }
189                 
190                 [Test]
191                 public void TestToArray ()
192                 {
193                         Stack <int> s = new Stack <int> ();
194                         
195                         int [] x = s.ToArray ();
196                         
197                         Assert.AreEqual (0, x.Length, "#1");
198                         
199                         s.Push (1);
200                         x = s.ToArray ();
201                         Assert.AreEqual (1, x.Length, "#2");
202                         Assert.AreEqual (1, x [0], "#3");
203                 }
204                 
205                 [Test]
206                 public void TestEnumerator ()
207                 {
208                         Stack <int> s = new Stack <int> ();
209                         
210                         foreach (int x in s)
211                                 Assert.Fail ("#1:" + x);
212                         
213                         s.Push (1);
214                         
215                         int i = 0;
216                         
217                         foreach (int x in s) {
218                                 Assert.AreEqual (0, i, "#2");
219                                 Assert.AreEqual (1, x, "#3");
220                                 i ++;
221                         }
222                         
223                         i = 0;
224                         
225                         s.Push (2);
226                         s.Push (3);
227                         
228                         foreach (int x in s) {
229                                 Assert.AreEqual (3 - i, x, "#4");
230                                 Assert.IsTrue (i < 3, "#5");
231                                 i ++;
232                         }
233                 }
234
235                 [Test]
236                 public void DisposeEnumerator ()
237                 {
238                         var stack = new Stack<int> ();
239                         stack.Push (1);
240                         stack.Push (2);
241
242                         var enumerator = stack.GetEnumerator ();
243                         Assert.IsTrue (enumerator.MoveNext ());
244                         enumerator.Dispose ();
245                         Assert.IsFalse (enumerator.MoveNext ());
246                 }
247
248                 [Test]
249                 public void TrimExcessTest ()
250                 {
251                         Stack <int> s = new Stack <int> ();
252                         s.TrimExcess ();
253                         Assert.AreEqual (0, s.Count, "#1");
254
255                         s.Push (1);
256                         s.Push (3);
257                         Assert.AreEqual (3, s.Pop (), "#2");
258                         Assert.AreEqual (1, s.Peek (), "#3");
259
260                         s.TrimExcess ();
261                         Assert.AreEqual (1, s.Count, "#4");
262                         Assert.AreEqual (1, s.Peek (), "#5");
263
264                         s.Push (2);
265                         Assert.AreEqual (2, s.Pop (), "#6");
266                         Assert.AreEqual (1, s.Pop (), "#7");
267
268                         s.TrimExcess ();
269                         Assert.AreEqual (0, s.Count, "#8");
270                 }
271
272                 [Test]
273                 [Category ("NotWorking")] // bug #80649
274                 public void SerializeTest ()
275                 {
276                         Stack <int> s = new Stack <int> ();
277                         s.Push (1);
278                         s.Push (3);
279                         s.Push (2);
280                         s.Pop ();
281
282                         BinaryFormatter bf = new BinaryFormatter ();
283                         MemoryStream ms = new MemoryStream ();
284                         bf.Serialize (ms, s);
285
286                         byte [] buffer = new byte [ms.Length];
287                         ms.Position = 0;
288                         ms.Read (buffer, 0, buffer.Length);
289
290                         Assert.AreEqual (_serializedStack, buffer);
291                 }
292
293                 [Test]
294                 public void DeserializeTest ()
295                 {
296                         MemoryStream ms = new MemoryStream ();
297                         ms.Write (_serializedStack, 0, _serializedStack.Length);
298                         ms.Position = 0;
299
300                         BinaryFormatter bf = new BinaryFormatter ();
301                         Stack<int> s = (Stack<int>) bf.Deserialize (ms);
302                         Assert.AreEqual (2, s.Count, "#1");
303                         Assert.AreEqual (3, s.Pop (), "#2");
304                         Assert.AreEqual (1, s.Pop (), "#3");
305                 }
306
307                 void AssertPop <T> (Stack <T> s, T t)
308                 {
309                         Assert.AreEqual  (t, s.Pop ());
310                 }
311
312                 static byte [] _serializedStack = new byte [] {
313                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
314                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x02, 0x00, 0x00, 0x00,
315                         0x49, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2c, 0x20, 0x56, 0x65,
316                         0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30,
317                         0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65,
318                         0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50,
319                         0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b,
320                         0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36,
321                         0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x05, 0x01, 0x00,
322                         0x00, 0x00, 0x7f, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43,
323                         0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
324                         0x47, 0x65, 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x53, 0x74, 0x61,
325                         0x63, 0x6b, 0x60, 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65,
326                         0x6d, 0x2e, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73,
327                         0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72,
328                         0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e,
329                         0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d,
330                         0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75,
331                         0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65,
332                         0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31,
333                         0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00,
334                         0x00, 0x00, 0x06, 0x5f, 0x61, 0x72, 0x72, 0x61, 0x79, 0x05, 0x5f,
335                         0x73, 0x69, 0x7a, 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69,
336                         0x6f, 0x6e, 0x07, 0x00, 0x00, 0x08, 0x08, 0x08, 0x02, 0x00, 0x00,
337                         0x00, 0x09, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04,
338                         0x00, 0x00, 0x00, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
339                         0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
340                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b };
341         }
342 }