コード例 #1
0
        public void Test_Uuid80_Ordering()
        {
            var a  = new Uuid80(123, 42);
            var a2 = new Uuid80(123, 42);
            var b  = new Uuid80(123, 77);
            var c  = new Uuid80(124, 41);

            Assert.That(a.CompareTo(a), Is.EqualTo(0));
            Assert.That(a.CompareTo(b), Is.EqualTo(-1));
            Assert.That(b.CompareTo(a), Is.EqualTo(+1));
            Assert.That(c.CompareTo(a), Is.EqualTo(+1));

            Assert.That(a < b, Is.True, "a < b");
            Assert.That(a <= b, Is.True, "a <= b");
            Assert.That(a < a2, Is.False, "a < a");
            Assert.That(a <= a2, Is.True, "a <= a");
            Assert.That(a < c, Is.True, "a < c");

            Assert.That(a > b, Is.False, "a > b");
            Assert.That(a >= b, Is.False, "a >= b");
            Assert.That(a > a2, Is.False, "a > a");
            Assert.That(a >= a2, Is.True, "a >= a");
            Assert.That(a > c, Is.False, "a > c");

            // parsed from string
            Assert.That(Uuid80.Parse("1234-137bcf31-0c8873a2") < Uuid80.Parse("1234-604bdf8a-2512b4ad"), Is.True);
            Assert.That(Uuid80.Parse("1234-d8f17a26-82adb1a4") < Uuid80.Parse("1234-22abbf33-1b2c1db0"), Is.False);
            Assert.That(Uuid80.Parse("{1234-137bcf31-0c8873a2}") > Uuid80.Parse("{1234-604bdf8a-2512b4ad}"), Is.False);
            Assert.That(Uuid80.Parse("{1234-d8f17a26-82adb1a4}") > Uuid80.Parse("{1234-22abbf33-1b2c1db0}"), Is.True);

            // verify byte ordering
            var d = new Uuid80(0x0001, 0x0000000200000003);
            var e = new Uuid80(0x0003, 0x0000000200000001);

            Assert.That(d.CompareTo(e), Is.LessThan(0));
            Assert.That(e.CompareTo(d), Is.GreaterThan(0));

            // verify that we can sort an array of Uuid80
            var uids = new Uuid80[100];

            for (int i = 0; i < uids.Length; i++)
            {
                uids[i] = Uuid80.NewUuid();
            }
            Assume.That(uids, Is.Not.Ordered, "This can happen with a very small probability. Please try again");
            Array.Sort(uids);
            Assert.That(uids, Is.Ordered);

            // ordering should be preserved in integer or textual form

            Assert.That(uids.Select(x => x.ToString()), Is.Ordered.Using <string>(StringComparer.Ordinal), "order should be preserved when ordering by text (hexa)");
        }
コード例 #2
0
        public void Test_Uuid80_NewUid()
        {
            var a = Uuid80.NewUuid();
            var b = Uuid80.NewUuid();

            Assert.That(a, Is.Not.EqualTo(b));

            const int N    = 1_000;
            var       uids = new HashSet <string>(StringComparer.Ordinal);

            for (int i = 0; i < N; i++)
            {
                var    uid = Uuid80.NewUuid();
                string s   = uid.ToString();
                if (uids.Contains(s))
                {
                    Assert.Fail("Duplicate Uuid80 generated: {0}", uid);
                }
                uids.Add(s);
            }
            Assert.That(uids.Count, Is.EqualTo(N));
        }