1 // ArraySegmentTest.cs - NUnit Test Cases for the System.ArraySegment class
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)
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2012 Xamarin, Inc (http://www.xamarin.com)
12 using NUnit.Framework;
14 using System.Collections.Generic;
16 namespace MonoTests.System
19 public class ArraySegmentTest
22 public void CtorTest1 ()
24 byte[] b_arr = new byte[4096];
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");
32 Assert.AreEqual (arr.Length, 4096, "#5");
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");
39 Assert.AreEqual (arr.Length, 4096, "#5");
43 public void CtorTest2 ()
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");
50 Array arr = seg.Array;
51 Assert.AreEqual (arr.Length, 4096, "#8");
55 public void CtorTest3 ()
57 EmptyArraySegTest (0);
58 EmptyArraySegTest (10);
61 private void EmptyArraySegTest (int len)
63 byte[] b_arr = new byte[len];
65 ArraySegment<byte> seg = new ArraySegment<byte> (b_arr, 0, b_arr.Length);
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);
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);
76 Assert.AreEqual (arr.Length, len, "#6 [array len {0}] ", len);
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);
82 Assert.AreEqual (arr.Length, len, "#9 [array len {0}] ", len);
86 [ExpectedException (typeof (ArgumentException))]
87 public void CtorErrorTest ()
89 byte[] arr = new byte[4096];
90 ArraySegment<byte> seg = new ArraySegment<byte> (arr, 1, arr.Length);
94 [ExpectedException (typeof (ArgumentException))]
95 public void CtorErrorTest2 ()
97 byte[] arr = new byte[4096];
98 ArraySegment<byte> seg = new ArraySegment<byte> (arr, 0, arr.Length + 2);
102 [ExpectedException (typeof (ArgumentOutOfRangeException))]
103 public void CtorErrorTest3 ()
105 byte[] arr = new byte[4096];
106 ArraySegment<byte> seg = new ArraySegment<byte> (arr, -1, arr.Length);
110 [ExpectedException (typeof (ArgumentOutOfRangeException))]
111 public void CtorErrorTest4 ()
113 byte[] arr = new byte[4096];
114 ArraySegment<byte> seg = new ArraySegment<byte> (arr, 2, -1);
118 [ExpectedException (typeof (ArgumentException))]
119 public void CtorErrorTest5 ()
121 byte[] arr = new byte[4096];
122 ArraySegment<byte> seg = new ArraySegment<byte> (arr, 0, arr.Length + 2);
126 [ExpectedException (typeof (ArgumentNullException))]
127 public void CtorNullTest1 ()
129 ArraySegment<byte> seg = new ArraySegment<byte> (null, 0, 1);
133 [ExpectedException (typeof (ArgumentNullException))]
134 public void CtorNullTest2 ()
136 ArraySegment<byte> seg = new ArraySegment<byte> (null);
140 public void TestArraySegmentEqual ()
142 string[] myArr_1 = { "The", "good" };
143 string[] myArr_2 = { "The", "good" };
145 ArraySegment<string> myArrSeg_1 = new ArraySegment<string> (myArr_1);
146 ArraySegment<string> myArrSeg_2 = new ArraySegment<string> (myArr_2);
148 // Should return true.
149 Assert.AreEqual (myArrSeg_1.Equals (myArrSeg_1), true);
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);
155 // Should return true.
156 Assert.AreEqual (myArrSeg_1 != myArrSeg_2, true);
161 public void IList_NotSupported ()
163 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
165 IList<long> s = new ArraySegment<long> (array, 2, 3);
170 } catch (NotSupportedException) {
176 } catch (NotSupportedException) {
182 } catch (NotSupportedException) {
188 } catch (NotSupportedException) {
194 } catch (NotSupportedException) {
199 public void IList_GetEnumerator ()
201 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
203 IList<long> s = new ArraySegment<long> (array, 2, 3);
207 foreach (var i in s) {
212 Assert.AreEqual (3, count, "#1");
213 Assert.AreEqual (12, total, "#2");
217 public void IList_IndexOf ()
219 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
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");
227 public void IList_Contains ()
229 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
231 IList<long> s = new ArraySegment<long> (array, 2, 3);
232 Assert.IsFalse (s.Contains (2), "#1");
233 Assert.IsTrue (s.Contains (4), "#2");
237 public void IList_CopyTo ()
239 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
241 IList<long> s = new ArraySegment<long> (array, 2, 3);
242 long[] target = new long[s.Count];
243 s.CopyTo (target, 0);
245 Assert.AreEqual (3, target[0], "#1");
246 Assert.AreEqual (4, target[1], "#2");
250 public void IList_Indexer ()
252 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 };
254 IList<long> s = new ArraySegment<long> (array, 2, 3);
255 Assert.AreEqual (3, s[0], "#1");
256 Assert.AreEqual (4, s[1], "#2");
258 // LAMESPEC: I have not idea why is this allowed on ReadOnly array
259 Assert.IsTrue (s.IsReadOnly, "#3");
261 Assert.AreEqual (-3, s[1], "#2a");