PageRenderTime 97ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Quartz.Tests/Runtime/RubyArrayTests.cs

http://quartz.codeplex.com
C# | 359 lines | 274 code | 68 blank | 17 comment | 94 complexity | 2d8c4e1e94e56e24b258acfe0c9120ed MD5 | raw file
Possible License(s): CPL-1.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using IronRuby.Builtins;
  17. using System.Text;
  18. using System.Diagnostics;
  19. using System.Collections.Generic;
  20. using IronRuby.Runtime;
  21. using Microsoft.Scripting;
  22. using System.Runtime.CompilerServices;
  23. using System.Collections;
  24. using Microsoft.Scripting.Utils;
  25. namespace IronRuby.Tests {
  26. public partial class Tests {
  27. private IEnumerable<int> Enumerable(int count) {
  28. for (int i = 0; i < count; i++) {
  29. yield return i + 1;
  30. }
  31. }
  32. private void AssertValueEquals(RubyArray/*!*/ array, params object[]/*!*/ expected) {
  33. Assert(ArrayUtils.ValueEquals(array.ToArray(), expected));
  34. array.RequireNullEmptySlots();
  35. }
  36. [Options(NoRuntime = true)]
  37. public void RubyArray_Ctors() {
  38. const int N = 10;
  39. var dict = new Dictionary<object, object>();
  40. for (int i = 0; i < N; i++) {
  41. dict.Add(i, i);
  42. }
  43. RubyArray a;
  44. a = RubyArray.Create(1);
  45. Assert(a.Count == 1 && (int)a[0] == 1);
  46. a = new RubyArray();
  47. Assert(a.Count == 0 && a.Capacity == 0);
  48. a = new RubyArray(1);
  49. Assert(a.Count == 0 && a.Capacity == Utils.MinListSize);
  50. a = new RubyArray(100);
  51. Assert(a.Count == 0 && a.Capacity == 100);
  52. a = new RubyArray((ICollection)dict.Values);
  53. Assert(a.Count == N);
  54. a = new RubyArray((IEnumerable)dict.Values);
  55. Assert(a.Count == N);
  56. a = new RubyArray((IList)new object[] { 1, 2, 3 });
  57. Assert(a.Count == 3);
  58. Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);
  59. a = new RubyArray((IList)new object[] { 1, 2, 3 }, 1, 1);
  60. Assert(a.Count == 1);
  61. Assert((int)a[0] == 2);
  62. a = new RubyArray((IList)new object[] { 1, 2, 3 }, 1, 0);
  63. Assert(a.Count == 0);
  64. a = new RubyArray((ICollection)new object[] { 1, 2, 3 });
  65. Assert(a.Count == 3);
  66. Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);
  67. a = new RubyArray((IEnumerable)new object[] { 1, 2, 3 });
  68. Assert(a.Count == 3);
  69. Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);
  70. a = new RubyArray(Enumerable(3));
  71. Assert(a.Count == 3);
  72. Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);
  73. a = new RubyArray(a);
  74. Assert(a.Count == 3);
  75. Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);
  76. a = new RubyArray(a, 0, 2);
  77. Assert(a.Count == 2);
  78. Assert((int)a[0] == 1 && (int)a[1] == 2);
  79. }
  80. [Options(NoRuntime = true)]
  81. public void RubyArray_Basic() {
  82. RubyArray a;
  83. a = new RubyArray(new object[] { 1, 2, 8 });
  84. Assert(!a.IsTainted && !a.IsFrozen && !a.IsReadOnly && !((IList)a).IsFixedSize && !((ICollection)a).IsSynchronized);
  85. Assert(ReferenceEquals(((ICollection)a).SyncRoot, a));
  86. a.IsTainted = true;
  87. Assert(a.IsTainted);
  88. a.IsTainted = false;
  89. Assert(!a.IsTainted);
  90. a[1] = 5;
  91. Assert((int)a[0] == 1 && (int)a[1] == 5 && (int)a[2] == 8);
  92. ((IRubyObjectState)a).Freeze();
  93. Assert(a.IsFrozen);
  94. AssertExceptionThrown<InvalidOperationException>(() => a.IsTainted = true);
  95. Assert(!a.IsTainted);
  96. AssertExceptionThrown<InvalidOperationException>(() => a[1] = 1);
  97. Assert((int)a[0] == 1 && (int)a[1] == 5 && (int)a[2] == 8);
  98. var values = new object[] { 1, 5, 8 };
  99. int i = 0;
  100. foreach (object item in a) {
  101. Assert(item.Equals(values[i]));
  102. i++;
  103. }
  104. Assert(i == values.Length);
  105. Assert(((IEnumerable)a).GetEnumerator() != null);
  106. AssertValueEquals(a, values);
  107. object[] array = new object[4];
  108. ((ICollection)a).CopyTo(array, 1);
  109. Assert(ArrayUtils.ValueEquals(ArrayUtils.ShiftLeft(array, 1), values));
  110. Assert(a.Contains(5));
  111. Assert(a.IndexOf(5) == 1);
  112. Assert(a.IndexOf(5, 2) == -1);
  113. Assert(a.IndexOf(5, 0, 1) == -1);
  114. Assert(a.FindIndex((x) => (int)x == 8) == 2);
  115. Assert(a.FindIndex(2, (x) => (int)x == 8) == 2);
  116. Assert(a.FindIndex(0, 2, (x) => (int)x == 8) == -1);
  117. }
  118. [Options(NoRuntime = true)]
  119. public void RubyArray_Add() {
  120. RubyArray a;
  121. a = new RubyArray();
  122. for (int i = 0; i < Utils.MinListSize; i++) {
  123. a.Add(i);
  124. Assert((int)a[i] == i && a.Count == i + 1 && a.Capacity == Utils.MinListSize);
  125. }
  126. Assert(((IList)a).Add(Utils.MinListSize) == Utils.MinListSize);
  127. Assert(a.Count == Utils.MinListSize + 1);
  128. for (int i = 0; i < a.Count; i++) {
  129. Assert((int)a[i] == i);
  130. }
  131. a = new RubyArray(new[] { 1,2,3 });
  132. a.AddCapacity(0);
  133. Assert(a.Count == 3);
  134. a.AddCapacity(100);
  135. Assert(a.Count == 3 && a.Capacity >= 103);
  136. a = new RubyArray(new[] { 1, 2, 3 });
  137. a.AddMultiple(0, 4);
  138. AssertValueEquals(a, 1, 2, 3);
  139. a.AddMultiple(5, 4);
  140. AssertValueEquals(a, 1, 2, 3, 4, 4, 4, 4, 4);
  141. a = new RubyArray(new[] { 1, 2, 3 });
  142. a.AddRange(new object[0]);
  143. AssertValueEquals(a, 1, 2, 3);
  144. a.AddRange(new[] { 4 });
  145. AssertValueEquals(a, 1, 2, 3, 4);
  146. a.AddRange(new[] { 5, 6, 7, 8, 9, 10 });
  147. AssertValueEquals(a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  148. a.AddRange(new[] { 11 });
  149. AssertValueEquals(a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  150. a = new RubyArray();
  151. a.AddRange((IEnumerable)new RubyArray(new[] { 1, 2, 3 }));
  152. a.AddRange((IList)new RubyArray(new[] { 1, 2, 3 }), 1, 2);
  153. AssertValueEquals(a, 1, 2, 3, 2, 3);
  154. a.Freeze();
  155. AssertExceptionThrown<InvalidOperationException>(() => a.Add(1));
  156. AssertExceptionThrown<InvalidOperationException>(() => a.AddCapacity(10));
  157. AssertExceptionThrown<InvalidOperationException>(() => a.AddMultiple(10, 10));
  158. AssertExceptionThrown<InvalidOperationException>(() => a.AddRange(new object[0]));
  159. AssertExceptionThrown<InvalidOperationException>(() => a.AddRange(Enumerable(0)));
  160. }
  161. [Options(NoRuntime = true)]
  162. public void RubyArray_Remove() {
  163. RubyArray a;
  164. a = new RubyArray(new[] { 1, 2, 3, 4, 5 });
  165. a.RemoveAt(1);
  166. AssertValueEquals(a, 1, 3, 4, 5);
  167. a.RemoveAt(1);
  168. AssertValueEquals(a, 1, 4, 5);
  169. a.RemoveAt(1);
  170. AssertValueEquals(a, 1, 5);
  171. a.RemoveAt(1);
  172. AssertValueEquals(a, 1);
  173. a.AddRange(new[] { 2, 3 });
  174. AssertValueEquals(a, 1, 2, 3);
  175. a.RemoveAt(0);
  176. AssertValueEquals(a, 2, 3);
  177. a.RemoveAt(0);
  178. AssertValueEquals(a, 3);
  179. a.RemoveAt(0);
  180. AssertValueEquals(a);
  181. a.AddRange(new[] { 1, 2, 3 });
  182. AssertValueEquals(a, 1, 2, 3);
  183. a = new RubyArray();
  184. a.AddMultiple(100, 1);
  185. a[0] = 0;
  186. a[99] = 99;
  187. a.RemoveRange(1, 98);
  188. AssertValueEquals(a, 0, 99);
  189. Assert(a.Capacity < 100, "array should shrink");
  190. ((IList)a).Remove(0);
  191. AssertValueEquals(a, 99);
  192. a.Clear();
  193. Assert(a.Count == 0);
  194. a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
  195. a.RemoveRange(0, 4);
  196. AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11);
  197. a.AddMultiple(3, 1);
  198. AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1);
  199. a.RemoveRange(0, 6);
  200. AssertValueEquals(a, 10, 11, 1, 1, 1);
  201. a.AddMultiple(2, 2);
  202. AssertValueEquals(a, 10, 11, 1, 1, 1, 2, 2);
  203. a = new RubyArray();
  204. a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
  205. a.RemoveRange(0, 4);
  206. AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11);
  207. a.AddMultiple(3, null);
  208. AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11, null, null, null);
  209. var vector = new object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  210. a = new RubyArray(vector);
  211. for (int i = 0, c = a.Count; i < c; i++) {
  212. a.RemoveAt(0);
  213. vector = ArrayUtils.ShiftLeft(vector, 1);
  214. AssertValueEquals(a, vector);
  215. }
  216. a = new RubyArray(new[] { 1, 2, 3 });
  217. a.Freeze();
  218. Assert(!a.Remove(0));
  219. AssertExceptionThrown<InvalidOperationException>(() => a.Remove(1));
  220. AssertExceptionThrown<InvalidOperationException>(() => a.Clear());
  221. AssertExceptionThrown<InvalidOperationException>(() => a.RemoveAt(0));
  222. AssertExceptionThrown<InvalidOperationException>(() => a.RemoveRange(0, 1));
  223. }
  224. [Options(NoRuntime = true)]
  225. public void RubyArray_Insert() {
  226. RubyArray a;
  227. a = new RubyArray(new[] { 1, 2, 3, 4, 5 });
  228. a.InsertRange(0, new[] { 10, 20, 30, 40 });
  229. AssertValueEquals(a, 10, 20, 30, 40, 1, 2, 3, 4, 5);
  230. a.InsertRange(2, new[] { 8 });
  231. AssertValueEquals(a,10, 20, 8, 30, 40, 1, 2, 3, 4, 5);
  232. a.InsertRange(10, new[] { 9 });
  233. AssertValueEquals(a, 10, 20, 8, 30, 40, 1, 2, 3, 4, 5, 9);
  234. a.RemoveRange(1, 5);
  235. AssertValueEquals(a, 10, 2, 3, 4, 5, 9);
  236. a.InsertRange(1, new[] { 0 });
  237. AssertValueEquals(a, 10, 0, 2, 3, 4, 5, 9);
  238. a.InsertRange(6, new[] { 8 });
  239. AssertValueEquals(a, 10, 0, 2, 3, 4, 5, 8, 9);
  240. a.RemoveRange(6, 2);
  241. AssertValueEquals(a, 10, 0, 2, 3, 4, 5);
  242. a.InsertRange(3, new[] { 11, 12, 13, 14 });
  243. AssertValueEquals(a, 10, 0, 2, 11, 12, 13, 14, 3, 4, 5);
  244. a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
  245. // make enough space on both sides:
  246. a.RemoveRange(0, 2);
  247. AssertValueEquals(a, 2, 3, 4, 5, 6, 7, 8, 9);
  248. a.RemoveRange(6, 2);
  249. AssertValueEquals(a, 2, 3, 4, 5, 6, 7);
  250. // closer to start:
  251. a.InsertRange(1, new[] { 10 });
  252. AssertValueEquals(a, 2, 10, 3, 4, 5, 6, 7);
  253. // closer to end:
  254. a.InsertRange(5, new[] { 11 });
  255. AssertValueEquals(a, 2, 10, 3, 4, 5, 11, 6, 7);
  256. a = new RubyArray(new[] { 1, 2, 3 });
  257. a.Insert(0, 0);
  258. AssertValueEquals(a, 0, 1, 2, 3);
  259. a.InsertRange(0, Enumerable(2));
  260. AssertValueEquals(a, 1, 2, 0, 1, 2, 3);
  261. a.InsertRange(0, new RubyArray(new[] { 1, 0 }), 1, 1);
  262. a.InsertRange(0, new object[] { 0, -1 }, 1, 1);
  263. a.InsertRange(0, new[] { 0, -2 }, 1, 1);
  264. AssertValueEquals(a, -2, -1, 0, 1, 2, 0, 1, 2, 3);
  265. a.InsertRange(0, (IEnumerable)new RubyArray(new[] { 30 }));
  266. a.InsertRange(0, (IEnumerable)new object[] { 20 });
  267. a.InsertRange(0, (IEnumerable)new[] { 10 });
  268. AssertValueEquals(a, 10, 20, 30, -2, -1, 0, 1, 2, 0, 1, 2, 3);
  269. a.Freeze();
  270. AssertExceptionThrown<InvalidOperationException>(() => a.Insert(0, 1));
  271. AssertExceptionThrown<InvalidOperationException>(() => a.InsertRange(0, new object[0]));
  272. AssertExceptionThrown<InvalidOperationException>(() => a.InsertRange(0, Enumerable(0)));
  273. }
  274. [Options(NoRuntime = true)]
  275. public void RubyArray_Misc() {
  276. RubyArray a;
  277. a = new RubyArray(new[] { 3, 5, 2, 4, 1 });
  278. a.InsertRange(0, new[] {10, 20});
  279. a.RemoveRange(0, 2);
  280. a.Sort();
  281. Assert(ArrayUtils.ValueEquals(a.ToArray(), new object[] { 1, 2, 3, 4, 5 }));
  282. a.Sort((x, y) => (int)x == (int)y ? 0 : ((int)x < (int)y ? 1 : -1));
  283. Assert(ArrayUtils.ValueEquals(a.ToArray(), new object[] { 5, 4, 3, 2, 1 }));
  284. a.Reverse();
  285. Assert(ArrayUtils.ValueEquals(a.ToArray(), new object[] { 1, 2, 3, 4, 5 }));
  286. a.Freeze();
  287. AssertExceptionThrown<InvalidOperationException>(() => a.Reverse());
  288. AssertExceptionThrown<InvalidOperationException>(() => a.Sort());
  289. }
  290. }
  291. }