Example #1
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 <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)));
        }
Example #2
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)));
        }
Example #3
0
        public MutableString _dump()
        {
            var array = new RubyArray(new[] { 3, Xsize, Ysize, Zsize, Xsize * Ysize * Zsize });

            array.AddRange(Data);
            return(Ruby.Pack(array, "LLLLLS*"));
        }
        // R(N, *, =)
        public override object InvokeSplatRhs(BlockParam /*!*/ param, object self, Proc procArg, object[] /*!*/ args, IList /*!*/ splattee, object rhs)
        {
            var list = new RubyArray(splattee.Count + 1);

            list.AddRange(splattee);
            list.Add(rhs);
            splattee = list;

            return(InvokeSplatInternal(param, self, procArg, args, list));
        }
Example #5
0
        // R(1, *)
        public override object InvokeSplat(BlockParam /*!*/ param, object self, object arg1, IList /*!*/ splattee)
        {
            if (splattee.Count > 0)
            {
                var array = new RubyArray(1 + splattee.Count);
                array.Add(arg1);
                array.AddRange(splattee);
                arg1 = array;

                if (!HasSingleCompoundParameter)
                {
                    param.MultipleValuesForBlockParameterWarning(array.Count);
                }
            }

            return(_block(param, self, arg1));
        }
Example #6
0
        // R(N, *, =)
        public override object InvokeSplatRhs(BlockParam /*!*/ param, object self, object[] /*!*/ args, object splattee, object rhs)
        {
            var list = splattee as List <object>;

            if (list != null)
            {
                var l = new RubyArray(list.Count + 1);
                l.AddRange(list);
                l.Add(rhs);
                list = l;
            }
            else
            {
                list = RubyOps.MakeArray2(splattee, rhs);
            }

            return(InvokeSplatInternal(param, self, args, list, list));
        }
Example #7
0
        public static RubyArray/*!*/ ValuesAt(ConversionStorage<int>/*!*/ fixnumCast, 
            CallSiteStorage<Func<CallSite, RubyClass, object>>/*!*/ allocateStorage,
             IList/*!*/ self, [NotNull]params object[]/*!*/ values) {
            RubyArray result = new RubyArray();

            for (int i = 0; i < values.Length; i++) {
                Range range = values[i] as Range;
                if (range != null) {
                    int start, count;
                    if (!NormalizeRange(fixnumCast, self.Count, range, out start, out count)) {
                        continue;
                    }

                    if (count > 0) {
                        result.AddRange(GetElements(allocateStorage, self, start, count));
                        if (start + count >= self.Count) {
                            result.Add(null);
                        }
                    }
                } else {
                    result.Add(GetElement(self, Protocols.CastToFixnum(fixnumCast, values[i])));
                }
            }

            return result;
        }
Example #8
0
        public static RubyArray/*!*/ ValuesAt(ConversionStorage<int>/*!*/ fixnumCast, 
            CallSiteStorage<Func<CallSite, RubyClass, object>>/*!*/ allocateStorage,
             IList/*!*/ self, [NotNull]params object[]/*!*/ values) {
            RubyArray result = new RubyArray();

            for (int i = 0; i < values.Length; i++) {
                Range range = values[i] as Range;
                if (range != null) {
                    IList fragment = GetElement(fixnumCast, allocateStorage, self, range);
                    if (fragment != null) {
                        result.AddRange(fragment);
                    }
                } else {
                    result.Add(GetElement(self, Protocols.CastToFixnum(fixnumCast, values[i])));
                }
            }

            return result;
        }
Example #9
0
        public void RubyArray_Ctors()
        {
            const int N    = 10;
            var       dict = new Dictionary <object, object>();

            for (int i = 0; i < N; i++)
            {
                dict.Add(i, i);
            }

            RubyArray a;

            a = RubyArray.Create(1);
            Assert(a.Count == 1 && (int)a[0] == 1);

            a = new RubyArray();
            Assert(a.Count == 0 && a.Capacity == 0);

            a = new RubyArray(1);
            Assert(a.Count == 0 && a.Capacity == Utils.MinListSize);

            a = new RubyArray(100);
            Assert(a.Count == 0 && a.Capacity == 100);

            a = new RubyArray((ICollection)dict.Values);
            Assert(a.Count == N);

            a = new RubyArray((IEnumerable)dict.Values);
            Assert(a.Count == N);

            a = new RubyArray((IList) new object[] { 1, 2, 3 });
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray((IList) new object[] { 1, 2, 3 }, 1, 1);
            Assert(a.Count == 1);
            Assert((int)a[0] == 2);

            a = new RubyArray((IList) new object[] { 1, 2, 3 }, 1, 0);
            Assert(a.Count == 0);

            a = new RubyArray((ICollection) new object[] { 1, 2, 3 });
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray((IEnumerable) new object[] { 1, 2, 3 });
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray(Enumerable(3));
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray(a);
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray(a, 0, 2);
            Assert(a.Count == 2);
            Assert((int)a[0] == 1 && (int)a[1] == 2);

            // prepare array [nil, 1, 3, nil]
            RubyArray b = new RubyArray(new[] { 1, 2, 3 });

            b.RemoveAt(1);
            a = new RubyArray(b);
            Assert(a.Count == 2);
            Assert((int)a[0] == 1 && (int)a[1] == 3);

            a = new RubyArray(b, 2, 0);
            Assert(a.Count == 0);

            a = new RubyArray();
            a.AddRange(b);
            Assert(a.Count == 2);
            Assert((int)a[0] == 1 && (int)a[1] == 3);

            a = new RubyArray();
            a.AddRange(b, 1, 1);
            Assert(a.Count == 1);
            Assert((int)a[0] == 3);

            a = new RubyArray(new[] { 1, 2, 3 });
            AssertExceptionThrown <ArgumentNullException>(() => new RubyArray(null, 1, 2));
            AssertExceptionThrown <ArgumentOutOfRangeException>(() => new RubyArray(a, -1, 2));
            AssertExceptionThrown <ArgumentOutOfRangeException>(() => new RubyArray(a, 3, 1));
            AssertExceptionThrown <ArgumentOutOfRangeException>(() => new RubyArray(a, 0, 4));
        }
