public void JsonValueRoundTripCastTests() { int seed = 1; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); this.DoRoundTripCasting(String.Empty, typeof(string)); this.DoRoundTripCasting("null", typeof(string)); string str; do { str = PrimitiveCreator.CreateInstanceOfString(rndGen); } while (str == null); this.DoRoundTripCasting(str, typeof(string)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt16(rndGen), typeof(int)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt32(rndGen), typeof(int)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt64(rndGen), typeof(int)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt16(rndGen), typeof(int)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt32(rndGen), typeof(int)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt64(rndGen), typeof(int)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfGuid(rndGen), typeof(Guid)); this.DoRoundTripCasting(new Uri("http://bug/test?param=hello%0a"), typeof(Uri)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfChar(rndGen), typeof(char)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfBoolean(rndGen), typeof(bool)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDateTime(rndGen), typeof(DateTime)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen), typeof(DateTimeOffset)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDouble(rndGen), typeof(double)); this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDouble(rndGen), typeof(float)); this.DoRoundTripCasting(0.12345f, typeof(double)); this.DoRoundTripCasting(0.12345f, typeof(float)); }
public void ValidJsonObjectRoundTrip() { bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds; CreatorSettings.CreateDateTimeWithSubMilliseconds = false; try { int seed = 1; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); JsonObject sourceJson = new JsonObject(new Dictionary <string, JsonValue>() { { "Name", PrimitiveCreator.CreateInstanceOfString(rndGen) }, { "Age", PrimitiveCreator.CreateInstanceOfInt32(rndGen) }, { "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) }, { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) } }); sourceJson.Add("NewItem1", PrimitiveCreator.CreateInstanceOfString(rndGen)); sourceJson.Add(new KeyValuePair <string, JsonValue>("NewItem2", PrimitiveCreator.CreateInstanceOfString(rndGen))); JsonObject newJson = (JsonObject)JsonValue.Parse(sourceJson.ToString()); newJson.Remove("NewItem1"); sourceJson.Remove("NewItem1"); Assert.False(newJson.ContainsKey("NewItem1")); Assert.False(!JsonValueVerifier.Compare(sourceJson, newJson)); } finally { CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue; } }
static JToken CreateJsonPrimitive(Random rndGen) { switch (rndGen.Next(17)) { case 0: return(PrimitiveCreator.CreateInstanceOfChar(rndGen)); case 1: return(new JValue(PrimitiveCreator.CreateInstanceOfByte(rndGen))); case 2: return(PrimitiveCreator.CreateInstanceOfSByte(rndGen)); case 3: return(PrimitiveCreator.CreateInstanceOfInt16(rndGen)); case 4: return(PrimitiveCreator.CreateInstanceOfUInt16(rndGen)); case 5: return(PrimitiveCreator.CreateInstanceOfInt32(rndGen)); case 6: return(PrimitiveCreator.CreateInstanceOfUInt32(rndGen)); case 7: return(PrimitiveCreator.CreateInstanceOfInt64(rndGen)); case 8: return(PrimitiveCreator.CreateInstanceOfUInt64(rndGen)); case 9: return(PrimitiveCreator.CreateInstanceOfDecimal(rndGen)); case 10: return(PrimitiveCreator.CreateInstanceOfDouble(rndGen)); case 11: return(PrimitiveCreator.CreateInstanceOfSingle(rndGen)); case 12: return(PrimitiveCreator.CreateInstanceOfDateTime(rndGen)); case 13: return(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen)); case 14: case 15: // TODO: 199532 fix uri comparer return(PrimitiveCreator.CreateInstanceOfString(rndGen)); default: return(PrimitiveCreator.CreateInstanceOfBoolean(rndGen)); } }
public void ValidJsonObjectDateTimeOffsetRoundTrip() { int seed = 1; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); JsonPrimitive sourceJson = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen)); JsonPrimitive newJson = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString()); Assert.True(JsonValueVerifier.Compare(sourceJson, newJson)); }
public void ValidJsonObjectDateTimeOffsetRoundTrip() { int seed = 1; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); JsonPrimitive sourceJson = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen)); JsonPrimitive newJson = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString()); if (!JsonValueVerifier.Compare(sourceJson, newJson)) { Assert.Fail("Test failed! The new JsonObject DateTimeOffset value does not equal to the original one."); } }
public static JsonValue CreateDeepLevelJsonValue() { int seed = Environment.TickCount; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); bool myBool = PrimitiveCreator.CreateInstanceOfBoolean(rndGen); byte myByte = PrimitiveCreator.CreateInstanceOfByte(rndGen); DateTime myDatetime = PrimitiveCreator.CreateInstanceOfDateTime(rndGen); DateTimeOffset myDateTimeOffset = PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen); decimal myDecimal = PrimitiveCreator.CreateInstanceOfDecimal(rndGen); double myDouble = PrimitiveCreator.CreateInstanceOfDouble(rndGen); short myInt16 = PrimitiveCreator.CreateInstanceOfInt16(rndGen); int myInt32 = PrimitiveCreator.CreateInstanceOfInt32(rndGen); long myInt64 = PrimitiveCreator.CreateInstanceOfInt64(rndGen); sbyte mySByte = PrimitiveCreator.CreateInstanceOfSByte(rndGen); float mySingle = PrimitiveCreator.CreateInstanceOfSingle(rndGen); string myString = PrimitiveCreator.CreateInstanceOfString(rndGen, 20, null); ushort myUInt16 = PrimitiveCreator.CreateInstanceOfUInt16(rndGen); uint myUInt32 = PrimitiveCreator.CreateInstanceOfUInt32(rndGen); ulong myUInt64 = PrimitiveCreator.CreateInstanceOfUInt64(rndGen); JsonArray myArray = new JsonArray { myBool, myByte, myDatetime, myDateTimeOffset, myDecimal, myDouble, myInt16, myInt32, myInt64, mySByte, mySingle, myString, myUInt16, myUInt32, myUInt64 }; JsonArray myArrayLevel2 = new JsonArray { myArray, myArray, myArray }; JsonArray myArrayLevel3 = new JsonArray { myArrayLevel2, myArrayLevel2, myArrayLevel2 }; JsonArray myArrayLevel4 = new JsonArray { myArrayLevel3, myArrayLevel3, myArrayLevel3 }; JsonArray myArrayLevel5 = new JsonArray { myArrayLevel4, myArrayLevel4, myArrayLevel4 }; JsonArray myArrayLevel6 = new JsonArray { myArrayLevel5, myArrayLevel5, myArrayLevel5 }; JsonArray myArrayLevel7 = new JsonArray { myArrayLevel6, myArrayLevel6, myArrayLevel6 }; JsonArray sourceJson = BuildJsonArrayinSequence1(myBool, myByte, myDatetime, myDateTimeOffset, myDecimal, myDouble, myInt16, myInt32, myInt64, mySByte, mySingle, myString, myUInt16, myUInt32, myUInt64, myArray, myArrayLevel2, myArrayLevel3, myArrayLevel4, myArrayLevel5, myArrayLevel6, myArrayLevel7); return(sourceJson); }
public void ValidJsonArrayRoundTrip() { bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds; CreatorSettings.CreateDateTimeWithSubMilliseconds = false; try { int seed = 1; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); JsonArray sourceJson = new JsonArray(new JsonValue[] { PrimitiveCreator.CreateInstanceOfBoolean(rndGen), PrimitiveCreator.CreateInstanceOfByte(rndGen), PrimitiveCreator.CreateInstanceOfDateTime(rndGen), PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen), PrimitiveCreator.CreateInstanceOfDecimal(rndGen), PrimitiveCreator.CreateInstanceOfDouble(rndGen), PrimitiveCreator.CreateInstanceOfInt16(rndGen), PrimitiveCreator.CreateInstanceOfInt32(rndGen), PrimitiveCreator.CreateInstanceOfInt64(rndGen), PrimitiveCreator.CreateInstanceOfSByte(rndGen), PrimitiveCreator.CreateInstanceOfSingle(rndGen), PrimitiveCreator.CreateInstanceOfString(rndGen), PrimitiveCreator.CreateInstanceOfUInt16(rndGen), PrimitiveCreator.CreateInstanceOfUInt32(rndGen), PrimitiveCreator.CreateInstanceOfUInt64(rndGen) }); JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString()); Log.Info("Original JsonArray object is: {0}", sourceJson); Log.Info("Round-tripped JsonArray object is: {0}", newJson); if (!JsonValueVerifier.Compare(sourceJson, newJson)) { Assert.Fail("Test failed! The new JsonValue does not equal to the original one."); } } finally { CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue; } }
public void MixedJsonTypeFunctionalTest() { bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds; CreatorSettings.CreateDateTimeWithSubMilliseconds = false; try { int seed = 1; for (int i = 0; i < iterationCount; i++) { seed++; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); JsonArray sourceJson = new JsonArray(new List <JsonValue>() { PrimitiveCreator.CreateInstanceOfBoolean(rndGen), PrimitiveCreator.CreateInstanceOfByte(rndGen), PrimitiveCreator.CreateInstanceOfDateTime(rndGen), PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen), PrimitiveCreator.CreateInstanceOfDecimal(rndGen), PrimitiveCreator.CreateInstanceOfDouble(rndGen), PrimitiveCreator.CreateInstanceOfInt16(rndGen), PrimitiveCreator.CreateInstanceOfInt32(rndGen), PrimitiveCreator.CreateInstanceOfInt64(rndGen), PrimitiveCreator.CreateInstanceOfSByte(rndGen), PrimitiveCreator.CreateInstanceOfSingle(rndGen), PrimitiveCreator.CreateInstanceOfString(rndGen), PrimitiveCreator.CreateInstanceOfUInt16(rndGen), PrimitiveCreator.CreateInstanceOfUInt32(rndGen), PrimitiveCreator.CreateInstanceOfUInt64(rndGen), new JsonObject(new Dictionary <string, JsonValue>() { { "Boolean", PrimitiveCreator.CreateInstanceOfBoolean(rndGen) }, { "Byte", PrimitiveCreator.CreateInstanceOfByte(rndGen) }, { "DateTime", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }, { "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) }, { "Decimal", PrimitiveCreator.CreateInstanceOfDecimal(rndGen) }, { "Double", PrimitiveCreator.CreateInstanceOfDouble(rndGen) }, { "Int16", PrimitiveCreator.CreateInstanceOfInt16(rndGen) }, { "Int32", PrimitiveCreator.CreateInstanceOfInt32(rndGen) }, { "Int64", PrimitiveCreator.CreateInstanceOfInt64(rndGen) }, { "SByte", PrimitiveCreator.CreateInstanceOfSByte(rndGen) }, { "Single", PrimitiveCreator.CreateInstanceOfSingle(rndGen) }, { "String", PrimitiveCreator.CreateInstanceOfString(rndGen) }, { "UInt16", PrimitiveCreator.CreateInstanceOfUInt16(rndGen) }, { "UInt32", PrimitiveCreator.CreateInstanceOfUInt32(rndGen) }, { "UInt64", PrimitiveCreator.CreateInstanceOfUInt64(rndGen) } }) }); JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString()); Assert.True(JsonValueVerifier.Compare(sourceJson, newJson)); } } finally { CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue; } }