public void RubyArray_Add() { RubyArray a; a = new RubyArray(); for (int i = 0; i < Utils.MinListSize; i++) { a.Add(i); Assert((int)a[i] == i && a.Count == i + 1 && a.Capacity == Utils.MinListSize); } Assert(((IList)a).Add(Utils.MinListSize) == Utils.MinListSize); Assert(a.Count == Utils.MinListSize + 1); for (int i = 0; i < a.Count; i++) { Assert((int)a[i] == i); } a = new RubyArray(new[] { 1, 2, 3 }); a.AddCapacity(0); Assert(a.Count == 3); a.AddCapacity(100); Assert(a.Count == 3 && a.Capacity >= 103); a = new RubyArray(new[] { 1, 2, 3 }); a.AddMultiple(0, 4); AssertValueEquals(a, 1, 2, 3); a.AddMultiple(5, 4); AssertValueEquals(a, 1, 2, 3, 4, 4, 4, 4, 4); a = new RubyArray(new[] { 1, 2, 3 }); a.AddRange(new object[0]); AssertValueEquals(a, 1, 2, 3); a.AddRange(new[] { 4 }); AssertValueEquals(a, 1, 2, 3, 4); a.AddRange(new[] { 5, 6, 7, 8, 9, 10 }); AssertValueEquals(a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); a.AddRange(new[] { 11 }); AssertValueEquals(a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); a = new RubyArray(); a.AddRange((IEnumerable) new RubyArray(new[] { 1, 2, 3 })); a.AddRange((IList) new RubyArray(new[] { 1, 2, 3 }), 1, 2); AssertValueEquals(a, 1, 2, 3, 2, 3); a.Freeze(); AssertExceptionThrown <InvalidOperationException>(() => a.Add(1)); AssertExceptionThrown <InvalidOperationException>(() => a.AddCapacity(10)); AssertExceptionThrown <InvalidOperationException>(() => a.AddMultiple(10, 10)); AssertExceptionThrown <InvalidOperationException>(() => a.AddRange(new object[0])); AssertExceptionThrown <InvalidOperationException>(() => a.AddRange(Enumerable(0))); }
public void RubyArray_Add() { RubyArray a; a = new RubyArray(); for (int i = 0; i < Utils.MinListSize; i++) { a.Add(i); Assert((int)a[i] == i && a.Count == i + 1 && a.Capacity == Utils.MinListSize); } Assert(((IList)a).Add(Utils.MinListSize) == Utils.MinListSize); Assert(a.Count == Utils.MinListSize + 1); for (int i = 0; i < a.Count; i++) { Assert((int)a[i] == i); } a = new RubyArray(new[] { 1,2,3 }); a.AddCapacity(0); Assert(a.Count == 3); a.AddCapacity(100); Assert(a.Count == 3 && a.Capacity >= 103); a = new RubyArray(new[] { 1, 2, 3 }); a.AddMultiple(0, 4); AssertValueEquals(a, 1, 2, 3); a.AddMultiple(5, 4); AssertValueEquals(a, 1, 2, 3, 4, 4, 4, 4, 4); a = new RubyArray(new[] { 1, 2, 3 }); a.AddRange(new object[0]); AssertValueEquals(a, 1, 2, 3); a.AddRange(new[] { 4 }); AssertValueEquals(a, 1, 2, 3, 4); a.AddRange(new[] { 5, 6, 7, 8, 9, 10 }); AssertValueEquals(a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); a.AddRange(new[] { 11 }); AssertValueEquals(a, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); a = new RubyArray(); a.AddRange((IEnumerable)new RubyArray(new[] { 1, 2, 3 })); a.AddRange((IList)new RubyArray(new[] { 1, 2, 3 }), 1, 2); AssertValueEquals(a, 1, 2, 3, 2, 3); a.Freeze(); AssertExceptionThrown<RuntimeError>(() => a.Add(1)); AssertExceptionThrown<RuntimeError>(() => a.AddCapacity(10)); AssertExceptionThrown<RuntimeError>(() => a.AddMultiple(10, 10)); AssertExceptionThrown<RuntimeError>(() => a.AddRange(new object[0])); AssertExceptionThrown<RuntimeError>(() => a.AddRange(Enumerable(0))); }
public static RubyArray/*!*/ Transpose(ConversionStorage<IList>/*!*/ arrayCast, IList/*!*/ self) { // Get the arrays. Note we need to check length as we go, so we call to_ary on all the // arrays we encounter before the error (if any). RubyArray result = new RubyArray(); for (int i = 0; i < self.Count; i++) { IList list = Protocols.CastToArray(arrayCast, self[i]); if (i == 0) { // initialize the result result.AddCapacity(list.Count); for (int j = 0; j < list.Count; j++) { result.Add(new RubyArray()); } } else if (list.Count != result.Count) { throw RubyExceptions.CreateIndexError(string.Format("element size differs ({0} should be {1})", list.Count, result.Count)); } // add items Debug.Assert(list.Count == result.Count); for (int j = 0; j < result.Count; j++) { ((RubyArray)result[j]).Add(list[j]); } } return result; }
public static RubyArray/*!*/ ValuesAt(ConversionStorage<int>/*!*/ fixnumCast, RubyStruct/*!*/ self, params object[]/*!*/ values) { RubyArray result = new RubyArray(); object[] data = self.Values; for (int i = 0; i < values.Length; ++i) { Range range = values[i] as Range; if (range != null) { int begin = Protocols.CastToFixnum(fixnumCast, range.Begin); int end = Protocols.CastToFixnum(fixnumCast, range.End); if (range.ExcludeEnd) { end -= 1; } begin = NormalizeIndex(data.Length, begin); end = NormalizeIndex(data.Length, end); Debug.Assert(end - begin <= data.Length); // because we normalized the indicies if (end - begin > 0) { result.AddCapacity(end - begin); for (int j = begin; j <= end; j++) { result.Add(data[j]); } } } else { int index = NormalizeIndex(data.Length, Protocols.CastToFixnum(fixnumCast, values[i])); result.Add(data[index]); } } return result; }