SqlBulkCopy Implementation
[mono.git] / mcs / class / corlib / Test / System / ArraySegmentTest.cs
1 // ArraySegmentTest.cs - NUnit Test Cases for the System.ArraySegment class
2 //
3 // Ankit Jain  <jankit@novell.com>
4 // Raja R Harinath  <rharinath@novell.com>
5 // Jensen Somers <jensen.somers@gmail.com>
6 // Marek Safar (marek.safar@gmail.com)
7 // 
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2012 Xamarin, Inc (http://www.xamarin.com)
10 // 
11
12 using NUnit.Framework;
13 using System;
14 using System.Collections.Generic;
15
16 namespace MonoTests.System
17 {
18         [TestFixture]
19         public class ArraySegmentTest
20         {
21                 [Test]
22                 public void CtorTest1 ()
23                 {
24                         byte[] b_arr = new byte[4096];
25                         Array arr;
26
27                         ArraySegment<byte> seg = new ArraySegment<byte> (b_arr, 0, b_arr.Length);
28                         Assert.AreEqual (seg.Count, b_arr.Length, "#1");
29                         Assert.AreEqual (seg.Offset, 0, "#2");
30
31                         arr = seg.Array;
32                         Assert.AreEqual (arr.Length, 4096, "#5");
33
34                         seg = new ArraySegment<byte> (b_arr, 100, b_arr.Length - 100);
35                         Assert.AreEqual (seg.Count, b_arr.Length - 100, "#3");
36                         Assert.AreEqual (seg.Offset, 100, "#4");
37
38                         arr = seg.Array;
39                         Assert.AreEqual (arr.Length, 4096, "#5");
40                 }
41
42                 [Test]
43                 public void CtorTest2 ()
44                 {
45                         byte[] b_arr = new byte[4096];
46                         ArraySegment<byte> seg = new ArraySegment<byte> (b_arr);
47                         Assert.AreEqual (seg.Count, b_arr.Length, "#6");
48                         Assert.AreEqual (seg.Offset, 0, "#7");
49
50                         Array arr = seg.Array;
51                         Assert.AreEqual (arr.Length, 4096, "#8");
52                 }
53
54                 [Test]
55                 public void CtorTest3 ()
56                 {
57                         EmptyArraySegTest (0);
58                         EmptyArraySegTest (10);
59                 }
60
61                 private void EmptyArraySegTest (int len)
62                 {
63                         byte[] b_arr = new byte[len];
64
65                         ArraySegment<byte> seg = new ArraySegment<byte> (b_arr, 0, b_arr.Length);
66
67                         Assert.AreEqual (seg.Count, b_arr.Length, "#1 [array len {0}] ", len);
68                         Assert.AreEqual (seg.Offset, 0, "#2 [array len {0}] ", len);
69                         Array arr = seg.Array;
70                         Assert.AreEqual (arr.Length, len, "#3 [array len {0}] ", len);
71
72                         seg = new ArraySegment<byte> (b_arr, b_arr.Length, 0);
73                         Assert.AreEqual (seg.Count, 0, "#4 [array len {0}] ", len);
74                         Assert.AreEqual (seg.Offset, b_arr.Length, "#5 [array len {0}] ", len);
75                         arr = seg.Array;
76                         Assert.AreEqual (arr.Length, len, "#6 [array len {0}] ", len);
77
78                         seg = new ArraySegment<byte> (b_arr);
79                         Assert.AreEqual (seg.Count, b_arr.Length, "#7 [array len {0}] ", len);
80                         Assert.AreEqual (seg.Offset, 0, "#8 [array len {0}] ", len);
81                         arr = seg.Array;
82                         Assert.AreEqual (arr.Length, len, "#9 [array len {0}] ", len);
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (ArgumentException))]
87                 public void CtorErrorTest ()
88                 {
89                         byte[] arr = new byte[4096];
90                         ArraySegment<byte> seg = new ArraySegment<byte> (arr, 1, arr.Length);
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (ArgumentException))]
95                 public void CtorErrorTest2 ()
96                 {
97                         byte[] arr = new byte[4096];
98                         ArraySegment<byte> seg = new ArraySegment<byte> (arr, 0, arr.Length + 2);
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
103                 public void CtorErrorTest3 ()
104                 {
105                         byte[] arr = new byte[4096];
106                         ArraySegment<byte> seg = new ArraySegment<byte> (arr, -1, arr.Length);
107                 }
108
109                 [Test]
110                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
111                 public void CtorErrorTest4 ()
112                 {
113                         byte[] arr = new byte[4096];
114                         ArraySegment<byte> seg = new ArraySegment<byte> (arr, 2, -1);
115                 }
116
117                 [Test]
118                 [ExpectedException (typeof (ArgumentException))]
119                 public void CtorErrorTest5 ()
120                 {
121                         byte[] arr = new byte[4096];
122                         ArraySegment<byte> seg = new ArraySegment<byte> (arr, 0, arr.Length + 2);
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (ArgumentNullException))]
127                 public void CtorNullTest1 ()
128                 {
129                         ArraySegment<byte> seg = new ArraySegment<byte> (null, 0, 1);
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (ArgumentNullException))]
134                 public void CtorNullTest2 ()
135                 {
136                         ArraySegment<byte> seg = new ArraySegment<byte> (null);
137                 }
138
139                 [Test]
140                 public void TestArraySegmentEqual ()
141                 {
142                         string[] myArr_1 = { "The", "good" };
143                         string[] myArr_2 = { "The", "good" };
144
145                         ArraySegment<string> myArrSeg_1 = new ArraySegment<string> (myArr_1);
146                         ArraySegment<string> myArrSeg_2 = new ArraySegment<string> (myArr_2);
147
148                         // Should return true.
149                         Assert.AreEqual (myArrSeg_1.Equals (myArrSeg_1), true);
150
151                         // Should return false. Allthough the strings are the same.
152                         Assert.AreEqual (myArrSeg_1.Equals (myArrSeg_2), false);
153                         Assert.AreEqual (myArrSeg_1 == myArrSeg_2, false);
154
155                         // Should return true.
156                         Assert.AreEqual (myArrSeg_1 != myArrSeg_2, true);
157                 }
158
159 #if NET_4_5
160                 [Test]
161                 public void IList_NotSupported ()
162                 {
163                         var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
164
165                         IList<long> s = new ArraySegment<long> (array, 2, 3);
166
167                         try {
168                                 s.Add (1);
169                                 Assert.Fail ("#1");
170                         } catch (NotSupportedException) {
171                         }
172
173                         try {
174                                 s.Clear ();
175                                 Assert.Fail ("#2");
176                         } catch (NotSupportedException) {
177                         }
178
179                         try {
180                                 s.Remove (3);
181                                 Assert.Fail ("#3");
182                         } catch (NotSupportedException) {
183                         }
184
185                         try {
186                                 s.RemoveAt (3);
187                                 Assert.Fail ("#4");
188                         } catch (NotSupportedException) {
189                         }
190
191                         try {
192                                 s.Insert (2, 3);
193                                 Assert.Fail ("#5");
194                         } catch (NotSupportedException) {
195                         }
196                 }
197
198                 [Test]
199                 public void IList_GetEnumerator ()
200                 {
201                         var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
202
203                         IList<long> s = new ArraySegment<long> (array, 2, 3);
204
205                         long total = 0;
206                         int count = 0;
207                         foreach (var i in s) {
208                                 count++;
209                                 total += i;
210                         }
211
212                         Assert.AreEqual (3, count, "#1");
213                         Assert.AreEqual (12, total, "#2");
214                 }
215
216                 [Test]
217                 public void IList_IndexOf ()
218                 {
219                         var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
220
221                         IList<long> s = new ArraySegment<long> (array, 2, 3);
222                         Assert.AreEqual (-1, s.IndexOf (2), "#1");
223                         Assert.AreEqual (1, s.IndexOf (4), "#2");
224                 }
225
226                 [Test]
227                 public void IList_Contains ()
228                 {
229                         var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
230
231                         IList<long> s = new ArraySegment<long> (array, 2, 3);
232                         Assert.IsFalse (s.Contains (2), "#1");
233                         Assert.IsTrue (s.Contains (4), "#2");
234                 }
235
236                 [Test]
237                 public void IList_CopyTo ()
238                 {
239                         var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
240
241                         IList<long> s = new ArraySegment<long> (array, 2, 3);
242                         long[] target = new long[s.Count];
243                         s.CopyTo (target, 0);
244
245                         Assert.AreEqual (3, target[0], "#1");
246                         Assert.AreEqual (4, target[1], "#2");
247                 }
248
249                 [Test]
250                 public void IList_Indexer ()
251                 {
252                         var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
253
254                         IList<long> s = new ArraySegment<long> (array, 2, 3);
255                         Assert.AreEqual (3, s[0], "#1");
256                         Assert.AreEqual (4, s[1], "#2");
257
258                         // LAMESPEC: I have not idea why is this allowed on ReadOnly array
259                         Assert.IsTrue (s.IsReadOnly, "#3");
260                         s[1] = -3;
261                         Assert.AreEqual (-3, s[1], "#2a");
262                 }
263 #endif
264         }
265 }