Priority Queues with C#: Listing 6.

Testing a priority queue.

static void TestPriorityQueue(int numOperations)
{
  Random rand = new Random(0);
  PriorityQueue <Employee > pq = 
    new PriorityQueue <Employee >();
  for (int op = 0; op  < numOperations; ++op)
  {
    int opType = rand.Next(0, 2);

    if (opType == 0) // enqueue
    {
      string lastName = op + "man";
      double priority = 
        (100.0 - 1.0) * rand.NextDouble() + 1.0;
      pq.Enqueue(new Employee(lastName, priority));
      if (pq.IsConsistent() == false)
      {
        Console.WriteLine(
          "Test fails after enqueue operation # " + op);
      }
    }
    else // Dequeue
    {
      if (pq.Count()  > 0)
      {
        Employee e = pq.Dequeue();
        if (pq.IsConsistent() == false)
        {
          Console.WriteLine(
            "Test fails after dequeue operation # " + op);
        }
      }
    }
  } // for
  Console.WriteLine("\nAll tests passed");
}

About the Author

Dr. James McCaffrey directs the data science and research efforts at Quaetrix, a data analytics company located near Redmond, Washington. Before joining Quaetrix, James was a senior research engineer at Microsoft. James can be reached at [email protected].

comments powered by Disqus

Featured

  • VS Code 1.125 Adds Copilot Spend Meter After Billing Shock

    VS Code 1.125 adds in-editor visibility into additional Copilot budget usage as GitHub's AI-credit billing model continues to draw developer scrutiny.

  • TypeScript 7.0 RC Moves Microsoft's Go Rewrite Into the Mainline Compiler

    Microsoft's Go-based TypeScript rewrite has reached Release Candidate status, moving from a separate native-preview package into the regular TypeScript npm package while leaving some ecosystem-facing API work for TypeScript 7.1 or later.

  • Microsoft Highlights Visual Studio Live! Event Lineup and Longtime Developer Community Role

    A Microsoft MVP Blog post on Visual Studio Live!'s longevity arrives as the 2026 conference series continues with upcoming stops at Microsoft HQ, San Diego and Orlando.

  • Using Local AI to Cut Copilot Usage-Based Billing Shock

    After being gobsmacked by the new billing plan using almost all my monthly credits in one or two days, I tried pushing some Copilot-style coding work onto local models in VS Code. What I found was less "free AI" and more "pick your pain": cloud charges on one side, heavy local resource use and long waits on the other.

Subscribe on YouTube