From 90313e260c8610ce71ac96a92e7280188b56c9f0 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Wed, 21 Oct 2020 22:40:52 +0200 Subject: [PATCH 1/2] fix SqlHierarchyId serialization and deserialization --- .../HierarchyId/DeserializeFromBinary.cs | 130 +++++++++++++++++- .../Microsoft.SqlServer.Types.Tests.csproj | 5 +- .../SqlHierarchy/BitPattern.cs | 9 +- .../SqlHierarchy/KnownPatterns.cs | 8 ++ .../SqlHierarchyId.cs | 22 ++- 5 files changed, 159 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs index a746714..7807679 100644 --- a/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs +++ b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs @@ -1,7 +1,11 @@ using Microsoft.SqlServer.Types; +using Microsoft.SqlServer.Types.SqlHierarchy; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; +using System.Collections; +using System.Data.SqlClient; using System.IO; +using System.Text; namespace Microsoft.SqlServer.Types.Tests.HierarchyId { @@ -48,5 +52,129 @@ public void TestSqlHiarchy2() } Assert.AreEqual("/1/-2.18/", hid.ToString()); } + + static bool CompareWithSqlServer = false; + + [DataTestMethod] + [DataRow("/-4294971464/")] + [DataRow("/4294972495/")] + [DataRow("/3.2725686107/")] + [DataRow("/0/")] + [DataRow("/1/")] + [DataRow("/1.0.2/")] + [DataRow("/1.1.2/")] + [DataRow("/1.2.2/")] + [DataRow("/1.3.2/")] + [DataRow("/3.0/")] + public void SerializeDeserialize(string route) + { + var parsed = SqlHierarchyId.Parse(route); + var ms = new MemoryStream(); + parsed.Write(new BinaryWriter(ms)); + ms.Position = 0; + var dumMem = Dump(ms); + ms.Position = 0; + var roundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId(); + roundTrip.Read(new BinaryReader(ms)); + if (parsed != roundTrip) + Assert.AreEqual(parsed, roundTrip); + + if (CompareWithSqlServer) + { + /* + CREATE TABLE [dbo].[TreeNode]( + [Id] [int] IDENTITY(1,1) NOT NULL, + [Route] [hierarchyid] NOT NULL + ) + */ + using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=HierarchyTest;Integrated Security=true")) + { + con.Open(); + var id = new SqlCommand($"INSERT INTO [dbo].[TreeNode] (Route) output INSERTED.ID VALUES ('{route}') ", con).ExecuteScalar(); + + using (var reader = new SqlCommand($"SELECT Route FROM [dbo].[TreeNode] WHERE ID = " + id, con).ExecuteReader()) + { + while (reader.Read()) + { + var sqlRoundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId(); + var dumSql = Dump(reader.GetStream(0)); + Assert.AreEqual(dumMem, dumSql); + sqlRoundTrip.Read(new BinaryReader(reader.GetStream(0))); + if (parsed != sqlRoundTrip) + Assert.AreEqual(parsed, sqlRoundTrip); + } + } + } + } + } + + [TestMethod] + public void DeserializeRandom() + { + Random r = new Random(); + for (int i = 0; i < 10000; i++) + { + SerializeDeserialize(RandomHierarhyId(r)); + } + } + + public static string RandomHierarhyId(Random random) + { + StringBuilder sb = new StringBuilder(); + sb.Append("/"); + var levels = random.Next(4); + for (int i = 0; i < levels; i++) + { + var subLevels = random.Next(1, 4); + for (int j = 0; j < subLevels; j++) + { + var pattern = KnownPatterns.RandomPattern(random); + sb.Append(random.NextLong(pattern.MinValue, pattern.MaxValue + 1).ToString()); + if (j < subLevels - 1) + sb.Append("."); + } + sb.Append("/"); + } + + return sb.ToString(); + } + + + static string Dump(Stream ms) + { + return new BitReader(new BinaryReader(ms)).ToString(); + } + } + + public static class RandomExtensionMethods + { + /// + /// Returns a random long from min (inclusive) to max (exclusive) + /// + /// The given random instance + /// The inclusive minimum bound + /// The exclusive maximum bound. Must be greater than min + public static long NextLong(this Random random, long min, long max) + { + if (max <= min) + throw new ArgumentOutOfRangeException("max", "max must be > min!"); + + //Working with ulong so that modulo works correctly with values > long.MaxValue + ulong uRange = (ulong)(max - min); + + //Prevent a modolo bias; see https://stackoverflow.com/a/10984975/238419 + //for more information. + //In the worst case, the expected number of calls is 2 (though usually it's + //much closer to 1) so this loop doesn't really hurt performance at all. + ulong ulongRand; + do + { + byte[] buf = new byte[8]; + random.NextBytes(buf); + ulongRand = (ulong)BitConverter.ToInt64(buf, 0); + } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); + + return (long)(ulongRand % uRange) + min; + } + } } -} diff --git a/src/Microsoft.SqlServer.Types.Tests/Microsoft.SqlServer.Types.Tests.csproj b/src/Microsoft.SqlServer.Types.Tests/Microsoft.SqlServer.Types.Tests.csproj index 3145998..3a63688 100644 --- a/src/Microsoft.SqlServer.Types.Tests/Microsoft.SqlServer.Types.Tests.csproj +++ b/src/Microsoft.SqlServer.Types.Tests/Microsoft.SqlServer.Types.Tests.csproj @@ -1,14 +1,17 @@  - + netcoreapp2.1;net461 false + enable + 8.0 + diff --git a/src/Microsoft.SqlServer.Types/SqlHierarchy/BitPattern.cs b/src/Microsoft.SqlServer.Types/SqlHierarchy/BitPattern.cs index 7f05f28..be27491 100644 --- a/src/Microsoft.SqlServer.Types/SqlHierarchy/BitPattern.cs +++ b/src/Microsoft.SqlServer.Types/SqlHierarchy/BitPattern.cs @@ -46,15 +46,12 @@ internal bool ContainsValue(long value) public override string ToString() => Pattern; - public ulong EncodeValue(long val, bool isLast) + public ulong EncodeValue(long val) { ulong expand = Expand(PatternMask, val - MinValue); ulong value = PatternOnes | expand | 1; - if (!isLast) - value++; - return value; } @@ -69,12 +66,12 @@ private ulong Expand(ulong mask, long value) return Expand(mask >> 1, value) << 1; } - internal int Decode(ulong encodedValue, out bool isLast) + internal long Decode(ulong encodedValue, out bool isLast) { var decodedValue = Compress(encodedValue, PatternMask); isLast = (encodedValue & 0x1) == 0x1; - return (int)((isLast ? decodedValue : decodedValue - 1) + MinValue); + return ((isLast ? decodedValue : decodedValue - 1) + MinValue); } private long Compress(ulong value, ulong mask) diff --git a/src/Microsoft.SqlServer.Types/SqlHierarchy/KnownPatterns.cs b/src/Microsoft.SqlServer.Types/SqlHierarchy/KnownPatterns.cs index 8a23b6d..c0a2079 100644 --- a/src/Microsoft.SqlServer.Types/SqlHierarchy/KnownPatterns.cs +++ b/src/Microsoft.SqlServer.Types/SqlHierarchy/KnownPatterns.cs @@ -27,6 +27,14 @@ internal static class KnownPatterns new BitPattern(-4294971464, -4169, "000101xxxxxxxxxxxxxxxxxxx0xxxxxx0xxx0x1xxxT"), }; + internal static BitPattern RandomPattern(Random r) + { + var index = r.Next(PositivePatterns.Length + NegativePatterns.Length); + + return index < PositivePatterns.Length ? PositivePatterns[index] : + NegativePatterns[index - PositivePatterns.Length]; + } + internal static BitPattern GetPatternByValue(long value) { if (value >= 0) diff --git a/src/Microsoft.SqlServer.Types/SqlHierarchyId.cs b/src/Microsoft.SqlServer.Types/SqlHierarchyId.cs index 7210394..9919ef4 100644 --- a/src/Microsoft.SqlServer.Types/SqlHierarchyId.cs +++ b/src/Microsoft.SqlServer.Types/SqlHierarchyId.cs @@ -260,15 +260,23 @@ public void Write(BinaryWriter w) var subNodes = nodes[i]; for (int j = 0; j < subNodes.Length; j++) { - long val = subNodes[j]; - - BitPattern p = KnownPatterns.GetPatternByValue(val); - bool isLast = j == (subNodes.Length - 1); - ulong value = p.EncodeValue(val, isLast); - bw.Write(value, p.BitLength); + long val = subNodes[j]; + + if (isLast) + { + BitPattern p = KnownPatterns.GetPatternByValue(val); + ulong value = p.EncodeValue(val); + bw.Write(value, p.BitLength); + } + else + { + BitPattern p = KnownPatterns.GetPatternByValue(val + 1); + ulong value = p.EncodeValue(val + 1) - 1; + bw.Write(value, p.BitLength); + } } } @@ -305,7 +313,7 @@ public void Read(BinaryReader r) ulong encodedValue = bitR.Read(p.BitLength); - int value = p.Decode(encodedValue, out bool isLast); + long value = p.Decode(encodedValue, out bool isLast); step.Add(value); From 87d44e442d757f3a2c60aca6858296e366e904ec Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Tue, 3 Nov 2020 21:46:43 +0100 Subject: [PATCH 2/2] add proper HierarchyDbTests --- .../DatabaseUtil.cs | 34 ++++ .../Geometry/DBTests.cs | 26 +-- .../HierarchyId/DeserializeFromBinary.cs | 180 ----------------- .../HierarchyId/DeserializeFromBinaryTests.cs | 56 ++++++ .../HierarchyId/HierarchyDbTests.cs | 189 ++++++++++++++++++ 5 files changed, 281 insertions(+), 204 deletions(-) create mode 100644 src/Microsoft.SqlServer.Types.Tests/DatabaseUtil.cs delete mode 100644 src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs create mode 100644 src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinaryTests.cs create mode 100644 src/Microsoft.SqlServer.Types.Tests/HierarchyId/HierarchyDbTests.cs diff --git a/src/Microsoft.SqlServer.Types.Tests/DatabaseUtil.cs b/src/Microsoft.SqlServer.Types.Tests/DatabaseUtil.cs new file mode 100644 index 0000000..f153692 --- /dev/null +++ b/src/Microsoft.SqlServer.Types.Tests/DatabaseUtil.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Microsoft.SqlServer.Types.Tests +{ + static class DatabaseUtil + { + internal static void CreateSqlDatabase(string filename) + { + string databaseName = System.IO.Path.GetFileNameWithoutExtension(filename); + if (File.Exists(filename)) + File.Delete(filename); + if (File.Exists(filename.Replace(".mdf", "_log.ldf"))) + File.Delete(filename.Replace(".mdf", "_log.ldf")); + using (var connection = new System.Data.SqlClient.SqlConnection( + @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=master; Integrated Security=true;")) + { + connection.Open(); + using (var command = connection.CreateCommand()) + { + command.CommandText = + String.Format("CREATE DATABASE {0} ON PRIMARY (NAME={0}, FILENAME='{1}')", databaseName, filename); + command.ExecuteNonQuery(); + + command.CommandText = + String.Format("EXEC sp_detach_db '{0}', 'true'", databaseName); + command.ExecuteNonQuery(); + } + } + } + } +} diff --git a/src/Microsoft.SqlServer.Types.Tests/Geometry/DBTests.cs b/src/Microsoft.SqlServer.Types.Tests/Geometry/DBTests.cs index 47b8a56..02a5474 100644 --- a/src/Microsoft.SqlServer.Types.Tests/Geometry/DBTests.cs +++ b/src/Microsoft.SqlServer.Types.Tests/Geometry/DBTests.cs @@ -33,7 +33,7 @@ public static void Init() if(path == null) { path = Path.Combine(new FileInfo(typeof(DBTests).Assembly.Location).Directory.FullName, "UnitTestData.mdf"); - CreateSqlDatabase(path); + DatabaseUtil.CreateSqlDatabase(path); using (var conn = new System.Data.SqlClient.SqlConnection(connstr + path)) { conn.Open(); @@ -58,29 +58,7 @@ public DBTests() conn.Open(); } - private static void CreateSqlDatabase(string filename) - { - string databaseName = System.IO.Path.GetFileNameWithoutExtension(filename); - if (File.Exists(filename)) - File.Delete(filename); - if (File.Exists(filename.Replace(".mdf","_log.ldf"))) - File.Delete(filename.Replace(".mdf", "_log.ldf")); - using (var connection = new System.Data.SqlClient.SqlConnection( - @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=master; Integrated Security=true;")) - { - connection.Open(); - using (var command = connection.CreateCommand()) - { - command.CommandText = - String.Format("CREATE DATABASE {0} ON PRIMARY (NAME={0}, FILENAME='{1}')", databaseName, filename); - command.ExecuteNonQuery(); - - command.CommandText = - String.Format("EXEC sp_detach_db '{0}', 'true'", databaseName); - command.ExecuteNonQuery(); - } - } - } + public void Dispose() { conn.Close(); diff --git a/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs deleted file mode 100644 index 7807679..0000000 --- a/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinary.cs +++ /dev/null @@ -1,180 +0,0 @@ -using Microsoft.SqlServer.Types; -using Microsoft.SqlServer.Types.SqlHierarchy; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.Collections; -using System.Data.SqlClient; -using System.IO; -using System.Text; - -namespace Microsoft.SqlServer.Types.Tests.HierarchyId -{ - /// - /// Deserialize tests based on examples in the UDT specification - /// - [TestClass] - [TestCategory("Deserialize")] - [TestCategory("SqlHierarchyId")] - public class DeserializeFromBinary - { - - [TestMethod] - public void TestSqlHiarchy1() - { - // The first child of the root node, with a logical representation of / 1 /, is represented as the following bit sequence: - // 01011000 - // The first two bits, 01, are the L1 field, meaning that the first node has a label between 0(zero) and 3.The next two bits, - // 01, are the O1 field and are interpreted as the integer 1.Adding this to the beginning of the range specified by the L1 yields 1. - // The next bit, with the value 1, is the F1 field, which means that this is a "real" level, with 1 followed by a slash in the logical - // representation.The final three bits, 000, are the W field, padding the representation to the nearest byte. - byte[] bytes = { 0x58 }; //01011000 - var hid = new Microsoft.SqlServer.Types.SqlHierarchyId(); - using (var r = new BinaryReader(new MemoryStream(bytes))) - { - hid.Read(r); - } - Assert.AreEqual("/1/", hid.ToString()); - } - - [TestMethod] - public void TestSqlHiarchy2() - { - // As a more complicated example, the node with logical representation / 1 / -2.18 / (the child with label - 2.18 of the child with label 1 of the root node) is represented as the following sequence of bits(a space has been inserted after every grouping of 8 bits to make the sequence easier to follow): - // 01011001 11111011 00000101 01000000 - // The first three fields are the same as in the first example.That is, the first two bits(01) are the L1 field, the second two bits(01) are the O1 field, and the fifth bit(1) is the F1 field.This encodes the / 1 / portion of the logical representation. - // The next 5 bits(00111) are the L2 field, so the next integer is between - 8 and - 1.The following 3 bits(111) are the O2 field, representing the offset 7 from the beginning of this range.Thus, the L2 and O2 fields together encode the integer - 1.The next bit(0) is the F2 field.Because it is 0(zero), this level is fake, and 1 has to be subtracted from the integer yielded by the L2 and O2 fields. Therefore, the L2, O2, and F2 fields together represent -2 in the logical representation of this node. - // The next 3 bits(110) are the L3 field, so the next integer is between 16 and 79.The subsequent 8 bits(00001010) are the L4 field. Removing the anti - ambiguity bits from there(the third bit(0) and the fifth bit(1)) leaves 000010, which is the binary representation of 2.Thus, the integer encoded by the L3 and O3 fields is 16 + 2, which is 18.The next bit(1) is the F3 field, representing the slash(/) after the 18 in the logical representation.The final 6 bits(000000) are the W field, padding the physical representation to the nearest byte. - byte[] bytes = { 0x59,0xFB,0x05,0x40 }; //01011001 11111011 00000101 01000000 - var hid = new Microsoft.SqlServer.Types.SqlHierarchyId(); - using (var r = new BinaryReader(new MemoryStream(bytes))) - { - hid.Read(r); - } - Assert.AreEqual("/1/-2.18/", hid.ToString()); - } - - static bool CompareWithSqlServer = false; - - [DataTestMethod] - [DataRow("/-4294971464/")] - [DataRow("/4294972495/")] - [DataRow("/3.2725686107/")] - [DataRow("/0/")] - [DataRow("/1/")] - [DataRow("/1.0.2/")] - [DataRow("/1.1.2/")] - [DataRow("/1.2.2/")] - [DataRow("/1.3.2/")] - [DataRow("/3.0/")] - public void SerializeDeserialize(string route) - { - var parsed = SqlHierarchyId.Parse(route); - var ms = new MemoryStream(); - parsed.Write(new BinaryWriter(ms)); - ms.Position = 0; - var dumMem = Dump(ms); - ms.Position = 0; - var roundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId(); - roundTrip.Read(new BinaryReader(ms)); - if (parsed != roundTrip) - Assert.AreEqual(parsed, roundTrip); - - if (CompareWithSqlServer) - { - /* - CREATE TABLE [dbo].[TreeNode]( - [Id] [int] IDENTITY(1,1) NOT NULL, - [Route] [hierarchyid] NOT NULL - ) - */ - using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=HierarchyTest;Integrated Security=true")) - { - con.Open(); - var id = new SqlCommand($"INSERT INTO [dbo].[TreeNode] (Route) output INSERTED.ID VALUES ('{route}') ", con).ExecuteScalar(); - - using (var reader = new SqlCommand($"SELECT Route FROM [dbo].[TreeNode] WHERE ID = " + id, con).ExecuteReader()) - { - while (reader.Read()) - { - var sqlRoundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId(); - var dumSql = Dump(reader.GetStream(0)); - Assert.AreEqual(dumMem, dumSql); - sqlRoundTrip.Read(new BinaryReader(reader.GetStream(0))); - if (parsed != sqlRoundTrip) - Assert.AreEqual(parsed, sqlRoundTrip); - } - } - } - } - } - - [TestMethod] - public void DeserializeRandom() - { - Random r = new Random(); - for (int i = 0; i < 10000; i++) - { - SerializeDeserialize(RandomHierarhyId(r)); - } - } - - public static string RandomHierarhyId(Random random) - { - StringBuilder sb = new StringBuilder(); - sb.Append("/"); - var levels = random.Next(4); - for (int i = 0; i < levels; i++) - { - var subLevels = random.Next(1, 4); - for (int j = 0; j < subLevels; j++) - { - var pattern = KnownPatterns.RandomPattern(random); - sb.Append(random.NextLong(pattern.MinValue, pattern.MaxValue + 1).ToString()); - if (j < subLevels - 1) - sb.Append("."); - } - sb.Append("/"); - } - - return sb.ToString(); - } - - - static string Dump(Stream ms) - { - return new BitReader(new BinaryReader(ms)).ToString(); - } - } - - public static class RandomExtensionMethods - { - /// - /// Returns a random long from min (inclusive) to max (exclusive) - /// - /// The given random instance - /// The inclusive minimum bound - /// The exclusive maximum bound. Must be greater than min - public static long NextLong(this Random random, long min, long max) - { - if (max <= min) - throw new ArgumentOutOfRangeException("max", "max must be > min!"); - - //Working with ulong so that modulo works correctly with values > long.MaxValue - ulong uRange = (ulong)(max - min); - - //Prevent a modolo bias; see https://stackoverflow.com/a/10984975/238419 - //for more information. - //In the worst case, the expected number of calls is 2 (though usually it's - //much closer to 1) so this loop doesn't really hurt performance at all. - ulong ulongRand; - do - { - byte[] buf = new byte[8]; - random.NextBytes(buf); - ulongRand = (ulong)BitConverter.ToInt64(buf, 0); - } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); - - return (long)(ulongRand % uRange) + min; - } - } - } diff --git a/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinaryTests.cs b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinaryTests.cs new file mode 100644 index 0000000..1d5deab --- /dev/null +++ b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/DeserializeFromBinaryTests.cs @@ -0,0 +1,56 @@ +using Microsoft.SqlServer.Types; +using Microsoft.SqlServer.Types.SqlHierarchy; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections; +using System.Data.SqlClient; +using System.IO; +using System.Text; + +namespace Microsoft.SqlServer.Types.Tests.HierarchyId +{ + /// + /// Deserialize tests based on examples in the UDT specification + /// + [TestClass] + [TestCategory("Deserialize")] + [TestCategory("SqlHierarchyId")] + public class DeserializeFromBinaryTests + { + + [TestMethod] + public void TestSqlHiarchy1() + { + // The first child of the root node, with a logical representation of / 1 /, is represented as the following bit sequence: + // 01011000 + // The first two bits, 01, are the L1 field, meaning that the first node has a label between 0(zero) and 3.The next two bits, + // 01, are the O1 field and are interpreted as the integer 1.Adding this to the beginning of the range specified by the L1 yields 1. + // The next bit, with the value 1, is the F1 field, which means that this is a "real" level, with 1 followed by a slash in the logical + // representation.The final three bits, 000, are the W field, padding the representation to the nearest byte. + byte[] bytes = { 0x58 }; //01011000 + var hid = new Microsoft.SqlServer.Types.SqlHierarchyId(); + using (var r = new BinaryReader(new MemoryStream(bytes))) + { + hid.Read(r); + } + Assert.AreEqual("/1/", hid.ToString()); + } + + [TestMethod] + public void TestSqlHiarchy2() + { + // As a more complicated example, the node with logical representation / 1 / -2.18 / (the child with label - 2.18 of the child with label 1 of the root node) is represented as the following sequence of bits(a space has been inserted after every grouping of 8 bits to make the sequence easier to follow): + // 01011001 11111011 00000101 01000000 + // The first three fields are the same as in the first example.That is, the first two bits(01) are the L1 field, the second two bits(01) are the O1 field, and the fifth bit(1) is the F1 field.This encodes the / 1 / portion of the logical representation. + // The next 5 bits(00111) are the L2 field, so the next integer is between - 8 and - 1.The following 3 bits(111) are the O2 field, representing the offset 7 from the beginning of this range.Thus, the L2 and O2 fields together encode the integer - 1.The next bit(0) is the F2 field.Because it is 0(zero), this level is fake, and 1 has to be subtracted from the integer yielded by the L2 and O2 fields. Therefore, the L2, O2, and F2 fields together represent -2 in the logical representation of this node. + // The next 3 bits(110) are the L3 field, so the next integer is between 16 and 79.The subsequent 8 bits(00001010) are the L4 field. Removing the anti - ambiguity bits from there(the third bit(0) and the fifth bit(1)) leaves 000010, which is the binary representation of 2.Thus, the integer encoded by the L3 and O3 fields is 16 + 2, which is 18.The next bit(1) is the F3 field, representing the slash(/) after the 18 in the logical representation.The final 6 bits(000000) are the W field, padding the physical representation to the nearest byte. + byte[] bytes = { 0x59, 0xFB, 0x05, 0x40 }; //01011001 11111011 00000101 01000000 + var hid = new Microsoft.SqlServer.Types.SqlHierarchyId(); + using (var r = new BinaryReader(new MemoryStream(bytes))) + { + hid.Read(r); + } + Assert.AreEqual("/1/-2.18/", hid.ToString()); + } + } +} diff --git a/src/Microsoft.SqlServer.Types.Tests/HierarchyId/HierarchyDbTests.cs b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/HierarchyDbTests.cs new file mode 100644 index 0000000..0387f11 --- /dev/null +++ b/src/Microsoft.SqlServer.Types.Tests/HierarchyId/HierarchyDbTests.cs @@ -0,0 +1,189 @@ +using Microsoft.SqlServer.Types.SqlHierarchy; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.IO; +using System.Text; + +namespace Microsoft.SqlServer.Types.Tests.HierarchyId +{ + [TestCategory("Database")] + [TestCategory("SqlHierarchyId")] + [TestClass] + public class HierarchyDbTests : IDisposable + { + const string DataSource = @"Data Source=(localdb)\mssqllocaldb;Integrated Security=True;AttachDbFileName="; + + + public static string DropTables = @"IF OBJECT_ID('dbo.TreeNode', 'U') IS NOT NULL DROP TABLE TreeNode;"; + + public static string CreateTables = @" +-- hierarchy_tests +CREATE TABLE [dbo].[TreeNode]([Id] [int] IDENTITY(1,1) NOT NULL, [Route] [hierarchyid] NOT NULL); +"; + + private readonly SqlConnection _connection; + private static string _path = null!; + private static readonly object lockObj = new object(); + static HierarchyDbTests() + { + Init(); + } + + private static void Init() + { + lock (lockObj) + { + if (_path == null) + { + _path = Path.Combine(new FileInfo(typeof(HierarchyDbTests).Assembly.Location).Directory.FullName, "HierarchyUnitTestData.mdf"); + DatabaseUtil.CreateSqlDatabase(_path); + using (var conn = new SqlConnection(DataSource + _path)) + { + conn.Open(); + var cmd = conn.CreateCommand(); + cmd.CommandText = DropTables; + cmd.ExecuteNonQuery(); + cmd.CommandText = CreateTables; + cmd.ExecuteNonQuery(); + conn.Close(); + } + } + } + } + + private static string ConnectionString => DataSource + _path; + + public HierarchyDbTests() + { + Init(); + _connection = new SqlConnection(ConnectionString); + _connection.Open(); + } + + public void Dispose() + { + _connection.Close(); + _connection.Dispose(); + } + + + + + [DataTestMethod] + [DataRow("/-4294971464/")] + [DataRow("/4294972495/")] + [DataRow("/3.2725686107/")] + [DataRow("/0/")] + [DataRow("/1/")] + [DataRow("/1.0.2/")] + [DataRow("/1.1.2/")] + [DataRow("/1.2.2/")] + [DataRow("/1.3.2/")] + [DataRow("/3.0/")] + public void SerializeDeserialize(string route) + { + var parsed = SqlHierarchyId.Parse(route); + var ms = new MemoryStream(); + parsed.Write(new BinaryWriter(ms)); + ms.Position = 0; + var dumMem = Dump(ms); + ms.Position = 0; + var roundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId(); + roundTrip.Read(new BinaryReader(ms)); + if (parsed != roundTrip) + Assert.AreEqual(parsed, roundTrip); //breakpoint here + + var id = new SqlCommand($"INSERT INTO [dbo].[TreeNode] (Route) output INSERTED.ID VALUES ('{route}') ", _connection).ExecuteScalar(); + + using (var reader = new SqlCommand($"SELECT Route FROM [dbo].[TreeNode] WHERE ID = " + id, _connection).ExecuteReader()) + { + while (reader.Read()) + { + var sqlRoundTrip = new Microsoft.SqlServer.Types.SqlHierarchyId(); + var dumSql = Dump(reader.GetStream(0)); + Assert.AreEqual(dumMem, dumSql); + sqlRoundTrip.Read(new BinaryReader(reader.GetStream(0))); + if (parsed != sqlRoundTrip) + Assert.AreEqual(parsed, sqlRoundTrip); //breakpoint here + } + } + } + + [DataTestMethod] + [DynamicData(nameof(GetData), DynamicDataSourceType.Method)] + public void SerializeDeserializeRandom(string route) + { + SerializeDeserialize(route); + } + + private const int CountOfGeneratedCases = 1000; + public static IEnumerable GetData() + { + Random r = new Random(); + for (var i = 0; i < CountOfGeneratedCases; i++) + { + yield return new object[] { RandomHierarhyId(r)}; + } + } + + public static string RandomHierarhyId(Random random) + { + StringBuilder sb = new StringBuilder(); + sb.Append("/"); + var levels = random.Next(4); + for (int i = 0; i < levels; i++) + { + var subLevels = random.Next(1, 4); + for (int j = 0; j < subLevels; j++) + { + var pattern = KnownPatterns.RandomPattern(random); + sb.Append(random.NextLong(pattern.MinValue, pattern.MaxValue + 1).ToString()); + if (j < subLevels - 1) + sb.Append("."); + } + sb.Append("/"); + } + + return sb.ToString(); + } + + static string Dump(Stream ms) + { + return new BitReader(new BinaryReader(ms)).ToString(); + } + } + + public static class RandomExtensionMethods + { + /// + /// Returns a random long from min (inclusive) to max (exclusive) + /// + /// The given random instance + /// The inclusive minimum bound + /// The exclusive maximum bound. Must be greater than min + public static long NextLong(this Random random, long min, long max) + { + if (max <= min) + throw new ArgumentOutOfRangeException("max", "max must be > min!"); + + //Working with ulong so that modulo works correctly with values > long.MaxValue + ulong uRange = (ulong)(max - min); + + //Prevent a modolo bias; see https://stackoverflow.com/a/10984975/238419 + //for more information. + //In the worst case, the expected number of calls is 2 (though usually it's + //much closer to 1) so this loop doesn't really hurt performance at all. + ulong ulongRand; + do + { + byte[] buf = new byte[8]; + random.NextBytes(buf); + ulongRand = (ulong)BitConverter.ToInt64(buf, 0); + } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); + + return (long)(ulongRand % uRange) + min; + } + } +}