diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 4f206b8a..88c334db 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -3,7 +3,7 @@ netstandard1.0 True - 4.4.2 + 4.4.4 Peter Occil A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949. A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949. @@ -13,6 +13,11 @@ CC0-1.0 https://github.com/peteroupc/CBOR +Version 4.4.4: + +- Bug fixes, including updating the Numbers library that has bug fixes. Some of + the bug fixes involve how equivalent forms of NaN are handled. + Version 4.4.2: - Performance improvements in some cases, especially involving date/time conversions diff --git a/CBOR20/Properties/AssemblyInfo.cs b/CBOR20/Properties/AssemblyInfo.cs index 1477b391..3c9b6bf4 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.2")] -[assembly: AssemblyVersion("4.4.2.0")] -[assembly: AssemblyFileVersion("4.4.2.0")] +[assembly: AssemblyInformationalVersion("4.4.4")] +[assembly: AssemblyVersion("4.4.4.0")] +[assembly: AssemblyFileVersion("4.4.4.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 f7249746..9fb0dea0 100644 --- a/CBORTest/CBORObjectTest.cs +++ b/CBORTest/CBORObjectTest.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using NUnit.Framework; using PeterO; @@ -1091,9 +1090,9 @@ public void TestCanFitInDoubleF() { Assert.Fail(cbornumber.ToString()); } } else { -if (cbornumber.CanFitInDouble()) { - Assert.Fail(cbornumber.ToString()); -} + if (cbornumber.CanFitInDouble()) { + Assert.Fail(cbornumber.ToString()); + } } } var rand = new RandomGenerator(); @@ -2187,16 +2186,16 @@ public void TestEncodeFloat64() { public void TestEncodeToBytes() { // Test minimum data length int[] ranges = { - -24, 23, 1, - -256, -25, 2, - 24, 255, 2, - 256, 266, 3, - -266, -257, 3, - 65525, 65535, 3, - -65536, -65525, 3, - 65536, 65546, 5, - -65547, -65537, 5, - }; + -24, 23, 1, + -256, -25, 2, + 24, 255, 2, + 256, 266, 3, + -266, -257, 3, + 65525, 65535, 3, + -65536, -65525, 3, + 65536, 65546, 5, + -65547, -65537, 5, + }; string[] bigRanges = { "4294967285", "4294967295", "4294967296", "4294967306", @@ -4182,9 +4181,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) { @@ -4194,9 +4193,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"); @@ -4231,9 +4230,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"); @@ -4245,82 +4244,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( @@ -4329,10 +4328,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( @@ -4341,10 +4340,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( @@ -4353,11 +4352,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( @@ -4366,11 +4365,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( @@ -4379,9 +4378,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( @@ -4390,10 +4389,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( @@ -4402,9 +4401,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( @@ -4413,10 +4412,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"); @@ -4428,11 +4427,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"); @@ -4444,10 +4443,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"); @@ -4459,12 +4458,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"); @@ -4476,10 +4475,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"); @@ -4491,10 +4490,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"); @@ -4506,10 +4505,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"); @@ -4521,9 +4520,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"); @@ -4558,9 +4557,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"); @@ -4572,9 +4571,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"); @@ -4586,9 +4585,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"); @@ -4600,9 +4599,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"); @@ -4614,9 +4613,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"); @@ -4628,9 +4627,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"); @@ -4642,9 +4641,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"); @@ -4656,9 +4655,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"); @@ -4670,9 +4669,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"); @@ -4684,9 +4683,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"); @@ -4699,9 +4698,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"); @@ -4713,9 +4712,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"); @@ -4727,9 +4726,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"); @@ -4741,9 +4740,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"); @@ -4755,9 +4754,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"); @@ -4769,9 +4768,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"); @@ -4783,9 +4782,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"); @@ -4797,9 +4796,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"); @@ -4813,8 +4812,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"); @@ -7683,64 +7682,64 @@ private static string DateTimeToString( } private static void TestDateTimeStringNumberOne(string str, long num) { - CBORObject dtstring = CBORObject.FromObject(str).WithTag(0); - CBORObject dtnum = CBORObject.FromObject(num).WithTag(1); - TestDateTimeStringNumberOne(dtstring, dtnum); + CBORObject dtstring = CBORObject.FromObject(str).WithTag(0); + CBORObject dtnum = CBORObject.FromObject(num).WithTag(1); + TestDateTimeStringNumberOne(dtstring, dtnum); } private static void TestDateTimeStringNumberOne(string str, double num) { - CBORObject dtstring = CBORObject.FromObject(str).WithTag(0); - CBORObject dtnum = CBORObject.FromObject(num).WithTag(1); - TestDateTimeStringNumberOne(dtstring, dtnum); + CBORObject dtstring = CBORObject.FromObject(str).WithTag(0); + CBORObject dtnum = CBORObject.FromObject(num).WithTag(1); + TestDateTimeStringNumberOne(dtstring, dtnum); } private static void TestDateTimeStringNumberOne(CBORObject dtstring, - CBORObject dtnum) { - CBORDateConverter convNumber = CBORDateConverter.TaggedNumber; - CBORDateConverter convString = CBORDateConverter.TaggedString; - CBORObject cbor; - var eiYear = new EInteger[1]; - var lesserFields = new int[7]; - string strnum = dtstring + ", " + dtnum; - cbor = convNumber.ToCBORObject(convNumber.FromCBORObject(dtstring)); - Assert.AreEqual(dtnum, cbor, strnum); - if (!convNumber.TryGetDateTimeFields(dtstring, eiYear, lesserFields)) { - Assert.Fail(strnum); - } - cbor = convNumber.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); - Assert.AreEqual(dtnum, cbor, strnum); - cbor = convString.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); - Assert.AreEqual(dtstring, cbor, strnum); - cbor = convString.ToCBORObject(convString.FromCBORObject(dtnum)); - Assert.AreEqual(dtstring, cbor, strnum); - if (!convString.TryGetDateTimeFields(dtnum, eiYear, lesserFields)) { - Assert.Fail(strnum); - } - cbor = convNumber.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); - Assert.AreEqual(dtnum, cbor, strnum); - cbor = convString.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); - Assert.AreEqual(dtstring, cbor, strnum); + CBORObject dtnum) { + CBORDateConverter convNumber = CBORDateConverter.TaggedNumber; + CBORDateConverter convString = CBORDateConverter.TaggedString; + CBORObject cbor; + var eiYear = new EInteger[1]; + var lesserFields = new int[7]; + string strnum = dtstring + ", " + dtnum; + cbor = convNumber.ToCBORObject(convNumber.FromCBORObject(dtstring)); + Assert.AreEqual(dtnum, cbor, strnum); + if (!convNumber.TryGetDateTimeFields(dtstring, eiYear, lesserFields)) { + Assert.Fail(strnum); + } + cbor = convNumber.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); + Assert.AreEqual(dtnum, cbor, strnum); + cbor = convString.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); + Assert.AreEqual(dtstring, cbor, strnum); + cbor = convString.ToCBORObject(convString.FromCBORObject(dtnum)); + Assert.AreEqual(dtstring, cbor, strnum); + if (!convString.TryGetDateTimeFields(dtnum, eiYear, lesserFields)) { + Assert.Fail(strnum); + } + cbor = convNumber.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); + Assert.AreEqual(dtnum, cbor, strnum); + cbor = convString.DateTimeFieldsToCBORObject(eiYear[0], lesserFields); + Assert.AreEqual(dtstring, cbor, strnum); } [Test] public void TestDateTimeStringNumber() { - TestDateTimeStringNumberOne("1970-01-01T00:00:00.25Z", 0.25); - TestDateTimeStringNumberOne("1970-01-01T00:00:00.75Z", 0.75); - TestDateTimeStringNumberOne("1969-12-31T23:59:59.75Z", -0.25); - TestDateTimeStringNumberOne("1969-12-31T23:59:59.25Z", -0.75); - TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800); - TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800); - TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800); - TestDateTimeStringNumberOne("2001-01-03T00:00:00Z", 978480000); - TestDateTimeStringNumberOne("2001-01-03T00:00:00.25Z", 978480000.25); - TestDateTimeStringNumberOne("1960-01-03T00:00:00Z", -315446400); - TestDateTimeStringNumberOne("1400-01-03T00:00:00Z", -17987270400L); - TestDateTimeStringNumberOne("2100-01-03T00:00:00Z", 4102617600L); - TestDateTimeStringNumberOne("1970-01-03T00:00:01Z", 172801); - TestDateTimeStringNumberOne("2001-01-03T00:00:01Z", 978480001); - TestDateTimeStringNumberOne("1960-01-03T00:00:01Z", -315446399); - TestDateTimeStringNumberOne("1960-01-03T00:00:00.25Z", -315446399.75); - TestDateTimeStringNumberOne("1960-01-03T00:00:00.75Z", -315446399.25); - TestDateTimeStringNumberOne("1400-01-03T00:00:01Z", -17987270399L); - TestDateTimeStringNumberOne("2100-01-03T00:00:01Z", 4102617601L); + TestDateTimeStringNumberOne("1970-01-01T00:00:00.25Z", 0.25); + TestDateTimeStringNumberOne("1970-01-01T00:00:00.75Z", 0.75); + TestDateTimeStringNumberOne("1969-12-31T23:59:59.75Z", -0.25); + TestDateTimeStringNumberOne("1969-12-31T23:59:59.25Z", -0.75); + TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800); + TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800); + TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800); + TestDateTimeStringNumberOne("2001-01-03T00:00:00Z", 978480000); + TestDateTimeStringNumberOne("2001-01-03T00:00:00.25Z", 978480000.25); + TestDateTimeStringNumberOne("1960-01-03T00:00:00Z", -315446400); + TestDateTimeStringNumberOne("1400-01-03T00:00:00Z", -17987270400L); + TestDateTimeStringNumberOne("2100-01-03T00:00:00Z", 4102617600L); + TestDateTimeStringNumberOne("1970-01-03T00:00:01Z", 172801); + TestDateTimeStringNumberOne("2001-01-03T00:00:01Z", 978480001); + TestDateTimeStringNumberOne("1960-01-03T00:00:01Z", -315446399); + TestDateTimeStringNumberOne("1960-01-03T00:00:00.25Z", -315446399.75); + TestDateTimeStringNumberOne("1960-01-03T00:00:00.75Z", -315446399.25); + TestDateTimeStringNumberOne("1400-01-03T00:00:01Z", -17987270399L); + TestDateTimeStringNumberOne("2100-01-03T00:00:01Z", 4102617601L); } [Test] @@ -8147,32 +8146,32 @@ public void TestDateTimeTag1() { } private static string RandomQueryStringLike(IRandomGenExtended irg) { - var sb = new StringBuilder(); - while (true) { - int x = irg.GetInt32(100); - if (x == 0) { - break; - } else if (x < 10) { - sb.Append('&'); - } else if (x < 20) { - sb.Append('='); - } else if (x < 25) { - string hex = "0123456789ABCDEF"; - sb.Append('%'); - sb.Append(hex[irg.GetInt32(hex.Length)]); - sb.Append(hex[irg.GetInt32(hex.Length)]); - } else if (x < 30) { - string hex = "0123456789abcdef"; - sb.Append('%'); - sb.Append(hex[irg.GetInt32(hex.Length)]); - sb.Append(hex[irg.GetInt32(hex.Length)]); - } else if (x < 95) { - sb.Append((char)(irg.GetInt32(0x5e) + 0x21)); - } else { - sb.Append((char)irg.GetInt32(0x80)); - } - } - return sb.ToString(); + var sb = new StringBuilder(); + while (true) { + int x = irg.GetInt32(100); + if (x == 0) { + break; + } else if (x < 10) { + sb.Append('&'); + } else if (x < 20) { + sb.Append('='); + } else if (x < 25) { + string hex = "0123456789ABCDEF"; + sb.Append('%'); + sb.Append(hex[irg.GetInt32(hex.Length)]); + sb.Append(hex[irg.GetInt32(hex.Length)]); + } else if (x < 30) { + string hex = "0123456789abcdef"; + sb.Append('%'); + sb.Append(hex[irg.GetInt32(hex.Length)]); + sb.Append(hex[irg.GetInt32(hex.Length)]); + } else if (x < 95) { + sb.Append((char)(irg.GetInt32(0x5e) + 0x21)); + } else { + sb.Append((char)irg.GetInt32(0x80)); + } + } + return sb.ToString(); } [Test] @@ -8181,7 +8180,7 @@ public void TestQueryStrings() { String test = "a=b&c=d&e=f&g\u005b0]=h&g\u005b1]=j&g\u005b2]\u005b"; test += "a]=k&g\u005b2]\u005bb]=m"; CBORObject cbor = -CBORObject.FromObject(QueryStringHelper.QueryStringToDict(test)); + CBORObject.FromObject(QueryStringHelper.QueryStringToDict(test)); Console.WriteLine(cbor.ToJSONString()); cbor = CBORObject.FromObject(QueryStringHelper.QueryStringToCBOR(test)); Console.WriteLine(cbor.ToJSONString()); @@ -8230,7 +8229,7 @@ public static void AssertJSONDouble( double cbordbl = cbor.AsDoubleValue(); if (dbl != cbordbl) { Assert.Fail("dbl = " + dbl + ", cbordbl = " + cbordbl + ", " + - json + " " + numconv + " " + dbl); + json + " " + numconv + " " + dbl); } } } @@ -8294,9 +8293,9 @@ public void TestFromJsonStringLongSpecific1() { } private static readonly JSONOptions JSONOptionsDouble = new JSONOptions( - "numberconversion=double"); + "numberconversion=double"); private static readonly JSONOptions JSONOptionsFull = new JSONOptions( - "numberconversion=full"); + "numberconversion=full"); public static void TestParseNumberFxxLine(string line) { // Parse test case format used in: @@ -8322,72 +8321,73 @@ public static void TestParseNumberFxxLine(string line) { public static void TestParseNumberFxx( string str, - short _f16, + short f16, int f32, long f64, string line) { - if (str[0] == '.' || str[str.Length - 1] == '.' || - str.Contains(".e") || str.Contains(".E")) { - // Not a valid JSON number, so skip - // Console.WriteLine(str); - return; - } - CBORObject cbor = CBORDataUtilities.ParseJSONNumber(str, - JSONOptionsDouble); - if (cbor == null) { - Console.WriteLine(str); - return; - } - Assert.AreEqual(f64, cbor.AsDoubleBits(), line); - cbor = CBORObject.FromJSONString(str, JSONOptionsDouble); - Assert.AreEqual(f64, cbor.AsDoubleBits(), line); - cbor = CBORObject.FromJSONBytes( + Assert.AreEqual(f16, f16); + if (str[0] == '.' || str[str.Length - 1] == '.' || + str.Contains(".e") || str.Contains(".E")) { + // Not a valid JSON number, so skip + // Console.WriteLine(str); + return; + } + CBORObject cbor = CBORDataUtilities.ParseJSONNumber(str, + JSONOptionsDouble); + if (cbor == null) { + Console.WriteLine(str); + return; + } + Assert.AreEqual(f64, cbor.AsDoubleBits(), line); + cbor = CBORObject.FromJSONString(str, JSONOptionsDouble); + Assert.AreEqual(f64, cbor.AsDoubleBits(), line); + cbor = CBORObject.FromJSONBytes( DataUtilities.GetUtf8Bytes(str, false), JSONOptionsDouble); - Assert.AreEqual(f64, cbor.AsDoubleBits(), line); - float sing = CBORObject.FromFloatingPointBits(f32, 4).AsSingle(); - cbor = CBORDataUtilities.ParseJSONNumber(str, JSONOptionsFull); - if (cbor == null) { - Assert.Fail(); - } - Assert.AreEqual(sing, cbor.AsSingle(), line); - cbor = CBORObject.FromJSONString(str, JSONOptionsFull); - Assert.AreEqual(sing, cbor.AsSingle(), line); - cbor = CBORObject.FromJSONBytes( + Assert.AreEqual(f64, cbor.AsDoubleBits(), line); + float sing = CBORObject.FromFloatingPointBits(f32, 4).AsSingle(); + cbor = CBORDataUtilities.ParseJSONNumber(str, JSONOptionsFull); + if (cbor == null) { + Assert.Fail(); + } + Assert.AreEqual(sing, cbor.AsSingle(), line); + cbor = CBORObject.FromJSONString(str, JSONOptionsFull); + Assert.AreEqual(sing, cbor.AsSingle(), line); + cbor = CBORObject.FromJSONBytes( DataUtilities.GetUtf8Bytes(str, false), JSONOptionsFull); - Assert.AreEqual(sing, cbor.AsSingle(), line); - // 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()); - } - } - } + Assert.AreEqual(sing, cbor.AsSingle(), line); + // 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() { @@ -8505,62 +8505,62 @@ public void TestEDecimalEFloatWithHighExponent() { string decstr = "0E100441809235791722330759976"; Assert.AreEqual(0L, EDecimal.FromString(decstr).ToDoubleBits()); Assert.AreEqual(0L, EFloat.FromString(decstr).ToDoubleBits()); - { + { object objectTemp = 0L; object objectTemp2 = EDecimal.FromString(decstr, - EContext.Decimal32).ToDoubleBits(); + EContext.Decimal32).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } { object objectTemp = 0L; object objectTemp2 = EFloat.FromString(decstr, - EContext.Binary64).ToDoubleBits(); + EContext.Binary64).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } decstr = "0E-100441809235791722330759976"; Assert.AreEqual(0L, EDecimal.FromString(decstr).ToDoubleBits()); Assert.AreEqual(0L, EFloat.FromString(decstr).ToDoubleBits()); - { + { object objectTemp = 0L; object objectTemp2 = EDecimal.FromString(decstr, - EContext.Decimal32).ToDoubleBits(); + EContext.Decimal32).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } { object objectTemp = 0L; object objectTemp2 = EFloat.FromString(decstr, - EContext.Binary64).ToDoubleBits(); + EContext.Binary64).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } decstr = "-0E100441809235791722330759976"; long negzero = 1L << 63; Assert.AreEqual(negzero, EDecimal.FromString(decstr).ToDoubleBits()); Assert.AreEqual(negzero, EFloat.FromString(decstr).ToDoubleBits()); - { + { object objectTemp = negzero; object objectTemp2 = EDecimal.FromString(decstr, - EContext.Decimal32).ToDoubleBits(); + EContext.Decimal32).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } { object objectTemp = negzero; object objectTemp2 = EFloat.FromString(decstr, - EContext.Binary64).ToDoubleBits(); + EContext.Binary64).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } decstr = "-0E-100441809235791722330759976"; Assert.AreEqual(negzero, EDecimal.FromString(decstr).ToDoubleBits()); Assert.AreEqual(negzero, EFloat.FromString(decstr).ToDoubleBits()); - { + { object objectTemp = negzero; object objectTemp2 = EDecimal.FromString(decstr, - EContext.Decimal32).ToDoubleBits(); + EContext.Decimal32).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } { object objectTemp = negzero; object objectTemp2 = EFloat.FromString(decstr, - EContext.Binary64).ToDoubleBits(); + EContext.Binary64).ToDoubleBits(); Assert.AreEqual(objectTemp, objectTemp2); } } @@ -8570,13 +8570,13 @@ public void TestFromJsonStringZeroWithHighExponent() { string decstr = "0E100441809235791722330759976"; EDecimal ed = EDecimal.FromString(decstr); double dbl = ed.ToDouble(); - Assert.AreEqual((double)0, dbl); + Assert.AreEqual((double)0d, dbl); AssertJSONDouble(decstr, "double", dbl); AssertJSONInteger(decstr, "intorfloat", 0); decstr = "0E1321909565013040040586"; ed = EDecimal.FromString(decstr); dbl = ed.ToDouble(); - Assert.AreEqual((double)0, dbl); + Assert.AreEqual((double)0d, dbl); AssertJSONDouble(decstr, "double", dbl); AssertJSONInteger(decstr, "intorfloat", 0); double dblnegzero = EFloat.FromString("-0").ToDouble(); @@ -8606,8 +8606,8 @@ public void TestFromJsonStringEDecimalSpec() { } double dbl = ed.ToDouble(); if (Double.IsPositiveInfinity(dbl) || - Double.IsNegativeInfinity(dbl) || - Double.IsNaN(dbl)) { + Double.IsNegativeInfinity(dbl) || + Double.IsNaN(dbl)) { continue; } AssertJSONDouble( @@ -8619,140 +8619,143 @@ public void TestFromJsonStringEDecimalSpec() { [Test] public void TestFromJsonCTLInString() { - for (var i = 0; i <= 0x20; ++i) { - byte[] bytes = {0x22, (byte)i, 0x22 }; - char[] chars = {(char)0x22, (char)i, (char)0x22 }; - string str = new String(chars, 0, chars.Length); - if (i == 0x20) { - try { - CBORObject.FromJSONString(str); -} catch (Exception ex) { -Assert.Fail(ex.ToString()); -throw new InvalidOperationException(String.Empty, ex); -} - try { - CBORObject.FromJSONBytes(bytes); -} catch (Exception ex) { -Assert.Fail(ex.ToString()); -throw new InvalidOperationException(String.Empty, ex); -} - } else { - try { - CBORObject.FromJSONString(str); - Assert.Fail("Should have failed"); -} catch (CBORException) { -// NOTE: Intentionally empty -} catch (Exception ex) { - Assert.Fail(ex.ToString()); - throw new InvalidOperationException(String.Empty, ex); -} - try { - CBORObject.FromJSONBytes(bytes); - Assert.Fail("Should have failed"); -} catch (CBORException) { -// NOTE: Intentionally empty -} catch (Exception ex) { - Assert.Fail(ex.ToString()); - throw new InvalidOperationException(String.Empty, ex); -} + for (var i = 0; i <= 0x20; ++i) { + byte[] bytes = { 0x22, (byte)i, 0x22 }; + char[] chars = new char[3]; + chars[0] = (char)bytes[0]; + chars[1] = (char)bytes[1]; + chars[2] = (char)bytes[2]; + string str = new String(chars, 0, chars.Length); + if (i == 0x20) { + try { + CBORObject.FromJSONString(str); + } catch (Exception ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); + } + try { + CBORObject.FromJSONBytes(bytes); + } catch (Exception ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); + } + } else { + try { + CBORObject.FromJSONString(str); + Assert.Fail("Should have failed"); + } catch (CBORException) { + // NOTE: Intentionally empty + } catch (Exception ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); + } + try { + CBORObject.FromJSONBytes(bytes); + Assert.Fail("Should have failed"); + } catch (CBORException) { + // NOTE: Intentionally empty + } catch (Exception ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); } } - } + } + } // [Test] public void TestFromJsonLeadingTrailingCTLBytes() { - // TODO: Reenable eventually, once UTF-8 only support - // for CBORObject.FromJSONBytes is implemented - for (var i = 0; i <= 0x20; ++i) { - // Leading CTL - byte[] bytes = { (byte)i, 0x31 }; - if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { - try { - CBORObject.FromJSONBytes(bytes); -} catch (Exception ex) { -Assert.Fail(ex.ToString() + "bytes " + i); -throw new InvalidOperationException(String.Empty, ex); -} - } else { - try { - CBORObject.FromJSONBytes(bytes); - Assert.Fail("Should have failed bytes " + i); -} catch (CBORException) { -// NOTE: Intentionally empty -} catch (Exception ex) { - Assert.Fail(ex.ToString()); - throw new InvalidOperationException(String.Empty, ex); -} + // TODO: Reenable eventually, once UTF-8 only support + // for CBORObject.FromJSONBytes is implemented + for (var i = 0; i <= 0x20; ++i) { + // Leading CTL + byte[] bytes = { (byte)i, 0x31 }; + if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { + try { + CBORObject.FromJSONBytes(bytes); + } catch (Exception ex) { + Assert.Fail(ex.ToString() + "bytes " + i); + throw new InvalidOperationException(String.Empty, ex); } - // Trailing CTL - bytes = new byte[] { 0x31, (byte)i }; - if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { - try { - CBORObject.FromJSONBytes(bytes); -} catch (Exception ex) { -Assert.Fail(ex.ToString() + "bytes " + i); -throw new InvalidOperationException(String.Empty, ex); -} - } else { - try { - CBORObject.FromJSONBytes(bytes); - Assert.Fail("Should have failed"); -} catch (CBORException) { -// NOTE: Intentionally empty -} catch (Exception ex) { - Assert.Fail(ex.ToString() + "bytes " + i); - throw new InvalidOperationException(String.Empty, ex); -} + } else { + try { + CBORObject.FromJSONBytes(bytes); + Assert.Fail("Should have failed bytes " + i); + } catch (CBORException) { + // NOTE: Intentionally empty + } catch (Exception ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); + } + } + // Trailing CTL + bytes = new byte[] { 0x31, (byte)i }; + if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { + try { + CBORObject.FromJSONBytes(bytes); + } catch (Exception ex) { + Assert.Fail(ex.ToString() + "bytes " + i); + throw new InvalidOperationException(String.Empty, ex); + } + } else { + try { + CBORObject.FromJSONBytes(bytes); + Assert.Fail("Should have failed"); + } catch (CBORException) { + // NOTE: Intentionally empty + } catch (Exception ex) { + Assert.Fail(ex.ToString() + "bytes " + i); + throw new InvalidOperationException(String.Empty, ex); } - } + } + } } [Test] public void TestFromJsonLeadingTrailingCTL() { - for (var i = 0; i <= 0x20; ++i) { - // Leading CTL - char[] chars = {(char)i, (char)0x31 }; - string str = new String(chars, 0, chars.Length); - if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { - try { - CBORObject.FromJSONString(str); -} catch (Exception ex) { -Assert.Fail(ex.ToString() + "string " + i); -throw new InvalidOperationException(String.Empty, ex); -} - } else { - try { - CBORObject.FromJSONString(str); - Assert.Fail("Should have failed string " + i); -} catch (CBORException) { -// NOTE: Intentionally empty -} catch (Exception ex) { - Assert.Fail(ex.ToString()); - throw new InvalidOperationException(String.Empty, ex); -} + for (var i = 0; i <= 0x20; ++i) { + // Leading CTL + char[] chars = { (char)i, (char)0x31 }; + string str = new String(chars, 0, chars.Length); + if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { + try { + CBORObject.FromJSONString(str); + } catch (Exception ex) { + Assert.Fail(ex.ToString() + "string " + i); + throw new InvalidOperationException(String.Empty, ex); } - // Trailing CTL - chars = new char[] { (char)0x31, (char)i}; - str = new String(chars, 0, chars.Length); - if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { - try { - CBORObject.FromJSONString(str); -} catch (Exception ex) { -Assert.Fail(ex.ToString() + "string " + i); -throw new InvalidOperationException(String.Empty, ex); -} - } else { - try { - CBORObject.FromJSONString(str); - Assert.Fail("Should have failed"); -} catch (CBORException) { -// NOTE: Intentionally empty -} catch (Exception ex) { - Assert.Fail(ex.ToString() + "string " + i); - throw new InvalidOperationException(String.Empty, ex); -} + } else { + try { + CBORObject.FromJSONString(str); + Assert.Fail("Should have failed string " + i); + } catch (CBORException) { + // NOTE: Intentionally empty + } catch (Exception ex) { + Assert.Fail(ex.ToString()); + throw new InvalidOperationException(String.Empty, ex); + } + } + // Trailing CTL + chars = new char[] { (char)0x31, (char)i }; + str = new String(chars, 0, chars.Length); + if (i == 0x09 || i == 0x0d || i == 0x0a || i == 0x20) { + try { + CBORObject.FromJSONString(str); + } catch (Exception ex) { + Assert.Fail(ex.ToString() + "string " + i); + throw new InvalidOperationException(String.Empty, ex); + } + } else { + try { + CBORObject.FromJSONString(str); + Assert.Fail("Should have failed"); + } catch (CBORException) { + // NOTE: Intentionally empty + } catch (Exception ex) { + Assert.Fail(ex.ToString() + "string " + i); + throw new InvalidOperationException(String.Empty, ex); } - } + } + } } [Test] diff --git a/CBORTest/JSONWithComments.cs b/CBORTest/JSONWithComments.cs index 74e8eb57..1fa95bc5 100644 --- a/CBORTest/JSONWithComments.cs +++ b/CBORTest/JSONWithComments.cs @@ -36,8 +36,8 @@ internal void RaiseError(string str) { private readonly string jstring; private readonly IList currPointer; - private int currPointerStackSize; private readonly JSONOptions options; + private int currPointerStackSize; private IList pointers; private int index; private int endPos; @@ -59,10 +59,12 @@ private CBORObject NextJSONString() { this.index = idx; if (escaped) { return CBORObject.FromJSONString(js.Substring( - startIndex - 1, - (endIndex - (startIndex - 1)))); } return -CBORObject.FromObject(js.Substring(startIndex, - (endIndex - 1) - startIndex)); + startIndex - 1, + endIndex - (startIndex - 1))); + } + return + CBORObject.FromObject(js.Substring(startIndex, + (endIndex - 1) - startIndex)); } else if (c == '\\') { this.index = idx++; escaped = true; @@ -81,16 +83,16 @@ private CBORObject NextJSONNumber( 0xffff : -1; if (!(c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9') || c == 'e' || c == 'E')) { - numberEndIndex = c < 0 ? this.index : this.index - 1; - obj = CBORDataUtilities.ParseJSONNumber( - this.jstring.Substring( - numberStartIndex, - numberEndIndex - numberStartIndex), - this.options); - if (obj == null) { - this.RaiseError("Invalid JSON number"); - } - break; + numberEndIndex = c < 0 ? this.index : this.index - 1; + obj = CBORDataUtilities.ParseJSONNumber( + this.jstring.Substring( + numberStartIndex, + numberEndIndex - numberStartIndex), + this.options); + if (obj == null) { + this.RaiseError("Invalid JSON number"); + } + break; } } c = numberEndIndex >= this.endPos ? -1 : this.jstring[numberEndIndex]; @@ -189,7 +191,8 @@ private CBORObject NextJSONValue( // Parse a nonnegative number return this.NextJSONNumber(nextChar); } - default: this.RaiseError("Value can't be parsed."); + default: + this.RaiseError("Value can't be parsed."); break; } return null; @@ -272,9 +275,9 @@ public static CBORObject FromJSONStringWithPointers( throw new ArgumentNullException(nameof(jstring)); } return FromJSONStringWithPointers( - jstring, - JSONOptions.Default, - valpointers); + jstring, + JSONOptions.Default, + valpointers); } public static CBORObject FromJSONStringWithPointers( @@ -285,16 +288,18 @@ public static CBORObject FromJSONStringWithPointers( throw new ArgumentNullException(nameof(jstring)); } return ParseJSONValueWithPointers( - jstring, - 0, - jstring.Length, - options, - valpointers); + jstring, + 0, + jstring.Length, + options, + valpointers); } - internal IList Pointers { get { - return this.pointers; - } } + internal IList Pointers { + get { + return this.pointers; + } + } internal static CBORObject ParseJSONValueWithPointers( string jstring, @@ -307,9 +312,10 @@ internal static CBORObject ParseJSONValueWithPointers( var i = 0; for (i = index; i < endPos; ++i) { if (jstring[i] == '#') { - {hasHash = true; + { hasHash = true; + } + break; } - break; } } // No nonstandard comments, so just use FromJSONString if (!hasHash) { @@ -342,9 +348,10 @@ internal static CBORObject ParseJSONValue( var i = 0; for (i = index; i < endPos; ++i) { if (jstring[i] == '#') { - {hasHash = true; + { hasHash = true; + } + break; } - break; } } // No nonstandard comments, so just use FromJSONString if (!hasHash) { @@ -381,31 +388,31 @@ private int NextComment(StringBuilder sb) { if (c < 0x10000) { ++this.index; } else { - this.index += 2; -} + this.index += 2; + } if (c == 0x0d || c == 0x09 || c == 0x20) { while (this.index < this.endPos) { - c = this.jstring[this.index++]; - if (c != 0x0d && c != 0x09 && c != 0x20) { - --this.index; - break; - } - } - sb.Append((char)0x20); + c = this.jstring[this.index++]; + if (c != 0x0d && c != 0x09 && c != 0x20) { + --this.index; + break; + } + } + sb.Append((char)0x20); } else if (c == 0x0a) { c = this.SkipWhitespaceJSON(); if (c != 0x23) { // '#' character - // Console.WriteLine("last: " + ((char)c)); - return c; - } + // Console.WriteLine("last: " + ((char)c)); + return c; + } } else { -if (c <= 0xffff) { - { sb.Append((char)c); - } -} else if (c <= 0x10ffff) { - sb.Append((char)((((c - 0x10000) >> 10) & 0x3ff) | 0xd800)); - sb.Append((char)(((c - 0x10000) & 0x3ff) | 0xdc00)); -} + if (c <= 0xffff) { + { sb.Append((char)c); + } + } else if (c <= 0x10ffff) { + sb.Append((char)((((c - 0x10000) >> 10) & 0x3ff) | 0xd800)); + sb.Append((char)(((c - 0x10000) & 0x3ff) | 0xdc00)); + } } } return -1; @@ -431,7 +438,7 @@ private string GetJSONPointer() { } } } else { - this.RaiseError("Internal error"); + this.RaiseError("Internal error"); } } return sb.ToString(); @@ -453,14 +460,14 @@ private CBORObject ParseJSONObject(int depth) { while (true) { c = this.SkipWhitespaceJSON(); if (c == '#') { - // Nonstandard comment - if (myHashMap.Count == 0) { - var sb = new StringBuilder(); - c = this.NextComment(sb); - commentKey = sb.ToString(); - } else { - this.RaiseError("Unexpected comment"); - } + // Nonstandard comment + if (myHashMap.Count == 0) { + var sb = new StringBuilder(); + c = this.NextComment(sb); + commentKey = sb.ToString(); + } else { + this.RaiseError("Unexpected comment"); + } } switch (c) { case -1: @@ -473,8 +480,8 @@ private CBORObject ParseJSONObject(int depth) { return null; } if (commentKey != null) { - string[] keyptr = { commentKey, this.GetJSONPointer() }; - this.pointers.Add(keyptr); + string[] keyptr = { commentKey, this.GetJSONPointer() }; + this.pointers.Add(keyptr); } this.PopPointer(); return CBORObject.FromObject(myHashMap); @@ -506,9 +513,9 @@ private CBORObject ParseJSONObject(int depth) { depth); int newCount = myHashMap.Count; if (!this.options.AllowDuplicateKeys && - oldCount == newCount) { - this.RaiseError("Duplicate key already exists"); - return null; + oldCount == newCount) { + this.RaiseError("Duplicate key already exists"); + return null; } switch (nextchar[0]) { case ',': @@ -517,36 +524,37 @@ private CBORObject ParseJSONObject(int depth) { case '}': this.PopPointer(); if (commentKey != null) { - string[] keyptr = { commentKey, this.GetJSONPointer() }; - this.pointers.Add(keyptr); + string[] keyptr = { commentKey, this.GetJSONPointer() }; + this.pointers.Add(keyptr); } return CBORObject.FromObject(myHashMap); - default: this.RaiseError("Expected a ',' or '}'"); + default: + this.RaiseError("Expected a ',' or '}'"); break; } } } private void SetPointer(CBORObject obj) { - this.currPointer[this.currPointerStackSize - 1] = obj; + this.currPointer[this.currPointerStackSize - 1] = obj; } private void PushPointer() { -if (this.currPointerStackSize > this.currPointer.Count) { - this.RaiseError("Internal error"); - }; - if (this.currPointerStackSize == this.currPointer.Count) { - this.currPointer.Add(CBORObject.Null); - } else { - this.currPointer[this.currPointerStackSize] = CBORObject.Null; - } - ++this.currPointerStackSize; + if (this.currPointerStackSize > this.currPointer.Count) { + this.RaiseError("Internal error"); + } + if (this.currPointerStackSize == this.currPointer.Count) { + this.currPointer.Add(CBORObject.Null); + } else { + this.currPointer[this.currPointerStackSize] = CBORObject.Null; + } + ++this.currPointerStackSize; } private void PopPointer() { -if (this.currPointerStackSize < 0) { - this.RaiseError("Internal error"); - }; - --this.currPointerStackSize; + if (this.currPointerStackSize < 0) { + this.RaiseError("Internal error"); + } + --this.currPointerStackSize; } internal CBORObject ParseJSONArray(int depth) { @@ -587,7 +595,8 @@ internal CBORObject ParseJSONArray(int depth) { break; case ']': return myArrayList; - default: this.RaiseError("Expected a ',' or ']'"); + default: + this.RaiseError("Expected a ',' or ']'"); break; } } diff --git a/CBORTest/QueryStringHelper.cs b/CBORTest/QueryStringHelper.cs index 4c9b824d..c42aae13 100644 --- a/CBORTest/QueryStringHelper.cs +++ b/CBORTest/QueryStringHelper.cs @@ -11,13 +11,13 @@ namespace Test { public sealed class QueryStringHelper { private QueryStringHelper() { -} + } private static string[] SplitAt(string s, string delimiter) { if (delimiter == null || delimiter.Length == 0) { throw new ArgumentException(); } if (s == null || s.Length == 0) { - return new string[] { String.Empty}; + return new string[] { String.Empty }; } var index = 0; var first = true; @@ -27,7 +27,7 @@ private static string[] SplitAt(string s, string delimiter) { int index2 = s.IndexOf(delimiter, index, StringComparison.Ordinal); if (index2 < 0) { if (first) { - return new string[] { s}; + return new string[] { s }; } strings.Add(s.Substring(index)); break; @@ -140,9 +140,9 @@ private static string PercentDecodeUTF8(string str) { } } else { retString.Append((char)((((ret - 0x10000) >> 10) & - 0x3ff) | 0xd800)); + 0x3ff) | 0xd800)); retString.Append((char)(((ret - 0x10000) & 0x3ff) | - 0xdc00)); + 0xdc00)); } continue; } @@ -161,9 +161,9 @@ private static string PercentDecodeUTF8(string str) { } if (bytesNeeded > 0) { // we expected further bytes here, - // so throw an exception - throw new InvalidOperationException(); - } + // so throw an exception + throw new InvalidOperationException(); + } return retString.ToString(); } public static IList ParseQueryString( @@ -204,7 +204,7 @@ public static IList ParseQueryString( } name = name.Replace('+', ' '); value = value.Replace('+', ' '); - var pair = new string[] { name, value}; + var pair = new string[] { name, value }; pairs.Add(pair); } foreach (string[] pair in pairs) { @@ -218,7 +218,7 @@ public static IList ParseQueryString( private static string[] GetKeyPath(string s) { int index = s.IndexOf('['); if (index < 0) { // start bracket not found - return new string[] { s}; + return new string[] { s }; } var path = new List(); path.Add(s.Substring(0, index - 0)); @@ -257,11 +257,11 @@ public static string IntToString(int value) { int count; if (value < 100000) { if (neg) { - chars = new char[6]; - count = 5; + chars = new char[6]; + count = 5; } else { - chars = new char[5]; - count = 4; + chars = new char[5]; + count = 4; } while (value > 9) { int intdivvalue = unchecked((((value >> 1) * 52429) >> 18) & 16383); @@ -363,7 +363,7 @@ private static CBORObject ConvertListsToCBOR(IList dict) { } private static CBORObject ConvertListsToCBOR(IDictionary -dict) { + dict) { CBORObject cbor = CBORObject.NewMap(); foreach (string key in new List(dict.Keys)) { object di = dict[key]; @@ -403,7 +403,7 @@ private static void ConvertLists(IList list) { } private static IDictionary ConvertLists( - IDictionary dict) { + IDictionary dict) { foreach (string key in new List(dict.Keys)) { object di = dict[key]; IDictionary value = di as IDictionary; @@ -465,15 +465,15 @@ private static IDictionary QueryStringToDictInternal( } } return root; - } + } - public static IDictionary QueryStringToDict(string query, + public static IDictionary QueryStringToDict(string query, string delimiter) { // Convert array-like dictionaries to ILists return ConvertLists(QueryStringToDictInternal(query, delimiter)); } - public static CBORObject QueryStringToCBOR(string query, + public static CBORObject QueryStringToCBOR(string query, string delimiter) { // Convert array-like dictionaries to ILists return ConvertListsToCBOR(QueryStringToDictInternal(query, delimiter)); diff --git a/docs/PeterO.Cbor.CBORObject.md b/docs/PeterO.Cbor.CBORObject.md index b8d61c79..78ff8dcf 100644 --- a/docs/PeterO.Cbor.CBORObject.md +++ b/docs/PeterO.Cbor.CBORObject.md @@ -2444,7 +2444,7 @@ A CBORObject object with the same value as the.NET decimal. public static PeterO.Cbor.CBORObject FromObject( double value); -Generates a CBOR object from a 64-bit floating-point number. +Generates a CBOR object from a 64-bit floating-point number. The input value can be a not-a-number (NaN) value (such as `Double.NaN` ); however, NaN values have multiple forms that are equivalent for many applications' purposes, and `Double.NaN` is only one of these equivalent forms. In fact, `CBORObject.FromObject(Double.NaN)` could produce a CBOR-encoded object that differs between DotNet and Java, because `Double.NaN` may have a different form in DotNet and Java (for example, the NaN value's sign may be negative in DotNet, but positive in Java). Parameters: @@ -2461,7 +2461,7 @@ A CBOR object generated from the given number. public static PeterO.Cbor.CBORObject FromObject( float value); -Generates a CBOR object from a 32-bit floating-point number. +Generates a CBOR object from a 32-bit floating-point number. The input value can be a not-a-number (NaN) value (such as `Single.NaN` in DotNet or Float.NaN in Java); however, NaN values have multiple forms that are equivalent for many applications' purposes, and `Single.NaN` / `Float.NaN` is only one of these equivalent forms. In fact, `CBORObject.FromObject(Single.NaN)` or `CBORObject.FromObject(Float.NaN)` could produce a CBOR-encoded object that differs between DotNet and Java, because `Single.NaN` / `Float.NaN` may have a different form in DotNet and Java (for example, the NaN value's sign may be negative in DotNet, but positive in Java). Parameters: @@ -4204,22 +4204,22 @@ A byte array containing the converted in JSON format. The example code given below (originally written in C# for the.NET version) can be used to write out certain keys of a CBOR map in a given order to a JSON string. /* Generates a JSON string of 'mapObj' whose keys are in the order - given - in 'keys' . Only keys found in 'keys' will be written if they exist in - 'mapObj'. */ private static string KeysToJSONMap(CBORObject mapObj, - IList keys) { if (mapObj == null) { throw new - ArgumentNullException)nameof(mapObj));} - if (keys == null) { throw new - ArgumentNullException)nameof(keys));} - if (obj.Type != CBORType.Map) { - throw new ArgumentException("'obj' is not a map."); } StringBuilder - builder = new StringBuilder(); var first = true; builder.Append("{"); - for (CBORObject key in keys) { if (mapObj.ContainsKey(key)) { if - (!first) {builder.Append(", ");} var keyString=(key.CBORType == - CBORType.String) ? key.AsString() : key.ToJSONString(); - builder.Append(CBORObject.FromObject(keyString) .ToJSONString()) - .Append(":").Append(mapObj[key].ToJSONString()); first=false; } } return - builder.Append("}").ToString(); } + given + in 'keys' . Only keys found in 'keys' will be written if they exist in + 'mapObj'. */ private static string KeysToJSONMap(CBORObject mapObj, + IList keys) { if (mapObj == null) { throw new + ArgumentNullException)nameof(mapObj));} + if (keys == null) { throw new + ArgumentNullException)nameof(keys));} + if (obj.Type != CBORType.Map) { + throw new ArgumentException("'obj' is not a map."); } StringBuilder + builder = new StringBuilder(); var first = true; builder.Append("{"); + for (CBORObject key in keys) { if (mapObj.ContainsKey(key)) { if + (!first) {builder.Append(", ");} var keyString=(key.CBORType == + CBORType.String) ? key.AsString() : key.ToJSONString(); + builder.Append(CBORObject.FromObject(keyString) .ToJSONString()) + .Append(":").Append(mapObj[key].ToJSONString()); first=false; } } return + builder.Append("}").ToString(); } .