Пример #1
0
        public void Test_UUid64_WriteTo()
        {
            var original = Uuid64.Parse("01234567-89ABCDEF");

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

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

            original.WriteTo(scratch);
            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 = Slice.Repeat(0xAA, 16);
            original.WriteTo(scratch.Substring(0, 8));
            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 = Slice.Repeat(0xAA, 16);
            original.WriteTo(scratch.Substring(4));
            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 = Slice.Repeat(0xAA, 16);
            original.WriteTo(scratch.Substring(4, 8));
            Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 01 23 45 67 89 AB CD EF AA AA AA AA"));

            scratch = Slice.Repeat(0xAA, 16);
            original.WriteToUnsafe(scratch.Array, scratch.Offset);
            Assert.That(scratch.ToString("X"), Is.EqualTo("01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA"));

            unsafe
            {
                byte *buf = stackalloc byte[16];
                UnsafeHelpers.FillUnsafe(buf, 16, 0xAA);

                original.WriteToUnsafe(buf + 2);
                Assert.That(Slice.Copy(buf, 16).ToString("X"), Is.EqualTo("AA AA 01 23 45 67 89 AB CD EF AA AA AA AA AA AA"));
            }

            // errors

            Assert.That(() => original.WriteTo(Slice.Empty), Throws.InstanceOf <ArgumentException>(), "Target buffer is empty");
            Assert.That(() => original.WriteTo(null, 8), Throws.InstanceOf <ArgumentException>(), "Target buffer is null");
            Assert.That(() => original.WriteTo(null, 0), Throws.InstanceOf <ArgumentException>(), "Target buffer is null");

            scratch = Slice.Repeat(0xAA, 16);
            Assert.That(() => original.WriteTo(scratch.Substring(0, 7)), 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!");
        }