Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Microsoft.ML.Core/Utilities/DoubleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public static Result Parse(ReadOnlySpan<char> span, out Single value, OptionFlag
value = default(Single);
return Result.Error;
}
ichEnd += ich;

// Make sure everything was consumed.
while (ichEnd < span.Length)
Expand Down Expand Up @@ -197,6 +198,7 @@ public static Result Parse(ReadOnlySpan<char> span, out Double value, OptionFlag
value = default(Double);
return Result.Error;
}
ichEnd += ich;

// Make sure everything was consumed.
while (ichEnd < span.Length)
Expand Down
55 changes: 55 additions & 0 deletions test/Microsoft.ML.Core.Tests/UnitTests/DoubleParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.TestFramework;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.ML.Core.Tests.UnitTests
{
public class DoubleParserTests : BaseTestClass
{
public DoubleParserTests(ITestOutputHelper output)
: base(output)
{
}

[Fact]
public void Parse_WithLeadingAndTrailingWhitespace_ReturnsGood()
{
var result = DoubleParser.Parse(" 1.234 ".AsSpan(), out double value);

Assert.Equal(DoubleParser.Result.Good, result);
Assert.Equal(1.234, value);
}

[Fact]
public void Parse_WithoutWhitespace_ReturnsGood()
{
var result = DoubleParser.Parse("1.234".AsSpan(), out double value);

Assert.Equal(DoubleParser.Result.Good, result);
Assert.Equal(1.234, value);
}

[Fact]
public void Parse_WithTrailingGarbageCharacter_ReturnsExtra()
{
var result = DoubleParser.Parse("1.234x".AsSpan(), out double value);

Assert.Equal(DoubleParser.Result.Extra, result);
}

[Fact]
public void Parse_Single_WithLeadingAndTrailingWhitespace_ReturnsGood()
{
var result = DoubleParser.Parse(" 1.234 ".AsSpan(), out float value);

Assert.Equal(DoubleParser.Result.Good, result);
Assert.Equal(1.234f, value);
}
}
}
Loading