ArraySerializationTest.cs 4.6 KB

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