From aa0fa7fc96877d7ee6673f202a012c1839fa3eee Mon Sep 17 00:00:00 2001 From: Peter O Date: Tue, 27 Apr 2021 04:57:52 -0400 Subject: [PATCH] Version 4.4.1 --- CBOR.nuspec | 8 +- CBOR/PeterO/Cbor/CBORJson2.cs | 11 +- CBOR20/Properties/AssemblyInfo.cs | 6 +- CBOR40/Properties/AssemblyInfo.cs | 6 +- CBORTest/CBORObjectTest.cs | 691 +++++++++++++++--------------- CBORTest/CBORTest.cs | 68 ++- 6 files changed, 407 insertions(+), 383 deletions(-) diff --git a/CBOR.nuspec b/CBOR.nuspec index 8297e63f..0e1e4b21 100644 --- a/CBOR.nuspec +++ b/CBOR.nuspec @@ -1,5 +1,9 @@ 4.4.0PeterO.CborfalseVersion 4.4: +>4.4.1PeterO.CborfalseVersion 4.4.1: + +- Fix bugs when parsing JSON with the JSON option 'numberconversion=double' + +Version 4.4: - Boolean constructors of PODOptions and CBOREncodeOptions were obsolete - Float64 option of CBOREncodeOptions for encoding floating-point values as 64-bit only @@ -7,4 +11,4 @@ date/time formats and CBOR objects - Added CanFitInUInt64 and CanTruncatedIntFitInUInt64 methods - Bug fixesCC0-1.0https://github.com/peteroupc/CBORPeter OccilA C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949.Peter OccilCBOR (Concise Binary Object Representation)cbor data serialization binary json +> \ No newline at end of file diff --git a/CBOR/PeterO/Cbor/CBORJson2.cs b/CBOR/PeterO/Cbor/CBORJson2.cs index 69951356..2aeeb58d 100644 --- a/CBOR/PeterO/Cbor/CBORJson2.cs +++ b/CBOR/PeterO/Cbor/CBORJson2.cs @@ -337,7 +337,7 @@ private CBORObject NextJSONNegativeNumber( numberEndIndex - numberStartIndex, this.options); #if DEBUG - if (this.options.NumberConversion == JSONOptions.ConversionMode.Full&& + if (this.options.NumberConversion == JSONOptions.ConversionMode.Full && ( (EDecimal)obj.ToObject( typeof(EDecimal))).CompareToValue(EDecimal.FromString(this.bytes, @@ -440,13 +440,8 @@ private CBORObject NextJSONNonnegativeNumber(int c, int[] nextChar) { numberStartIndex, numberEndIndex - numberStartIndex, this.options); - /* - DebugUtility.Log("ParseJSONNumber -{0}->{1}",EDecimal.FromString(this.bytes, - numberStartIndex, - numberEndIndex - numberStartIndex), obj); - */ #if DEBUG - if (this.options.NumberConversion == JSONOptions.ConversionMode.Full&& + #if DEBUG + if (this.options.NumberConversion == JSONOptions.ConversionMode.Full && ( (EDecimal)obj.ToObject( typeof(EDecimal))).CompareToValue(EDecimal.FromString(this.bytes, diff --git a/CBOR20/Properties/AssemblyInfo.cs b/CBOR20/Properties/AssemblyInfo.cs index 67eff54e..cc561299 100644 --- a/CBOR20/Properties/AssemblyInfo.cs +++ b/CBOR20/Properties/AssemblyInfo.cs @@ -1,8 +1,8 @@ using System.Reflection; [assembly: System.CLSCompliant(true)] -[assembly: AssemblyInformationalVersion("4.4.0")] -[assembly: AssemblyVersion("4.4.0.0")] -[assembly: AssemblyFileVersion("4.4.0.0")] +[assembly: AssemblyInformationalVersion("4.4.1")] +[assembly: AssemblyVersion("4.4.1.0")] +[assembly: AssemblyFileVersion("4.4.1.0")] [assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" + "on)")] [assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" + diff --git a/CBOR40/Properties/AssemblyInfo.cs b/CBOR40/Properties/AssemblyInfo.cs index 67eff54e..cc561299 100644 --- a/CBOR40/Properties/AssemblyInfo.cs +++ b/CBOR40/Properties/AssemblyInfo.cs @@ -1,8 +1,8 @@ using System.Reflection; [assembly: System.CLSCompliant(true)] -[assembly: AssemblyInformationalVersion("4.4.0")] -[assembly: AssemblyVersion("4.4.0.0")] -[assembly: AssemblyFileVersion("4.4.0.0")] +[assembly: AssemblyInformationalVersion("4.4.1")] +[assembly: AssemblyVersion("4.4.1.0")] +[assembly: AssemblyFileVersion("4.4.1.0")] [assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" + "on)")] [assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" + diff --git a/CBORTest/CBORObjectTest.cs b/CBORTest/CBORObjectTest.cs index 8564db57..fe161149 100644 --- a/CBORTest/CBORObjectTest.cs +++ b/CBORTest/CBORObjectTest.cs @@ -2039,71 +2039,71 @@ public void TestReadSequence() { [Test] public void TestEncodeFloat64() { - try { - var rg = new RandomGenerator(); - var options = new CBOREncodeOptions("float64=true"); - for (var i = 0; i < 10000; ++i) { - double dbl = 0.0; - dbl = (i == 0) ? Double.PositiveInfinity : ((i == 1) ? - Double.NegativeInfinity : RandomObjects.RandomDouble(rg)); - CBORObject cbor = CBORObject.FromObject(dbl); - byte[] bytes = cbor.EncodeToBytes(options); - Assert.AreEqual(9, bytes.Length); - TestCommon.AssertEqualsHashCode( - cbor, - CBORObject.DecodeFromBytes(bytes)); - using (var ms = new MemoryStream()) { - cbor.WriteTo(ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(9, bytes.Length); - } - using (var ms = new MemoryStream()) { - CBORObject.Write(dbl, ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(9, bytes.Length); - } - using (var ms = new MemoryStream()) { - CBORObject.Write(cbor, ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(9, bytes.Length); - } - CBORObject cbor2 = CBORObject.NewArray().Add(cbor); - bytes = cbor2.EncodeToBytes(options); - TestCommon.AssertEqualsHashCode( - cbor2, - CBORObject.DecodeFromBytes(bytes)); - Assert.AreEqual(10, bytes.Length); - using (var ms = new MemoryStream()) { - cbor2.WriteTo(ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(10, bytes.Length); - } - using (var ms = new MemoryStream()) { - CBORObject.Write(cbor2, ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(10, bytes.Length); - } - cbor2 = cbor.WithTag(1); - bytes = cbor2.EncodeToBytes(options); - Assert.AreEqual(10, bytes.Length); - TestCommon.AssertEqualsHashCode( - cbor2, - CBORObject.DecodeFromBytes(bytes)); - using (var ms = new MemoryStream()) { - cbor2.WriteTo(ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(10, bytes.Length); - } - using (var ms = new MemoryStream()) { - CBORObject.Write(cbor2, ms, options); - bytes = ms.ToArray(); - Assert.AreEqual(10, bytes.Length); - } - } - } catch (IOException ex) { - Assert.Fail(ex.ToString()); - throw new InvalidOperationException(String.Empty, ex); + try { + var rg = new RandomGenerator(); + var options = new CBOREncodeOptions("float64=true"); + for (var i = 0; i < 10000; ++i) { + double dbl = 0.0; + dbl = (i == 0) ? Double.PositiveInfinity : ((i == 1) ? + Double.NegativeInfinity : RandomObjects.RandomDouble(rg)); + CBORObject cbor = CBORObject.FromObject(dbl); + byte[] bytes = cbor.EncodeToBytes(options); + Assert.AreEqual(9, bytes.Length); + TestCommon.AssertEqualsHashCode( + cbor, + CBORObject.DecodeFromBytes(bytes)); + using (var ms = new MemoryStream()) { + cbor.WriteTo(ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(9, bytes.Length); + } + using (var ms = new MemoryStream()) { + CBORObject.Write(dbl, ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(9, bytes.Length); + } + using (var ms = new MemoryStream()) { + CBORObject.Write(cbor, ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(9, bytes.Length); + } + CBORObject cbor2 = CBORObject.NewArray().Add(cbor); + bytes = cbor2.EncodeToBytes(options); + TestCommon.AssertEqualsHashCode( + cbor2, + CBORObject.DecodeFromBytes(bytes)); + Assert.AreEqual(10, bytes.Length); + using (var ms = new MemoryStream()) { + cbor2.WriteTo(ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(10, bytes.Length); + } + using (var ms = new MemoryStream()) { + CBORObject.Write(cbor2, ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(10, bytes.Length); + } + cbor2 = cbor.WithTag(1); + bytes = cbor2.EncodeToBytes(options); + Assert.AreEqual(10, bytes.Length); + TestCommon.AssertEqualsHashCode( + cbor2, + CBORObject.DecodeFromBytes(bytes)); + using (var ms = new MemoryStream()) { + cbor2.WriteTo(ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(10, bytes.Length); + } + using (var ms = new MemoryStream()) { + CBORObject.Write(cbor2, ms, options); + bytes = ms.ToArray(); + Assert.AreEqual(10, bytes.Length); + } } + } catch (IOException ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); + } } [Test] @@ -4105,9 +4105,9 @@ public void TestReadJSON() { } } using (var ms = new MemoryStream(new byte[] { - 0xef, 0xbb, 0xbf, - 0x7b, 0x7d, - })) { + 0xef, 0xbb, 0xbf, + 0x7b, 0x7d, + })) { try { CBORObject.ReadJSON(ms); } catch (Exception ex) { @@ -4117,9 +4117,9 @@ public void TestReadJSON() { } // whitespace followed by BOM using (var ms2 = new MemoryStream(new byte[] { - 0x20, 0xef, 0xbb, - 0xbf, 0x7b, 0x7d, - })) { + 0x20, 0xef, 0xbb, + 0xbf, 0x7b, 0x7d, + })) { try { CBORObject.ReadJSON(ms2); Assert.Fail("Should have failed"); @@ -4154,9 +4154,9 @@ public void TestReadJSON() { } // two BOMs using (var ms3 = new MemoryStream(new byte[] { - 0xef, 0xbb, 0xbf, - 0xef, 0xbb, 0xbf, 0x7b, 0x7d, - })) { + 0xef, 0xbb, 0xbf, + 0xef, 0xbb, 0xbf, 0x7b, 0x7d, + })) { try { CBORObject.ReadJSON(ms3); Assert.Fail("Should have failed"); @@ -4168,82 +4168,82 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0xfe, 0xff, 0, - 0, - 0, - 0x74, 0, 0, 0, 0x72, 0, 0, 0, 0x75, 0, 0, 0, - 0x65, - })) { + 0, 0, 0xfe, 0xff, 0, + 0, + 0, + 0x74, 0, 0, 0, 0x72, 0, 0, 0, 0x75, 0, 0, 0, + 0x65, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0, 0x74, 0, 0, - 0, 0x72, 0, - 0, 0, 0x75, 0, 0, 0, 0x65, - })) { + 0, 0, 0, 0x74, 0, 0, + 0, 0x72, 0, + 0, 0, 0x75, 0, 0, 0, 0x65, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0, 0, - 0x74, 0, 0, 0, - 0x72, 0, 0, 0, 0x75, 0, 0, 0, 0x65, 0, 0, 0, - })) { + 0xff, 0xfe, 0, 0, + 0x74, 0, 0, 0, + 0x72, 0, 0, 0, 0x75, 0, 0, 0, 0x65, 0, 0, 0, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0x74, 0, 0, 0, 0x72, - 0, - 0, - 0, - 0x75, 0, 0, 0, 0x65, 0, 0, 0, - })) { + 0x74, 0, 0, 0, 0x72, + 0, + 0, + 0, + 0x75, 0, 0, 0, 0x65, 0, 0, 0, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0, 0x74, - 0, 0x72, 0, - 0x75, 0, 0x65, - })) { + 0xfe, 0xff, 0, 0x74, + 0, 0x72, 0, + 0x75, 0, 0x65, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0, 0x74, 0, 0x72, 0, - 0x75, 0, 0x65, - })) { + 0, 0x74, 0, 0x72, 0, + 0x75, 0, 0x65, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x74, 0, - 0x72, - 0, - 0x75, - 0, 0x65, 0, - })) { + 0xff, 0xfe, 0x74, 0, + 0x72, + 0, + 0x75, + 0, 0x65, 0, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0x74, 0, 0x72, 0, - 0x75, 0, 0x65, 0, - })) { + 0x74, 0, 0x72, 0, + 0x75, 0, 0x65, 0, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0xef, 0xbb, 0xbf, - 0x74, 0x72, 0x75, 0x65, - })) { + 0xef, 0xbb, 0xbf, + 0x74, 0x72, 0x75, 0x65, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0x74, 0x72, 0x75, - 0x65, - })) { + 0x74, 0x72, 0x75, + 0x65, + })) { Assert.AreEqual(CBORObject.True, CBORObject.ReadJSON(msjson)); } using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0xfe, 0xff, 0, - 0, 0, 0x22, - 0, 1, 0, 0, 0, 0, 0, 0x22, - })) { + 0, 0, 0xfe, 0xff, 0, + 0, 0, 0x22, + 0, 1, 0, 0, 0, 0, 0, 0x22, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4252,10 +4252,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0, 0x22, 0, 1, - 0, 0, 0, 0, - 0, 0x22, - })) { + 0, 0, 0, 0x22, 0, 1, + 0, 0, 0, 0, + 0, 0x22, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4264,10 +4264,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0, 0, - 0x22, 0, 0, 0, - 0, 0, 1, 0, 0x22, 0, 0, 0, - })) { + 0xff, 0xfe, 0, 0, + 0x22, 0, 0, 0, + 0, 0, 1, 0, 0x22, 0, 0, 0, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4276,11 +4276,11 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0x22, 0, 0, 0, 0, 0, - 1, 0, 0x22, - 0, - 0, 0, - })) { + 0x22, 0, 0, 0, 0, 0, + 1, 0, 0x22, + 0, + 0, 0, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4289,11 +4289,11 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0, - 0x22, 0xd8, - 0, - 0xdc, 0, 0, 0x22, - })) { + 0xfe, 0xff, 0, + 0x22, 0xd8, + 0, + 0xdc, 0, 0, 0x22, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4302,9 +4302,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0, 0x22, 0xd8, 0, - 0xdc, 0, 0, 0x22, - })) { + 0, 0x22, 0xd8, 0, + 0xdc, 0, 0, 0x22, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4313,10 +4313,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x22, 0, - 0, 0xd8, 0, - 0xdc, 0x22, 0, - })) { + 0xff, 0xfe, 0x22, 0, + 0, 0xd8, 0, + 0xdc, 0x22, 0, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4325,9 +4325,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0x22, 0, 0, 0xd8, 0, - 0xdc, 0x22, 0, - })) { + 0x22, 0, 0, 0xd8, 0, + 0xdc, 0x22, 0, + })) { { string stringTemp = CBORObject.ReadJSON(msjson).AsString(); Assert.AreEqual( @@ -4336,10 +4336,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0xfe, 0xff, 0, - 0, 0, 0x22, - 0, 0, 0xd8, 0, 0, 0, 0, 0x22, - })) { + 0, 0, 0xfe, 0xff, 0, + 0, 0, 0x22, + 0, 0, 0xd8, 0, 0, 0, 0, 0x22, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4351,11 +4351,11 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0, 0x22, 0, 0, - 0xd8, 0, 0, - 0, - 0, 0x22, - })) { + 0, 0, 0, 0x22, 0, 0, + 0xd8, 0, 0, + 0, + 0, 0x22, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4367,10 +4367,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0, 0, - 0x22, 0, 0, 0, - 0, 0xd8, 0, 0, 0x22, 0, 0, 0, - })) { + 0xff, 0xfe, 0, 0, + 0x22, 0, 0, 0, + 0, 0xd8, 0, 0, 0x22, 0, 0, 0, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4382,12 +4382,12 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0x22, 0, 0, 0, 0, - 0xd8, - 0, - 0, - 0x22, 0, 0, 0, - })) { + 0x22, 0, 0, 0, 0, + 0xd8, + 0, + 0, + 0x22, 0, 0, 0, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4399,10 +4399,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0, 0x22, - 0, 0xdc, 0, - 0xdc, 0, 0, 0x22, - })) { + 0xfe, 0xff, 0, 0x22, + 0, 0xdc, 0, + 0xdc, 0, 0, 0x22, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4414,10 +4414,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0, 0x22, 0, 0xdc, 0, - 0xdc, 0, 0, - 0x22, - })) { + 0, 0x22, 0, 0xdc, 0, + 0xdc, 0, 0, + 0x22, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4429,10 +4429,10 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x22, 0, - 0, 0xdc, 0, - 0xdc, 0x22, 0, - })) { + 0xff, 0xfe, 0x22, 0, + 0, 0xdc, 0, + 0xdc, 0x22, 0, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4444,9 +4444,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0x22, 0, 0, 0xdc, 0, - 0xdc, 0x22, 0, - })) { + 0x22, 0, 0, 0xdc, 0, + 0xdc, 0x22, 0, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4481,9 +4481,9 @@ public void TestReadJSON() { } // Illegal UTF-16 using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0x20, - 0x20, 0x20, - })) { + 0xfe, 0xff, 0x20, + 0x20, 0x20, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4495,9 +4495,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x20, - 0x20, 0x20, - })) { + 0xff, 0xfe, 0x20, + 0x20, 0x20, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4509,9 +4509,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xd8, - 0x00, - })) { + 0xfe, 0xff, 0xd8, + 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4523,9 +4523,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xdc, - 0x00, - })) { + 0xfe, 0xff, 0xdc, + 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4537,9 +4537,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xd8, - 0x00, 0x20, 0x00, - })) { + 0xfe, 0xff, 0xd8, + 0x00, 0x20, 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4551,9 +4551,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xdc, - 0x00, 0x20, 0x00, - })) { + 0xfe, 0xff, 0xdc, + 0x00, 0x20, 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4565,9 +4565,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xd8, - 0x00, 0xd8, 0x00, - })) { + 0xfe, 0xff, 0xd8, + 0x00, 0xd8, 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4579,9 +4579,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xdc, - 0x00, 0xd8, 0x00, - })) { + 0xfe, 0xff, 0xdc, + 0x00, 0xd8, 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4593,9 +4593,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xdc, - 0x00, 0xd8, 0x00, 0xdc, 0x00, - })) { + 0xfe, 0xff, 0xdc, + 0x00, 0xd8, 0x00, 0xdc, 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4607,9 +4607,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xfe, 0xff, 0xdc, - 0x00, 0xdc, 0x00, - })) { + 0xfe, 0xff, 0xdc, + 0x00, 0xdc, 0x00, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4622,9 +4622,9 @@ public void TestReadJSON() { } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xd8, - })) { + 0xff, 0xfe, 0x00, + 0xd8, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4636,9 +4636,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xdc, - })) { + 0xff, 0xfe, 0x00, + 0xdc, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4650,9 +4650,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xd8, 0x00, 0x20, - })) { + 0xff, 0xfe, 0x00, + 0xd8, 0x00, 0x20, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4664,9 +4664,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xdc, 0x00, 0x20, - })) { + 0xff, 0xfe, 0x00, + 0xdc, 0x00, 0x20, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4678,9 +4678,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xd8, 0x00, 0xd8, - })) { + 0xff, 0xfe, 0x00, + 0xd8, 0x00, 0xd8, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4692,9 +4692,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xdc, 0x00, 0xd8, - })) { + 0xff, 0xfe, 0x00, + 0xdc, 0x00, 0xd8, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4706,9 +4706,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xdc, 0x00, 0xd8, 0x00, 0xdc, - })) { + 0xff, 0xfe, 0x00, + 0xdc, 0x00, 0xd8, 0x00, 0xdc, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4720,9 +4720,9 @@ public void TestReadJSON() { } } using (var msjson = new MemoryStream(new byte[] { - 0xff, 0xfe, 0x00, - 0xdc, 0x00, 0xdc, - })) { + 0xff, 0xfe, 0x00, + 0xdc, 0x00, 0xdc, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -4736,8 +4736,8 @@ public void TestReadJSON() { // Illegal UTF-32 using (var msjson = new MemoryStream(new byte[] { - 0, 0, 0, 0x20, 0, - })) { + 0, 0, 0, 0x20, 0, + })) { try { CBORObject.ReadJSON(msjson); Assert.Fail("Should have failed"); @@ -5098,7 +5098,7 @@ public void TestCalcEncodedSizeCircularRefs3bc() { cbor = CBORObject.NewOrderedMap().Add("ghi", 2).Add("abc", 4); { object objectTemp = CBORObject.NewOrderedMap().Add(cbor, - "jkl").Add("mno", + "jkl").Add("mno", 1); object objectTemp2 = "test"; cbor.Add(objectTemp, objectTemp2); @@ -8025,10 +8025,10 @@ public static void AssertJSONDouble( string json, string numconv, double dbl) { - JSONOptions opt=new JSONOptions("numberconversion=" + numconv); + var opt = new JSONOptions("numberconversion=" + numconv); CBORObject[] cbors = { FromJSON(json, numconv), - CBORDataUtilities.ParseJSONNumber(json, opt) + CBORDataUtilities.ParseJSONNumber(json, opt), }; foreach (CBORObject cbor in cbors) { if (cbor.Type != CBORType.FloatingPoint) { @@ -8048,21 +8048,21 @@ public static void AssertJSONInteger( string json, string numconv, long longval) { - JSONOptions opt=new JSONOptions("numberconversion=" + numconv); + var opt = new JSONOptions("numberconversion=" + numconv); CBORObject[] cbors = { FromJSON(json, numconv), - CBORDataUtilities.ParseJSONNumber(json, opt) + CBORDataUtilities.ParseJSONNumber(json, opt), }; foreach (CBORObject cbor in cbors) { if (cbor.Type != CBORType.Integer) { - string msg = json + " " + numconv + " " + longval; - msg = msg.Substring(0, Math.Min(100, msg.Length)); - if (msg.Length > 100) { - msg += "..."; + string msg = json + " " + numconv + " " + longval; + msg = msg.Substring(0, Math.Min(100, msg.Length)); + if (msg.Length > 100) { + msg += "..."; + } + Assert.AreEqual(CBORType.Integer, cbor.Type, msg); } - Assert.AreEqual(CBORType.Integer, cbor.Type, msg); - } - Assert.AreEqual(longval, cbor.AsInt64Value()); + Assert.AreEqual(longval, cbor.AsInt64Value()); } } @@ -8070,21 +8070,21 @@ public static void AssertJSONInteger( string json, string numconv, int intval) { - JSONOptions opt=new JSONOptions("numberconversion=" + numconv); + var opt = new JSONOptions("numberconversion=" + numconv); CBORObject[] cbors = { FromJSON(json, numconv), - CBORDataUtilities.ParseJSONNumber(json, opt) + CBORDataUtilities.ParseJSONNumber(json, opt), }; foreach (CBORObject cbor in cbors) { if (cbor.Type != CBORType.Integer) { - string msg = json + " " + numconv + " " + intval; - msg = msg.Substring(0, Math.Min(100, msg.Length)); - if (msg.Length > 100) { - msg += "..."; + string msg = json + " " + numconv + " " + intval; + msg = msg.Substring(0, Math.Min(100, msg.Length)); + if (msg.Length > 100) { + msg += "..."; + } + Assert.AreEqual(CBORType.Integer, cbor.Type, msg); } - Assert.AreEqual(CBORType.Integer, cbor.Type, msg); - } - Assert.AreEqual(intval, cbor.AsInt32Value()); + Assert.AreEqual(intval, cbor.AsInt32Value()); } } @@ -8200,106 +8200,107 @@ public void TestFromJsonStringFastCases() { [Test] public void TestFromJsonStringFiniteDoubleSpec() { - RandomGenerator rg = new RandomGenerator(); - for (var i = 0; i < 10000; ++i) { - double dbl = RandomObjects.RandomFiniteDouble(rg); - EFloat efd = EFloat.FromDouble(dbl); - AssertJSONDouble( - efd.ToShortestString(EContext.Binary64), - "double", - dbl); - AssertJSONDouble( - efd.ToString(), - "double", - dbl); - } + var rg = new RandomGenerator(); + for (var i = 0; i < 10000; ++i) { + double dbl = RandomObjects.RandomFiniteDouble(rg); + EFloat efd = EFloat.FromDouble(dbl); + AssertJSONDouble( + efd.ToShortestString(EContext.Binary64), + "double", + dbl); + AssertJSONDouble( + efd.ToString(), + "double", + dbl); + } } [Test] public void TestFromJsonStringEDecimalSpec() { - RandomGenerator rg = new RandomGenerator(); - for (var i = 0; i < 1000; ++i) { - string[] decstring = new string[1]; - EDecimal ed = RandomObjects.RandomEDecimal(rg, decstring); - if ((decstring[0]) == null) { - Assert.Fail(); - } - double dbl = ed.ToDouble(); - if (Double.IsPositiveInfinity(dbl) || - Double.IsNegativeInfinity(dbl) || - Double.IsNaN(dbl)) { - continue; - } - AssertJSONDouble( - decstring[0], - "double", - dbl); - } + var rg = new RandomGenerator(); + for (var i = 0; i < 1000; ++i) { + var decstring = new string[1]; + EDecimal ed = RandomObjects.RandomEDecimal(rg, decstring); + if (decstring[0] == null) { + Assert.Fail(); + } + double dbl = ed.ToDouble(); + if (Double.IsPositiveInfinity(dbl) || + Double.IsNegativeInfinity(dbl) || + Double.IsNaN(dbl)) { + continue; + } + AssertJSONDouble( + decstring[0], + "double", + dbl); + } } [Test] public void TestFromJsonStringSmallDoubleSpec() { - RandomGenerator rg = new RandomGenerator(); - for (var i = 0; i < 10000; ++i) { - int rv = rg.GetInt32(Int32.MaxValue) * (rg.GetInt32(2)*2-1); - string rvstring = TestCommon.IntToString(rv); - AssertJSONDouble( - rvstring, - "double", - (double)rv); - AssertJSONInteger( - rvstring, - "intorfloat", - rv); - } - AssertJSONDouble("511","double",511); - AssertJSONDouble("-511","double",-511); - AssertJSONDouble( - TestCommon.IntToString(Int32.MaxValue), - "double", - (double)Int32.MaxValue); - AssertJSONDouble( - TestCommon.IntToString(Int32.MaxValue), - "double", - (double)Int32.MaxValue); - AssertJSONDouble( - TestCommon.IntToString(Int32.MinValue), - "double", - (double)Int32.MinValue); + var rg = new RandomGenerator(); + for (var i = 0; i < 10000; ++i) { + int rv = rg.GetInt32(Int32.MaxValue) * ((rg.GetInt32(2) * 2) - 1); + string rvstring = TestCommon.IntToString(rv); + AssertJSONDouble( + rvstring, + "double", + (double)rv); + AssertJSONInteger( + rvstring, + "intorfloat", + rv); + } + AssertJSONDouble("511", "double", 511); + AssertJSONDouble("-511", "double", -511); + AssertJSONDouble( + TestCommon.IntToString(Int32.MaxValue), + "double", + (double)Int32.MaxValue); + AssertJSONDouble( + TestCommon.IntToString(Int32.MaxValue), + "double", + (double)Int32.MaxValue); + AssertJSONDouble( + TestCommon.IntToString(Int32.MinValue), + "double", + (double)Int32.MinValue); } [Test] [Timeout(10000)] public void TestFromJsonStringSmallDouble() { - CBORObject cbor; - AssertJSONDouble("0","double",0.0); - cbor=FromJSON("[0, 1, 2, 3]", "double"); - Assert.AreEqual(4, cbor.Count); - Assert.AreEqual(0.0, cbor[0].AsDouble()); - Assert.AreEqual(1.0, cbor[1].AsDouble()); - Assert.AreEqual(2.0, cbor[2].AsDouble()); - Assert.AreEqual(3.0, cbor[3].AsDouble()); - cbor=FromJSON("[0]", "double"); - Assert.AreEqual(1, cbor.Count); - Assert.AreEqual(0.0, cbor[0].AsDouble()); - cbor=FromJSON("[-0]", "double"); - Assert.AreEqual(1, cbor.Count); - cbor=FromJSON("[1]", "double"); - Assert.AreEqual(1, cbor.Count); - Assert.AreEqual(1.0, cbor[0].AsDouble()); - cbor=FromJSON("[-1]", "double"); - Assert.AreEqual(1, cbor.Count); - Assert.AreEqual(-1.0, cbor[0].AsDouble()); - cbor=FromJSON("[-1022,-1023,-1024,-1025,1022,1023,1024,1025]", "double"); - Assert.AreEqual(8, cbor.Count); - Assert.AreEqual(-1022.0, cbor[0].AsDouble()); - Assert.AreEqual(-1023.0, cbor[1].AsDouble()); - Assert.AreEqual(-1024.0, cbor[2].AsDouble()); - Assert.AreEqual(-1025.0, cbor[3].AsDouble()); - Assert.AreEqual(1022.0, cbor[4].AsDouble()); - Assert.AreEqual(1023.0, cbor[5].AsDouble()); - Assert.AreEqual(1024.0, cbor[6].AsDouble()); - Assert.AreEqual(1025.0, cbor[7].AsDouble()); + CBORObject cbor; + AssertJSONDouble("0", "double", 0.0); + cbor = FromJSON("[0, 1, 2, 3]", "double"); + Assert.AreEqual(4, cbor.Count); + Assert.AreEqual((double)0.0, cbor[0].AsDouble()); + Assert.AreEqual((double)1.0, cbor[1].AsDouble()); + Assert.AreEqual((double)2.0, cbor[2].AsDouble()); + Assert.AreEqual((double)3.0, cbor[3].AsDouble()); + cbor = FromJSON("[0]", "double"); + Assert.AreEqual(1, cbor.Count); + Assert.AreEqual((double)0.0, cbor[0].AsDouble()); + cbor = FromJSON("[-0]", "double"); + Assert.AreEqual(1, cbor.Count); + cbor = FromJSON("[1]", "double"); + Assert.AreEqual(1, cbor.Count); + Assert.AreEqual((double)1.0, cbor[0].AsDouble()); + cbor = FromJSON("[-1]", "double"); + Assert.AreEqual(1, cbor.Count); + Assert.AreEqual((double)-1.0, cbor[0].AsDouble()); + cbor = FromJSON("[-1022,-1023,-1024,-1025,1022,1023,1024,1025]", + "double"); + Assert.AreEqual(8, cbor.Count); + Assert.AreEqual((double)-1022.0, cbor[0].AsDouble()); + Assert.AreEqual((double)-1023.0, cbor[1].AsDouble()); + Assert.AreEqual((double)-1024.0, cbor[2].AsDouble()); + Assert.AreEqual((double)-1025.0, cbor[3].AsDouble()); + Assert.AreEqual((double)1022.0, cbor[4].AsDouble()); + Assert.AreEqual((double)1023.0, cbor[5].AsDouble()); + Assert.AreEqual((double)1024.0, cbor[6].AsDouble()); + Assert.AreEqual((double)1025.0, cbor[7].AsDouble()); } [Test] diff --git a/CBORTest/CBORTest.cs b/CBORTest/CBORTest.cs index 7b1f1c37..8cb17e07 100644 --- a/CBORTest/CBORTest.cs +++ b/CBORTest/CBORTest.cs @@ -4983,45 +4983,69 @@ public static bool CheckUtf16(string str) { [Test] public void TestJSONOptions() { - var jsonop1=new JSONOptions("numberconversion=intorfloat"); - Assert.AreEqual(jsonop1.ToString(), new -JSONOptions(jsonop1.ToString()).ToString()); - var jsonop2=new JSONOptions("numberconversion=decimal128"); - Assert.AreEqual(jsonop2.ToString(), new -JSONOptions(jsonop2.ToString()).ToString()); - var jsonop3=new JSONOptions("numberconversion=intorfloatfromdouble"); - Assert.AreEqual(jsonop3.ToString(), new -JSONOptions(jsonop3.ToString()).ToString()); - var jsonop4=new JSONOptions("numberconversion=double"); - Assert.AreEqual(jsonop4.ToString(), new -JSONOptions(jsonop4.ToString()).ToString()); + var jsonop1 = new JSONOptions("numberconversion=intorfloat"); + { + object objectTemp = jsonop1.ToString(); + object objectTemp2 = new +JSONOptions(jsonop1.ToString()).ToString(); + Assert.AreEqual(objectTemp, objectTemp2); + } + var jsonop2 = new JSONOptions("numberconversion=decimal128"); + { + object objectTemp = jsonop2.ToString(); + object objectTemp2 = new +JSONOptions(jsonop2.ToString()).ToString(); + Assert.AreEqual(objectTemp, objectTemp2); + } + var jsonop3 = new JSONOptions("numberconversion=intorfloatfromdouble"); + { + object objectTemp = jsonop3.ToString(); + object objectTemp2 = new +JSONOptions(jsonop3.ToString()).ToString(); + Assert.AreEqual(objectTemp, objectTemp2); + } + var jsonop4 = new JSONOptions("numberconversion=double"); + { + object objectTemp = jsonop4.ToString(); + object objectTemp2 = new +JSONOptions(jsonop4.ToString()).ToString(); + Assert.AreEqual(objectTemp, objectTemp2); + } } [Test] public void TestPODOptions() { PODOptions podop = PODOptions.Default; - Assert.AreEqual(podop.ToString(), new -PODOptions(podop.ToString()).ToString()); + { + object objectTemp = podop.ToString(); + object objectTemp2 = new +PODOptions(podop.ToString()).ToString(); + Assert.AreEqual(objectTemp, objectTemp2); + } } [Test] public void TestCBOREncodeOptions() { CBOREncodeOptions encodeop = CBOREncodeOptions.Default; - Assert.AreEqual(encodeop.ToString(), new -CBOREncodeOptions(encodeop.ToString()).ToString()); + { + object objectTemp = encodeop.ToString(); + object objectTemp2 = new +CBOREncodeOptions(encodeop.ToString()).ToString(); + Assert.AreEqual(objectTemp, objectTemp2); + } } [Test] public void TestRandomJSON() { var jsongen = new JSONGenerator(); var rg = new RandomGenerator(); - var jsonop1=new JSONOptions("numberconversion=intorfloat"); - var jsonop2=new JSONOptions("numberconversion=decimal128"); - var jsonop3=new JSONOptions("numberconversion=intorfloatfromdouble"); - var jsonop4=new JSONOptions("numberconversion=double"); + var jsonop1 = new JSONOptions("numberconversion=intorfloat"); + var jsonop2 = new JSONOptions("numberconversion=decimal128"); + var jsonop3 = new JSONOptions("numberconversion=intorfloatfromdouble"); + var jsonop4 = new JSONOptions("numberconversion=double"); for (var i = 0; i < 200; ++i) { byte[] json = jsongen.Generate(rg); - Console.WriteLine("" + i + " len=" + (json.Length)); + Console.WriteLine(String.Empty + i + " len=" + json.Length); JSONOptions currop = null; try { currop = jsonop1; @@ -5034,7 +5058,7 @@ public void TestRandomJSON() { CBORObject.FromJSONBytes(json, jsonop4); } catch (CBORException ex) { string msg = ex.Message + "\n" + - DataUtilities.GetUtf8String(json,true) + "\n" + currop; + DataUtilities.GetUtf8String(json, true) + "\n" + currop; throw new InvalidOperationException(msg, ex); } }