Example #10
0
        public void RubyArray_Remove()
        {
            RubyArray a;

            a = new RubyArray(new[] { 1, 2, 3, 4, 5 });

            a.RemoveAt(1);
            AssertValueEquals(a, 1, 3, 4, 5);

            a.RemoveAt(1);
            AssertValueEquals(a, 1, 4, 5);

            a.RemoveAt(1);
            AssertValueEquals(a, 1, 5);

            a.RemoveAt(1);
            AssertValueEquals(a, 1);

            a.AddRange(new[] { 2, 3 });
            AssertValueEquals(a, 1, 2, 3);

            a.RemoveAt(0);
            AssertValueEquals(a, 2, 3);

            a.RemoveAt(0);
            AssertValueEquals(a, 3);

            a.RemoveAt(0);
            AssertValueEquals(a);

            a.AddRange(new[] { 1, 2, 3 });
            AssertValueEquals(a, 1, 2, 3);

            a = new RubyArray();
            a.AddMultiple(100, 1);
            a[0]  = 0;
            a[99] = 99;
            a.RemoveRange(1, 98);
            AssertValueEquals(a, 0, 99);
            Assert(a.Capacity < 100, "array should shrink");

            ((IList)a).Remove(0);
            AssertValueEquals(a, 99);
            a.Clear();
            Assert(a.Count == 0);

            a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
            a.RemoveRange(0, 4);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11);
            a.AddMultiple(3, 1);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1);
            a.RemoveRange(0, 6);
            AssertValueEquals(a, 10, 11, 1, 1, 1);
            a.AddMultiple(2, 2);
            AssertValueEquals(a, 10, 11, 1, 1, 1, 2, 2);

            a = new RubyArray();
            a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
            a.RemoveRange(0, 4);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11);
            a.AddMultiple(3, null);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11, null, null, null);

            var vector = new object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

            a = new RubyArray(vector);
            for (int i = 0, c = a.Count; i < c; i++)
            {
                a.RemoveAt(0);
                vector = ArrayUtils.ShiftLeft(vector, 1);
                AssertValueEquals(a, vector);
            }

            a = new RubyArray(new[] { 1, 2, 3 });
            a.Freeze();
            Assert(!a.Remove(0));
            AssertExceptionThrown <InvalidOperationException>(() => a.Remove(1));
            AssertExceptionThrown <InvalidOperationException>(() => a.Clear());
            AssertExceptionThrown <InvalidOperationException>(() => a.RemoveAt(0));
            AssertExceptionThrown <InvalidOperationException>(() => a.RemoveRange(0, 1));
        }
