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