Exemplo n.º 1
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));
                }
            }
        }
Exemplo n.º 2
0
        public void JsonArrayItemsFunctionalTest()
        {
            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.Contains(JsonValue)
                // JsonArray.IndexOf(JsonValue)
                JsonArray sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                for (int j = 0; j < sourceJson.Count; j++)
                {
                    if (!sourceJson.Contains(sourceJson[j]))
                    {
                        Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Contains(JsonValue) failed to function properly.");
                        retValue = false;
                    }
                    else
                    {
                        Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Contains(JsonValue) passed test.");
                    }

                    if (sourceJson.IndexOf(sourceJson[j]) != j)
                    {
                        Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.IndexOf(JsonValue) failed to function properly.");
                        retValue = false;
                    }
                    else
                    {
                        Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.IndexOf(JsonValue) passed test.");
                    }
                }

                // JsonArray.Insert(int, JsonValue)
                JsonValue newItem = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);
                sourceJson.Insert(3, newItem);
                if (sourceJson[3] != newItem || sourceJson.Count != arrayLength + 1)
                {
                    Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Insert(int, JsonValue) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Insert(int, JsonValue) passed test.");
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonArrayItemsFunctionalTest] Test failed!");
                }
            }
        }
Exemplo n.º 3
0
        public void JsonArrayEventsTest()
        {
            int       seed           = 1;
            const int maxArrayLength = 1024;
            Random    rand           = new Random(seed);
            JsonArray ja             = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, rand.Next(maxArrayLength));
            int       addPosition    = ja.Count;
            JsonValue insertValue    = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr.Add(insertValue),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, addPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, addPosition)),
            });

            addPosition = ja.Count;
            JsonValue jv1 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);
            JsonValue jv2 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr.AddRange(jv1, jv2),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv1,
                                                                          JsonValueChange.Add, addPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv2,
                                                                          JsonValueChange.Add, addPosition + 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv1,
                                                                          JsonValueChange.Add, addPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja,
                                                                      new JsonValueChangeEventArgs(
                                                                          jv2,
                                                                          JsonValueChange.Add, addPosition + 1)),
            });

            int       replacePosition = rand.Next(ja.Count - 1);
            JsonValue oldValue        = ja[replacePosition];
            JsonValue newValue        = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr[replacePosition] = newValue,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(newValue, JsonValueChange.Replace, replacePosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(oldValue, JsonValueChange.Replace, replacePosition)),
            });

            int insertPosition = rand.Next(ja.Count - 1);

            insertValue = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                ja,
                arr => arr.Insert(insertPosition, insertValue),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, insertPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, insertPosition)),
            });

            TestEvents(
                ja,
                arr => arr.RemoveAt(insertPosition),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, insertPosition)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, insertPosition)),
            });

            ja.Insert(0, insertValue);
            TestEvents(
                ja,
                arr => arr.Remove(insertValue),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, 0)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, 0)),
            });

            TestEvents(
                ja,
                arr => arr.Clear(),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)),
            });

            ja = new JsonArray(1, 2, 3);
            TestEvents(
                ja,
                arr => arr.Remove(new JsonPrimitive("Not there")),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >());

            JsonValue elementInArray = ja[1];

            TestEvents(
                ja,
                arr => arr.Remove(elementInArray),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)),
            });
        }
