Пример #1
0
        public void Ulid_GetHashCode_WorksCorrectly()
        {
            var rng       = new FakeUlidRng();
            var hashcodes = new List <int>()
            {
                Ulid.MinValue.GetHashCode(),
                Ulid.MaxValue.GetHashCode(),
                Ulid.NewUlid().GetHashCode(),
            };

            hashcodes.AddRange(Enumerable.Range(0, 1000).Select(i => Ulid.NewUlid(KNOWNTIMESTAMP_DTO.AddMilliseconds(i)).GetHashCode()));
            hashcodes.AddRange(Enumerable.Range(0, 1000).Select(i => Ulid.NewUlid(KNOWNTIMESTAMP_DTO.AddMilliseconds(i), rng).GetHashCode()));

            Assert.AreEqual(3 + 1000 + 1000, hashcodes.Distinct().Count());
        }
Пример #2
0
        public void MonotonicRng_Ensure_MaskMSBWorksCorrectly()
        {
            var rng = new FakeUlidRng(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 });
            var ts  = DateTimeOffset.Now;

            Assert.IsTrue(new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 0).GetRandomBytes(ts)), "Mask 0");
            Assert.IsTrue(new byte[] { 127, 255, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 1).GetRandomBytes(ts)), "Mask 1");
            Assert.IsTrue(new byte[] { 63, 255, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 2).GetRandomBytes(ts)), "Mask 2");

            Assert.IsTrue(new byte[] { 1, 255, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 7).GetRandomBytes(ts)), "Mask 7");
            Assert.IsTrue(new byte[] { 0, 255, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 8).GetRandomBytes(ts)), "Mask 8");
            Assert.IsTrue(new byte[] { 0, 127, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 9).GetRandomBytes(ts)), "Mask 9");

            Assert.IsTrue(new byte[] { 0, 1, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 15).GetRandomBytes(ts)), "Mask 15");
            Assert.IsTrue(new byte[] { 0, 0, 255, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 16).GetRandomBytes(ts)), "Mask 16");
            Assert.IsTrue(new byte[] { 0, 0, 127, 255, 255, 255, 255, 255, 255, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 17).GetRandomBytes(ts)), "Mask 17");

            Assert.IsTrue(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 7, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 69).GetRandomBytes(ts)), "Mask 69");
            Assert.IsTrue(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 3, 255 }.SequenceEqual(new MonotonicUlidRng(rng, 70).GetRandomBytes(ts)), "Mask 70");
        }
Пример #3
0
        public void Ulid_NewUlid_ThrowsInvalidOperationException_OnRNGReturningInsufficientBytes()
        {
            var rng = new FakeUlidRng(new byte[] { 1, 2, 3 });

            Ulid.NewUlid(rng);
        }
Пример #4
0
 public void Ulid_NewUlid_ThrowsInvalidOperationException_OnRNGReturningInsufficientBytes()
 {
     var rng = new FakeUlidRng(new byte[] { 1, 2, 3 });
     Ulid.NewUlid(rng);
 }
Пример #5
0
        public void Ulid_GetHashCode_WorksCorrectly()
        {
            var rng = new FakeUlidRng();
            var hashcodes = new List<int>()
            {
                Ulid.MinValue.GetHashCode(),
                Ulid.MaxValue.GetHashCode(),
                Ulid.NewUlid().GetHashCode(),
            };
            hashcodes.AddRange(Enumerable.Range(0, 1000).Select(i => Ulid.NewUlid(KNOWNTIMESTAMP_DTO.AddMilliseconds(i)).GetHashCode()));
            hashcodes.AddRange(Enumerable.Range(0, 1000).Select(i => Ulid.NewUlid(KNOWNTIMESTAMP_DTO.AddMilliseconds(i), rng).GetHashCode()));

            Assert.AreEqual(3 + 1000 + 1000, hashcodes.Distinct().Count());
        }