Beispiel #1
0
        public static void test_pq()
        {
            System.Console.WriteLine("testing priority queues...");

            int MAXID    = 1000000;
            int MAXCOUNT = 100;
            int mod      = 31337;

            for (int kk = 0; kk < 3; ++kk)
            {
                if (kk == 1)
                {
                    MAXCOUNT = MAXID / 10;
                }
                else if (kk == 2)
                {
                    MAXCOUNT = MAXID;
                }

                IndexPriorityQueue PQ_Index = new IndexPriorityQueue(MAXID);
                DynamicPriorityQueue <TestDynamicNode> PQ_Dynamic   = new DynamicPriorityQueue <TestDynamicNode>();
                MemoryPool <TestDynamicNode>           Dynamic_Pool = new MemoryPool <TestDynamicNode>();
                Dictionary <int, TestDynamicNode>      DynamicNodes = new Dictionary <int, TestDynamicNode>();

                System.Console.WriteLine("inserting {0} of {1}", MAXCOUNT, MAXID);

                int count = 0;
                int id    = 0;
                while (count < MAXCOUNT)
                {
                    id = (id + mod) % MAXID;

                    PQ_Index.Enqueue(id, count);
                    TestDynamicNode node = Dynamic_Pool.Allocate();
                    node.Initialize(id);
                    PQ_Dynamic.Enqueue(node, count);
                    DynamicNodes[id] = node;
                    count++;
                }
                Util.gDevAssert(PQ_Index.IsValidQueue());
                Util.gDevAssert(PQ_Dynamic.IsValidQueue());
                Util.gDevAssert(PQ_Index.Count == PQ_Dynamic.Count);
                Util.gDevAssert(PQ_Index.First == PQ_Dynamic.First.id);
                check_same(PQ_Index, PQ_Dynamic);

                Random r = new Random(31337);

                System.Console.WriteLine("updating...");

                id = 0; count = 0;
                while (count++ < MAXCOUNT)
                {
                    id = (id + mod) % MAXID;
                    float new_p = count + ((r.Next() % 1000) - 1000);
                    PQ_Index.Update(id, new_p);
                    PQ_Dynamic.Update(DynamicNodes[id], new_p);
                }
                Util.gDevAssert(PQ_Index.IsValidQueue());
                Util.gDevAssert(PQ_Dynamic.IsValidQueue());
                check_same(PQ_Index, PQ_Dynamic);

                System.Console.WriteLine("removing...");

                while (PQ_Index.Count > 0)
                {
                    int             index_id = PQ_Index.Dequeue();
                    TestDynamicNode node     = PQ_Dynamic.Dequeue();
                    Util.gDevAssert(index_id == node.id);
                }
            }
        }