Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:49:48+00:00 2026-05-27T03:49:48+00:00

Could anyone tell me / explain how can I make a proper test of

  • 0

Could anyone tell me / explain how can I make a proper test of a Dequeue?
I have implemented a Priority Queue and in order to verify it I have done some junit tests.
I’m rather new to java so maybe I’m making some huge mistakes when trying to verify my implementation of a priority queue.

The test code :

@Test
public void testDequeue() throws MyException {

    System.out.println("Dequeue");

    PQueue q=new PQueue();
    PQueue o=new PQueue();        

    q.Enqueue("abc", 1); // Enqueue with an object and a priority
    q.Dequeue();
    System.out.println(q.dim()); // to see if the dequeue worked 

    o.Enqueue("def", 2);

    assertTrue(o.equals(q));
}

Pqueue Code:

public class PQueue<E> implements IPQueue<E>,Serializable{

    private int size,front,rear;
    private LinkedList<ListNode> list;

    public PQueue()
    {
        front=0;
        rear=0;
        list=new LinkedList<ListNode>();
    }

    public void Enqueue(E obj, int p) throws MyException
    {
        if (obj==null)  throw new MyException("Did not enqueued");

        if (rear==0)
        {
            front=rear=1;
            list.add(new ListNode(obj, p));
        }
        else
        {
            rear++;
            int x=  list.size();
            for(int i=0;i<x-1;++i)
            {
                if(list.get(i).GetPriority() < p) list.add(i, new ListNode(obj, p));
            }
        }
    }

    public E Dequeue() throws MyException
    {
        if(rear==0) throw new MyException("Cannot dequeue; queue is empty!");

        rear--;
        return (E) list.getLast();
    }

    public int IsEmpty()
    {
        if(rear==0)
            return 1;
        else
            return 0;
    }

    public int IsFull()
    {
        if(rear-front+2>size)
            return 1;
        else
            return 0;
    }

    public void MakeEmpty()
    {
        size=0;
    }

    public int dim()
    {
        return rear;
    }

    public LinkedList<ListNode> getList()
    {
        return list;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        }
        if (!(obj instanceof PQueue)) {
            return false; 
        }
        PQueue p = (PQueue)obj;
        return (obj==p);
    }       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:49:49+00:00Added an answer on May 27, 2026 at 3:49 am

    Your tests should test all possiblities of how the code may react to inputs. It is usually helpful to think about the testcases prior of coding the actual code which shall be tested. (Search for ‘Test Driven Development’ for a interesting, more dogmatic view on this issue)

    I just wrote 4 tests: 2 testing the regular behavior, 2 testing exceptional cases.

    I usually create some ‘instance‘ member which I used for testing, which reduces each unit test by one line where I would otherwise have to create an instance (less code, less work).

    Do not test ListNode in the code (that should be tested in ListNodeTest).

    My tests below assume that new ListNode(2,1).equals( new ListNode(2,1) ).

    private final PQueue<Integer> instance = new PQueue<Integer>();
    
    
    @Test
    public void testDequeue() throws Exception
    {
      System.out.println( "Dequeue" );
    
      instance.Enqueue( 2, 1 );
      assertEquals( new ListNode<Integer>(2, 1), instance.Dequeue() );
    }
    
    
    @Test
    public void testDequeue_DequeuedTwice() throws Exception
    {
      System.out.println( "Dequeue_DequeuedTwice" );
    
      instance.Enqueue( 2, 1 );
      instance.Enqueue( 3, 2 );
      assertEquals( new ListNode<Integer>(2, 1), instance.Dequeue() );
    }
    
    
    @Test( expected=MyException.class) 
    public void testDequeue_Empty() throws Exception
    {
      System.out.println( "Dequeue_Empty" );
    
      instance.Dequeue();
    }
    
    
    @Test( expected=MyException.class) 
    public void testDequeue_DequeuedTwice() throws Exception
    {
      System.out.println( "Dequeue_DequeuedTwice" );
    
      instance.Enqueue( 2, 1 );
      instance.Dequeue();
      instance.Dequeue();
    }
    

    One point, you may define new ListNode<Integer>(2, 1) as a static final for the test. I did not. Maybe I would have if I had used it 3 times…

    Some other notes:
    Have a look at http://www.oracle.com/technetwork/java/codeconventions-135099.html#367. Method names in Java are supposed to start with a lowercase letter.

    You may argue that I myself violate that convention by introducing underscores ‘_’ in the method names of testcase. I think thats handy, so I knowningly violate that convention for unit tests. Flame me for that.

    Maybe you should also have a closer look at the junit FAQ http://junit.sourceforge.net/doc/faq/faq.htm.

    You may think about changing the name of PQueue to PrioQueue or PriorityQueue.

    And I would heavily recommend to test the equals() method thoroughly, in order to get from the code what you expect. Have a look what equals() is usually supposed to do. You are also missing a hashCode() method, which is commonly implemented when overwriting equals() yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone tell me how to make RTC generate periodic interrupts? Here's what I
Could anyone tell me why this code doesn't work? I can't even get the
Could anyone tell me the appropriate way to design this? I have one service
I am planning to make a CMS using jsp and servlets. Could anyone tell
could anyone tell me the difference between Terminal and non-terminal symbol in the case
Could anyone tell me if it is possible to use the flex 4 framework
Could anyone tell me how to bulk insert data from a ref cursor to
Could anyone tell me if SQL Server 2008 has a way to prevent keywords
Here's the code, I don't quite understand, how does it work. Could anyone tell,
Could anyone please tell me why the following line about filter init method invocation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.