Skip to content

Commit

Permalink
Fixed bug in parsing decimal values (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasherceg authored Apr 29, 2024
1 parent 2953313 commit 62a3b5c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/BinaryFormatDataStructure/BinaryFormatterReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -509,7 +510,7 @@ private object ReadArray(ArraySinglePrimitiveRecord record)

for (int i = 0; i < result.Length; i++)
{
result[i] = _reader.ReadDecimal();
result[i] = decimal.Parse(_reader.ReadString(), CultureInfo.InvariantCulture);
}

return result;
Expand Down
3 changes: 2 additions & 1 deletion src/BinaryFormatDataStructure/PrimitiveReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -61,7 +62,7 @@ public static object Read(PrimitiveType type, BinaryReader reader)
return reader.ReadUInt64();

case PrimitiveType.Decimal:
return reader.ReadDecimal();
return decimal.Parse(reader.ReadString(), CultureInfo.InvariantCulture);

case PrimitiveType.TimeSpan:
return ReadTimeSpan(reader);
Expand Down
10 changes: 9 additions & 1 deletion src/Tests/CustomClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestCustomClass()
BinaryObject objectResult = (BinaryObject)result;
Assert.AreEqual("Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", objectResult.AssemblyName);
Assert.AreEqual("BinaryFormatDataStructureTests.CustomClassTests+TestModel", objectResult.TypeName);
Assert.AreEqual(2, objectResult.Keys.Count());
Assert.AreEqual(3, objectResult.Keys.Count());

var enumer = objectResult.GetEnumerator();
enumer.MoveNext();
Expand All @@ -41,6 +41,12 @@ public void TestCustomClass()
Assert.AreEqual("_two", current.Key);
Assert.IsInstanceOfType(current.Value, typeof(bool));
Assert.AreEqual(true, (bool)current.Value);

enumer.MoveNext();
current = enumer.Current;
Assert.AreEqual("_three", current.Key);
Assert.IsInstanceOfType(current.Value, typeof(decimal));
Assert.AreEqual(decimal.MaxValue, (decimal)current.Value);
}

[TestMethod]
Expand Down Expand Up @@ -99,6 +105,8 @@ private class TestModel
#pragma warning restore IDE0044 // Add readonly modifier

public decimal Three { get { return 0; } set { } }

private decimal _three = decimal.MaxValue;
}

private enum TestEnum
Expand Down
20 changes: 20 additions & 0 deletions src/Tests/PrimitiveArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public void TestIntArray()
Assert.AreEqual(3, arrayResult[2]);
}

[TestMethod]
public void TestDecimalArray()
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();

formatter.Serialize(ms, new decimal[] { 1.1m, 2.2m, 3.3m });
ms.Position = 0;

object result = NRBFReader.ReadStream(ms);

Assert.IsInstanceOfType(result, typeof(decimal[]));

decimal[] arrayResult = (decimal[])result;
Assert.AreEqual(3, arrayResult.Length);
Assert.AreEqual(1.1m, arrayResult[0]);
Assert.AreEqual(2.2m, arrayResult[1]);
Assert.AreEqual(3.3m, arrayResult[2]);
}

[TestMethod]
public void TestTimeSpanArray()
{
Expand Down

0 comments on commit 62a3b5c

Please sign in to comment.