2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / Test / System.Runtime.Serialization / ArraySerializationTest.cs
1 // ArraySerializationTest.cs
2 //
3 // Author:
4 //   Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2005 Lluis Sanchez Gual
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31
32 using System;
33 using System.IO;
34 using System.Runtime.Serialization;
35 using System.Runtime.Serialization.Formatters.Binary;
36 using System.Reflection;
37 using System.Collections;
38 using NUnit.Framework;
39
40 namespace MonoTests.System.Runtime.Serialization
41 {
42         [TestFixture]
43         public class ArraySerializationTest
44         {
45                 [Test]
46                 public void TestBoolArray ()
47                 {
48                         bool[] array = new bool[2000];
49                         for (int n=0; n<2000; n++)
50                                 array [n] = (n % 3) == 0;
51                         CheckArray (array);
52                 }
53                 
54                 [Test]
55                 public void TestByteArray ()
56                 {
57                         byte[] array = new byte[10000];
58                         for (int n=0; n<10000; n++)
59                                 array [n] = (byte) (n % 253);
60                         CheckArray (array);
61                 }
62                 
63                 [Test]
64                 public void TestCharArray ()
65                 {
66                         char[] array = new char[2000];
67                         for (int n=0; n<2000; n++)
68                                 array [n] = (char) n;
69                         CheckArray (array);
70                 }
71                 
72                 [Test]
73                 public void TestDateTimeArray ()
74                 {
75                         DateTime[] array = new DateTime[10000];
76                         for (int n=0; n<10000; n++)
77                                 array [n] = new DateTime (n);
78                         CheckArray (array);
79                 }
80                 
81                 [Test]
82                 public void TestDecimalArray ()
83                 {
84                         decimal[] array = new decimal[2000];
85                         for (int n=0; n<2000; n++)
86                                 array [n] = decimal.MaxValue / (decimal) (n+1);
87                         CheckArray (array);
88                 }
89                 
90                 [Test]
91                 public void TestDoubleArray ()
92                 {
93                         double[] array = new double[10000];
94                         for (int n=0; n<10000; n++)
95                                 array [n] = (double) n;
96                         CheckArray (array);
97                 }
98                 
99                 [Test]
100                 public void TestShortArray ()
101                 {
102                         short[] array = new short[10000];
103                         for (int n=0; n<10000; n++)
104                                 array [n] = (short) n;
105                         CheckArray (array);
106                 }
107                 
108                 [Test]
109                 public void TestIntArray ()
110                 {
111                         int[] array = new int[10000];
112                         for (int n=0; n<10000; n++)
113                                 array [n] = n;
114                         CheckArray (array);
115                 }
116                 
117                 [Test]
118                 public void TestLongArray ()
119                 {
120                         long[] array = new long[10000];
121                         for (int n=0; n<10000; n++)
122                                 array [n] = n;
123                         CheckArray (array);
124                 }
125                 
126                 [Test]
127                 public void TestSByteArray ()
128                 {
129                         sbyte[] array = new sbyte[10000];
130                         for (int n=0; n<10000; n++)
131                                 array [n] = (sbyte) (n % 121);
132                         CheckArray (array);
133                 }
134                 
135                 [Test]
136                 public void TestFloatArray ()
137                 {
138                         float[] array = new float[10000];
139                         for (int n=0; n<10000; n++)
140                                 array [n] = (float) n;
141                         CheckArray (array);
142                 }
143                 
144                 [Test]
145                 public void TestUShortArray ()
146                 {
147                         ushort[] array = new ushort[10000];
148                         for (int n=0; n<10000; n++)
149                                 array [n] = (ushort) n;
150                         CheckArray (array);
151                 }
152                 
153                 [Test]
154                 public void TestUIntArray ()
155                 {
156                         uint[] array = new uint[10000];
157                         for (int n=0; n<10000; n++)
158                                 array [n] = (uint) n;
159                         CheckArray (array);
160                 }
161                 
162                 [Test]
163                 public void TestULongArray ()
164                 {
165                         ulong[] array = new ulong[10000];
166                         for (int n=0; n<10000; n++)
167                                 array [n] = (ulong) n;
168                         CheckArray (array);
169                 }
170                 
171                 [Test]
172                 public void TestStringArray ()
173                 {
174                         string[] array = new string[10000];
175                         for (int n=0; n<10000; n++)
176                                 array [n] = n.ToString ();
177                         CheckArray (array);
178                 }
179                 
180                 void CheckArray (Array src)
181                 {
182                         MemoryStream ms = new MemoryStream ();
183                         BinaryFormatter bf = new BinaryFormatter ();
184                         bf.Serialize (ms, src);
185                         ms.Position = 0;
186                         Array des = (Array) bf.Deserialize (ms);
187                         CompareArrays (src.GetType().ToString(), src, des);
188                 }
189                 
190                 void CompareArrays (string txt, Array a1, Array a2)
191                 {
192                         Assertion.AssertEquals (txt + " length", a1.Length, a2.Length);
193                         for (int n=0; n<a1.Length; n++)
194                                 Assertion.AssertEquals (txt + " [" + n + "]", a1.GetValue (n), a2.GetValue (n));
195                 }
196         }
197 }