-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 7.7] Fix bug in IsLong implementation (#4658)
Relates: #4649 This commit fixes a bug in the IsLong implementation, and prefers reading long values over double values in PrimitiveObjectFormatter when the bytes represent a valid long value, to avoid floating point rounding errors that may happen when parsing a double from a long value. The bug in IsLong was that the loop over the array segment bytes was exiting early when the count of bytes was the same as long.Min/MaxValue length, and the current loop byte is less than the byte at index in long.Min/MaxValue. In this scenario, it should not exit early, but not continue to check following loop bytes against byte at index in long.Min/MaxValue. Following loop iterations still need to check whether remaining bytes are numbers. Co-authored-by: Russ Cam <russ.cam@elastic.co>
- Loading branch information
1 parent
9a9141b
commit d84c0c6
Showing
3 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/Tests/CodeStandards/Serialization/PrimitiveObjectFormatterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Text; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using Elasticsearch.Net; | ||
using FluentAssertions; | ||
using Tests.Core.Client; | ||
|
||
namespace Tests.CodeStandards.Serialization | ||
{ | ||
public class PrimitiveObjectFormatterTests | ||
{ | ||
private static DynamicValue GetValue(string json) | ||
{ | ||
var bytes = Encoding.UTF8.GetBytes(json); | ||
var client = TestClient.FixedInMemoryClient(bytes); | ||
var response = client.LowLevel.Search<DynamicResponse>(PostData.Empty); | ||
return response.Body["value"]; | ||
} | ||
|
||
[U] | ||
public void ReadDoubleWhenContainsDecimalPoint() | ||
{ | ||
var value = GetValue(@"{""value"":0.43066659210472646}"); | ||
value.Value.GetType().Should().Be<double>(); | ||
value.Value.Should().Be(0.43066659210472646); | ||
} | ||
|
||
[U] | ||
public void ReadDoubleWhenContainsENotation() | ||
{ | ||
var value = GetValue(@"{""value"":1.7976931348623157E+308}"); | ||
value.Value.GetType().Should().Be<double>(); | ||
value.Value.Should().Be(double.MaxValue); | ||
} | ||
|
||
[U] | ||
public void ReadDoubleWhenGreaterThanLongMaxValue() | ||
{ | ||
var value = GetValue($"{{\"value\":{((double)long.MaxValue) + 1}}}"); | ||
value.Value.GetType().Should().Be<double>(); | ||
value.Value.Should().Be(((double)long.MaxValue) + 1); | ||
} | ||
|
||
[U] | ||
public void ReadDoubleWhenLessThanLongMinValue() | ||
{ | ||
var value = GetValue($"{{\"value\":{((double)long.MinValue) - 1}}}"); | ||
value.Value.GetType().Should().Be<double>(); | ||
value.Value.Should().Be(((double)long.MinValue) - 1); | ||
} | ||
|
||
[U] | ||
public void ReadLongWhenLongMaxValue() | ||
{ | ||
var value = GetValue($"{{\"value\":{long.MaxValue}}}"); | ||
value.Value.GetType().Should().Be<long>(); | ||
value.Value.Should().Be(long.MaxValue); | ||
} | ||
|
||
[U] | ||
public void ReadLongWhenLessThanLongMaxValue() | ||
{ | ||
var value = GetValue($"{{\"value\":{long.MaxValue - 1}}}"); | ||
value.Value.GetType().Should().Be<long>(); | ||
value.Value.Should().Be(long.MaxValue - 1); | ||
} | ||
|
||
[U] | ||
public void ReadLongWhenGreaterThanLongMinValue() | ||
{ | ||
var value = GetValue($"{{\"value\":{long.MinValue + 1}}}"); | ||
value.Value.GetType().Should().Be<long>(); | ||
value.Value.Should().Be(long.MinValue + 1); | ||
} | ||
|
||
[U] | ||
public void ReadLongWhenLongMinValue() | ||
{ | ||
var value = GetValue($"{{\"value\":{long.MinValue}}}"); | ||
value.Value.GetType().Should().Be<long>(); | ||
value.Value.Should().Be(long.MinValue); | ||
} | ||
} | ||
} | ||
|