Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix the sgen issue when use NaN as the default value. (#23366)
Browse files Browse the repository at this point in the history
* Fix the codegen issue when use NaN as the default value. Move the NaN test from runtimeonly to XmlTest so it will be included in Sgen test run.

* Add brackets.
  • Loading branch information
huanwu authored and shmao committed Jan 2, 2018
1 parent da72d8a commit 97076d8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,16 @@ private void WriteValue(object value)
else if (type == typeof(Int32))
Writer.Write(((Int32)value).ToString(null, NumberFormatInfo.InvariantInfo));
else if (type == typeof(Double))
Writer.Write(((Double)value).ToString("R", NumberFormatInfo.InvariantInfo));
{
if (double.IsNaN((Double)value))
{
Writer.Write("double.NaN");
}
else
{
Writer.Write(((Double)value).ToString("R", NumberFormatInfo.InvariantInfo));
}
}
else if (type == typeof(Boolean))
Writer.Write((bool)value ? "true" : "false");
else if ((type == typeof(Int16)) || (type == typeof(Int64)) || (type == typeof(UInt16)) || (type == typeof(UInt32)) || (type == typeof(UInt64)) || (type == typeof(Byte)) || (type == typeof(SByte)))
Expand All @@ -2058,8 +2067,15 @@ private void WriteValue(object value)
}
else if (type == typeof(Single))
{
Writer.Write(((Single)value).ToString("R", NumberFormatInfo.InvariantInfo));
Writer.Write("f");
if (Single.IsNaN((Single)value))
{
Writer.Write("System.Single.NaN");
}
else
{
Writer.Write(((Single)value).ToString("R", NumberFormatInfo.InvariantInfo));
Writer.Write("f");
}
}
else if (type == typeof(Decimal))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2036,22 +2036,6 @@ public static void XmlMembersMapping_CompositeType()
Assert.Equal(requestBodyValue.composite.StringValue, requestBodyActual.composite.StringValue);
}

[Fact]
public static void Xml_DefaultValueAttributeSetToNaNTest()
{
var value = new DefaultValuesSetToNaN();
var actual = SerializeAndDeserialize(value,
@"<?xml version=""1.0""?>
<DefaultValuesSetToNaN xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<DoubleField>0</DoubleField>
<SingleField>0</SingleField>
<DoubleProp>0</DoubleProp>
<FloatProp>0</FloatProp>
</DefaultValuesSetToNaN>");
Assert.NotNull(actual);
Assert.Equal(value, actual);
}

[Fact]
public static void XmlMembersMapping_SimpleType_HasWrapperElement()
{
Expand Down
16 changes: 16 additions & 0 deletions src/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,22 @@ public static void Xml_Soap_TypeWithMyCollectionField()
Assert.True(value.Collection.SequenceEqual(actual.Collection));
}

[Fact]
public static void Xml_DefaultValueAttributeSetToNaNTest()
{
var value = new DefaultValuesSetToNaN();
var actual = SerializeAndDeserialize(value,
@"<?xml version=""1.0""?>
<DefaultValuesSetToNaN xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<DoubleField>0</DoubleField>
<SingleField>0</SingleField>
<DoubleProp>0</DoubleProp>
<FloatProp>0</FloatProp>
</DefaultValuesSetToNaN>");
Assert.NotNull(actual);
Assert.Equal(value, actual);
}

private static readonly string s_defaultNs = "http://tempuri.org/";
private static T RoundTripWithXmlMembersMapping<T>(object requestBodyValue, string memberName, string baseline, bool skipStringCompare = false, string wrapperName = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3553,35 +3553,6 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
}
}

public class DefaultValuesSetToNaN
{
[DefaultValue(double.NaN)]
public double DoubleProp { get; set; }

[DefaultValue(float.NaN)]
public float FloatProp { get; set; }

[DefaultValue(Double.NaN)]
public Double DoubleField;

[DefaultValue(Single.NaN)]
public Single SingleField;

public override bool Equals(object obj)
{
var other = obj as DefaultValuesSetToNaN;
return other == null ? false :
other.DoubleProp == this.DoubleProp && other.FloatProp == this.FloatProp &&
other.DoubleField == this.DoubleField && other.SingleField == this.SingleField;
}

public override int GetHashCode()
{
return this.DoubleProp.GetHashCode() ^ this.FloatProp.GetHashCode() ^
this.DoubleField.GetHashCode() ^ this.SingleField.GetHashCode();
}
}

public class JsonTypes
{
public Dictionary<string, string> StringKeyValue
Expand Down
29 changes: 29 additions & 0 deletions src/System.Runtime.Serialization.Xml/tests/SerializationTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,33 @@ public class TypeWithVirtualGenericProperty<T>
public class TypeWithVirtualGenericPropertyDerived<T> : TypeWithVirtualGenericProperty<T>
{
public override T Value { get; set; }
}

public class DefaultValuesSetToNaN
{
[DefaultValue(double.NaN)]
public double DoubleProp { get; set; }

[DefaultValue(float.NaN)]
public float FloatProp { get; set; }

[DefaultValue(Double.NaN)]
public Double DoubleField;

[DefaultValue(Single.NaN)]
public Single SingleField;

public override bool Equals(object obj)
{
var other = obj as DefaultValuesSetToNaN;
return other == null ? false :
other.DoubleProp == this.DoubleProp && other.FloatProp == this.FloatProp &&
other.DoubleField == this.DoubleField && other.SingleField == this.SingleField;
}

public override int GetHashCode()
{
return this.DoubleProp.GetHashCode() ^ this.FloatProp.GetHashCode() ^
this.DoubleField.GetHashCode() ^ this.SingleField.GetHashCode();
}
}

0 comments on commit 97076d8

Please sign in to comment.