public void Test_Uuid64_TryWriteTo() { var original = Uuid64.Parse("01234567-89ABCDEF"); Assume.That(original.ToUInt64(), Is.EqualTo(0x0123456789ABCDEF)); // span with more space var scratch = Slice.Repeat(0xAA, 16); Assert.That(original.TryWriteTo(scratch), 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 = Slice.Repeat(0xAA, 16); Assert.That(original.TryWriteTo(scratch.Substring(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 = Slice.Repeat(0xAA, 16); Assert.That(original.TryWriteTo(scratch.Substring(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 = Slice.Repeat(0xAA, 16); Assert.That(original.TryWriteTo(scratch.Substring(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(Slice.Empty), Is.False, "Target buffer is empty"); scratch = Slice.Repeat(0xAA, 16); Assert.That(original.TryWriteTo(scratch.Substring(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_Uuid64_Casting() { // implicit Uuid64 a = (long)0; Uuid64 b = (long)42; Uuid64 c = (long)0xDEADBEEF; Uuid64 d = 0xBADC0FFEE0DDF00DUL; Uuid64 e = ulong.MaxValue; // ToUInt64 Assert.That(a.ToUInt64(), Is.EqualTo(0UL)); Assert.That(b.ToUInt64(), Is.EqualTo(42UL)); Assert.That(c.ToUInt64(), Is.EqualTo(3735928559UL)); Assert.That(d.ToUInt64(), Is.EqualTo(13464654573299691533UL)); Assert.That(e.ToUInt64(), Is.EqualTo(ulong.MaxValue)); // ToInt64 Assert.That(a.ToInt64(), Is.EqualTo(0L)); Assert.That(b.ToInt64(), Is.EqualTo(42L)); Assert.That(c.ToInt64(), Is.EqualTo(3735928559L)); Assert.That(d.ToInt64(), Is.EqualTo(-4982089500409860083L)); Assert.That(e.ToInt64(), Is.EqualTo(-1L)); // explict Assert.That((long)a, Is.EqualTo(0)); Assert.That((long)b, Is.EqualTo(42)); Assert.That((long)c, Is.EqualTo(0xDEADBEEF)); Assert.That((ulong)d, Is.EqualTo(13464654573299691533UL)); Assert.That((ulong)e, Is.EqualTo(ulong.MaxValue)); Assert.That((long)e, Is.EqualTo(-1L)); }
public void Test_Uuid64_Read_From_Bytes() { // test buffer with included padding byte[] buf = { 0x55, 0x55, 0x55, 0x55, /* start */ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, /* stop */ 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; var original = Uuid64.Parse("01234567-89ABCDEF"); Assume.That(original.ToUInt64(), Is.EqualTo(0x0123456789ABCDEF)); #if ENABLE_SPAN // ReadOnlySpan<byte> Assert.That(Uuid64.Read(buf.AsSpan(4, 8)), Is.EqualTo(original)); #endif // Slice Assert.That(Uuid64.Read(buf.AsSlice(4, 8)), Is.EqualTo(original)); // byte[] Assert.That(Uuid64.Read(buf.AsSlice(4, 8).GetBytesOrEmpty()), Is.EqualTo(original)); unsafe { fixed(byte *ptr = &buf[4]) { Assert.That(Uuid64.Read(ptr, 8), Is.EqualTo(original)); #if ENABLE_SPAN Assert.That(Uuid64.Read(new ReadOnlySpan <byte>(ptr, 8)), Is.EqualTo(original)); #endif } } }
public void Test_Uuid64_Empty() { Assert.That(Uuid64.Empty.ToString(), Is.EqualTo("00000000-00000000")); Assert.That(Uuid64.Empty, Is.EqualTo(default(Uuid64))); Assert.That(Uuid64.Empty, Is.EqualTo(new Uuid64(0L))); Assert.That(Uuid64.Empty, Is.EqualTo(new Uuid64(0UL))); Assert.That(Uuid64.Empty, Is.EqualTo(Uuid64.Read(new byte[8]))); }
public void Test_FdbKey_PrettyPrint() { // verify that the pretty printing of keys produce a user friendly output Assert.That(FdbKey.Dump(Slice.Nil), Is.EqualTo("<null>")); Assert.That(FdbKey.Dump(Slice.Empty), Is.EqualTo("<empty>")); Assert.That(FdbKey.Dump(Slice.FromByte(0)), Is.EqualTo("<00>")); Assert.That(FdbKey.Dump(Slice.FromByte(255)), Is.EqualTo("<FF>")); Assert.That(FdbKey.Dump(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }.AsSlice()), Is.EqualTo("<00><01><02><03><04><05><06><07>")); Assert.That(FdbKey.Dump(new byte[] { 255, 254, 253, 252, 251, 250, 249, 248 }.AsSlice()), Is.EqualTo("<FF><FE><FD><FC><FB><FA><F9><F8>")); Assert.That(FdbKey.Dump(Slice.FromString("hello")), Is.EqualTo("hello")); Assert.That(FdbKey.Dump(Slice.FromString("héllø")), Is.EqualTo("h<C3><A9>ll<C3><B8>")); // tuples should be decoded properly Assert.That(FdbKey.Dump(TuPack.EncodeKey(123)), Is.EqualTo("(123,)"), "Singleton tuples should end with a ','"); Assert.That(FdbKey.Dump(TuPack.EncodeKey(Slice.FromByteString("hello"))), Is.EqualTo("(`hello`,)"), "ASCII strings should use single back quotes"); Assert.That(FdbKey.Dump(TuPack.EncodeKey("héllø")), Is.EqualTo("(\"héllø\",)"), "Unicode strings should use double quotes"); Assert.That(FdbKey.Dump(TuPack.EncodeKey(new byte[] { 1, 2, 3 }.AsSlice())), Is.EqualTo("(`<01><02><03>`,)")); Assert.That(FdbKey.Dump(TuPack.EncodeKey(123, 456)), Is.EqualTo("(123, 456)"), "Elements should be separated with a space, and not end up with ','"); Assert.That(FdbKey.Dump(TuPack.EncodeKey(default(object), true, false)), Is.EqualTo("(null, true, false)"), "Booleans should be displayed as numbers, and null should be in lowercase"); //note: even though it's tempting to using Python's "Nil", it's not very ".NETty" //note: the string representation of double is not identical between NetFx and .NET Core! So we cannot used a constant literal here Assert.That(FdbKey.Dump(TuPack.EncodeKey(1.0d, Math.PI, Math.E)), Is.EqualTo("(1, " + Math.PI.ToString("R", CultureInfo.InvariantCulture) + ", " + Math.E.ToString("R", CultureInfo.InvariantCulture) + ")"), "Doubles should used dot and have full precision (17 digits)"); Assert.That(FdbKey.Dump(TuPack.EncodeKey(1.0f, (float)Math.PI, (float)Math.E)), Is.EqualTo("(1, " + ((float)Math.PI).ToString("R", CultureInfo.InvariantCulture) + ", " + ((float)Math.E).ToString("R", CultureInfo.InvariantCulture) + ")"), "Singles should used dot and have full precision (10 digits)"); var guid = Guid.NewGuid(); Assert.That(FdbKey.Dump(TuPack.EncodeKey(guid)), Is.EqualTo($"({guid:B},)"), "GUIDs should be displayed as a string literal, surrounded by {{...}}, and without quotes"); var uuid128 = Uuid128.NewUuid(); Assert.That(FdbKey.Dump(TuPack.EncodeKey(uuid128)), Is.EqualTo($"({uuid128:B},)"), "Uuid128s should be displayed as a string literal, surrounded by {{...}}, and without quotes"); var uuid64 = Uuid64.NewUuid(); Assert.That(FdbKey.Dump(TuPack.EncodeKey(uuid64)), Is.EqualTo($"({uuid64:B},)"), "Uuid64s should be displayed as a string literal, surrounded by {{...}}, and without quotes"); // ranges should be decoded when possible var key = TuPack.ToRange(STuple.Create("hello")); // "<02>hello<00><00>" .. "<02>hello<00><FF>" Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",).<00>")); Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",).<FF>")); key = KeyRange.StartsWith(TuPack.EncodeKey("hello")); // "<02>hello<00>" .. "<02>hello<01>" Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",)")); Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",) + 1")); var t = TuPack.EncodeKey(123); Assert.That(FdbKey.PrettyPrint(t, FdbKey.PrettyPrintMode.Single), Is.EqualTo("(123,)")); Assert.That(FdbKey.PrettyPrint(TuPack.ToRange(t).Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(123,).<00>")); Assert.That(FdbKey.PrettyPrint(TuPack.ToRange(t).End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(123,).<FF>")); }
public void Test_FdbKey_PrettyPrint() { // verify that the pretty printing of keys produce a user friendly output Assert.That(FdbKey.Dump(Slice.Nil), Is.EqualTo("<null>")); Assert.That(FdbKey.Dump(Slice.Empty), Is.EqualTo("<empty>")); Assert.That(FdbKey.Dump(Slice.FromByte(0)), Is.EqualTo("<00>")); Assert.That(FdbKey.Dump(Slice.FromByte(255)), Is.EqualTo("<FF>")); Assert.That(FdbKey.Dump(Slice.Create(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 })), Is.EqualTo("<00><01><02><03><04><05><06><07>")); Assert.That(FdbKey.Dump(Slice.Create(new byte[] { 255, 254, 253, 252, 251, 250, 249, 248 })), Is.EqualTo("<FF><FE><FD><FC><FB><FA><F9><F8>")); Assert.That(FdbKey.Dump(Slice.FromString("hello")), Is.EqualTo("hello")); Assert.That(FdbKey.Dump(Slice.FromString("héllø")), Is.EqualTo("h<C3><A9>ll<C3><B8>")); // tuples should be decoded properly Assert.That(FdbKey.Dump(FdbTuple.Pack(123)), Is.EqualTo("(123,)"), "Singleton tuples should end with a ','"); Assert.That(FdbKey.Dump(FdbTuple.Pack(Slice.FromAscii("hello"))), Is.EqualTo("('hello',)"), "ASCII strings should use single quotes"); Assert.That(FdbKey.Dump(FdbTuple.Pack("héllø")), Is.EqualTo("(\"héllø\",)"), "Unicode strings should use double quotes"); Assert.That(FdbKey.Dump(FdbTuple.Pack(Slice.Create(new byte[] { 1, 2, 3 }))), Is.EqualTo("(<01 02 03>,)")); Assert.That(FdbKey.Dump(FdbTuple.Pack(123, 456)), Is.EqualTo("(123, 456)"), "Elements should be separated with a space, and not end up with ','"); Assert.That(FdbKey.Dump(FdbTuple.Pack(true, false, default(object))), Is.EqualTo("(1, 0, null)"), "Booleans should be displayed as numbers, and null should be in lowercase"); //note: even though it's tempting to using Python's "Nil", it's not very ".NETty" Assert.That(FdbKey.Dump(FdbTuple.Pack(1.0d, Math.PI, Math.E)), Is.EqualTo("(1, 3.1415926535897931, 2.7182818284590451)"), "Doubles should used dot and have full precision (17 digits)"); Assert.That(FdbKey.Dump(FdbTuple.Pack(1.0f, (float)Math.PI, (float)Math.E)), Is.EqualTo("(1, 3.14159274, 2.71828175)"), "Singles should used dot and have full precision (10 digits)"); var guid = Guid.NewGuid(); Assert.That(FdbKey.Dump(FdbTuple.Pack(guid)), Is.EqualTo(String.Format("({0},)", guid.ToString("D"))), "GUIDs should be displayed as a string literal, without quotes"); var uuid128 = Uuid128.NewUuid(); Assert.That(FdbKey.Dump(FdbTuple.Pack(uuid128)), Is.EqualTo(String.Format("({0},)", uuid128.ToString("D"))), "Uuid128s should be displayed as a string literal, without quotes"); var uuid64 = Uuid64.NewUuid(); Assert.That(FdbKey.Dump(FdbTuple.Pack(uuid64)), Is.EqualTo(String.Format("({0},)", uuid64.ToString("D"))), "Uuid64s should be displayed as a string literal, without quotes"); // ranges should be decoded when possible var key = FdbTuple.ToRange(FdbTuple.Create("hello")); // "<02>hello<00><00>" .. "<02>hello<00><FF>" Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",).<00>")); Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",).<FF>")); key = FdbKeyRange.StartsWith(FdbTuple.Pack("hello")); // "<02>hello<00>" .. "<02>hello<01>" Assert.That(FdbKey.PrettyPrint(key.Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(\"hello\",)")); Assert.That(FdbKey.PrettyPrint(key.End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(\"hello\",) + 1")); var t = FdbTuple.Pack(123); Assert.That(FdbKey.PrettyPrint(t, FdbKey.PrettyPrintMode.Single), Is.EqualTo("(123,)")); Assert.That(FdbKey.PrettyPrint(FdbTuple.ToRange(t).Begin, FdbKey.PrettyPrintMode.Begin), Is.EqualTo("(123,).<00>")); Assert.That(FdbKey.PrettyPrint(FdbTuple.ToRange(t).End, FdbKey.PrettyPrintMode.End), Is.EqualTo("(123,).<FF>")); }
/// <summary>Writes a 64-bit UUID</summary> public static void WriteUuid64(ref TupleWriter writer, Uuid64 value) { writer.Output.EnsureBytes(9); writer.Output.UnsafeWriteByte(FdbTupleTypes.Uuid64); unsafe { byte *ptr = stackalloc byte[8]; value.WriteTo(ptr); writer.Output.UnsafeWriteBytes(ptr, 8); } }
public void Test_Uuid64_Ordering() { var a = new Uuid64(42); var a2 = new Uuid64(42); var b = new Uuid64(77); 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(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 > 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"); // parsed from string Assert.That(new Uuid64("137bcf31-0c8873a2") < new Uuid64("604bdf8a-2512b4ad"), Is.True); Assert.That(new Uuid64("d8f17a26-82adb1a4") < new Uuid64("22abbf33-1b2c1db0"), Is.False); Assert.That(new Uuid64("{137bcf31-0c8873a2}") > new Uuid64("{604bdf8a-2512b4ad}"), Is.False); Assert.That(new Uuid64("{d8f17a26-82adb1a4}") > new Uuid64("{22abbf33-1b2c1db0}"), Is.True); Assert.That(new Uuid64("2w6CTjUiXVp") < new Uuid64("DVM0UnynZ1Q"), Is.True); Assert.That(new Uuid64("0658JY2ORSJ") > new Uuid64("FMPaNaMEUWc"), Is.False); // verify byte ordering var c = new Uuid64(0x0000000100000002); var d = new Uuid64(0x0000000200000001); Assert.That(c.CompareTo(d), Is.EqualTo(-1)); Assert.That(d.CompareTo(c), Is.EqualTo(+1)); // verify that we can sort an array of Uuid64 var uids = new Uuid64[100]; for (int i = 0; i < uids.Length; i++) { uids[i] = Uuid64.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.ToUInt64()), Is.Ordered, "order should be preserved when ordering by unsigned value"); //note: ToInt64() will not work because of negative values Assert.That(uids.Select(x => x.ToString()), Is.Ordered.Using <string>(StringComparer.Ordinal), "order should be preserved when ordering by text (hexa)"); Assert.That(uids.Select(x => x.ToString("Z")), Is.Ordered.Using <string>(StringComparer.Ordinal), "order should be preserved when ordering by text (base62)"); //note: ToString("C") will not work for ordering because it will produce "z" > "aa", instead of expected "0z" < "aa" }
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.AsSpan()); 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.AsSpan(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.AsSpan(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.AsSpan(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]; var span = USlice.FromUnmanagedPointer(buf, 16); span.Fill(0xAA); original.WriteToUnsafe(buf + 2); Assert.That(span.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(Span <byte> .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.AsSpan(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!"); }
public void Test_Uuid64_Parse_Hexa16() { var uuid = Uuid64.Parse("badc0ffe-e0ddf00d"); Assert.That(uuid.ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); uuid = Uuid64.Parse("{badc0ffe-e0ddf00d}"); Assert.That(uuid.ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); uuid = Uuid64.Parse("00000000-deadbeef"); Assert.That(uuid.ToUInt64(), Is.EqualTo(0xDEADBEEFUL)); uuid = Uuid64.Parse("{00000000-deadbeef}"); Assert.That(uuid.ToUInt64(), Is.EqualTo(0xDEADBEEFUL)); }
public void Test_Uuid64_ToString() { var guid = new Uuid64(0xBADC0FFEE0DDF00DUL); Assert.That(guid.ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(guid.ToString(), Is.EqualTo("badc0ffe-e0ddf00d")); Assert.That(guid.ToString("X"), Is.EqualTo("badc0ffee0ddf00d")); Assert.That(guid.ToString("B"), Is.EqualTo("{badc0ffe-e0ddf00d}")); Assert.That(guid.ToString("C"), Is.EqualTo("G2eGAUq82Hd")); guid = new Uuid64(0xDEADBEEFUL); Assert.That(guid.ToUInt64(), Is.EqualTo(0xDEADBEEFUL)); Assert.That(guid.ToString(), Is.EqualTo("00000000-deadbeef")); Assert.That(guid.ToString("X"), Is.EqualTo("00000000deadbeef")); Assert.That(guid.ToString("B"), Is.EqualTo("{00000000-deadbeef}")); Assert.That(guid.ToString("C"), Is.EqualTo("44pZgF")); }
public void Test_Uuid64_ToString() { var guid = new Uuid64(0xBADC0FFEE0DDF00DUL); Assert.That(guid.ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(guid.ToString(), Is.EqualTo("BADC0FFE-E0DDF00D")); Assert.That(guid.ToString("X"), Is.EqualTo("BADC0FFEE0DDF00D")); Assert.That(guid.ToString("B"), Is.EqualTo("{BADC0FFE-E0DDF00D}")); Assert.That(guid.ToString("C"), Is.EqualTo("G2eGAUq82Hd")); guid = new Uuid64(0xDEADBEEFUL); Assert.That(guid.ToUInt64(), Is.EqualTo(0xDEADBEEFUL)); Assert.That(guid.ToString(), Is.EqualTo("00000000-DEADBEEF")); Assert.That(guid.ToString("X"), Is.EqualTo("00000000DEADBEEF")); Assert.That(guid.ToString("B"), Is.EqualTo("{00000000-DEADBEEF}")); Assert.That(guid.ToString("C"), Is.EqualTo("44pZgF")); }
public void Test_Uuid64_Parse_Base62() { Assert.That(Uuid64.Parse("0").ToUInt64(), Is.EqualTo(0)); Assert.That(Uuid64.Parse("9").ToUInt64(), Is.EqualTo(9)); Assert.That(Uuid64.Parse("A").ToUInt64(), Is.EqualTo(10)); Assert.That(Uuid64.Parse("Z").ToUInt64(), Is.EqualTo(35)); Assert.That(Uuid64.Parse("a").ToUInt64(), Is.EqualTo(36)); Assert.That(Uuid64.Parse("z").ToUInt64(), Is.EqualTo(61)); Assert.That(Uuid64.Parse("10").ToUInt64(), Is.EqualTo(62)); Assert.That(Uuid64.Parse("zz").ToUInt64(), Is.EqualTo(3843)); Assert.That(Uuid64.Parse("100").ToUInt64(), Is.EqualTo(3844)); Assert.That(Uuid64.Parse("zzzzzzzzzz").ToUInt64(), Is.EqualTo(839299365868340223UL)); Assert.That(Uuid64.Parse("10000000000").ToUInt64(), Is.EqualTo(839299365868340224UL)); Assert.That(Uuid64.Parse("LygHa16AHYF").ToUInt64(), Is.EqualTo(ulong.MaxValue), "ulong.MaxValue in base 62"); // well known values Assert.That(Uuid64.Parse("narf").ToUInt64(), Is.EqualTo(0xB45B07)); Assert.That(Uuid64.Parse("zort").ToUInt64(), Is.EqualTo(0xE0D0ED)); Assert.That(Uuid64.Parse("44pZgF").ToUInt64(), Is.EqualTo(0xDEADBEEF)); Assert.That(Uuid64.Parse("0000044pZgF").ToUInt64(), Is.EqualTo(0xDEADBEEF)); Assert.That(Uuid64.Parse("G2eGAUq82Hd").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("4gfFC3").ToUInt64(), Is.EqualTo(uint.MaxValue)); Assert.That(Uuid64.Parse("000004gfFC3").ToUInt64(), Is.EqualTo(uint.MaxValue)); // invalid chars Assert.That(() => Uuid64.Parse("/"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("@"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("["), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("`"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("{"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("zaz/"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("z/o&r=g"), Throws.InstanceOf <FormatException>()); // overflow Assert.That(() => Uuid64.Parse("zzzzzzzzzzz"), Throws.InstanceOf <OverflowException>(), "62^11 - 1 => OVERFLOW"); Assert.That(() => Uuid64.Parse("LygHa16AHYG"), Throws.InstanceOf <OverflowException>(), "ulong.MaxValue + 1 => OVERFLOW"); // invalid length Assert.That(() => Uuid64.Parse(null), Throws.InstanceOf <ArgumentNullException>()); Assert.That(() => Uuid64.Parse(""), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("100000000000"), Throws.InstanceOf <FormatException>(), "62^11 => TOO BIG"); }
public void Test_Uuid64_Equality_Check() { var a = new Uuid64(42); var b = new Uuid64(42); var c = new Uuid64(40) + 2; var d = new Uuid64(0xDEADBEEF); // Equals(Uuid64) Assert.That(a.Equals(a), Is.True, "a == a"); Assert.That(a.Equals(b), Is.True, "a == b"); Assert.That(a.Equals(c), Is.True, "a == c"); Assert.That(a.Equals(d), Is.False, "a != d"); // == Uuid64 Assert.That(a == b, Is.True, "a == b"); Assert.That(a == c, Is.True, "a == c"); Assert.That(a == d, Is.False, "a != d"); // != Uuid64 Assert.That(a != b, Is.False, "a == b"); Assert.That(a != c, Is.False, "a == c"); Assert.That(a != d, Is.True, "a != d"); // == numbers Assert.That(a == 42L, Is.True, "a == 42"); Assert.That(a == 42UL, Is.True, "a == 42"); Assert.That(d == 42L, Is.False, "d != 42"); Assert.That(d == 42UL, Is.False, "d != 42"); // != numbers Assert.That(a != 42L, Is.False, "a == 42"); Assert.That(a != 42UL, Is.False, "a == 42"); Assert.That(d != 42L, Is.True, "d != 42"); Assert.That(d != 42UL, Is.True, "d != 42"); // Equals(objecct) Assert.That(a.Equals((object)a), Is.True, "a == a"); Assert.That(a.Equals((object)b), Is.True, "a == b"); Assert.That(a.Equals((object)c), Is.True, "a == c"); Assert.That(a.Equals((object)d), Is.False, "a != d"); Assert.That(a.Equals((object)42L), Is.True, "a == 42"); Assert.That(a.Equals((object)42UL), Is.True, "a == 42"); Assert.That(d.Equals((object)42L), Is.False, "d != 42"); Assert.That(d.Equals((object)42UL), Is.False, "d != 42"); }
public void Test_UUid64_WriteTo() { var original = Uuid64.Parse("01234567-89ABCDEF"); Assume.That(original.ToUInt64(), Is.EqualTo(0x0123456789ABCDEF)); // span with more space var scratch = MutableSlice.Repeat(0xAA, 16); original.WriteTo(scratch.Span); 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); original.WriteTo(scratch.Span.Slice(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 = MutableSlice.Repeat(0xAA, 16); original.WriteTo(scratch.Span.Slice(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 = MutableSlice.Repeat(0xAA, 16); original.WriteTo(scratch.Span.Slice(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")); unsafe { Span <byte> buf = stackalloc byte[16]; buf.Fill(0xAA); original.WriteToUnsafe(buf.Slice(2)); Assert.That(buf.ToArray().AsSlice().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(Span <byte> .Empty), Throws.InstanceOf <ArgumentException>(), "Target buffer is empty"); scratch = MutableSlice.Repeat(0xAA, 16); Assert.That(() => original.WriteTo(scratch.Span.Slice(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!"); }
public void Test_Uuid64_NewUid() { var a = Uuid64.NewUuid(); var b = Uuid64.NewUuid(); Assert.That(a.ToUInt64(), Is.Not.EqualTo(b.ToUInt64())); Assert.That(a, Is.Not.EqualTo(b)); const int N = 1 * 1000; var uids = new HashSet <ulong>(); for (int i = 0; i < N; i++) { var uid = Uuid64.NewUuid(); if (uids.Contains(uid.ToUInt64())) { Assert.Fail("Duplicate Uuid64 generated: {0}", uid); } uids.Add(uid.ToUInt64()); } Assert.That(uids.Count, Is.EqualTo(N)); }
public void Test_Uuid64_Arithmetic() { var uid = Uuid64.Empty; Assert.That(uid + 42L, Is.EqualTo(new Uuid64(42))); Assert.That(uid + 42UL, Is.EqualTo(new Uuid64(42))); uid++; Assert.That(uid.ToInt64(), Is.EqualTo(1)); uid++; Assert.That(uid.ToInt64(), Is.EqualTo(2)); uid--; Assert.That(uid.ToInt64(), Is.EqualTo(1)); uid--; Assert.That(uid.ToInt64(), Is.EqualTo(0)); uid = Uuid64.NewUuid(); Assert.That(uid + 123L, Is.EqualTo(new Uuid64(uid.ToInt64() + 123))); Assert.That(uid + 123UL, Is.EqualTo(new Uuid64(uid.ToUInt64() + 123))); Assert.That(uid - 123L, Is.EqualTo(new Uuid64(uid.ToInt64() - 123))); Assert.That(uid - 123UL, Is.EqualTo(new Uuid64(uid.ToUInt64() - 123))); }
public void Test_Slice_FromUuid64() { // Verify that FoundationDb.Client.Uuid64 are stored as 64-bit UUIDs in big-endian Slice slice; // empty guid should be all zeroes slice = Slice.FromUuid64(Uuid64.Empty); Assert.That(slice.ToHexaString(), Is.EqualTo("0000000000000000")); // UUIDs should be stored in lexicographical order var uuid = new Uuid64("01234567-89abcdef"); // byte order should follow the string! slice = Slice.FromUuid64(uuid); Assert.That(slice.ToHexaString(), Is.EqualTo("0123456789abcdef"), "Slice.FromUuid64() should preserve ordering"); // ToByteArray() should also be safe slice = Slice.Create(uuid.ToByteArray()); Assert.That(slice.ToHexaString(), Is.EqualTo("0123456789abcdef")); }
public void Test_Uuid64_Ordering() { var a = new Uuid64(42); var a2 = new Uuid64(42); var b = new Uuid64(77); 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(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 > 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"); // parsed from string Assert.That(new Uuid64("137bcf31-0c8873a2") < new Uuid64("604bdf8a-2512b4ad"), Is.True); Assert.That(new Uuid64("d8f17a26-82adb1a4") < new Uuid64("22abbf33-1b2c1db0"), Is.False); Assert.That(new Uuid64("{137bcf31-0c8873a2}") > new Uuid64("{604bdf8a-2512b4ad}"), Is.False); Assert.That(new Uuid64("{d8f17a26-82adb1a4}") > new Uuid64("{22abbf33-1b2c1db0}"), Is.True); Assert.That(new Uuid64("2w6CTjUiXVp") < new Uuid64("DVM0UnynZ1Q"), Is.True); Assert.That(new Uuid64("0658JY2ORSJ") > new Uuid64("FMPaNaMEUWc"), Is.False); // verify byte ordering var c = new Uuid64(0x0000000100000002); var d = new Uuid64(0x0000000200000001); Assert.That(c.CompareTo(d), Is.EqualTo(-1)); Assert.That(d.CompareTo(c), Is.EqualTo(+1)); // verify that we can sort an array of Uuid64 var uids = new Uuid64[100]; for (int i = 0; i < uids.Length; i++) { uids[i] = Uuid64.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.ToUInt64()), Is.Ordered, "order should be preserved when ordering by unsigned value"); //note: ToInt64() will not work because of negative values Assert.That(uids.Select(x => x.ToString()), Is.Ordered.Using<string>(StringComparer.Ordinal), "order should be preserved when ordering by text (hexa)"); Assert.That(uids.Select(x => x.ToString("Z")), Is.Ordered.Using<string>(StringComparer.Ordinal), "order should be preserved when ordering by text (base62)"); //note: ToString("C") will not work for ordering because it will produce "z" > "aa", instead of expected "0z" < "aa" }
/// <summary>Register all the default converters</summary> private static void RegisterDefaultConverters() { //TODO: there is too much generic type combinations! need to refactor this ... RegisterUnsafe <bool, Slice>((value) => Slice.FromByte(value ? (byte)1 : default(byte))); RegisterUnsafe <bool, byte[]>((value) => Slice.FromByte(value ? (byte)1 : default(byte)).GetBytes()); RegisterUnsafe <bool, string>((value) => value ? "true" : "false"); RegisterUnsafe <bool, sbyte>((value) => value ? (sbyte)1 : default(sbyte)); RegisterUnsafe <bool, byte>((value) => value ? (byte)1 : default(byte)); RegisterUnsafe <bool, short>((value) => value ? (short)1 : default(short)); RegisterUnsafe <bool, ushort>((value) => value ? (ushort)1 : default(ushort)); RegisterUnsafe <bool, int>((value) => value ? 1 : default(int)); RegisterUnsafe <bool, uint>((value) => value ? 1U : default(uint)); RegisterUnsafe <bool, long>((value) => value ? 1L : default(long)); RegisterUnsafe <bool, ulong>((value) => value ? 1UL : default(ulong)); RegisterUnsafe <bool, double>((value) => value ? 1.0d : default(double)); RegisterUnsafe <bool, float>((value) => value ? 1.0f : default(float)); RegisterUnsafe <bool, decimal>((value) => value ? 1m : default(decimal)); RegisterUnsafe <int, Slice>(Slice.FromInt32); RegisterUnsafe <int, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <int, string>(StringConverters.ToString); RegisterUnsafe <int, bool>((value) => value != 0); RegisterUnsafe <int, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <int, byte>((value) => checked ((byte)value)); RegisterUnsafe <int, short>((value) => checked ((short)value)); RegisterUnsafe <int, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <int, uint>((value) => (uint)value); RegisterUnsafe <int, long>((value) => value); RegisterUnsafe <int, ulong>((value) => (ulong)value); RegisterUnsafe <int, double>((value) => value); RegisterUnsafe <int, float>((value) => value); // possible loss of precision RegisterUnsafe <int, decimal>((value) => value); RegisterUnsafe <uint, Slice>(Slice.FromUInt32); RegisterUnsafe <uint, byte[]>((value) => Slice.FromUInt32(value).GetBytes()); RegisterUnsafe <uint, string>(StringConverters.ToString); RegisterUnsafe <uint, bool>((value) => value != 0); RegisterUnsafe <uint, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <uint, byte>((value) => checked ((byte)value)); RegisterUnsafe <uint, short>((value) => checked ((short)value)); RegisterUnsafe <uint, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <uint, int>((value) => (int)value); RegisterUnsafe <uint, long>((value) => value); RegisterUnsafe <uint, ulong>((value) => value); RegisterUnsafe <uint, double>((value) => value); RegisterUnsafe <uint, float>((value) => value); // possible loss of precision RegisterUnsafe <uint, decimal>((value) => value); RegisterUnsafe <long, Slice>(Slice.FromInt64); RegisterUnsafe <long, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <long, string>(StringConverters.ToString); RegisterUnsafe <long, bool>((value) => value != 0); RegisterUnsafe <long, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <long, byte>((value) => checked ((byte)value)); RegisterUnsafe <long, short>((value) => checked ((short)value)); RegisterUnsafe <long, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <long, int>((value) => checked ((int)value)); RegisterUnsafe <long, uint>((value) => (uint)value); RegisterUnsafe <long, ulong>((value) => (ulong)value); RegisterUnsafe <long, double>((value) => value); // possible loss of precision RegisterUnsafe <long, float>((value) => value); // possible loss of precision RegisterUnsafe <long, TimeSpan>(TimeSpan.FromTicks); RegisterUnsafe <long, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <long, System.Net.IPAddress>((value) => new System.Net.IPAddress(value)); RegisterUnsafe <long, decimal>((value) => value); RegisterUnsafe <ulong, Slice>(Slice.FromUInt64); RegisterUnsafe <ulong, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <ulong, string>(StringConverters.ToString); RegisterUnsafe <ulong, bool>((value) => value != 0); RegisterUnsafe <ulong, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ulong, byte>((value) => checked ((byte)value)); RegisterUnsafe <ulong, short>((value) => checked ((short)value)); RegisterUnsafe <ulong, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <ulong, int>((value) => checked ((int)value)); RegisterUnsafe <ulong, uint>((value) => checked ((uint)value)); RegisterUnsafe <ulong, long>((value) => checked ((long)value)); RegisterUnsafe <ulong, double>((value) => value); // possible loss of precision RegisterUnsafe <ulong, float>((value) => value); // possible loss of precision RegisterUnsafe <ulong, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <ulong, TimeSpan>((value) => TimeSpan.FromTicks(checked ((long)value))); RegisterUnsafe <ulong, decimal>((value) => value); RegisterUnsafe <short, Slice>(Slice.FromInt16); RegisterUnsafe <short, byte[]>((value) => Slice.FromInt16(value).GetBytes()); RegisterUnsafe <short, string>((value) => StringConverters.ToString(value)); RegisterUnsafe <short, bool>((value) => value != 0); RegisterUnsafe <short, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <short, byte>((value) => checked ((byte)value)); RegisterUnsafe <short, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <short, int>((value) => value); RegisterUnsafe <short, uint>((value) => checked ((uint)value)); RegisterUnsafe <short, long>((value) => value); RegisterUnsafe <short, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <short, double>((value) => value); RegisterUnsafe <short, float>((value) => value); RegisterUnsafe <short, decimal>((value) => value); RegisterUnsafe <ushort, Slice>(Slice.FromUInt16); RegisterUnsafe <ushort, byte[]>((value) => Slice.FromUInt16(value).GetBytes()); RegisterUnsafe <ushort, string>((value) => StringConverters.ToString(value)); RegisterUnsafe <ushort, bool>((value) => value != 0); RegisterUnsafe <ushort, byte>((value) => checked ((byte)value)); RegisterUnsafe <ushort, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ushort, short>((value) => checked ((short)value)); RegisterUnsafe <ushort, int>((value) => value); RegisterUnsafe <ushort, uint>((value) => value); RegisterUnsafe <ushort, long>((value) => value); RegisterUnsafe <ushort, ulong>((value) => value); RegisterUnsafe <ushort, double>((value) => value); RegisterUnsafe <ushort, float>((value) => value); RegisterUnsafe <ushort, decimal>((value) => value); RegisterUnsafe <byte, Slice>(Slice.FromByte); RegisterUnsafe <byte, byte[]>((value) => Slice.FromByte(value).GetBytes()); RegisterUnsafe <byte, string>((value) => StringConverters.ToString(value)); RegisterUnsafe <byte, bool>((value) => value != 0); RegisterUnsafe <byte, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <byte, short>((value) => value); RegisterUnsafe <byte, ushort>((value) => value); RegisterUnsafe <byte, int>((value) => value); RegisterUnsafe <byte, uint>((value) => value); RegisterUnsafe <byte, long>((value) => value); RegisterUnsafe <byte, ulong>((value) => value); RegisterUnsafe <byte, double>((value) => value); RegisterUnsafe <byte, float>((value) => value); RegisterUnsafe <byte, decimal>((value) => value); RegisterUnsafe <sbyte, Slice>((value) => Slice.FromInt64(value)); RegisterUnsafe <sbyte, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <sbyte, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <sbyte, bool>((value) => value != 0); RegisterUnsafe <sbyte, byte>((value) => checked ((byte)value)); RegisterUnsafe <sbyte, short>((value) => value); RegisterUnsafe <sbyte, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <sbyte, int>((value) => value); RegisterUnsafe <sbyte, uint>((value) => checked ((uint)value)); RegisterUnsafe <sbyte, long>((value) => value); RegisterUnsafe <sbyte, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <sbyte, double>((value) => value); RegisterUnsafe <sbyte, float>((value) => value); RegisterUnsafe <sbyte, decimal>((value) => value); RegisterUnsafe <float, Slice>(Slice.FromSingle); RegisterUnsafe <float, byte[]>((value) => Slice.FromSingle(value).GetBytes()); RegisterUnsafe <float, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <float, bool>((value) => !(value == 0f || float.IsNaN(value))); RegisterUnsafe <float, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <float, byte>((value) => checked ((byte)value)); RegisterUnsafe <float, short>((value) => checked ((short)value)); RegisterUnsafe <float, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <float, int>((value) => checked ((int)value)); RegisterUnsafe <float, uint>((value) => (uint)value); RegisterUnsafe <float, long>((value) => checked ((long)value)); RegisterUnsafe <float, ulong>((value) => (ulong)value); RegisterUnsafe <float, double>((value) => value); RegisterUnsafe <float, decimal>((value) => (decimal)value); // possible loss of precision RegisterUnsafe <double, Slice>((value) => Slice.FromDouble(value)); RegisterUnsafe <double, byte[]>((value) => Slice.FromDouble(value).GetBytes()); RegisterUnsafe <double, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <double, bool>((value) => !(value == 0d || double.IsNaN(value))); RegisterUnsafe <double, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <double, byte>((value) => checked ((byte)value)); RegisterUnsafe <double, short>((value) => checked ((short)value)); RegisterUnsafe <double, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <double, int>((value) => checked ((int)value)); RegisterUnsafe <double, uint>((value) => (uint)value); RegisterUnsafe <double, long>((value) => checked ((long)value)); RegisterUnsafe <double, ulong>((value) => (ulong)value); RegisterUnsafe <double, float>((value) => (float)value); // possible loss of precision RegisterUnsafe <double, decimal>((value) => (decimal)value); // possible loss of precision RegisterUnsafe <string, Slice>((value) => Slice.FromString(value)); RegisterUnsafe <string, byte[]>((value) => Slice.FromString(value).GetBytes()); //REVIEW: string=>byte[] use UTF-8, but byte[]=>string uses Base64 ? RegisterUnsafe <string, bool>((value) => !string.IsNullOrEmpty(value)); RegisterUnsafe <string, sbyte>((value) => string.IsNullOrEmpty(value) ? default(sbyte) : sbyte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, byte>((value) => string.IsNullOrEmpty(value) ? default(byte) : byte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, short>((value) => string.IsNullOrEmpty(value) ? default(short) : short.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ushort>((value) => string.IsNullOrEmpty(value) ? default(ushort) : ushort.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, int>((value) => string.IsNullOrEmpty(value) ? default(int) : int.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, uint>((value) => string.IsNullOrEmpty(value) ? default(uint) : uint.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, long>((value) => string.IsNullOrEmpty(value) ? default(long) : long.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ulong>((value) => string.IsNullOrEmpty(value) ? default(ulong) : ulong.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, float>((value) => string.IsNullOrEmpty(value) ? default(float) : float.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, double>((value) => string.IsNullOrEmpty(value) ? default(double) : double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, decimal>((value) => string.IsNullOrEmpty(value) ? default(decimal) : decimal.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, Guid>((value) => string.IsNullOrEmpty(value) ? default(Guid) : Guid.Parse(value)); RegisterUnsafe <string, Uuid128>((value) => string.IsNullOrEmpty(value) ? default(Uuid128) : Uuid128.Parse(value)); RegisterUnsafe <string, Uuid64>((value) => string.IsNullOrEmpty(value) ? default(Uuid64) : Uuid64.Parse(value)); RegisterUnsafe <string, System.Net.IPAddress>((value) => string.IsNullOrEmpty(value) ? default(System.Net.IPAddress) : System.Net.IPAddress.Parse(value)); RegisterUnsafe <byte[], Slice>((value) => value.AsSlice()); RegisterUnsafe <byte[], string>((value) => value == null ? default(string) : value.Length == 0 ? string.Empty : System.Convert.ToBase64String(value)); //REVIEW: string=>byte[] use UTF-8, but byte[]=>string uses Base64 ? RegisterUnsafe <byte[], bool>((value) => value != null && value.Length > 0); RegisterUnsafe <byte[], sbyte>((value) => value == null ? default(sbyte) : value.AsSlice().ToSByte()); RegisterUnsafe <byte[], byte>((value) => value == null ? default(byte) : value.AsSlice().ToByte()); RegisterUnsafe <byte[], short>((value) => value == null ? default(short) : value.AsSlice().ToInt16()); RegisterUnsafe <byte[], ushort>((value) => value == null ? default(ushort) : value.AsSlice().ToUInt16()); RegisterUnsafe <byte[], int>((value) => value == null ? 0 : value.AsSlice().ToInt32()); RegisterUnsafe <byte[], uint>((value) => value == null ? 0U : value.AsSlice().ToUInt32()); RegisterUnsafe <byte[], long>((value) => value == null ? 0L : value.AsSlice().ToInt64()); RegisterUnsafe <byte[], ulong>((value) => value == null ? 0UL : value.AsSlice().ToUInt64()); RegisterUnsafe <byte[], Guid>((value) => value == null || value.Length == 0 ? default(Guid) : new Uuid128(value).ToGuid()); RegisterUnsafe <byte[], Uuid128>((value) => value == null || value.Length == 0 ? default(Uuid128) : new Uuid128(value)); RegisterUnsafe <byte[], Uuid64>((value) => value != null ? Uuid64.Read(value) : default(Uuid64)); RegisterUnsafe <byte[], TimeSpan>((value) => value == null ? TimeSpan.Zero : TimeSpan.FromTicks(value.AsSlice().ToInt64())); RegisterUnsafe <byte[], System.Net.IPAddress>((value) => value == null || value.Length == 0 ? default(System.Net.IPAddress) : new System.Net.IPAddress(value)); RegisterUnsafe <Guid, Slice>((value) => Slice.FromGuid(value)); RegisterUnsafe <Guid, byte[]>((value) => Slice.FromGuid(value).GetBytes()); RegisterUnsafe <Guid, string>((value) => value.ToString("D", null)); RegisterUnsafe <Guid, Uuid128>((value) => new Uuid128(value)); RegisterUnsafe <Guid, bool>((value) => value != Guid.Empty); RegisterUnsafe <Guid, System.Net.IPAddress>((value) => new System.Net.IPAddress(new Uuid128(value).ToByteArray())); //REVIEW: custom converter for Guid=>IPv6? RegisterUnsafe <Uuid128, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid128, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid128, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid128, Guid>((value) => value.ToGuid()); RegisterUnsafe <Uuid128, bool>((value) => value != Uuid128.Empty); RegisterUnsafe <Uuid128, System.Net.IPAddress>((value) => new System.Net.IPAddress(value.ToByteArray())); //REVIEW: custom converter for Guid=>IPv6? RegisterUnsafe <Uuid64, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid64, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid64, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid64, long>((value) => value.ToInt64()); RegisterUnsafe <Uuid64, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Uuid64, bool>((value) => value.ToInt64() != 0L); RegisterUnsafe <TimeSpan, Slice>((value) => Slice.FromInt64(value.Ticks)); RegisterUnsafe <TimeSpan, byte[]>((value) => Slice.FromInt64(value.Ticks).GetBytes()); RegisterUnsafe <TimeSpan, long>((value) => value.Ticks); RegisterUnsafe <TimeSpan, ulong>((value) => checked ((ulong)value.Ticks)); RegisterUnsafe <TimeSpan, double>((value) => value.TotalSeconds); RegisterUnsafe <TimeSpan, bool>((value) => value == TimeSpan.Zero); RegisterUnsafe <System.Net.IPAddress, Slice>((value) => (value?.GetAddressBytes()).AsSlice()); RegisterUnsafe <System.Net.IPAddress, byte[]>((value) => value?.GetAddressBytes()); RegisterUnsafe <System.Net.IPAddress, string>((value) => value?.ToString()); #pragma warning disable 618 RegisterUnsafe <System.Net.IPAddress, int>((value) => (int)(value?.Address ?? 0)); #pragma warning restore 618 RegisterUnsafe <Slice, byte[]>((value) => value.GetBytes()); RegisterUnsafe <Slice, string>((value) => value.ToUnicode()); RegisterUnsafe <Slice, bool>((value) => value.ToBool()); RegisterUnsafe <Slice, sbyte>((value) => value.ToSByte()); RegisterUnsafe <Slice, byte>((value) => value.ToByte()); RegisterUnsafe <Slice, short>((value) => value.ToInt16()); RegisterUnsafe <Slice, ushort>((value) => value.ToUInt16()); RegisterUnsafe <Slice, int>((value) => value.ToInt32()); RegisterUnsafe <Slice, uint>((value) => value.ToUInt32()); RegisterUnsafe <Slice, long>((value) => value.ToInt64()); RegisterUnsafe <Slice, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Slice, float>((value) => value.ToSingle()); RegisterUnsafe <Slice, double>((value) => value.ToDouble()); RegisterUnsafe <Slice, decimal>((value) => value.ToDecimal()); RegisterUnsafe <Slice, Guid>((value) => value.ToGuid()); RegisterUnsafe <Slice, Uuid128>((value) => value.ToUuid128()); RegisterUnsafe <Slice, Uuid64>((value) => value.ToUuid64()); RegisterUnsafe <Slice, TimeSpan>((value) => TimeSpan.FromTicks(value.ToInt64())); RegisterUnsafe <Slice, System.Net.IPAddress>((value) => !value.IsNullOrEmpty ? new System.Net.IPAddress(value.GetBytesOrEmpty()) : null); }
public static string Stringify(Uuid64 item) => item.ToString("B", CultureInfo.InstalledUICulture); /* {xxxxxxxx-xxxxxxxx} */
public Slice EncodeValue(Uuid64 value) => Slice.FromUuid64(value);
public void Test_Uuid64_ToString_Base62() { char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); Assert.That(chars.Length, Is.EqualTo(62)); // single digit for (int i = 0; i < 62; i++) { Assert.That(new Uuid64(i).ToString("C"), Is.EqualTo(chars[i].ToString())); Assert.That(new Uuid64(i).ToString("Z"), Is.EqualTo("0000000000" + chars[i])); } // two digits for (int j = 1; j < 62; j++) { var prefix = chars[j].ToString(); for (int i = 0; i < 62; i++) { Assert.That(new Uuid64(j * 62 + i).ToString("C"), Is.EqualTo(prefix + chars[i])); Assert.That(new Uuid64(j * 62 + i).ToString("Z"), Is.EqualTo("000000000" + prefix + chars[i])); } } // 4 digits var rnd = new Random(); for (int i = 0; i < 100 * 1000; i++) { var a = rnd.Next(2) == 0 ? 0 : rnd.Next(62); var b = rnd.Next(2) == 0 ? 0 : rnd.Next(62); var c = rnd.Next(2) == 0 ? 0 : rnd.Next(62); var d = rnd.Next(62); ulong x = (ulong)a; x += 62 * (ulong)b; x += 62 * 62 * (ulong)c; x += 62 * 62 * 62 * (ulong)d; var uuid = new Uuid64(x); // no padding string expected = d > 0 ? ("" + chars[d] + chars[c] + chars[b] + chars[a]) : c > 0 ? ("" + chars[c] + chars[b] + chars[a]) : b > 0 ? ("" + chars[b] + chars[a]) : ("" + chars[a]); Assert.That(uuid.ToString("C"), Is.EqualTo(expected)); // padding Assert.That(uuid.ToString("Z"), Is.EqualTo("0000000" + chars[d] + chars[c] + chars[b] + chars[a])); } // Numbers of the form 62^n should be encoded as '1' followed by n x '0', for n from 0 to 10 ulong val = 1; for (int i = 0; i <= 10; i++) { Assert.That(new Uuid64(val).ToString("C"), Is.EqualTo("1" + new string('0', i)), "62^{0}", i); val *= 62; } // Numbers of the form 62^n - 1 should be encoded as n x 'z', for n from 1 to 10 val = 0; for (int i = 1; i <= 10; i++) { val += 61; Assert.That(new Uuid64(val).ToString("C"), Is.EqualTo(new string('z', i)), "62^{0} - 1", i); val *= 62; } // well known values Assert.That(new Uuid64(0xB45B07).ToString("C"), Is.EqualTo("narf")); Assert.That(new Uuid64(0xE0D0ED).ToString("C"), Is.EqualTo("zort")); Assert.That(new Uuid64(0xDEADBEEF).ToString("C"), Is.EqualTo("44pZgF")); Assert.That(new Uuid64(0xDEADBEEF).ToString("Z"), Is.EqualTo("0000044pZgF")); Assert.That(new Uuid64(0xBADC0FFEE0DDF00DUL).ToString("C"), Is.EqualTo("G2eGAUq82Hd")); Assert.That(new Uuid64(0xBADC0FFEE0DDF00DUL).ToString("Z"), Is.EqualTo("G2eGAUq82Hd")); Assert.That(new Uuid64(255).ToString("C"), Is.EqualTo("47")); Assert.That(new Uuid64(ushort.MaxValue).ToString("C"), Is.EqualTo("H31")); Assert.That(new Uuid64(uint.MaxValue).ToString("C"), Is.EqualTo("4gfFC3")); Assert.That(new Uuid64(ulong.MaxValue - 1).ToString("C"), Is.EqualTo("LygHa16AHYE")); Assert.That(new Uuid64(ulong.MaxValue).ToString("C"), Is.EqualTo("LygHa16AHYF")); }
/// <summary>Register all the default converters</summary> private static void RegisterDefaultConverters() { //TODO: there is too much generic type combinations! need to refactor this ... RegisterUnsafe <bool, Slice>((value) => Slice.FromByte(value ? (byte)1 : default(byte))); RegisterUnsafe <bool, byte[]>((value) => Slice.FromByte(value ? (byte)1 : default(byte)).GetBytes()); RegisterUnsafe <bool, string>((value) => value ? "true" : "false"); RegisterUnsafe <bool, sbyte>((value) => value ? (sbyte)1 : default(sbyte)); RegisterUnsafe <bool, byte>((value) => value ? (byte)1 : default(byte)); RegisterUnsafe <bool, short>((value) => value ? (short)1 : default(short)); RegisterUnsafe <bool, ushort>((value) => value ? (ushort)1 : default(ushort)); RegisterUnsafe <bool, int>((value) => value ? 1 : default(int)); RegisterUnsafe <bool, uint>((value) => value ? 1U : default(uint)); RegisterUnsafe <bool, long>((value) => value ? 1L : default(long)); RegisterUnsafe <bool, ulong>((value) => value ? 1UL : default(ulong)); RegisterUnsafe <bool, double>((value) => value ? 0.0d : 1.0d); RegisterUnsafe <bool, float>((value) => value ? 0.0f : 1.0f); RegisterUnsafe <int, Slice>((value) => Slice.FromInt32(value)); RegisterUnsafe <int, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <int, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <int, bool>((value) => value != 0); RegisterUnsafe <int, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <int, byte>((value) => checked ((byte)value)); RegisterUnsafe <int, short>((value) => checked ((short)value)); RegisterUnsafe <int, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <int, uint>((value) => (uint)value); RegisterUnsafe <int, long>((value) => value); RegisterUnsafe <int, ulong>((value) => (ulong)value); RegisterUnsafe <int, double>((value) => value); RegisterUnsafe <int, float>((value) => checked ((float)value)); RegisterUnsafe <int, FdbTupleAlias>((value) => (FdbTupleAlias)value); RegisterUnsafe <uint, Slice>((value) => Slice.FromUInt64(value)); RegisterUnsafe <uint, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <uint, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <uint, bool>((value) => value != 0); RegisterUnsafe <uint, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <uint, byte>((value) => checked ((byte)value)); RegisterUnsafe <uint, short>((value) => checked ((short)value)); RegisterUnsafe <uint, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <uint, int>((value) => (int)value); RegisterUnsafe <uint, long>((value) => value); RegisterUnsafe <uint, ulong>((value) => value); RegisterUnsafe <uint, double>((value) => value); RegisterUnsafe <uint, float>((value) => checked ((float)value)); RegisterUnsafe <long, Slice>((value) => Slice.FromInt64(value)); RegisterUnsafe <long, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <long, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <long, bool>((value) => value != 0); RegisterUnsafe <long, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <long, byte>((value) => checked ((byte)value)); RegisterUnsafe <long, short>((value) => checked ((short)value)); RegisterUnsafe <long, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <long, int>((value) => checked ((int)value)); RegisterUnsafe <long, uint>((value) => (uint)value); RegisterUnsafe <long, ulong>((value) => (ulong)value); RegisterUnsafe <long, double>((value) => checked ((double)value)); RegisterUnsafe <long, float>((value) => checked ((float)value)); RegisterUnsafe <long, TimeSpan>((value) => TimeSpan.FromTicks(value)); RegisterUnsafe <long, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <long, System.Net.IPAddress>((value) => new System.Net.IPAddress(value)); RegisterUnsafe <ulong, Slice>((value) => Slice.FromUInt64(value)); RegisterUnsafe <ulong, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <ulong, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <ulong, bool>((value) => value != 0); RegisterUnsafe <ulong, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ulong, byte>((value) => checked ((byte)value)); RegisterUnsafe <ulong, short>((value) => checked ((short)value)); RegisterUnsafe <ulong, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <ulong, int>((value) => checked ((int)value)); RegisterUnsafe <ulong, uint>((value) => checked ((uint)value)); RegisterUnsafe <ulong, long>((value) => checked ((long)value)); RegisterUnsafe <ulong, double>((value) => checked ((double)value)); RegisterUnsafe <ulong, float>((value) => checked ((float)value)); RegisterUnsafe <ulong, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <ulong, TimeSpan>((value) => TimeSpan.FromTicks(checked ((long)value))); RegisterUnsafe <short, Slice>((value) => Slice.FromInt32(value)); RegisterUnsafe <short, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <short, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <short, bool>((value) => value != 0); RegisterUnsafe <short, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <short, byte>((value) => checked ((byte)value)); RegisterUnsafe <short, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <short, int>((value) => value); RegisterUnsafe <short, uint>((value) => checked ((uint)value)); RegisterUnsafe <short, long>((value) => value); RegisterUnsafe <short, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <short, double>((value) => value); RegisterUnsafe <short, float>((value) => value); RegisterUnsafe <short, FdbTupleAlias>((value) => (FdbTupleAlias)value); RegisterUnsafe <ushort, Slice>((value) => Slice.FromUInt64(value)); RegisterUnsafe <ushort, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <ushort, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <ushort, bool>((value) => value != 0); RegisterUnsafe <ushort, byte>((value) => checked ((byte)value)); RegisterUnsafe <ushort, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ushort, short>((value) => checked ((short)value)); RegisterUnsafe <ushort, int>((value) => value); RegisterUnsafe <ushort, uint>((value) => value); RegisterUnsafe <ushort, long>((value) => value); RegisterUnsafe <ushort, ulong>((value) => value); RegisterUnsafe <ushort, double>((value) => value); RegisterUnsafe <ushort, float>((value) => value); RegisterUnsafe <byte, Slice>((value) => Slice.FromInt32(value)); RegisterUnsafe <byte, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <byte, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <byte, bool>((value) => value != 0); RegisterUnsafe <byte, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <byte, short>((value) => value); RegisterUnsafe <byte, ushort>((value) => value); RegisterUnsafe <byte, int>((value) => value); RegisterUnsafe <byte, uint>((value) => value); RegisterUnsafe <byte, long>((value) => value); RegisterUnsafe <byte, ulong>((value) => value); RegisterUnsafe <byte, double>((value) => value); RegisterUnsafe <byte, float>((value) => value); RegisterUnsafe <byte, FdbTupleAlias>((value) => (FdbTupleAlias)value); RegisterUnsafe <sbyte, Slice>((value) => Slice.FromInt64(value)); RegisterUnsafe <sbyte, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <sbyte, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <sbyte, bool>((value) => value != 0); RegisterUnsafe <sbyte, byte>((value) => checked ((byte)value)); RegisterUnsafe <sbyte, short>((value) => value); RegisterUnsafe <sbyte, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <sbyte, int>((value) => value); RegisterUnsafe <sbyte, uint>((value) => checked ((uint)value)); RegisterUnsafe <sbyte, long>((value) => value); RegisterUnsafe <sbyte, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <sbyte, double>((value) => value); RegisterUnsafe <sbyte, float>((value) => value); RegisterUnsafe <float, Slice>((value) => Slice.FromSingle(value)); RegisterUnsafe <float, byte[]>((value) => Slice.FromSingle(value).GetBytes()); RegisterUnsafe <float, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <float, bool>((value) => !(value == 0f || float.IsNaN(value))); RegisterUnsafe <float, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <float, byte>((value) => checked ((byte)value)); RegisterUnsafe <float, short>((value) => checked ((short)value)); RegisterUnsafe <float, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <float, int>((value) => checked ((int)value)); RegisterUnsafe <float, uint>((value) => (uint)value); RegisterUnsafe <float, long>((value) => checked ((long)value)); RegisterUnsafe <float, ulong>((value) => (ulong)value); RegisterUnsafe <float, double>((value) => value); RegisterUnsafe <double, Slice>((value) => Slice.FromDouble(value)); RegisterUnsafe <double, byte[]>((value) => Slice.FromDouble(value).GetBytes()); RegisterUnsafe <double, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <double, bool>((value) => !(value == 0d || double.IsNaN(value))); RegisterUnsafe <double, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <double, byte>((value) => checked ((byte)value)); RegisterUnsafe <double, short>((value) => checked ((short)value)); RegisterUnsafe <double, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <double, int>((value) => checked ((int)value)); RegisterUnsafe <double, uint>((value) => (uint)value); RegisterUnsafe <double, long>((value) => checked ((long)value)); RegisterUnsafe <double, ulong>((value) => (ulong)value); RegisterUnsafe <double, float>((value) => checked ((float)value)); RegisterUnsafe <string, Slice>((value) => Slice.FromString(value)); RegisterUnsafe <string, byte[]>((value) => Slice.FromString(value).GetBytes()); RegisterUnsafe <string, bool>((value) => !string.IsNullOrEmpty(value)); RegisterUnsafe <string, sbyte>((value) => string.IsNullOrEmpty(value) ? default(sbyte) : SByte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, byte>((value) => string.IsNullOrEmpty(value) ? default(byte) : Byte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, short>((value) => string.IsNullOrEmpty(value) ? default(short) : Int16.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ushort>((value) => string.IsNullOrEmpty(value) ? default(ushort) : UInt16.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, int>((value) => string.IsNullOrEmpty(value) ? default(int) : Int32.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, uint>((value) => string.IsNullOrEmpty(value) ? default(uint) : UInt32.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, long>((value) => string.IsNullOrEmpty(value) ? default(long) : Int64.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ulong>((value) => string.IsNullOrEmpty(value) ? default(ulong) : UInt64.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, float>((value) => string.IsNullOrEmpty(value) ? default(float) : Single.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, double>((value) => string.IsNullOrEmpty(value) ? default(double) : Double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, Guid>((value) => string.IsNullOrEmpty(value) ? default(Guid) : Guid.Parse(value)); RegisterUnsafe <string, Uuid128>((value) => string.IsNullOrEmpty(value) ? default(Uuid128) : Uuid128.Parse(value)); RegisterUnsafe <string, Uuid64>((value) => string.IsNullOrEmpty(value) ? default(Uuid64) : Uuid64.Parse(value)); RegisterUnsafe <string, System.Net.IPAddress>((value) => string.IsNullOrEmpty(value) ? default(System.Net.IPAddress) : System.Net.IPAddress.Parse(value)); RegisterUnsafe <byte[], Slice>((value) => Slice.Create(value)); RegisterUnsafe <byte[], string>((value) => value == null ? default(string) : value.Length == 0 ? String.Empty : System.Convert.ToBase64String(value)); RegisterUnsafe <byte[], bool>((value) => value != null && value.Length > 0); RegisterUnsafe <byte[], sbyte>((value) => value == null ? default(sbyte) : Slice.Create(value).ToSByte()); RegisterUnsafe <byte[], byte>((value) => value == null ? default(byte) : Slice.Create(value).ToByte()); RegisterUnsafe <byte[], short>((value) => value == null ? default(short) : Slice.Create(value).ToInt16()); RegisterUnsafe <byte[], ushort>((value) => value == null ? default(ushort) : Slice.Create(value).ToUInt16()); RegisterUnsafe <byte[], int>((value) => value == null ? 0 : Slice.Create(value).ToInt32()); RegisterUnsafe <byte[], uint>((value) => value == null ? 0U : Slice.Create(value).ToUInt32()); RegisterUnsafe <byte[], long>((value) => value == null ? 0L : Slice.Create(value).ToInt64()); RegisterUnsafe <byte[], ulong>((value) => value == null ? 0UL : Slice.Create(value).ToUInt64()); RegisterUnsafe <byte[], Guid>((value) => value == null || value.Length == 0 ? default(Guid) : new Uuid128(value).ToGuid()); RegisterUnsafe <byte[], Uuid128>((value) => value == null || value.Length == 0 ? default(Uuid128) : new Uuid128(value)); RegisterUnsafe <byte[], Uuid64>((value) => value == null || value.Length == 0 ? default(Uuid64) : new Uuid64(value)); RegisterUnsafe <byte[], TimeSpan>((value) => value == null ? TimeSpan.Zero : TimeSpan.FromTicks(Slice.Create(value).ToInt64())); RegisterUnsafe <byte[], System.Net.IPAddress>((value) => value == null || value.Length == 0 ? default(System.Net.IPAddress) : new System.Net.IPAddress(value)); RegisterUnsafe <Guid, Slice>((value) => Slice.FromGuid(value)); RegisterUnsafe <Guid, byte[]>((value) => Slice.FromGuid(value).GetBytes()); RegisterUnsafe <Guid, string>((value) => value.ToString("D", null)); RegisterUnsafe <Guid, Uuid128>((value) => new Uuid128(value)); RegisterUnsafe <Guid, bool>((value) => value != Guid.Empty); RegisterUnsafe <Guid, System.Net.IPAddress>((value) => new System.Net.IPAddress(new Uuid128(value).ToByteArray())); RegisterUnsafe <Uuid128, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid128, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid128, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid128, Guid>((value) => value.ToGuid()); RegisterUnsafe <Uuid128, bool>((value) => value != Uuid128.Empty); RegisterUnsafe <Guid, System.Net.IPAddress>((value) => new System.Net.IPAddress(value.ToByteArray())); RegisterUnsafe <Uuid64, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid64, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid64, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid64, long>((value) => value.ToInt64()); RegisterUnsafe <Uuid64, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Uuid64, bool>((value) => value.ToInt64() != 0L); RegisterUnsafe <TimeSpan, Slice>((value) => Slice.FromInt64(value.Ticks)); RegisterUnsafe <TimeSpan, byte[]>((value) => Slice.FromInt64(value.Ticks).GetBytes()); RegisterUnsafe <TimeSpan, long>((value) => value.Ticks); RegisterUnsafe <TimeSpan, ulong>((value) => checked ((ulong)value.Ticks)); RegisterUnsafe <TimeSpan, double>((value) => value.TotalSeconds); RegisterUnsafe <TimeSpan, bool>((value) => value == TimeSpan.Zero); RegisterUnsafe <System.Net.IPAddress, Slice>((value) => value != null ? Slice.Create(value.GetAddressBytes()) : Slice.Nil); RegisterUnsafe <System.Net.IPAddress, byte[]>((value) => value != null ? value.GetAddressBytes() : null); RegisterUnsafe <System.Net.IPAddress, string>((value) => value != null ? value.ToString() : null); RegisterUnsafe <FdbTupleAlias, byte>((value) => (byte)value); RegisterUnsafe <FdbTupleAlias, int>((value) => (int)value); RegisterUnsafe <FdbTupleAlias, Slice>((value) => Slice.FromByte((byte)value)); //REVIEW: this should go in the Tuples layer ! RegisterUnsafe <Slice, byte[]>((value) => value.GetBytes()); RegisterUnsafe <Slice, string>((value) => value.ToUnicode()); RegisterUnsafe <Slice, bool>((value) => value.ToBool()); RegisterUnsafe <Slice, sbyte>((value) => value.ToSByte()); RegisterUnsafe <Slice, byte>((value) => value.ToByte()); RegisterUnsafe <Slice, short>((value) => value.ToInt16()); RegisterUnsafe <Slice, ushort>((value) => value.ToUInt16()); RegisterUnsafe <Slice, int>((value) => value.ToInt32()); RegisterUnsafe <Slice, uint>((value) => value.ToUInt32()); RegisterUnsafe <Slice, long>((value) => value.ToInt64()); RegisterUnsafe <Slice, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Slice, Guid>((value) => value.ToGuid()); RegisterUnsafe <Slice, Uuid128>((value) => value.ToUuid128()); RegisterUnsafe <Slice, Uuid64>((value) => value.ToUuid64()); RegisterUnsafe <Slice, TimeSpan>((value) => TimeSpan.FromTicks(value.ToInt64())); RegisterUnsafe <Slice, FdbTupleAlias>((value) => (FdbTupleAlias)value.ToByte()); RegisterUnsafe <Slice, System.Net.IPAddress>((value) => !value.IsNullOrEmpty ? new System.Net.IPAddress(value.GetBytes()) : null); }
/// <summary>Writes a 64-bit UUID</summary> public static void WriteUuid64(ref SliceWriter writer, Uuid64 value) { writer.EnsureBytes(9); writer.UnsafeWriteByte(FdbTupleTypes.Uuid64); unsafe { byte* ptr = stackalloc byte[8]; value.WriteTo(ptr); writer.UnsafeWriteBytes(ptr, 8); } }
public void Test_Uuid64_Parse_Hexa16() { // string Assert.That(Uuid64.Parse("badc0ffe-e0ddf00d").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("BADC0FFE-E0DDF00D").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL), "Should be case-insensitive"); Assert.That(Uuid64.Parse("badc0ffee0ddf00d").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("BADC0FFEE0DDF00D").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL), "Should be case-insensitive"); Assert.That(Uuid64.Parse("{badc0ffe-e0ddf00d}").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("{BADC0FFE-E0DDF00D}").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL), "Should be case-insensitive"); Assert.That(Uuid64.Parse("{badc0ffee0ddf00d}").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("{BADC0FFEE0DDF00D}").ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL), "should be case-insensitive"); Assert.That(Uuid64.Parse("00000000-deadbeef").ToUInt64(), Is.EqualTo(0xDEADBEEFUL)); Assert.That(Uuid64.Parse("{00000000-deadbeef}").ToUInt64(), Is.EqualTo(0xDEADBEEFUL)); // errors Assert.That(() => Uuid64.Parse(default(string)), Throws.ArgumentNullException); Assert.That(() => Uuid64.Parse("hello"), Throws.InstanceOf <FormatException>()); Assert.That(() => Uuid64.Parse("12345678-9ABCDEFG"), Throws.InstanceOf <FormatException>(), "Invalid hexa character 'G'"); Assert.That(() => Uuid64.Parse("00000000-0000000 "), Throws.InstanceOf <FormatException>(), "Two short + extra space"); Assert.That(() => Uuid64.Parse("zzzzzzzz-zzzzzzzz"), Throws.InstanceOf <FormatException>(), "Invalid char"); Assert.That(() => Uuid64.Parse("badc0ffe-e0ddf00"), Throws.InstanceOf <FormatException>(), "Missing last char"); Assert.That(() => Uuid64.Parse("baadc0ffe-e0ddf00"), Throws.InstanceOf <FormatException>(), "'-' at invalid position"); Assert.That(() => Uuid64.Parse("badc0fe-ee0ddf00d"), Throws.InstanceOf <FormatException>(), "'-' at invalid position"); Assert.That(() => Uuid64.Parse("badc0ffe-e0ddf00d "), Throws.InstanceOf <FormatException>(), "Extra space at the end"); Assert.That(() => Uuid64.Parse(" badc0ffe-e0ddf00d"), Throws.InstanceOf <FormatException>(), "Extra space at the start"); #if ENABLE_SPAN // span from string Assert.That(Uuid64.Parse("badc0ffe-e0ddf00d".AsSpan()).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("badc0ffee0ddf00d".AsSpan()).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("hello badc0ffe-e0ddf00d world!".AsSpan().Slice(6, 17)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("hello badc0ffee0ddf00d world!".AsSpan().Slice(6, 16)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); // span from char[] Assert.That(Uuid64.Parse("badc0ffe-e0ddf00d".ToCharArray().AsSpan()).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("badc0ffee0ddf00d".ToCharArray().AsSpan()).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("hello badc0ffe-e0ddf00d world!".ToCharArray().AsSpan().Slice(6, 17)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); Assert.That(Uuid64.Parse("hello badc0ffee0ddf00d world!".ToCharArray().AsSpan().Slice(6, 16)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); // span from stackalloc unsafe { char *buf = stackalloc char[64]; var span = new Span <char>(buf, 64); span.Clear(); "badc0ffe-e0ddf00d".AsSpan().CopyTo(span); Assert.That(Uuid64.Parse(span.Slice(0, 17)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); span.Clear(); "badc0ffee0ddf00d".AsSpan().CopyTo(span); Assert.That(Uuid64.Parse(span.Slice(0, 16)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); span.Clear(); "{badc0ffe-e0ddf00d}".AsSpan().CopyTo(span); Assert.That(Uuid64.Parse(span.Slice(0, 19)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); span.Clear(); "{badc0ffee0ddf00d}".AsSpan().CopyTo(span); Assert.That(Uuid64.Parse(span.Slice(0, 18)).ToUInt64(), Is.EqualTo(0xBADC0FFEE0DDF00DUL)); } #endif }
public void Test_Uuid64_ToString_Base62() { char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); Assert.That(chars.Length, Is.EqualTo(62)); // single digit for (int i = 0; i < 62;i++) { Assert.That(new Uuid64(i).ToString("C"), Is.EqualTo(chars[i].ToString())); Assert.That(new Uuid64(i).ToString("Z"), Is.EqualTo("0000000000" + chars[i])); } // two digits for (int j = 1; j < 62; j++) { var prefix = chars[j].ToString(); for (int i = 0; i < 62; i++) { Assert.That(new Uuid64(j * 62 + i).ToString("C"), Is.EqualTo(prefix + chars[i])); Assert.That(new Uuid64(j * 62 + i).ToString("Z"), Is.EqualTo("000000000" + prefix + chars[i])); } } // 4 digits var rnd = new Random(); for (int i = 0; i < 100 * 1000; i++) { var a = rnd.Next(2) == 0 ? 0 : rnd.Next(62); var b = rnd.Next(2) == 0 ? 0 : rnd.Next(62); var c = rnd.Next(2) == 0 ? 0 : rnd.Next(62); var d = rnd.Next(62); ulong x = (ulong)a; x += 62 * (ulong)b; x += 62 * 62 * (ulong)c; x += 62 * 62 * 62 * (ulong)d; var uuid = new Uuid64(x); // no padding string expected = d > 0 ? ("" + chars[d] + chars[c] + chars[b] + chars[a]) : c > 0 ? ("" + chars[c] + chars[b] + chars[a]) : b > 0 ? ("" + chars[b] + chars[a]) : ("" + chars[a]); Assert.That(uuid.ToString("C"), Is.EqualTo(expected)); // padding Assert.That(uuid.ToString("Z"), Is.EqualTo("0000000" + chars[d] + chars[c] + chars[b] + chars[a])); } // Numbers of the form 62^n should be encoded as '1' followed by n x '0', for n from 0 to 10 ulong val = 1; for (int i = 0; i <= 10; i++) { Assert.That(new Uuid64(val).ToString("C"), Is.EqualTo("1" + new string('0', i)), "62^{0}", i); val *= 62; } // Numbers of the form 62^n - 1 should be encoded as n x 'z', for n from 1 to 10 val = 0; for (int i = 1; i <= 10; i++) { val += 61; Assert.That(new Uuid64(val).ToString("C"), Is.EqualTo(new string('z', i)), "62^{0} - 1", i); val *= 62; } // well known values Assert.That(new Uuid64(0xB45B07).ToString("C"), Is.EqualTo("narf")); Assert.That(new Uuid64(0xE0D0ED).ToString("C"), Is.EqualTo("zort")); Assert.That(new Uuid64(0xDEADBEEF).ToString("C"), Is.EqualTo("44pZgF")); Assert.That(new Uuid64(0xDEADBEEF).ToString("Z"), Is.EqualTo("0000044pZgF")); Assert.That(new Uuid64(0xBADC0FFEE0DDF00DUL).ToString("C"), Is.EqualTo("G2eGAUq82Hd")); Assert.That(new Uuid64(0xBADC0FFEE0DDF00DUL).ToString("Z"), Is.EqualTo("G2eGAUq82Hd")); Assert.That(new Uuid64(255).ToString("C"), Is.EqualTo("47")); Assert.That(new Uuid64(ushort.MaxValue).ToString("C"), Is.EqualTo("H31")); Assert.That(new Uuid64(uint.MaxValue).ToString("C"), Is.EqualTo("4gfFC3")); Assert.That(new Uuid64(ulong.MaxValue - 1).ToString("C"), Is.EqualTo("LygHa16AHYE")); Assert.That(new Uuid64(ulong.MaxValue).ToString("C"), Is.EqualTo("LygHa16AHYF")); }