public void Test_Uuid64_TryWriteTo()
        {
            var original = Uuid64.Parse("01234567-89ABCDEF");

            Assume.That(original.ToUInt64(), Is.EqualTo(0x0123456789ABCDEF));

            // span with more space
            var scratch = MutableSlice.Repeat(0xAA, 16);

            Assert.That(original.TryWriteTo(scratch.Span), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            // span with no offset and exact size
            scratch = MutableSlice.Repeat(0xAA, 16);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(0, 8)), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            // span with offset
            scratch = MutableSlice.Repeat(0xAA, 16);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(4)), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 01 23 45 67 89 AB CD EF AA AA AA AA"));

            // span with offset and exact size
            scratch = MutableSlice.Repeat(0xAA, 16);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(4, 8)), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 01 23 45 67 89 AB CD EF AA AA AA AA"));

            // errors

            Assert.That(original.TryWriteTo(Span <byte> .Empty), Is.False, "Target buffer is empty");

            scratch = MutableSlice.Repeat(0xAA, 16);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(0, 7)), Is.False, "Target buffer is too small");
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA"), "Buffer should not have been overwritten!");
        }
        public void Test_Uuid96_WriteTo()
        {
            var original = Uuid96.Parse("1E2D3C4B-01234567-89ABCDEF");

            (var hi, var lo) = original;
            Assume.That(hi, Is.EqualTo(0x1E2D3C4B));
            Assume.That(lo, Is.EqualTo(0x0123456789ABCDEF));

            // span with more space
            var scratch = MutableSlice.Repeat(0xAA, 20);

            original.WriteTo(scratch.Span);
            Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            // span with no offset and exact size
            scratch = MutableSlice.Repeat(0xAA, 20);
            original.WriteTo(scratch.Span.Slice(0, 12));
            Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            // span with offset
            scratch = MutableSlice.Repeat(0xAA, 20);
            original.WriteTo(scratch.Span.Slice(4));
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA"));

            // span with offset and exact size
            scratch = MutableSlice.Repeat(0xAA, 20);
            original.WriteTo(scratch.Span.Slice(4, 12));
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA"));

            unsafe
            {
                Span <byte> buf = stackalloc byte[20];
                buf.Fill(0xAA);
                original.WriteToUnsafe(buf.Slice(3));
                Assert.That(buf.ToArray().AsSlice().ToString("X"), Is.EqualTo("AA AA AA 1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA AA"));
            }

            // errors

            Assert.That(() => original.WriteTo(Span <byte> .Empty), Throws.InstanceOf <ArgumentException>(), "Target buffer is empty");

            scratch = MutableSlice.Repeat(0xAA, 16);
            Assert.That(() => original.WriteTo(scratch.Span.Slice(0, 11)), Throws.InstanceOf <ArgumentException>(), "Target buffer is too small");
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA"), "Buffer should not have been overwritten!");
        }
        public void Test_Uuid96_TryWriteTo()
        {
            var original = Uuid96.Parse("1E2D3C4B-01234567-89ABCDEF");

            (var hi, var lo) = original;
            Assume.That(hi, Is.EqualTo(0x1E2D3C4B));
            Assume.That(lo, Is.EqualTo(0x0123456789ABCDEF));

            // span with more space
            var scratch = MutableSlice.Repeat(0xAA, 20);

            Assert.That(original.TryWriteTo(scratch.Span), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            // span with no offset and exact size
            scratch = MutableSlice.Repeat(0xAA, 20);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(0, 12)), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            // span with offset
            scratch = MutableSlice.Repeat(0xAA, 20);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(4)), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA"));

            // span with offset and exact size
            scratch = MutableSlice.Repeat(0xAA, 20);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(4, 12)), Is.True);
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 3C 4B 01 23 45 67 89 AB CD EF AA AA AA AA"));

            // errors

            Assert.That(original.TryWriteTo(Span <byte> .Empty), Is.False, "Target buffer is empty");

            scratch = MutableSlice.Repeat(0xAA, 20);
            Assert.That(original.TryWriteTo(scratch.Span.Slice(0, 11)), Is.False, "Target buffer is too small");
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA"), "Buffer should not have been overwritten!");
        }
