public void Test_Uuid80_Read_From_Bytes() { // test buffer with included padding byte[] buf = { 0x55, 0x55, 0x55, 0x55, /* start */ 0x1E, 0x2D, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, /* stop */ 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; var original = Uuid80.Parse("1E2D-01234567-89ABCDEF"); (var hi, var lo) = original; Assume.That(hi, Is.EqualTo(0x1E2D)); Assume.That(lo, Is.EqualTo(0x0123456789ABCDEF)); // Slice Assert.That(Uuid80.Read(buf.AsSlice(4, 10)), Is.EqualTo(original)); // byte[] Assert.That(Uuid80.Read(buf.AsSlice(4, 10).GetBytesOrEmpty()), Is.EqualTo(original)); #if ENABLE_SPAN // ReadOnlySpan<byte> Assert.That(Uuid80.Read(buf.AsSpan(4, 10)), Is.EqualTo(original)); unsafe { fixed(byte *ptr = &buf[4]) { Assert.That(Uuid80.Read(ptr, 10), Is.EqualTo(original)); Assert.That(Uuid80.Read(USlice.FromUnmanagedPointer(ptr, 10)), Is.EqualTo(original)); Assert.That(Uuid80.Read(new ReadOnlySpan <byte>(ptr, 10)), Is.EqualTo(original)); } } #endif }
public void Test_Uuid80_Ordering() { var a = new Uuid80(123, 42); var a2 = new Uuid80(123, 42); var b = new Uuid80(123, 77); var c = new Uuid80(124, 41); Assert.That(a.CompareTo(a), Is.EqualTo(0)); Assert.That(a.CompareTo(b), Is.EqualTo(-1)); Assert.That(b.CompareTo(a), Is.EqualTo(+1)); Assert.That(c.CompareTo(a), Is.EqualTo(+1)); Assert.That(a < b, Is.True, "a < b"); Assert.That(a <= b, Is.True, "a <= b"); Assert.That(a < a2, Is.False, "a < a"); Assert.That(a <= a2, Is.True, "a <= a"); Assert.That(a < c, Is.True, "a < c"); Assert.That(a > b, Is.False, "a > b"); Assert.That(a >= b, Is.False, "a >= b"); Assert.That(a > a2, Is.False, "a > a"); Assert.That(a >= a2, Is.True, "a >= a"); Assert.That(a > c, Is.False, "a > c"); // parsed from string Assert.That(Uuid80.Parse("1234-137bcf31-0c8873a2") < Uuid80.Parse("1234-604bdf8a-2512b4ad"), Is.True); Assert.That(Uuid80.Parse("1234-d8f17a26-82adb1a4") < Uuid80.Parse("1234-22abbf33-1b2c1db0"), Is.False); Assert.That(Uuid80.Parse("{1234-137bcf31-0c8873a2}") > Uuid80.Parse("{1234-604bdf8a-2512b4ad}"), Is.False); Assert.That(Uuid80.Parse("{1234-d8f17a26-82adb1a4}") > Uuid80.Parse("{1234-22abbf33-1b2c1db0}"), Is.True); // verify byte ordering var d = new Uuid80(0x0001, 0x0000000200000003); var e = new Uuid80(0x0003, 0x0000000200000001); Assert.That(d.CompareTo(e), Is.LessThan(0)); Assert.That(e.CompareTo(d), Is.GreaterThan(0)); // verify that we can sort an array of Uuid80 var uids = new Uuid80[100]; for (int i = 0; i < uids.Length; i++) { uids[i] = Uuid80.NewUuid(); } Assume.That(uids, Is.Not.Ordered, "This can happen with a very small probability. Please try again"); Array.Sort(uids); Assert.That(uids, Is.Ordered); // ordering should be preserved in integer or textual form Assert.That(uids.Select(x => x.ToString()), Is.Ordered.Using <string>(StringComparer.Ordinal), "order should be preserved when ordering by text (hexa)"); }
public void Test_Uuid80_WriteTo() { var original = Uuid80.Parse("1E2D-01234567-89ABCDEF"); (var hi, var lo) = original; Assume.That(hi, Is.EqualTo(0x1E2D)); 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 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA AA AA")); // span with no offset and exact size scratch = MutableSlice.Repeat(0xAA, 20); original.WriteTo(scratch.Substring(0, 10).Span); Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA AA AA")); // span with offset scratch = MutableSlice.Repeat(0xAA, 20); original.WriteTo(scratch.Substring(4).Span); Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA")); // span with offset and exact size scratch = MutableSlice.Repeat(0xAA, 20); original.WriteTo(scratch.Substring(4, 10).Span); Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 01 23 45 67 89 AB CD EF AA AA 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 01 23 45 67 89 AB CD EF AA AA 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.Substring(0, 9).Span), 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_Uuid80_WriteTo() { var original = Uuid80.Parse("1E2D-01234567-89ABCDEF"); (var hi, var lo) = original; Assume.That(hi, Is.EqualTo(0x1E2D)); Assume.That(lo, Is.EqualTo(0x0123456789ABCDEF)); // span with more space var scratch = Slice.Repeat(0xAA, 20); original.WriteTo(scratch); Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA AA AA")); // span with no offset and exact size scratch = Slice.Repeat(0xAA, 20); original.WriteTo(scratch.Substring(0, 10)); Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA AA AA")); // span with offset scratch = Slice.Repeat(0xAA, 20); original.WriteTo(scratch.Substring(4)); Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA")); // span with offset and exact size scratch = Slice.Repeat(0xAA, 20); original.WriteTo(scratch.Substring(4, 10)); Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA")); scratch = Slice.Repeat(0xAA, 20); original.WriteToUnsafe(scratch.Array, scratch.Offset); Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA 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, 10), 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, 9)), 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_Uuid80_TryWriteTo() { var original = Uuid80.Parse("1E2D-01234567-89ABCDEF"); (var hi, var lo) = original; Assume.That(hi, Is.EqualTo(0x1E2D)); Assume.That(lo, Is.EqualTo(0x0123456789ABCDEF)); // span with more space var scratch = Slice.Repeat(0xAA, 20); Assert.That(original.TryWriteTo(scratch), Is.True); Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA AA AA")); // span with no offset and exact size scratch = Slice.Repeat(0xAA, 20); Assert.That(original.TryWriteTo(scratch.Substring(0, 10)), Is.True); Assert.That(scratch.ToString("X"), Is.EqualTo("1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA AA AA AA AA")); // span with offset scratch = Slice.Repeat(0xAA, 20); Assert.That(original.TryWriteTo(scratch.Substring(4)), Is.True); Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA")); // span with offset and exact size scratch = Slice.Repeat(0xAA, 20); Assert.That(original.TryWriteTo(scratch.Substring(4, 10)), Is.True); Assert.That(scratch.ToString("X"), Is.EqualTo("AA AA AA AA 1E 2D 01 23 45 67 89 AB CD EF AA AA AA AA AA AA")); // errors Assert.That(original.TryWriteTo(Slice.Empty), Is.False, "Target buffer is empty"); scratch = Slice.Repeat(0xAA, 20); Assert.That(original.TryWriteTo(scratch.Substring(0, 9)), 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!"); }
public void Test_Uuid80_Parse_Hexa16() { // string Assert.That(Uuid80.Parse("abcd-badc0ffe-e0ddf00d"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("ABCD-BADC0FFE-E0DDF00D"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL)), "Should be case-insensitive"); Assert.That(Uuid80.Parse("abcdbadc0ffee0ddf00d"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("ABCDBADC0FFEE0DDF00D"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL)), "Should be case-insensitive"); Assert.That(Uuid80.Parse("{abcd-badc0ffe-e0ddf00d}"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("{ABCD-BADC0FFE-E0DDF00D}"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL)), "Should be case-insensitive"); Assert.That(Uuid80.Parse("{abcdbadc0ffee0ddf00d}"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("{ABCDBADC0FFEE0DDF00D}"), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL)), "should be case-insensitive"); Assert.That(Uuid80.Parse("ffff-00000000-deadbeef"), Is.EqualTo(new Uuid80(0xFFFF, 0, 0xDEADBEEFU))); Assert.That(Uuid80.Parse("{ffff-00000000-deadbeef}"), Is.EqualTo(new Uuid80(0xFFFF, 0, 0xDEADBEEFU))); // errors Assert.That(() => Uuid80.Parse(default(string)), Throws.ArgumentNullException); Assert.That(() => Uuid80.Parse("hello"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid80.Parse("abcd-12345678-9ABCDEFG"), Throws.InstanceOf <FormatException>(), "Invalid hexa character 'G'"); Assert.That(() => Uuid80.Parse("0000-00000000-0000000 "), Throws.InstanceOf <FormatException>(), "Two short + extra space"); Assert.That(() => Uuid80.Parse("zzzz-zzzzzzzz-zzzzzzzz"), Throws.InstanceOf <FormatException>(), "Invalid char"); Assert.That(() => Uuid80.Parse("abcd-badc0ffe-e0ddf00"), Throws.InstanceOf <FormatException>(), "Missing last char"); Assert.That(() => Uuid80.Parse("abcdbaadc0ffe-e0ddf00"), Throws.InstanceOf <FormatException>(), "'-' at invalid position"); Assert.That(() => Uuid80.Parse("abcd-badc0fe-ee0ddf00d"), Throws.InstanceOf <FormatException>(), "'-' at invalid position"); Assert.That(() => Uuid80.Parse("abcdb-adc0ffe-e0ddf00d"), Throws.InstanceOf <FormatException>(), "'-' at invalid position"); Assert.That(() => Uuid80.Parse("abcd-badc0ffe-e0ddf00d "), Throws.InstanceOf <FormatException>(), "Extra space at the end"); Assert.That(() => Uuid80.Parse(" abcd-badc0ffe-e0ddf00d"), Throws.InstanceOf <FormatException>(), "Extra space at the start"); #if ENABLE_SPAN // span from string Assert.That(Uuid80.Parse("abcd-badc0ffe-e0ddf00d".AsSpan()), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("abcdbadc0ffee0ddf00d".AsSpan()), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("hello abcd-badc0ffe-e0ddf00d world!".AsSpan().Slice(6, 22)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("hello abcdbadc0ffee0ddf00d world!".AsSpan().Slice(6, 20)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); // span from char[] Assert.That(Uuid80.Parse("abcd-badc0ffe-e0ddf00d".ToCharArray().AsSpan()), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("abcdbadc0ffee0ddf00d".ToCharArray().AsSpan()), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("hello abcd-badc0ffe-e0ddf00d world!".ToCharArray().AsSpan().Slice(6, 22)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); Assert.That(Uuid80.Parse("hello abcdbadc0ffee0ddf00d world!".ToCharArray().AsSpan().Slice(6, 20)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); // span from stackalloc unsafe { char *buf = stackalloc char[64]; var span = new Span <char>(buf, 64); span.Clear(); "abcd-badc0ffe-e0ddf00d".AsSpan().CopyTo(span); Assert.That(Uuid80.Parse(span.Slice(0, 22)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); span.Clear(); "abcdbadc0ffee0ddf00d".AsSpan().CopyTo(span); Assert.That(Uuid80.Parse(span.Slice(0, 20)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); span.Clear(); "{abcd-badc0ffe-e0ddf00d}".AsSpan().CopyTo(span); Assert.That(Uuid80.Parse(span.Slice(0, 24)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); span.Clear(); "{abcdbadc0ffee0ddf00d}".AsSpan().CopyTo(span); Assert.That(Uuid80.Parse(span.Slice(0, 22)), Is.EqualTo(new Uuid80(0xABCD, 0xBADC0FFEE0DDF00DUL))); } #endif }