public void Ulid_Parse_Handles_IiLl_TreatedAs_One() //https://www.crockford.com/base32.html { Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), Ulid.Parse(KNOWNTIMESTAMP_STRING.Replace('1', 'i') + KNOWNRANDOMSEQ_STRING)); Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), Ulid.Parse(KNOWNTIMESTAMP_STRING.Replace('1', 'I') + KNOWNRANDOMSEQ_STRING)); Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), Ulid.Parse(KNOWNTIMESTAMP_STRING.Replace('1', 'l') + KNOWNRANDOMSEQ_STRING)); Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), Ulid.Parse(KNOWNTIMESTAMP_STRING.Replace('1', 'L') + KNOWNRANDOMSEQ_STRING)); }
private static bool HandleStringToSpecialCaseTypes(object sourceValue, Type targetType, IFormatProvider targetFormatProvider, ref object targetValue) { var sourceValueAsString = sourceValue as string; if (sourceValueAsString == null) { return(false); } if (targetType == typeof(Guid)) { targetValue = new Guid(sourceValueAsString); return(true); } if (targetType == typeof(Ulid)) { targetValue = Ulid.Parse(sourceValueAsString); return(true); } if (targetType == typeof(Uri)) { targetValue = new Uri(sourceValueAsString, UriKind.RelativeOrAbsolute); return(true); } if (targetType == typeof(DateTime)) { targetValue = DateTime.Parse(sourceValueAsString, targetFormatProvider, DateTimeStyles.RoundtripKind); return(true); } if (targetType == typeof(DateTimeOffset)) { targetValue = DateTimeOffset.Parse(sourceValueAsString, targetFormatProvider, DateTimeStyles.RoundtripKind); return(true); } if (targetType == typeof(TimeSpan)) { targetValue = TimeSpan.Parse(sourceValueAsString, targetFormatProvider); return(true); } if (targetType == typeof(byte[])) { targetValue = System.Convert.FromBase64String(sourceValueAsString); return(true); } if (typeof(Type).IsAssignableFrom(targetType)) { targetValue = Type.GetType(sourceValueAsString, true); return(true); } return(false); }
public void Parse() { for (int i = 0; i < 100; i++) { var nulid = NUlid.Ulid.NewUlid(); Ulid.Parse(nulid.ToString()).ToByteArray().Should().BeEquivalentTo(nulid.ToByteArray()); } }
public void Ulid_Parse_ParsesCorrectly() { var ulid = Ulid.NewUlid(); var target = Ulid.Parse(ulid.ToString()); Assert.IsTrue(target.Random.SequenceEqual(ulid.Random)); Assert.AreEqual(ulid.Time, target.Time); }
// ReSharper disable once UnusedMember.Global public ChannelReader <string> CheckJobStatus(string grainId, CancellationToken cancellationToken) { var channel = Channel.CreateUnbounded <string>(); _ = DetectLongRunningTaskStatus(channel.Writer, Ulid.Parse(grainId), 3, cancellationToken); return(channel.Reader); }
static void LiteId(string[] args) { if (args.Length < 1) { Console.WriteLine("LiteDb complex id insertion benchmark. Supply number of items to insert"); Console.WriteLine("Example: benchmark.exe liteId 10000"); return; } var count = int.Parse(args[0]); var longId = 1L; var intId = 1; DoBenchmark("Base", () => new LiteEntityInt { Id = intId++, Address = "some address long", Amount = 123, PersonName = "some person name" }); DoBenchmark("Base", () => new LiteEntityLong { Id = longId++, Address = "some address long", Amount = 123, PersonName = "some person name" }); DoBenchmark("SequentialGuid", () => new LiteEntityId { Id = new Id <LiteEntityId>(SequentialGuid.SequentialGuidGenerator.Instance.NewGuid()), Address = "some address long", Amount = 123, PersonName = "some person name" }); BsonMapper.Global.RegisterType(x => x.ToString(), x => Ulid.Parse(x)); DoBenchmark("String Ulid", () => new LiteEntityLid { Id = Lid <LiteEntityLid> .NewId(), Address = "some address long", Amount = 123, PersonName = "some person name" }); BsonMapper.Global.RegisterIdBsonMappers(typeof(Program).Assembly); DoBenchmark("String SGuid", () => new LiteEntityId { Id = new Id <LiteEntityId>(SequentialGuid.SequentialGuidGenerator.Instance.NewGuid()), Address = "some address long", Amount = 123, PersonName = "some person name" }); void DoBenchmark <T>(string name, Func <T> faker) where T : class { var storageFile = name + faker.GetType().GenericTypeArguments[0].Name + ".litedb"; if (File.Exists(storageFile)) { File.Delete(storageFile); } using var repo = LiteRepo.WithUtcDate(storageFile); var sw = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { repo.Insert(faker()); } sw.Restart(); for (int i = 0; i < count; i++) { repo.Insert(faker()); } sw.Stop(); Console.WriteLine($"{name,-20} {typeof(T).Name,-20}: {sw.ElapsedMilliseconds,6} ms, {count * 1000 / sw.ElapsedMilliseconds,5} iops"); } }
public void MonotonicRng_Sequence_Testvectors() { var target = Ulid.Parse("01BX5ZZKBKACTAV9WEVGEMMVRY"); var rng = new MonotonicUlidRng(new FakeUlidRng(), lastValue: target); Assert.AreEqual("01BX5ZZKBKACTAV9WEVGEMMVRZ", Ulid.NewUlid(target.Time, rng).ToString()); Assert.AreEqual("01BX5ZZKBKACTAV9WEVGEMMVS0", Ulid.NewUlid(target.Time, rng).ToString()); Assert.AreEqual("01BX5ZZKBKACTAV9WEVGEMMVS1", Ulid.NewUlid(target.Time, rng).ToString()); }
public void MonotonicRng_Sequence_Throws_OnOverflow() { var target = Ulid.Parse("01BX5ZZKBKZZZZZZZZZZZZZZZX"); var rng = new MonotonicUlidRng(new FakeUlidRng(), lastValue: target); Assert.IsTrue(Ulid.NewUlid(target.Time, rng).ToString().EndsWith("ZZZZZZZZZZZZZZZY")); Assert.IsTrue(Ulid.NewUlid(target.Time, rng).ToString().EndsWith("ZZZZZZZZZZZZZZZZ")); Ulid.NewUlid(target.Time, rng); // Should throw }
public void Ulid_Parse_WorksCorrectly() { Assert.AreEqual(Ulid.MinValue, Ulid.Parse(Ulid.MinValue.ToString())); Assert.AreEqual(Ulid.MaxValue, Ulid.Parse(Ulid.MaxValue.ToString())); var target = Ulid.NewUlid(KNOWNTIMESTAMP_DTO, new FakeUlidRng()); Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), target); Assert.AreEqual(new Ulid(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), target); }
public void MonotonicRng_Sequence_Resets_OnNewTimeStamp() { var target = Ulid.Parse("01BX5ZZKBKZZZZZZZZZZZZZZZX"); var rng = new MonotonicUlidRng(new FakeUlidRng(), lastValue: target); Assert.IsTrue(Ulid.NewUlid(target.Time, rng).ToString().EndsWith("ZZZZZZZZZZZZZZZY")); Assert.IsTrue(Ulid.NewUlid(target.Time, rng).ToString().EndsWith("ZZZZZZZZZZZZZZZZ")); // Now we change the time, JUST in time before we overflow var result = Ulid.NewUlid(target.Time.Add(TimeSpan.FromMilliseconds(1)), rng); // Should NOT throw Assert.AreEqual("01BX5ZZKBM00ADBEEFDEADBEEF", result.ToString()); // We should have a new "random" value and timestamp should have increased by one }
public void Ulid_ConvertedTo_Guid_MaintainsSortOrder() { //Chosen because the non-corrected GUIDs that are created from these ULIDs result in the //GUID created from the larger ULID lexicographically smaller than the GUID created from the //smaller ULID; specifically: // - smallerGuid: {43867701-fffd-0600-1c67-0bd309234c7c} rather than {01778643-fdff-0006-1c67-0bd309234c7c} // - largerGuid: {43867701-00fe-0000-2a61-953560310b0f} rather than {01778643-fe00-0000-2a61-953560310b0f} var smallerUlid = Ulid.Parse("01EY347ZFZ0031RSRBTC4J6K3W"); var largerUlid = Ulid.Parse("01EY347ZG00002MRCN6NG322RF"); Assert.AreEqual(-1, smallerUlid.CompareTo(largerUlid)); var smallerGuid = smallerUlid.ToGuid(); var largerGuid = largerUlid.ToGuid(); Assert.AreEqual(-1, smallerGuid.CompareTo(largerGuid)); }
static void Main() { var simplerng = new SimpleUlidRng(); var csrng = new CSUlidRng(); var simplemonotonicrng = new MonotonicUlidRng(simplerng); var csmonotonicrng = new MonotonicUlidRng(csrng); var plainrng = new Random(); var pt = new SimpleBencher(10000000); //pt.BenchRunning += (s, e) => Console.WriteLine($"Running {e.Title}, {e.Iterations:N0} iterations..."); //pt.BenchWarmup += (s, e) => Console.WriteLine($"Warmup {e.Title}, {e.Iterations:N0} iterations..."); pt.BenchComplete += (s, e) => Console.WriteLine($"Completed {e.Title,-40}: {e.Result.OperationsPerSecond(),15:N0}/sec."); var d = DateTimeOffset.Now; var benchtests = new IBench[] { new ForBench("Guid.NewGuid()", () => Guid.NewGuid()), new ForBench("Ulid.NewUlid(SimpleUlidRng)", () => Ulid.NewUlid(simplerng)), new ForBench("Ulid.NewUlid(CSUlidRng)", () => Ulid.NewUlid(csrng)), new ForBench("Ulid.NewUlid(SimpleMonotonicUlidRng)", () => Ulid.NewUlid(simplemonotonicrng)), new ForBench("Ulid.NewUlid(CSMonotonicUlidRng)", () => Ulid.NewUlid(csmonotonicrng)), new ForEachBench <string>("Guid.Parse(string)", (i) => Guid.Parse(i), (i) => Enumerable.Range(0, i).Select(n => Guid.NewGuid().ToString())), new ForEachBench <string>("Ulid.Parse(string)", (i) => Ulid.Parse(i), (i) => Enumerable.Range(0, i).Select(n => Ulid.NewUlid().ToString())), new ForEachBench <Guid>("Guid.ToString()", (i) => i.ToString(), (i) => Enumerable.Range(0, i).Select(n => Guid.NewGuid())), new ForEachBench <Ulid>("Ulid.ToString()", (i) => i.ToString(), (i) => Enumerable.Range(0, i).Select(n => Ulid.NewUlid())), new ForEachBench <byte[]>("new Guid(byte[])", (i) => new Guid(i), (i) => Enumerable.Range(0, i).Select(n => { var b = new byte[16]; plainrng.NextBytes(b); return(b); })), new ForEachBench <byte[]>("new Ulid(byte[])", (i) => new Ulid(i), (i) => Enumerable.Range(0, i).Select(n => { var b = new byte[16]; plainrng.NextBytes(b); return(b); })), new ForEachBench <Guid>("Guid.ToByteArray()", (i) => i.ToByteArray(), (i) => Enumerable.Range(0, i).Select(n => Guid.NewGuid())), new ForEachBench <Ulid>("Ulid.ToByteArray()", (i) => i.ToByteArray(), (i) => Enumerable.Range(0, i).Select(n => Ulid.NewUlid())), new ForEachBench <Ulid>("Ulid.ToGuid()", (i) => i.ToGuid(), (i) => Enumerable.Range(0, i).Select(n => Ulid.NewUlid())), new ForEachBench <Guid>("new Ulid(Guid)", (i) => new Ulid(i), (i) => Enumerable.Range(0, i).Select(n => Guid.NewGuid())), }; var results = pt.BenchMark(benchtests).ToArray(); Console.WriteLine("Done."); }
public void TestUlid() { // just create var anything = Ulid.Create(); Assert.IsTrue(anything.ToString() != null); // from timestamp var dto = DateTimeOffset.FromUnixTimeMilliseconds(1484581420); var ulid = Ulid.Create(dto); Assert.IsTrue(ulid.ToString().StartsWith("0001C7STHC")); // string roundtrip var str = anything.ToString(); var rev = Ulid.Parse(str); Assert.IsTrue(str == rev.ToString()); // bytes roundtrip var bytes = new byte[16]; anything.AsBytes(bytes); var fromBytes = new Ulid(bytes); Assert.IsTrue(anything.Equals(fromBytes)); Assert.IsTrue(anything.ToString() == fromBytes.ToString()); // test operators var one = new Ulid(100UL, 200UL); var two = new Ulid(42UL, 43UL); var three = new Ulid(42UL, 43UL); Assert.IsTrue(one != two); Assert.IsTrue(two == three); }
public void Ulid_Parse_Handles_Oo_TreatedAs_Zero() //https://www.crockford.com/base32.html { Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), Ulid.Parse(KNOWNTIMESTAMP_STRING.Replace('0', 'o') + KNOWNRANDOMSEQ_STRING)); Assert.AreEqual(Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING), Ulid.Parse(KNOWNTIMESTAMP_STRING.Replace('0', 'O') + KNOWNRANDOMSEQ_STRING)); }
public FileRef(string id) { Id = Ulid.Parse(id); }
public void Parse_Allows_Hyphens() { Assert.AreEqual(Ulid.Parse("01BX5ZZKBKACTAV9WEVGEMMVRZ"), Ulid.Parse("01BX5ZZKBK-ACTA-V9WE-VGEM-MVRZ")); Assert.AreEqual(Ulid.Parse("01BX5ZZKBKACTAV9WEVGEMMVRZ"), Ulid.Parse("01BX5ZZKBK-ACTAV9WEVGEMMVRZ")); }
public void Ulid_Parse_ThrowsFormatException_OnInvalidString2() => Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING.Replace('E', '{')); // Test char after last index in C2B32 array
public void Ulid_Parse_ThrowsFormatException_OnInvalidString1() => Ulid.Parse(KNOWNTIMESTAMP_STRING + KNOWNRANDOMSEQ_STRING.Replace('E', 'U')); // U is not in BASE32 alphabet
public Ulid Ulid_() { return(Ulid.Parse(ulid)); }
public void Ulid_Parse_ThrowsFormatException_OnInvalidLengthString() => Ulid.Parse("TEST");
public void Ulid_Parse_ThrowsArgumentNullException_OnEmptyString() => Ulid.Parse(string.Empty);
public void Ulid_Parse_ThrowsArgumentNullException_OnNull() => Ulid.Parse(null);
public void UlidParseRejectsInvalidStrings() { Assert.Throws <ArgumentException>(() => Ulid.Parse("1234")); Assert.Throws <ArgumentException>(() => Ulid.Parse(Guid.NewGuid().ToString())); }
static void Main(string[] args) { int iterations = 1000000; Time <object>((p, it) => { for (int i = 0; i < it; i++) { Guid.NewGuid(); } }, iterations, "Guid.NewGuid(): {0,15:N0}/sec" ); Time <object>( (p, it) => { var simplerng = new SimpleUlidRng(); for (int i = 0; i < it; i++) { Ulid.NewUlid(simplerng); } }, iterations, "Ulid.NewUlid(SimpleUlidRng): {0,15:N0}/sec" ); Time <object>( (p, it) => { var csrng = new CSUlidRng(); for (int i = 0; i < it; i++) { Ulid.NewUlid(csrng); } }, iterations, "Ulid.NewUlid(CSUlidRng): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Guid.NewGuid().ToString())); }, (p, it) => { foreach (var i in p) { Guid.Parse(i); } }, iterations, "Guid.Parse(string): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Ulid.NewUlid().ToString())); }, (p, it) => { foreach (var i in p) { Ulid.Parse(i); } }, iterations, "Ulid.Parse(string): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Guid.NewGuid())); }, (p, it) => { foreach (var i in p) { i.ToString(); } }, iterations, "Guid.ToString(): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Ulid.NewUlid())); }, (p, it) => { foreach (var i in p) { i.ToString(); } }, iterations, "Ulid.ToString(): {0,15:N0}/sec" ); Time( (it) => { var r = new Random(); return(Enumerable.Range(0, it).Select(n => { var b = new byte[16]; r.NextBytes(b); return b; })); }, (p, it) => { foreach (var i in p) { new Guid(i); } }, iterations, "new Guid(byte[]): {0,15:N0}/sec" ); Time( (it) => { var r = new Random(); return(Enumerable.Range(0, it).Select(n => { var b = new byte[16]; r.NextBytes(b); return b; })); }, (p, it) => { foreach (var i in p) { new Ulid(i); } }, iterations, "new Ulid(byte[]): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Guid.NewGuid())); }, (p, it) => { foreach (var i in p) { i.ToByteArray(); } }, iterations, "Guid.ToByteArray(): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Ulid.NewUlid())); }, (p, it) => { foreach (var i in p) { i.ToByteArray(); } }, iterations, "Ulid.ToByteArray(): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Ulid.NewUlid())); }, (p, it) => { foreach (var i in p) { i.ToGuid(); } }, iterations, "Ulid.ToGuid(): {0,15:N0}/sec" ); Time( (it) => { return(Enumerable.Range(0, it).Select(n => Guid.NewGuid())); }, (p, it) => { foreach (var i in p) { new Ulid(i); } }, iterations, "new Ulid(Guid): {0,15:N0}/sec" ); Console.ReadKey(); }