Ejemplo n.º 1
0
        public static void CopyTo()
        {
            JsonNode node1 = 1;
            JsonNode node2 = 2;

            IList <JsonNode?> jArray = new JsonArray(node1, node2, null);
            var arr = new JsonNode[4];

            jArray.CopyTo(arr, 0);

            Assert.Same(node1, arr[0]);
            Assert.Same(node2, arr[1]);
            Assert.Null(arr[2]);

            arr = new JsonNode[5];
            jArray.CopyTo(arr, 1);
            Assert.Null(arr[0]);
            Assert.Same(node1, arr[1]);
            Assert.Same(node2, arr[2]);
            Assert.Null(arr[3]);
            Assert.Null(arr[4]);

            arr = new JsonNode[3];
            Assert.Throws <ArgumentException>(() => jArray.CopyTo(arr, 1));

            Assert.Throws <ArgumentOutOfRangeException>(() => jArray.CopyTo(arr, -1));
        }
Ejemplo n.º 2
0
        internal IEnumerable <JsonValue> Find(JsonArray json, JsonValue root)
        {
            if (Index.HasValue)
            {
                return(Index.Value < 0 || json.Count <= Index.Value
                                                   ? Enumerable.Empty <JsonValue>()
                                                   : new[] { json[Index.Value] });
            }

            var start = _ResolveIndex(_start ?? 0, json.Count);
            var end   = _ResolveIndex(_end ?? json.Count, json.Count);
            var step  = Math.Max(_step ?? 1, 1);

            // quick copy
            if (start == 0 && end == json.Count && step == 1)
            {
                return(new List <JsonValue>(json));
            }

            if (step == 1)
            {
                var result = new JsonValue[end - start];
                json.CopyTo(start, result, 0, end - start);
                return(result);
            }
            else
            {
                return(_FindSlow(json, start, end, step));
            }
        }
Ejemplo n.º 3
0
        public void CompareJsonArrays(JsonArray aryOne, JsonArray aryTwo)
        {
            var loopLength = GetLoopLength(aryOne.Count, aryTwo.Count);

            if (CompareItemLengths(aryOne.Count, aryTwo.Count) != string.Empty)
            {
                _builder.Append($"Array Lengths: {CompareItemLengths(aryOne.Count, aryTwo.Count)}");
            }

            for (var i = 0; i < loopLength; i++)
            {
                var objOne = aryOne[i] as JsonObject;
                var objTwo = aryTwo[i] as JsonObject;
                CompareJsonObjects(objOne, objTwo);
            }

            var valueArray1 = new JsonValue[aryOne.Count];

            aryOne.CopyTo(valueArray1, 0);
            var valueArray2 = new JsonValue[aryTwo.Count];

            aryTwo.CopyTo(valueArray2, 0);

            _builder.Append(GetRemainingItems(ValueToString(valueArray1), ValueToString(valueArray2)));
        }
Ejemplo n.º 4
0
        public void JsonArrayCopytoFunctionalTest()
        {
            int seed = 1;

            for (int i = 0; i < iterationCount / 10; i++)
            {
                seed++;
                Log.Info("Seed: {0}", seed);
                Random rndGen = new Random(seed);

                bool retValue = true;

                JsonArray   sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                JsonValue[] destJson   = new JsonValue[arrayLength];
                sourceJson.CopyTo(destJson, 0);

                for (int k = 0; k < destJson.Length; k++)
                {
                    if (destJson[k] != sourceJson[k])
                    {
                        retValue = false;
                    }
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonArrayCopytoFunctionalTest] JsonArray.CopyTo() failed to function properly. destJson.GetLength(0) = " + destJson.GetLength(0));
                }
            }
        }
Ejemplo n.º 5
0
        public void CopyToTest()
        {
            JsonValue item1  = AnyInstance.AnyJsonValue1;
            JsonValue item2  = AnyInstance.AnyJsonValue2;
            JsonArray target = new JsonArray(item1, item2);

            JsonValue[] array = new JsonValue[target.Count + 1];

            target.CopyTo(array, 0);
            Assert.AreEqual(item1, array[0]);
            Assert.AreEqual(item2, array[1]);

            target.CopyTo(array, 1);
            Assert.AreEqual(item1, array[1]);
            Assert.AreEqual(item2, array[2]);

            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => target.CopyTo(null, 0));
            ExceptionTestHelper.ExpectException <ArgumentOutOfRangeException>(() => target.CopyTo(array, -1));
            ExceptionTestHelper.ExpectException <ArgumentException>(() => target.CopyTo(array, array.Length - target.Count + 1));
        }
Ejemplo n.º 6
0
        public static void JsonArrayMethodsTest()
        {
            var       arr  = new JsonArray();
            var       list = new List <JsonValue>();
            JsonValue value;

            Assert.False(arr.IsReadOnly);

            value = "aloha oe";
            arr.Add(value);
            list.Add(value);

            value = 4242;
            arr.Add(value);
            list.Add(value);

            value = false;
            arr.Add(value);
            list.Add(value);

            value = new JsonNull();
            arr.Add(value);
            list.Add(value);

            Assert.Equal(4, arr.Count);

            var varr = new JsonValue[4];

            arr.CopyTo(varr, 0);

            Assert.Equal(list, varr);

            int ptr = 0;

            foreach (var v in arr)
            {
                Assert.Equal(list[ptr++], v);
            }

            value  = 43.3336;
            arr[2] = value;
            Assert.Equal(value, arr[2]);
            Assert.Equal(2, arr.IndexOf(value));
            Assert.Contains(value, arr);
            Assert.True(arr.Remove(value));
            arr.RemoveAt(2);
            Assert.Equal(2, arr.Count);
            arr.Insert(2, value);
            Assert.Equal(value, arr[2]);
            arr.Clear();
            Assert.Empty(arr);
        }
Ejemplo n.º 7
0
        public void CopyTo(int arrayIndex)
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));

            JsonValue[] copy = new JsonValue[array.Count + arrayIndex];
            array.CopyTo(copy, arrayIndex);

            for (int i = 0; i < arrayIndex; i++)
            {
                Assert.Null(copy[i]);
            }
            for (int i = arrayIndex; i < copy.Length; i++)
            {
                Assert.Same(array[i - arrayIndex], copy[i]);
            }
        }
Ejemplo n.º 8
0
        internal IEnumerable <JsonValue> Find(JsonArray json, JsonValue root)
        {
            if (Index.HasValue)
            {
                var index = Index.Value;
                if (index < 0)
                {
                    index += json.Count;
                }

                return(index < 0 || json.Count <= index
                                        ? Enumerable.Empty <JsonValue>()
                                        : new[] { json[index] });
            }

            var start = Math.Max(_ResolveIndex(_start ?? 0, json.Count), 0);
            var end   = _ResolveIndex(_end ?? json.Count, json.Count);

            if (start == end)
            {
                return(Enumerable.Empty <JsonValue>());
            }
            //var step = _step ?? (start < end ? 1 : -1);
            var step = _step ?? 1;

            if (start == 0 && end == json.Count && step == 1)
            {
                return(json);
            }

            if (step > 0 && end <= start)
            {
                return(Enumerable.Empty <JsonValue>());
            }

            if (step == 1)
            {
                var result = new JsonValue[end - start];
                json.CopyTo(start, result, 0, end - start);
                return(result);
            }

            return(_FindSlow(json, start, end, step));
        }