Ejemplo n.º 1
0
        public void Sort_Tests()
        {
            Assert.Throws(typeof(ArgumentNullException), () => { SysUtils.QuickSort <ValItem>(null, null); });
            Assert.Throws(typeof(ArgumentNullException), () => { SysUtils.MergeSort <ValItem>(null, null); });
            Assert.Throws(typeof(ArgumentNullException), () => { ListTimSort <int> .Sort(null, null); });

            Random rnd = new Random();

            List <ValItem> listQS = new List <ValItem>();
            List <ValItem> listMS = new List <ValItem>();
            List <ValItem> listTS = new List <ValItem>();
            List <ValItem> listCS = new List <ValItem>();

            //const int MaxCount = 1000000; // for performance test
            const int MaxCount = 1000; // for common test

            for (int i = 0; i < MaxCount; i++)
            {
                double val = rnd.NextDouble();

                listTS.Add(new ValItem(val));
                listQS.Add(new ValItem(val));
                listMS.Add(new ValItem(val));
                listCS.Add(new ValItem(val));
            }

            listCS.Sort(CompareItems);

            SysUtils.QuickSort(listQS, CompareItems);

            SysUtils.MergeSort(listMS, CompareItems);

            ListTimSort <ValItem> .Sort(listTS, CompareItems);

            // test for sort valid
            //(only for numbers, because some methods is with the permutations, and part - no)
            for (int i = 0; i < MaxCount; i++)
            {
                Assert.AreEqual(listTS[i].Value, listQS[i].Value);
                Assert.AreEqual(listQS[i].Value, listMS[i].Value);
                Assert.AreEqual(listMS[i].Value, listCS[i].Value);
            }
        }
