Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Jun 30, 2021
1 parent c91ab5c commit a2268b5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
34 changes: 31 additions & 3 deletions CBORTest/CBORObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8731,7 +8731,8 @@ public static void TestParseNumberFxx(
int f32,
long f64,
string line) {
if (str[0] == '.') {
if (str[0] == '.' || str[str.Length - 1] =='.' ||
str.Contains(".e") || str.Contains(".E")) {
// Not a valid JSON number, so skip
// Console.WriteLine(str);
return;
Expand Down Expand Up @@ -8764,6 +8765,35 @@ public static void TestParseNumberFxx(
// TODO: Test f16
}

[Test]
public void TestCloseToPowerOfTwo() {
for (var i = 31; i < 129; ++i) {
EInteger ei = EInteger.FromInt32(1).ShiftLeft(i);
{
AssertJSONDouble(
ei.ToString(),
"double",
EFloat.FromEInteger(ei).ToDouble());
AssertJSONDouble(
ei.Add(1).ToString(),
"double",
EFloat.FromEInteger(ei.Add(1)).ToDouble());
AssertJSONDouble(
ei.Subtract(2).ToString(),
"double",
EFloat.FromEInteger(ei.Subtract(2)).ToDouble());
AssertJSONDouble(
ei.Add(2).ToString(),
"double",
EFloat.FromEInteger(ei.Add(2)).ToDouble());
AssertJSONDouble(
ei.Subtract(2).ToString(),
"double",
EFloat.FromEInteger(ei.Subtract(2)).ToDouble());
}
}
}

[Test]
public void TestFromJsonStringFastCases() {
var op = new JSONOptions("numberconversion=double");
Expand All @@ -8774,8 +8804,6 @@ public void TestFromJsonStringFastCases() {
Assert.AreEqual(
JSONOptions.ConversionMode.IntOrFloat,
op.NumberConversion);
// var sw = new System.Diagnostics.Stopwatch();
// sw.Start();
string manyzeros = TestCommon.Repeat("0", 1000000);
string manythrees = TestCommon.Repeat("3", 1000000);
AssertJSONDouble(
Expand Down
16 changes: 16 additions & 0 deletions CBORTest/CBORTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,22 @@ public void TestTag268() {
}
}

[Test]
public void TestRoundTripNaN() {
long doublennan = unchecked((long)0xfff8000000000000L);
long doublepnan = unchecked((long)0x7ff8000000000000L);
int singlennan = unchecked((int)0xffc00000);
int singlepnan = unchecked((int)0x7fc00000);
int halfnnan = 0xfe00;
int halfpnan = 0x7e00;
Assert.AreEqual(doublennan, CBORObject.FromFloatingPointBits(doublennan,8).AsDoubleBits());
Assert.AreEqual(doublepnan, CBORObject.FromFloatingPointBits(doublepnan,8).AsDoubleBits());
Assert.AreEqual(doublennan, CBORObject.FromFloatingPointBits(singlennan,4).AsDoubleBits());
Assert.AreEqual(doublepnan, CBORObject.FromFloatingPointBits(singlepnan,4).AsDoubleBits());
Assert.AreEqual(doublennan, CBORObject.FromFloatingPointBits(halfnnan,2).AsDoubleBits());
Assert.AreEqual(doublepnan, CBORObject.FromFloatingPointBits(halfpnan,2).AsDoubleBits());
}

[Test]
public void TestPlist() {
CBORObject o;
Expand Down

0 comments on commit a2268b5

Please sign in to comment.