Esempio n. 1
0
        public void DescendingLastRunAtSort()
        {
            var chatScores = new Dictionary <Chat, double>();

            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(Util.EpochMs()), 4);
            chatScores.Add(Chat.Create("c5").LastRunAt(Util.EpochMs()), 5);

            var chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);

            //chats.ForEach((obj) => Console.WriteLine(obj.Key.text));

            for (int i = 0; i < 5; i++)
            {
                Assert.That(chats[i].Key.text, Is.EqualTo("c" + (5 - i)));
            }

            ////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();
            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(Util.EpochMs()), 5);
            chatScores.Add(Chat.Create("c5").LastRunAt(Util.EpochMs() - 1), 5);

            chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
            //chats.ForEach((obj) => Console.WriteLine(obj.Key.text));

            for (int i = 0; i < 5; i++)
            {
                Assert.That(chats[i].Key.text, Is.EqualTo("c" + (5 - i)));
            }

            ////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();
            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(Util.EpochMs()), 5);
            chatScores.Add(Chat.Create("c5").LastRunAt(-1), 5);

            chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
            //chats.ForEach((obj) => Console.WriteLine(obj.Key.text));

            for (int i = 0; i < 5; i++)
            {
                Assert.That(chats[i].Key.text, Is.EqualTo("c" + (5 - i)));
            }

            //////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();
            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs()), 2);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs()), 3);
            chatScores.Add(Chat.Create("c4").LastRunAt(-1), 5);
            chatScores.Add(Chat.Create("c5").LastRunAt(-1), 5);

            for (int j = 0; j < 10; j++) // repeat a few times
            {
                chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
                //chats.ForEach(obj => Console.Write(obj.Key.text+ ", "));Console.WriteLine();
                Assert.That(chats[0].Key.text, Is.EqualTo("c5").Or.EqualTo("c4"));
                Assert.That(chats[0].Key.text, Is.EqualTo("c4").Or.EqualTo("c5"));
                Assert.That(chats[2].Key.text, Is.EqualTo("c3"));
                Assert.That(chats[3].Key.text, Is.EqualTo("c2"));
                Assert.That(chats[4].Key.text, Is.EqualTo("c1"));
            }

            //////////////////////////////////////////////////////////////////////

            chatScores = new Dictionary <Chat, double>();

            chatScores.Add(Chat.Create("c1").LastRunAt(Util.EpochMs()), 1);
            chatScores.Add(Chat.Create("c2").LastRunAt(Util.EpochMs() - 1000), 1);
            chatScores.Add(Chat.Create("c3").LastRunAt(Util.EpochMs() - 2000), 1);
            chatScores.Add(Chat.Create("c4"), 0);
            chatScores.Add(Chat.Create("c5"), 10);

            chats = FuzzySearch.DescendingScoreLastRunAtRandomizedSort(chatScores);
            for (int i = 0; i < chats.Count; i++)
            {
                //Console.WriteLine(chats[i].Key + "(" + chats[i].Key.lastRunAt + ") -> " + chats[i].Value);
                if (i + 1 < chats.Count)
                {
                    Assert.That(chats[i + 1].Value <= chats[i].Value, Is.True);
                }
            }

            Assert.That(chats[0].Key.text, Is.EqualTo("c5"));
            Assert.That(chats[1].Key.text, Is.EqualTo("c3"));
            Assert.That(chats[2].Key.text, Is.EqualTo("c2"));
            Assert.That(chats[3].Key.text, Is.EqualTo("c1"));
            Assert.That(chats[4].Key.text, Is.EqualTo("c4"));
        }