Example #1
0
        public void ClampLong()
        {
            var random = new Random();
            var i      = 0;

            while (++i < 101)
            {
                var val = random.NextLong(long.MinValue, long.MaxValue);
                var min = random.NextLong(long.MinValue, long.MaxValue);
                var max = random.NextLong(long.MinValue, long.MaxValue);
                if (min > max)
                {
                    ValueUtils.Swap(ref min, ref max);
                }

                var expected = val;
                if (val < min)
                {
                    expected = min;
                }

                if (val > max)
                {
                    expected = max;
                }

                Assert.AreEqual(
                    expected, MathHelper.Clamp(val, min, max), $"val={val}, min={min}, max={max}, expected={expected}"
                    );
            }

            Assert.IsTrue(i > 100);
        }
Example #2
0
        public void SwapTest()
        {
            string a = "Test", b = "Unit";

            Assert.AreEqual("Unit", b);
            Assert.AreNotEqual("Unit", a);
            Assert.AreEqual("Test", a);
            Assert.AreNotEqual("Test", b);
            ValueUtils.Swap(ref a, ref b);
            Assert.AreEqual("Unit", a);
            Assert.AreNotEqual("Unit", b);
            Assert.AreEqual("Test", b);
            Assert.AreNotEqual("Test", a);
        }
Example #3
0
        /// <summary>
        /// Version 5 UUID implementation of RFC 4122 ยง4.3.
        /// </summary>
        /// <param name="namespaceId">the UUID of the namespace</param>
        /// <param name="name">the name to generate the UUID for</param>
        /// <returns>a Version 5 UUID generated from the provided namespace UUID and text name</returns>
        public static Guid CreateNamed(Guid namespaceId, [NotNull] byte[] name)
        {
            if (name.Length < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(name));
            }

            var namespaceIdData = namespaceId.ToByteArray();

            ValueUtils.Swap(ref namespaceIdData[0], ref namespaceIdData[3]);
            ValueUtils.Swap(ref namespaceIdData[2], ref namespaceIdData[1]);
            ValueUtils.Swap(ref namespaceIdData[4], ref namespaceIdData[5]);
            ValueUtils.Swap(ref namespaceIdData[6], ref namespaceIdData[7]);

            var data = new byte[namespaceIdData.Length + name.Length];

            Buffer.BlockCopy(namespaceIdData, 0, data, 0, namespaceIdData.Length);
            Buffer.BlockCopy(name, 0, data, namespaceIdData.Length, name.Length);

            byte[] hash;
            using (var algorithm = SHA1.Create())
            {
                hash = algorithm?.ComputeHash(data) ?? throw new InvalidOperationException();
            }

            var namedIdData = new byte[16];

            Buffer.BlockCopy(hash, 0, namedIdData, 0, 16);

            namedIdData[6] &= 0x0F;
            namedIdData[6] |= 5 << 4;
            namedIdData[8] &= 0x3F;
            namedIdData[8] |= 0x80;

            ValueUtils.Swap(ref namedIdData[0], ref namedIdData[3]);
            ValueUtils.Swap(ref namedIdData[2], ref namedIdData[1]);
            ValueUtils.Swap(ref namedIdData[4], ref namedIdData[5]);
            ValueUtils.Swap(ref namedIdData[6], ref namedIdData[7]);

            return(new Guid(namedIdData));
        }