Exemplo n.º 4
0
        public void JsonObjectEventsTest()
        {
            int       seed   = 1;
            const int maxObj = 10;

            const string key1 = "first";
            const string key2 = "second";
            const string key3 = "third";
            const string key4 = "fourth";
            const string key5 = "fifth";
            JsonObject   jo   = new JsonObject
            {
                { key1, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) },
                { key2, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) },
                { key3, null },
            };

            JsonObject objToAdd = SpecialJsonValueHelper.CreateRandomPopulatedJsonObject(seed, maxObj);

            TestEvents(
                jo,
                obj => obj.Add(key4, objToAdd),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(objToAdd, JsonValueChange.Add, key4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(objToAdd, JsonValueChange.Add, key4)),
            },
                obj => obj.Add("key44", objToAdd));

            JsonArray jaToAdd  = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, maxObj);
            JsonValue replaced = jo[key2];

            TestEvents(
                jo,
                obj => obj[key2] = jaToAdd,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jaToAdd, JsonValueChange.Replace, key2)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(replaced, JsonValueChange.Replace, key2)),
            });

            JsonValue jpToAdd = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                jo,
                obj => obj[key5] = jpToAdd,
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jpToAdd, JsonValueChange.Add, key5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jpToAdd, JsonValueChange.Add, key5)),
            });

            jo.Remove(key4);
            jo.Remove(key5);

            JsonValue jp1 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);
            JsonValue jp2 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed);

            TestEvents(
                jo,
                obj => obj.AddRange(new JsonObject {
                { key4, jp1 }, { key5, jp1 }
            }),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp1, JsonValueChange.Add, key4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Add, key5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp1, JsonValueChange.Add, key4)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Add, key5)),
            },
                obj => obj.AddRange(new JsonObject {
                { "new key", jp1 }, { "newnewKey", jp2 }
            }));

            TestEvents(
                jo,
                obj => obj.Remove(key5),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Remove, key5)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Remove, key5)),
            });

            TestEvents(
                jo,
                obj => obj.Remove("not there"),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >());

            jo = new JsonObject {
                { key1, 1 }, { key2, 2 }, { key3, 3 }
            };

            TestEvents(
                jo,
                obj => obj.Clear(),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)),
            });

            jo = new JsonObject {
                { key1, 1 }, { key2, 2 }, { key3, 3 }
            };
            TestEvents(
                jo,
                obj => ((IDictionary <string, JsonValue>)obj).Remove(new KeyValuePair <string, JsonValue>(key2, jo[key2])),
                new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >
            {
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)),
                new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)),
            },
                obj => ((IDictionary <string, JsonValue>)obj).Remove(new KeyValuePair <string, JsonValue>(key1, jo[key1])));
        }
Exemplo n.º 5
0
        public void JsonArrayAddRemoveFunctionalTest()
        {
            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[] cloneJson  = SpecialJsonValueHelper.CreatePrePopulatedJsonValueArray(seed, 3);

                // JsonArray.AddRange(JsonValue[])
                sourceJson.AddRange(cloneJson);
                if (sourceJson.Count != arrayLength + cloneJson.Length)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue[]) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue[]) passed test.");
                }

                // JsonArray.RemoveAt(int)
                int count = sourceJson.Count;
                for (int j = 0; j < count; j++)
                {
                    sourceJson.RemoveAt(0);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.RemoveAt(int) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.RemoveAt(int) passed test.");
                }

                // JsonArray.JsonType
                if (sourceJson.JsonType != JsonType.Array)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.JsonType failed to function properly.");
                    retValue = false;
                }

                // JsonArray.Clear()
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                sourceJson.Clear();
                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Clear() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Clear() passed test.");
                }

                // JsonArray.AddRange(JsonValue)
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);

                // adding one additional value to the array
                sourceJson.AddRange(SpecialJsonValueHelper.GetRandomJsonPrimitives(seed));
                if (sourceJson.Count != arrayLength + 1)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue) passed test.");
                }

                // JsonArray.AddRange(IEnumerable<JsonValue> items)
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                MyJsonValueCollection <JsonValue> myCols = new MyJsonValueCollection <JsonValue>();
                myCols.Add(new JsonPrimitive(PrimitiveCreator.CreateInstanceOfUInt32(rndGen)));
                string str;
                do
                {
                    str = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (str == null);

                myCols.Add(new JsonPrimitive(str));
                myCols.Add(new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTime(rndGen)));

                // adding 3 additional value to the array
                sourceJson.AddRange(myCols);
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(IEnumerable<JsonValue> items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(IEnumerable<JsonValue> items) passed test.");
                }

                // JsonArray[index].set_Item
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                string temp;
                do
                {
                    temp = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (temp == null);

                sourceJson[1] = temp;
                if ((string)sourceJson[1] != temp)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray[index].set_Item failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray[index].set_Item passed test.");
                }

                // JsonArray.Remove(JsonValue)
                count = sourceJson.Count;
                for (int j = 0; j < count; j++)
                {
                    sourceJson.Remove(sourceJson[0]);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Remove(JsonValue) failed to function properly.");
                    retValue = false;
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonArrayAddRemoveFunctionalTest] Test failed!");
                }
            }
        }