StefH / XPath2.Net

Lightweight XPath2 for .NET
Microsoft Public License
36 stars 14 forks source link

Fix Round() #26

Closed StefH closed 5 years ago

Lej commented 5 years ago

This seems to fix round for the 2.5 case, but round still fails for negative numbers when MidpointRounding.ToEven causes rounding towards negative infinity.

Below expanded unit test where the case [InlineData("-3.5", -3)] fails (it is rounded to -4 when it should be rounded to -3). Note that I changed string xpath = "round(number(value))"; to string xpath = "round(xs:double(value))"; to test some edge cases.

        // https://www.w3.org/TR/xpath-functions/#func-round
        [Theory]
        [InlineData("2.51", 3)]
        [InlineData("2.5", 3)]
        [InlineData("2.4999", 2)]
        [InlineData("0", 0)]
        [InlineData("-0", 0)]
        [InlineData("-2.4999", -2)]
        [InlineData("-2.5", -2)]
        [InlineData("-2.51", -3)]
        [InlineData("-3.5", -3)]
        [InlineData("INF", double.PositiveInfinity)]
        [InlineData("-INF", double.NegativeInfinity)]
        [InlineData("NaN", double.NaN)]
        public void XPath2Evaluate_round(string value, object expected)
        {
            // Arrange
            string xpath = "round(xs:double(value))";
            var xml = new XmlDocument { InnerXml = $"<value>{value}</value>" };
            var nav = xml.CreateNavigator();

            // Act
            var result = nav.XPath2Evaluate(xpath);

            // Assert
            result.Should().Be(expected);
        }
StefH commented 5 years ago

@Lej Thanks for your review.

Code has been changed. Please check again.

Lej commented 5 years ago

@Lej Thanks for your review.

Code has been changed. Please check again.

Looks like it should work now. Had a small comment, not sure if relevant.