Example #11
0
        public void RubyArray_Ctors() {
            const int N = 10;
            var dict = new Dictionary<object, object>();
            for (int i = 0; i < N; i++) {
                dict.Add(i, i);
            }

            RubyArray a;

            a = RubyArray.Create(1);
            Assert(a.Count == 1 && (int)a[0] == 1);

            a = new RubyArray();
            Assert(a.Count == 0 && a.Capacity == 0);

            a = new RubyArray(1);
            Assert(a.Count == 0 && a.Capacity == Utils.MinListSize);

            a = new RubyArray(100);
            Assert(a.Count == 0 && a.Capacity == 100);
            
            a = new RubyArray((ICollection)dict.Values);
            Assert(a.Count == N);

            a = new RubyArray((IEnumerable)dict.Values);
            Assert(a.Count == N);

            a = new RubyArray((IList)new object[] { 1, 2, 3 });
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray((IList)new object[] { 1, 2, 3 }, 1, 1);
            Assert(a.Count == 1);
            Assert((int)a[0] == 2);

            a = new RubyArray((IList)new object[] { 1, 2, 3 }, 1, 0);
            Assert(a.Count == 0);

            a = new RubyArray((ICollection)new object[] { 1, 2, 3 });
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray((IEnumerable)new object[] { 1, 2, 3 });
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray(Enumerable(3));
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray(a);
            Assert(a.Count == 3);
            Assert((int)a[0] == 1 && (int)a[1] == 2 && (int)a[2] == 3);

            a = new RubyArray(a, 0, 2);
            Assert(a.Count == 2);
            Assert((int)a[0] == 1 && (int)a[1] == 2);

            // prepare array [nil, 1, 3, nil]
            RubyArray b = new RubyArray(new[] { 1, 2, 3 });
            b.RemoveAt(1);
            a = new RubyArray(b);
            Assert(a.Count == 2);
            Assert((int)a[0] == 1 && (int)a[1] == 3);

            a = new RubyArray(b, 2, 0);
            Assert(a.Count == 0);

            a = new RubyArray();
            a.AddRange(b);
            Assert(a.Count == 2);
            Assert((int)a[0] == 1 && (int)a[1] == 3);

            a = new RubyArray();
            a.AddRange(b, 1, 1);
            Assert(a.Count == 1);
            Assert((int)a[0] == 3);

            a = new RubyArray(new[] { 1, 2, 3 });
            AssertExceptionThrown<ArgumentNullException>(() => new RubyArray(null, 1, 2));
            AssertExceptionThrown<ArgumentOutOfRangeException>(() => new RubyArray(a, -1, 2));
            AssertExceptionThrown<ArgumentOutOfRangeException>(() => new RubyArray(a, 3, 1));
            AssertExceptionThrown<ArgumentOutOfRangeException>(() => new RubyArray(a, 0, 4));
        }
Example #12
0
        public void RubyArray_Remove() {
            RubyArray a;
            a = new RubyArray(new[] { 1, 2, 3, 4, 5 });

            a.RemoveAt(1);
            AssertValueEquals(a, 1, 3, 4, 5);

            a.RemoveAt(1);
            AssertValueEquals(a, 1, 4, 5);

            a.RemoveAt(1);
            AssertValueEquals(a, 1, 5);

            a.RemoveAt(1);
            AssertValueEquals(a, 1);

            a.AddRange(new[] { 2, 3 });
            AssertValueEquals(a, 1, 2, 3);

            a.RemoveAt(0);
            AssertValueEquals(a, 2, 3);

            a.RemoveAt(0);
            AssertValueEquals(a, 3);

            a.RemoveAt(0);
            AssertValueEquals(a);

            a.AddRange(new[] { 1, 2, 3 });
            AssertValueEquals(a, 1, 2, 3);

            a = new RubyArray();
            a.AddMultiple(100, 1);
            a[0] = 0;
            a[99] = 99;
            a.RemoveRange(1, 98);
            AssertValueEquals(a, 0, 99);
            Assert(a.Capacity < 100, "array should shrink");

            ((IList)a).Remove(0);
            AssertValueEquals(a, 99);
            a.Clear();
            Assert(a.Count == 0);

            a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
            a.RemoveRange(0, 4);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11);
            a.AddMultiple(3, 1);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11, 1, 1, 1);
            a.RemoveRange(0, 6);
            AssertValueEquals(a, 10, 11, 1, 1, 1);
            a.AddMultiple(2, 2);
            AssertValueEquals(a, 10, 11, 1, 1, 1, 2, 2);

            a = new RubyArray();
            a = new RubyArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
            a.RemoveRange(0, 4);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11);
            a.AddMultiple(3, null);
            AssertValueEquals(a, 4, 5, 6, 7, 8, 9, 10, 11, null, null, null);

            var vector = new object[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            a = new RubyArray(vector);
            for (int i = 0, c = a.Count; i < c; i++) {
                a.RemoveAt(0);
                vector = ArrayUtils.ShiftLeft(vector, 1);
                AssertValueEquals(a, vector);
            }
            
            a = new RubyArray(new[] { 1, 2, 3 });
            a.Freeze();
            Assert(!a.Remove(0));
            AssertExceptionThrown<RuntimeError>(() => a.Remove(1));
            AssertExceptionThrown<RuntimeError>(() => a.Clear());
            AssertExceptionThrown<RuntimeError>(() => a.RemoveAt(0));
            AssertExceptionThrown<RuntimeError>(() => a.RemoveRange(0, 1));
        }
Example #13
0
        public static object SplatPair(object value, object array) {
            var list = array as IList;
            if (list != null) {
                if (list.Count == 0) {
                    return value;
                }

                RubyArray result = new RubyArray(list.Count + 1);
                result.Add(value);
                result.AddRange(list);
                return result;
            }

            return MakeArray2(value, array);
        }
        // R(N, *, =)
        public override object InvokeSplatRhs(BlockParam/*!*/ param, object self, object[]/*!*/ args, object splattee, object rhs) {
            var list = splattee as IList;
            if (list != null) {
                var l = new RubyArray(list.Count + 1);
                l.AddRange(list);
                l.Add(rhs);
                list = l;
            } else {
                list = RubyOps.MakeArray2(splattee, rhs);
            }

            return InvokeSplatInternal(param, self, args, list, list);
        }