Exemple #4
0
        public void Test_Incomplete_VersionStamp()
        {
            {             // 80-bits (no user version)
                var vs = VersionStamp.Incomplete();
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(ulong.MaxValue));
                Assert.That(vs.TransactionOrder, Is.EqualTo(ushort.MaxValue));
                Assert.That(vs.IsIncomplete, Is.True);
                Assert.That(vs.HasUserVersion, Is.False, "80-bits VersionStamps don't have a user version");
                Assert.That(vs.UserVersion, Is.Zero, "80-bits VersionStamps don't have a user version");

                Assert.That(vs.GetLength(), Is.EqualTo(10));
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("FF FF FF FF FF FF FF FF FF FF"));
                Assert.That(vs.ToString(), Is.EqualTo("@?"));
            }

            {             // 96-bits, default user version
                var vs = VersionStamp.Incomplete(0);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(ulong.MaxValue));
                Assert.That(vs.TransactionOrder, Is.EqualTo(ushort.MaxValue));
                Assert.That(vs.IsIncomplete, Is.True);
                Assert.That(vs.HasUserVersion, Is.True, "96-bits VersionStamps have a user version");
                Assert.That(vs.UserVersion, Is.EqualTo(0));

                Assert.That(vs.GetLength(), Is.EqualTo(12));
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("FF FF FF FF FF FF FF FF FF FF 00 00"));
                Assert.That(vs.ToString(), Is.EqualTo("@?#0"));
            }

            {             // 96 bits, custom user version
                var vs = VersionStamp.Incomplete(123);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(ulong.MaxValue));
                Assert.That(vs.TransactionOrder, Is.EqualTo(ushort.MaxValue));
                Assert.That(vs.HasUserVersion, Is.True);
                Assert.That(vs.UserVersion, Is.EqualTo(123));
                Assert.That(vs.IsIncomplete, Is.True);
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("FF FF FF FF FF FF FF FF FF FF 00 7B"));
                Assert.That(vs.ToString(), Is.EqualTo("@?#123"));
            }

            {             // 96 bits, large user version
                var vs = VersionStamp.Incomplete(12345);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(ulong.MaxValue));
                Assert.That(vs.TransactionOrder, Is.EqualTo(ushort.MaxValue));
                Assert.That(vs.HasUserVersion, Is.True);
                Assert.That(vs.UserVersion, Is.EqualTo(12345));
                Assert.That(vs.IsIncomplete, Is.True);
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("FF FF FF FF FF FF FF FF FF FF 30 39"));
                Assert.That(vs.ToString(), Is.EqualTo("@?#12345"));
            }

            Assert.That(() => VersionStamp.Incomplete(-1), Throws.ArgumentException, "User version cannot be negative");
            Assert.That(() => VersionStamp.Incomplete(65536), Throws.ArgumentException, "User version cannot be larger than 0xFFFF");

            {
                var writer = default(SliceWriter);
                writer.WriteFixed24BE(0xAAAAAA);
                VersionStamp.Incomplete(123).WriteTo(ref writer);
                writer.WriteFixed24BE(0xAAAAAA);
                Assert.That(writer.ToSlice().ToHexaString(' '), Is.EqualTo("AA AA AA FF FF FF FF FF FF FF FF FF FF 00 7B AA AA AA"));

                var reader = new SliceReader(writer.ToSlice());
                reader.Skip(3);
                var vs = VersionStamp.Parse(reader.ReadBytes(12));
                Assert.That(reader.Remaining, Is.EqualTo(3));

                Assert.That(vs.TransactionVersion, Is.EqualTo(ulong.MaxValue));
                Assert.That(vs.TransactionOrder, Is.EqualTo(ushort.MaxValue));
                Assert.That(vs.UserVersion, Is.EqualTo(123));
                Assert.That(vs.IsIncomplete, Is.True);
            }

            {
                var buf = MutableSlice.Repeat(0xAA, 18);
                VersionStamp.Incomplete(123).WriteTo(buf.Substring(3, 12));
                Assert.That(buf.ToHexaString(' '), Is.EqualTo("AA AA AA FF FF FF FF FF FF FF FF FF FF 00 7B AA AA AA"));
            }
        }
