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
10 changes: 2 additions & 8 deletions lang/csharp/src/apache/main/AvroDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,20 +1140,14 @@ public int CompareTo(AvroDecimal other)
var unscaledValueCompare = UnscaledValue.CompareTo(other.UnscaledValue);
var scaleCompare = Scale.CompareTo(other.Scale);

// if both are the same value, return the value
if (unscaledValueCompare == scaleCompare)
{
return unscaledValueCompare;
}

// if the scales are both the same return unscaled value
if (scaleCompare == 0)
{
return unscaledValueCompare;
}

var scaledValue = BigInteger.Divide(UnscaledValue, BigInteger.Pow(new BigInteger(10), Scale));
var otherScaledValue = BigInteger.Divide(other.UnscaledValue, BigInteger.Pow(new BigInteger(10), other.Scale));
var scaledValue = (decimal) UnscaledValue / (decimal) Math.Pow(10, Scale);
var otherScaledValue = (decimal) other.UnscaledValue / (decimal) Math.Pow(10, other.Scale);

return scaledValue.CompareTo(otherScaledValue);
}
Expand Down
56 changes: 42 additions & 14 deletions lang/csharp/src/apache/test/AvroDecimalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@
* limitations under the License.
*/

using System.Globalization;
using NUnit.Framework;

namespace Avro.test
{
[TestFixture]
class AvroDecimalTest
{
[TestCase(1)]
[TestCase(1000)]
[TestCase(10.10)]
[TestCase(0)]
[TestCase(0.1)]
[TestCase(0.01)]
[TestCase(-1)]
[TestCase(-1000)]
[TestCase(-10.10)]
[TestCase(-0.1)]
[TestCase(-0.01)]
public void TestAvroDecimalToString(decimal value)
//Use strings as parameters as otherwise doubles will be used intermediately by C# and scale will be lost in this process
Comment thread
ChristophMajcenAtXxxlutz marked this conversation as resolved.
[TestCase("1")]
[TestCase("1000")]
[TestCase("10.10")]
[TestCase("0")]
[TestCase("0.1")]
[TestCase("0.01")]
[TestCase("-1")]
[TestCase("-1000")]
[TestCase("-10.10")]
[TestCase("-0.1")]
[TestCase("-0.01")]
public void TestAvroDecimalToString(string value)
{
var valueString = value.ToString();
var valueDecimal = decimal.Parse(value, CultureInfo.InvariantCulture);
var valueString = valueDecimal.ToString();

var avroDecimal = new AvroDecimal(value);
var avroDecimal = new AvroDecimal(valueDecimal);
var avroDecimalString = avroDecimal.ToString();

Assert.AreEqual(valueString, avroDecimalString);
Expand All @@ -63,5 +66,30 @@ public void TestHighPrecisionAvroDecimalToString()

Assert.AreEqual(valueString, avroDecimalString);
}

//Use strings as parameters as otherwise doubles will be used intermediately by C# and scale will be lost in this process
[TestCase("0", "0", ExpectedResult = 0)]
[TestCase("1", "0", ExpectedResult = 1)]
[TestCase("0", "1", ExpectedResult = -1)]
[TestCase("1.0", "1.0", ExpectedResult = 0)]
[TestCase("1.0", "1", ExpectedResult = 0)]
[TestCase("1", "1.0", ExpectedResult = 0)]
[TestCase("1.0", "0", ExpectedResult = 1)]
[TestCase("0", "1.0", ExpectedResult = -1)]
[TestCase("-0.5", "-1.0", ExpectedResult = 1)]
[TestCase("-1.0", "-0.5", ExpectedResult = -1)]
[TestCase("0.1", "0.01", ExpectedResult = 1)]
[TestCase("0.01", "0.1", ExpectedResult = -1)]
[TestCase("-0.1", "-0.01", ExpectedResult = -1)]
[TestCase("-0.01", "-0.1", ExpectedResult = 1)]
public int TestAvroDecimalCompareTo(string left, string right)
{
var leftDecimal = decimal.Parse(left, CultureInfo.InvariantCulture);
var rightDecimal = decimal.Parse(right, CultureInfo.InvariantCulture);
var leftAvroDecimal = new AvroDecimal(leftDecimal);
var rightAvroDecimal = new AvroDecimal(rightDecimal);

return leftAvroDecimal.CompareTo(rightAvroDecimal);
}
}
}
Loading