Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jul 6, 2017
2 parents b4cabed + 7c2021c commit 3cc8130
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class MathHelpersTests : TestFixtureBase
[Test]
public void IsDoubleMultipleTests()
{
Assert.AreEqual(true, MathHelpers.IsDoubleMultiple(5555555555555555555555555555.01d, 0.01));
Assert.AreEqual(false, MathHelpers.IsDoubleMultiple(5555555555555555555555555555.01d, 0.013453));
Assert.AreEqual(true, MathHelpers.IsDoubleMultiple(555555555555555555555555555.01m, 0.01));

Assert.AreEqual(true, MathHelpers.IsDoubleMultiple(3199.981, 0.001));
Assert.AreEqual(true, MathHelpers.IsDoubleMultiple(3199.980, 0.001));
Assert.AreEqual(true, MathHelpers.IsDoubleMultiple(540.1, 0.001));
Expand Down
177 changes: 177 additions & 0 deletions Src/Newtonsoft.Json.Schema.Tests/Issues/Issue94Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#region License
// Copyright (c) Newtonsoft. All Rights Reserved.
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema.Generation;
using Newtonsoft.Json.Schema.Infrastructure;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Schema.Tests.Issues
{
[TestFixture]
public class Issue94Tests : TestFixtureBase
{
[Test]
public void Test()
{
JSchema s = JSchema.Parse(Schema);

JObject o = JObject.Parse(Json);

Assert.AreEqual(true, o.IsValid(s));
}

public const string Schema = @"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""type"": ""object"",
""properties"": {
""lifeEventID"": {
""type"": ""string"",
""pattern"": ""^[0-9]{10}$""
},
""transactionType"": {
""type"": ""string"",
""enum"": [
""Bonus"",
""Penalty""
]
},
""bonusDetail"": {
""type"": ""object"",
""properties"": {
""duePeriodAmount"": {
""$ref"": ""#/definitions/decimalValue""
},
""dueYTD"": {
""$ref"": ""#/definitions/decimalValue""
},
""paidYTD"": {
""$ref"": ""#/definitions/decimalValue""
}
},
""required"": [
""duePeriodAmount"",
""dueYTD""
],
""additionalProperties"": false
},
""claimDetail"": {
""type"": ""object"",
""properties"": {
""startPeriod"": {
""$ref"": ""#/definitions/dateString""
},
""endPeriod"": {
""$ref"": ""#/definitions/dateString""
},
""reason"": {
""type"": ""string"",
""enum"": [
""Life Event"",
""Regular Bonus""
]
}
},
""required"": [
""startPeriod"",
""endPeriod"",
""reason""
],
""additionalProperties"": false
},
""transferDetail"": {
""type"": ""object"",
""properties"": {
""inAmountForPeriod"": {
""$ref"": ""#/definitions/decimalValue""
},
""amountYTD"": {
""$ref"": ""#/definitions/decimalValue""
}
},
""additionalProperties"": false
},
""paymentsDetail"": {
""type"": ""object"",
""properties"": {
""newSubsForPeriod"": {
""$ref"": ""#/definitions/decimalValue""
},
""newSubsYTD"": {
""$ref"": ""#/definitions/decimalValue""
},
""totalSubsForPeriod"": {
""$ref"": ""#/definitions/decimalValue""
},
""totalSubsYTD"": {
""$ref"": ""#/definitions/decimalValue""
}
},
""required"": [
""newSubsYTD"",
""totalSubsForPeriod"",
""totalSubsYTD""
],
""additionalProperties"": false
}
},
""required"": [
""transactionType"",
""bonusDetail"",
""claimDetail"",
""paymentsDetail""
],
""additionalProperties"": false,
""definitions"": {
""dateString"": {
""type"": ""string"",
""pattern"": ""^[2][0][0-9][0-9][-][0-1][0-9][-][0-3][0-9]$"",
""description"": ""YYYY-MM-DD""
},
""decimalValue"": {
""type"": ""number"",
""multipleOf"": 0.01
}
}
}";

public const string Json = @"{
""lifeEventID"": ""1234567891"",
""transactionType"": ""Bonus"",
""bonusDetail"": {
""duePeriodAmount"": 5555555555555555555555555555.01,
""dueYTD"": 40000,
""paidYTD"": 40000
},
""claimDetail"": {
""startPeriod"": ""2017-04-06"",
""endPeriod"": ""2017-04-06"",
""reason"": ""Life Event""
},
""transferDetail"": {
""inAmountForPeriod"": 40000,
""amountYTD"": 40000
},
""paymentsDetail"": {
""newSubsForPeriod"": 4000,
""newSubsYTD"": 4000,
""totalSubsForPeriod"": 4000,
""totalSubsYTD"": 4000
}
}";
}
}
48 changes: 48 additions & 0 deletions Src/Newtonsoft.Json.Schema.Tests/Issues/Issue95Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#region License
// Copyright (c) Newtonsoft. All Rights Reserved.
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema.Generation;
using Newtonsoft.Json.Schema.Infrastructure;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Schema.Tests.Issues
{
[TestFixture]
public class Issue95Tests : TestFixtureBase
{
[Test]
public void Test()
{
JSchema s = new JSchema();
s.Minimum = 1;
s.Maximum = 1000;

StringAssert.AreEqual(@"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""minimum"": 1.0,
""maximum"": 1000.0
}", s.ToString(SchemaVersion.Draft4));

StringAssert.AreEqual(@"{
""$schema"": ""http://json-schema.org/draft-06/schema#"",
""minimum"": 1.0,
""maximum"": 1000.0
}", s.ToString(SchemaVersion.Draft6));
}
}
}
2 changes: 1 addition & 1 deletion Src/Newtonsoft.Json.Schema/Infrastructure/JSchemaWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private void WriteSchemaObjectInternal(JSchema schema)
if (EnsureVersion(SchemaVersion.Draft6))
{
WritePropertyIfNotNull(_writer, schema.ExclusiveMinimum ? Constants.PropertyNames.ExclusiveMinimum : Constants.PropertyNames.Minimum, schema.Minimum);
WritePropertyIfNotNull(_writer, schema.ExclusiveMaximum ? Constants.PropertyNames.ExclusiveMaximum : Constants.PropertyNames.Minimum, schema.Maximum);
WritePropertyIfNotNull(_writer, schema.ExclusiveMaximum ? Constants.PropertyNames.ExclusiveMaximum : Constants.PropertyNames.Maximum, schema.Maximum);
}
else
{
Expand Down
19 changes: 18 additions & 1 deletion Src/Newtonsoft.Json.Schema/Infrastructure/MathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,24 @@ public static bool IsDoubleMultiple(object value, double multipleOf)

if ((FitsInDecimal(d) || value is decimal) && FitsInDecimal(multipleOf))
{
return IsDoubleMultiple(Convert.ToDecimal(value, CultureInfo.InvariantCulture), Convert.ToDecimal(multipleOf));
decimal multipleOfD = Convert.ToDecimal(multipleOf);
decimal valueD = Convert.ToDecimal(value, CultureInfo.InvariantCulture);

// check if value divided by multipleOf is too big for decimal - the modulus will error if it is
if (!FitsInDecimal(d / multipleOf))
{
// check if remainder is between 0 and 1 and multipleOf fits into 0
// remove whole numbers from value to avoid overflow error
if (Math.Abs(multipleOfD) < 1 && 1 % multipleOfD == 0)
{
valueD = valueD % 1;
return IsDoubleMultiple(valueD, multipleOfD);
}
}
else
{
return IsDoubleMultiple(valueD, multipleOfD);
}
}

double remainder = d % multipleOf;
Expand Down

0 comments on commit 3cc8130

Please sign in to comment.