Exemple #5
0
        public void Test_Complete_VersionStamp()
        {
            {             // 80-bits, no user version
                var vs = VersionStamp.Complete(0x0123456789ABCDEFUL, 123);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(0x0123456789ABCDEFUL));
                Assert.That(vs.TransactionOrder, Is.EqualTo(123));
                Assert.That(vs.HasUserVersion, Is.False);
                Assert.That(vs.UserVersion, Is.Zero);
                Assert.That(vs.IsIncomplete, Is.False);
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("01 23 45 67 89 AB CD EF 00 7B"));
                Assert.That(vs.ToString(), Is.EqualTo("@81985529216486895-123"));
            }

            {             // 96 bits, default user version
                var vs = VersionStamp.Complete(0x0123456789ABCDEFUL, 123, 0);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(0x0123456789ABCDEFUL));
                Assert.That(vs.TransactionOrder, Is.EqualTo(123));
                Assert.That(vs.HasUserVersion, Is.True);
                Assert.That(vs.UserVersion, Is.Zero);
                Assert.That(vs.IsIncomplete, Is.False);
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("01 23 45 67 89 AB CD EF 00 7B 00 00"));
                Assert.That(vs.ToString(), Is.EqualTo("@81985529216486895-123#0"));
            }

            {             // custom user version
                var vs = VersionStamp.Complete(0x0123456789ABCDEFUL, 123, 456);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(0x0123456789ABCDEFUL));
                Assert.That(vs.TransactionOrder, Is.EqualTo(123));
                Assert.That(vs.HasUserVersion, Is.True);
                Assert.That(vs.UserVersion, Is.EqualTo(456));
                Assert.That(vs.IsIncomplete, Is.False);
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("01 23 45 67 89 AB CD EF 00 7B 01 C8"));
                Assert.That(vs.ToString(), Is.EqualTo("@81985529216486895-123#456"));
            }

            {             // two bytes user version
                var vs = VersionStamp.Complete(0x0123456789ABCDEFUL, 12345, 6789);
                Dump(vs);
                Assert.That(vs.TransactionVersion, Is.EqualTo(0x0123456789ABCDEFUL));
                Assert.That(vs.TransactionOrder, Is.EqualTo(12345));
                Assert.That(vs.UserVersion, Is.EqualTo(6789));
                Assert.That(vs.IsIncomplete, Is.False);
                Assert.That(vs.ToSlice().ToHexaString(' '), Is.EqualTo("01 23 45 67 89 AB CD EF 30 39 1A 85"));
                Assert.That(vs.ToString(), Is.EqualTo("@81985529216486895-12345#6789"));
            }

            Assert.That(() => VersionStamp.Complete(0x0123456789ABCDEFUL, 0, -1), Throws.ArgumentException, "User version cannot be negative");
            Assert.That(() => VersionStamp.Complete(0x0123456789ABCDEFUL, 0, 65536), Throws.ArgumentException, "User version cannot be larger than 0xFFFF");

            {
                var writer = default(SliceWriter);
                writer.WriteFixed24BE(0xAAAAAA);
                VersionStamp.Complete(0x0123456789ABCDEFUL, 123, 456).WriteTo(ref writer);
                writer.WriteFixed24BE(0xAAAAAA);
                Assert.That(writer.ToSlice().ToHexaString(' '), Is.EqualTo("AA AA AA 01 23 45 67 89 AB CD EF 00 7B 01 C8 AA AA AA"));

                var reader = new SliceReader(writer.ToSlice());
                reader.Skip(3);
                var vs = VersionStamp.Parse(reader.ReadBytes(12));
                Assert.That(reader.Remaining, Is.EqualTo(3));

                Assert.That(vs.TransactionVersion, Is.EqualTo(0x0123456789ABCDEFUL));
                Assert.That(vs.TransactionOrder, Is.EqualTo(123));
                Assert.That(vs.UserVersion, Is.EqualTo(456));
                Assert.That(vs.IsIncomplete, Is.False);
            }

            {
                var buf = MutableSlice.Repeat(0xAA, 18);
                VersionStamp.Complete(0x0123456789ABCDEFUL, 123, 456).WriteTo(buf.Substring(3, 12));
                Assert.That(buf.ToHexaString(' '), Is.EqualTo("AA AA AA 01 23 45 67 89 AB CD EF 00 7B 01 C8 AA AA AA"));
            }
        }