Ejemplo n.º 2
0
        public void Test_Sort()
        {
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, 05, 05, "2016/05/05 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, 05, 04, "2016/05/04 [g]"));

            fDates.Add(new UDNRecord(UDNCalendarType.ctJulian, 2016, 04, 21, "2016/05/04 [g] = 2016/04/21 [j]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctJulian, 2016, 04, 23, "2016/05/06 [g] = 2016/04/23 [j]"));

            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, 05, UDN.UnknownDay, "2016/05/?? [g]")); // must be first
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, 06, UDN.UnknownDay, "2016/06/?? [g]")); // must be last

            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, UDN.UnknownYear, UDN.UnknownMonth, UDN.UnknownDay, "??/??/?? [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, UDN.UnknownYear, 04, 23, "??/04/23 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, UDN.UnknownYear, 03, 23, "??/03/23 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, UDN.UnknownYear, UDN.UnknownMonth, 23, "??/??/23 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, UDN.UnknownMonth, UDN.UnknownDay, "2016/??/?? [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, UDN.UnknownMonth, 10, "2016/??/10 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2015, 03, 23, "2015/03/23 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2014, UDN.UnknownMonth, 23, "2014/??/23 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, 05, 31, "2016/05/31 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2016, 05, 31, "2016/05/31 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, -4712, 1, 2, "-4712/01/02 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, -4712, 1, 3, "-4712/01/03 [g]"));

            fDates.Add(new UDNRecord(UDNCalendarType.ctHebrew, 5564, 04, 04, "1804/06/13 [g] = 5564/04/04 [h]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctIslamic, 1216, 01, 04, "1801/05/17 [g] = 1216/01/04 [i]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 1802, 05, 01, "1802/05/01 [g]"));

            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 0, 1, 3, "0000/01/03 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, -1, 1, 3, "-0001/01/03 [g]"));

            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 1, 1, 3, "0001/01/03 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 2015, 2, 27, "2015/02/27 [g]"));
            fDates.Add(new UDNRecord(UDNCalendarType.ctGregorian, 3268, 1, 23, "3268/01/23 [g]"));

            // Add dates before.
            fDates.Add(new UDNRecord(UDN.CreateBefore(
                                         UDNCalendarType.ctGregorian, 1, 1, 4), UDNCalendarType.ctGregorian, "before 0001/01/04 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateBefore(
                                         UDNCalendarType.ctGregorian, 2016, 05, 31), UDNCalendarType.ctGregorian, "before 2016/05/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateBefore(
                                         UDNCalendarType.ctGregorian, -4712, 1, 2), UDNCalendarType.ctGregorian, "before -4712/01/02 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateBefore(
                                         UDNCalendarType.ctGregorian, UDN.UnknownYear, 05, 31), UDNCalendarType.ctGregorian, "before ????/05/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateBefore(
                                         UDNCalendarType.ctGregorian, 2015, UDN.UnknownMonth, 31), UDNCalendarType.ctGregorian, "before 2015/??/31 [g]"));
            // Add dates after.
            fDates.Add(new UDNRecord(UDN.CreateAfter(
                                         UDNCalendarType.ctGregorian, 2016, 05, 31), UDNCalendarType.ctGregorian, "after 2016/05/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateAfter(
                                         UDNCalendarType.ctGregorian, UDN.UnknownYear, 05, 31), UDNCalendarType.ctGregorian, "after ????/05/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateAfter(
                                         UDNCalendarType.ctGregorian, UDN.UnknownYear, 06, 15), UDNCalendarType.ctGregorian, "after ????/06/15 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateAfter(
                                         UDNCalendarType.ctGregorian, 2015, UDN.UnknownMonth, 31), UDNCalendarType.ctGregorian, "after 2015/??/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateAfter(
                                         UDNCalendarType.ctGregorian, 2015, UDN.UnknownMonth, 30), UDNCalendarType.ctGregorian, "after 2015/??/30 [g]"));
            // Add approximate dates.
            fDates.Add(new UDNRecord(UDN.CreateApproximate(
                                         UDNCalendarType.ctGregorian, 2016, 05, 31), UDNCalendarType.ctGregorian, "~ 2016/05/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateApproximate(
                                         UDNCalendarType.ctGregorian, 1, 1, 4), UDNCalendarType.ctGregorian, "~ 0001/01/04 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateApproximate(
                                         UDNCalendarType.ctGregorian, 1, 1, UDN.UnknownDay), UDNCalendarType.ctGregorian, "~ 0001/01/?? [g]"));
            fDates.Add(new UDNRecord(UDN.CreateApproximate(
                                         UDNCalendarType.ctGregorian, UDN.UnknownYear, 05, 31), UDNCalendarType.ctGregorian, "~ ????/05/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateApproximate(
                                         UDNCalendarType.ctGregorian, 2015, UDN.UnknownMonth, 31), UDNCalendarType.ctGregorian, "~ 2015/??/31 [g]"));
            fDates.Add(new UDNRecord(UDN.CreateApproximate(
                                         UDNCalendarType.ctGregorian, 2015, 2, 28), UDNCalendarType.ctGregorian, "~ 2015/02/28 [g]"));

            // standart sort for .NET and Mono gives different result at items 31 and 32
            SysUtils.QuickSort(fDates, delegate(UDNRecord left, UDNRecord right) { return(left.Value.CompareTo(right.Value)); });
            //fDates.Sort(delegate(UDNRecord left, UDNRecord right) { return left.Value.CompareTo(right.Value); });

            Assert.AreEqual("??/??/?? [g]", fDates[0].Description, "(00)");
            Assert.AreEqual("??/??/23 [g]", fDates[1].Description, "(01)");
            Assert.AreEqual("0000/01/03 [g]", fDates[2].Description, "(02)");
            Assert.AreEqual("??/03/23 [g]", fDates[3].Description, "(03)");
            Assert.AreEqual("??/04/23 [g]", fDates[4].Description, "(04)");
            Assert.AreEqual("before ????/05/31 [g]", fDates[5].Description, "(05)");
            Assert.AreEqual("~ ????/05/31 [g]", fDates[6].Description, "(06)");
            Assert.AreEqual("after ????/05/31 [g]", fDates[7].Description, "(07)");
            Assert.AreEqual("after ????/06/15 [g]", fDates[8].Description, "(08)");
            Assert.AreEqual("before -4712/01/02 [g]", fDates[9].Description, "(09)");

            Assert.AreEqual("-4712/01/02 [g]", fDates[10].Description, "(10)");
            Assert.AreEqual("-4712/01/03 [g]", fDates[11].Description, "(11)");
            Assert.AreEqual("-0001/01/03 [g]", fDates[12].Description, "(12)");
            Assert.AreEqual("~ 0001/01/?? [g]", fDates[13].Description, "(13)");
            Assert.AreEqual("0001/01/03 [g]", fDates[14].Description, "(14)");
            Assert.AreEqual("before 0001/01/04 [g]", fDates[15].Description, "(15)");
            Assert.AreEqual("~ 0001/01/04 [g]", fDates[16].Description, "(16)");
            Assert.AreEqual("1801/05/17 [g] = 1216/01/04 [i]", fDates[17].Description, "(17)");
            Assert.AreEqual("1802/05/01 [g]", fDates[18].Description, "(18)");
            Assert.AreEqual("1804/06/13 [g] = 5564/04/04 [h]", fDates[19].Description, "(19)");

            Assert.AreEqual("2014/??/23 [g]", fDates[20].Description, "(20)");
            Assert.AreEqual("after 2015/??/30 [g]", fDates[21].Description, "(21)");
            Assert.AreEqual("before 2015/??/31 [g]", fDates[22].Description, "(22)");
            Assert.AreEqual("~ 2015/??/31 [g]", fDates[23].Description, "(23)");
            Assert.AreEqual("after 2015/??/31 [g]", fDates[24].Description, "(24)");
            Assert.AreEqual("2015/02/27 [g]", fDates[25].Description, "(25)");
            Assert.AreEqual("~ 2015/02/28 [g]", fDates[26].Description, "(26)");
            Assert.AreEqual("2015/03/23 [g]", fDates[27].Description, "(27)");
            Assert.AreEqual("2016/??/?? [g]", fDates[28].Description, "(28)");
            Assert.AreEqual("2016/??/10 [g]", fDates[29].Description, "(29)");

            Assert.AreEqual("2016/05/?? [g]", fDates[30].Description, "(30)");

            Assert.AreEqual("2016/05/04 [g]", fDates[31].Description, "(31)");
            Assert.AreEqual("2016/05/04 [g] = 2016/04/21 [j]", fDates[32].Description, "(32)");

            Assert.AreEqual("2016/05/05 [g]", fDates[33].Description, "(33)");
            Assert.AreEqual("2016/05/06 [g] = 2016/04/23 [j]", fDates[34].Description, "(34)");
            Assert.AreEqual("before 2016/05/31 [g]", fDates[35].Description, "(35)");
            Assert.AreEqual("~ 2016/05/31 [g]", fDates[36].Description, "(36)");
            Assert.AreEqual("2016/05/31 [g]", fDates[37].Description, "(37)");
            Assert.AreEqual("2016/05/31 [g]", fDates[38].Description, "(38)");
            Assert.AreEqual("after 2016/05/31 [g]", fDates[39].Description, "(39)");

            Assert.AreEqual("2016/06/?? [g]", fDates[40].Description, "(40)");
            Assert.AreEqual("3268/01/23 [g]", fDates[41].Description, "(41)");

            UDNRecord rec;

            Assert.Throws(typeof(ArgumentOutOfRangeException), () => { rec = fDates[42]; }, "(42)"); // end
        }