Exemplo n.º 1
0
        public void JsonObjectCopytoFunctionalTest()
        {
            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;

                JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);
                KeyValuePair <string, JsonValue>[] destJson = new KeyValuePair <string, JsonValue> [arrayLength];
                if (sourceJson != null && destJson != null)
                {
                    sourceJson.CopyTo(destJson, 0);
                }
                else
                {
                    Log.Info("[JsonObjectCopytoFunctionalTest] sourceJson.ToString() = " + sourceJson.ToString());
                    Log.Info("[JsonObjectCopytoFunctionalTest] destJson.ToString() = " + destJson.ToString());
                    Assert.Fail("[JsonObjectCopytoFunctionalTest] failed to create the source JsonObject object.");
                    return;
                }

                if (destJson.Length == arrayLength)
                {
                    for (int k = 0; k < destJson.Length; k++)
                    {
                        JsonValue temp;
                        sourceJson.TryGetValue(k.ToString(), out temp);
                        if (!(temp != null && destJson[k].Value == temp))
                        {
                            retValue = false;
                        }
                    }
                }
                else
                {
                    retValue = false;
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonObjectCopytoFunctionalTest] JsonObject.CopyTo() failed to function properly. destJson.GetLength(0) = " + destJson.GetLength(0));
                }
            }
        }
Exemplo n.º 2
0
        public void JsonObjectItemsFunctionalTest()
        {
            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;

                JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

                // JsonObject[key].set_Item
                sourceJson["1"] = new JsonPrimitive(true);
                if (sourceJson["1"].ToString() != "true")
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[key].set_Item failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[key].set_Item passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item)
                KeyValuePair <string, System.Json.JsonValue> kp = new KeyValuePair <string, JsonValue>("5", sourceJson["5"]);
                if (!((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Contains(kp))
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly
                if (((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).IsReadOnly)
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item)
                kp = new KeyValuePair <string, JsonValue>("100", new JsonPrimitive(100));
                ((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Add(kp);
                if (sourceJson.Count != arrayLength + 1)
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item)
                ((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Remove(kp);
                if (sourceJson.Count != arrayLength)
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator()
                JsonObject jo = new JsonObject {
                    { "member 1", 123 }, { "member 2", new JsonArray {
                                               1, 2, 3
                                           } }
                };
                List <string> expected = new List <string> {
                    "member 1 - 123", "member 2 - [1,2,3]"
                };
                expected.Sort();
                IEnumerator <KeyValuePair <string, JsonValue> > ko = ((ICollection <KeyValuePair <string, JsonValue> >)jo).GetEnumerator();
                List <string> actual = new List <string>();
                ko.Reset();
                ko.MoveNext();
                do
                {
                    actual.Add(String.Format("{0} - {1}", ko.Current.Key, ko.Current.Value));
                    Log.Info("added one item: {0}", String.Format("{0} - {1}", ko.Current.Key, ko.Current.Value));
                    ko.MoveNext();
                }while (ko.Current.Value != null);

                actual.Sort();
                if (!JsonValueVerifier.CompareStringLists(expected, actual))
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() passed test.");
                }

                // JsonObject.Values
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);
                JsonValue[] manyValues = SpecialJsonValueHelper.CreatePrePopulatedJsonValueArray(seed, arrayLength);
                JsonObject  jov        = new JsonObject();
                for (int j = 0; j < manyValues.Length; j++)
                {
                    jov.Add("member" + j, manyValues[j]);
                }

                List <string> expectedList = new List <string>();
                foreach (JsonValue v in manyValues)
                {
                    expectedList.Add(v.ToString());
                }

                expectedList.Sort();
                List <string> actualList = new List <string>();
                foreach (JsonValue v in jov.Values)
                {
                    actualList.Add(v.ToString());
                }

                actualList.Sort();
                if (!JsonValueVerifier.CompareStringLists(expectedList, actualList))
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Values failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Values passed test.");
                }

                for (int j = 0; j < sourceJson.Count; j++)
                {
                    // JsonObject.Contains(Key)
                    if (!sourceJson.ContainsKey(j.ToString()))
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Contains(Key) failed to function properly.");
                        retValue = false;
                    }
                    else
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Contains(Key) passed test.");
                    }

                    // JsonObject.TryGetValue(String, out JsonValue)
                    JsonValue retJson;
                    if (!sourceJson.TryGetValue(j.ToString(), out retJson))
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.TryGetValue(String, out JsonValue) failed to function properly.");
                        retValue = false;
                    }
                    else if (retJson != sourceJson[j.ToString()])
                    {
                        // JsonObjectthis[string key]
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[string key] or JsonObject.TryGetValue(String, out JsonValue) failed to function properly.");
                        retValue = false;
                    }
                    else
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.TryGetValue(String, out JsonValue) & JsonObject[string key] passed test.");
                    }
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonObjectItemsFunctionalTest] Test failed!");
                }
            }
        }
Exemplo n.º 3
0
        public void JsonObjectAddRemoveFunctionalTest()
        {
            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;

                JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

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

                // JsonObject.Add(KeyValuePair<string, JsonValue> item)
                // JsonObject.Add(string key, JsonValue value)
                // + various numers below so .AddRange() won't try to add an already existing value
                sourceJson.Add(SpecialJsonValueHelper.GetUniqueNonNullInstanceOfString(seed + 3, sourceJson), SpecialJsonValueHelper.GetUniqueValue(seed, sourceJson));
                KeyValuePair <string, JsonValue> kvp;
                int startingSeed = seed + 1;
                do
                {
                    kvp = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(startingSeed);
                    startingSeed++;
                }while (sourceJson.ContainsKey(kvp.Key));

                sourceJson.Add(kvp);
                do
                {
                    kvp = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(startingSeed);
                    startingSeed++;
                }while (sourceJson.ContainsKey(kvp.Key));

                sourceJson.Add(kvp);
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Add() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Add() passed test.");
                }

                // JsonObject.Clear()
                sourceJson.Clear();
                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Clear() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Clear() passed test.");
                }

                // JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items)
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

                // + various numers below so .AddRange() won't try to add an already existing value
                sourceJson.AddRange(SpecialJsonValueHelper.CreatePrePopulatedListofKeyValuePair(seed + 13 + (arrayLength * 2), 5));
                if (sourceJson.Count != arrayLength + 5)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) passed test.");
                }

                // JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items)
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

                // + various numers below so .AddRange() won't try to add an already existing value
                KeyValuePair <string, JsonValue> item1 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 41);
                KeyValuePair <string, JsonValue> item2 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 47);
                KeyValuePair <string, JsonValue> item3 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 53);
                sourceJson.AddRange(new KeyValuePair <string, JsonValue>[] { item1, item2, item3 });
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) passed test.");
                }

                sourceJson.Clear();

                // JsonObject.Remove(Key)
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);
                int           count = sourceJson.Count;
                List <string> keys  = new List <string>(sourceJson.Keys);
                foreach (string key in keys)
                {
                    sourceJson.Remove(key);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Remove(Key) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Remove(Key) passed test.");
                }

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