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] CtorTest1()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] CtorTest2()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] CtorTest3()55 public void CtorTest3 () 56 { 57 EmptyArraySegTest (0); 58 EmptyArraySegTest (10); 59 } 60 EmptyArraySegTest(int len)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))] CtorErrorTest()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))] CtorErrorTest2()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))] CtorErrorTest3()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))] CtorErrorTest4()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))] CtorErrorTest5()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))] CtorNullTest1()127 public void CtorNullTest1 () 128 { 129 ArraySegment<byte> seg = new ArraySegment<byte> (null, 0, 1); 130 } 131 132 [Test] 133 [ExpectedException (typeof (ArgumentNullException))] CtorNullTest2()134 public void CtorNullTest2 () 135 { 136 ArraySegment<byte> seg = new ArraySegment<byte> (null); 137 } 138 139 [Test] TestArraySegmentEqual()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 [Test] IList_NotSupported()160 public void IList_NotSupported () 161 { 162 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 }; 163 164 IList<long> s = new ArraySegment<long> (array, 2, 3); 165 166 try { 167 s.Add (1); 168 Assert.Fail ("#1"); 169 } catch (NotSupportedException) { 170 } 171 172 try { 173 s.Clear (); 174 Assert.Fail ("#2"); 175 } catch (NotSupportedException) { 176 } 177 178 try { 179 s.Remove (3); 180 Assert.Fail ("#3"); 181 } catch (NotSupportedException) { 182 } 183 184 try { 185 s.RemoveAt (3); 186 Assert.Fail ("#4"); 187 } catch (NotSupportedException) { 188 } 189 190 try { 191 s.Insert (2, 3); 192 Assert.Fail ("#5"); 193 } catch (NotSupportedException) { 194 } 195 } 196 197 [Test] IList_GetEnumerator()198 public void IList_GetEnumerator () 199 { 200 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 }; 201 202 IList<long> s = new ArraySegment<long> (array, 2, 3); 203 204 long total = 0; 205 int count = 0; 206 foreach (var i in s) { 207 count++; 208 total += i; 209 } 210 211 Assert.AreEqual (3, count, "#1"); 212 Assert.AreEqual (12, total, "#2"); 213 } 214 215 [Test] IList_IndexOf()216 public void IList_IndexOf () 217 { 218 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 }; 219 220 IList<long> s = new ArraySegment<long> (array, 2, 3); 221 Assert.AreEqual (-1, s.IndexOf (2), "#1"); 222 Assert.AreEqual (1, s.IndexOf (4), "#2"); 223 } 224 225 [Test] IList_Contains()226 public void IList_Contains () 227 { 228 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 }; 229 230 IList<long> s = new ArraySegment<long> (array, 2, 3); 231 Assert.IsFalse (s.Contains (2), "#1"); 232 Assert.IsTrue (s.Contains (4), "#2"); 233 } 234 235 [Test] IList_CopyTo()236 public void IList_CopyTo () 237 { 238 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 }; 239 240 IList<long> s = new ArraySegment<long> (array, 2, 3); 241 long[] target = new long[s.Count]; 242 s.CopyTo (target, 0); 243 244 Assert.AreEqual (3, target[0], "#1"); 245 Assert.AreEqual (4, target[1], "#2"); 246 } 247 248 [Test] IList_Indexer()249 public void IList_Indexer () 250 { 251 var array = new long[] { 1, 2, 3, 4, 5, 6, -10 }; 252 253 IList<long> s = new ArraySegment<long> (array, 2, 3); 254 Assert.AreEqual (3, s[0], "#1"); 255 Assert.AreEqual (4, s[1], "#2"); 256 257 // LAMESPEC: I have not idea why is this allowed on ReadOnly array 258 Assert.IsTrue (s.IsReadOnly, "#3"); 259 s[1] = -3; 260 Assert.AreEqual (-3, s[1], "#2a"); 261 } 262 263 [Test] 264 [ExpectedException (typeof (ArgumentOutOfRangeException))] IList_IndexerErrorTest1()265 public void IList_IndexerErrorTest1 () 266 { 267 byte[] arr = new byte[4]; 268 IList<byte> seg = new ArraySegment<byte> (arr, 1, 2); 269 seg[-1] = 3; 270 } 271 272 [Test] 273 [ExpectedException (typeof (ArgumentOutOfRangeException))] IList_IndexerErrorTest2()274 public void IList_IndexerErrorTest2 () 275 { 276 byte[] arr = new byte[4]; 277 IList<byte> seg = new ArraySegment<byte> (arr); 278 seg[4] = 3; 279 } 280 } 281 } 282