1

I am struggling to implement test cases on this program. The program takes in a queue and sorts it in ascending order. It's working fine. However, when I try to test it's returned result using JUnit 5, I get an error. I am putting the expected result and the actual result in the testing method of the testing class. It still does not work. Please help!

(Edit: Testing methods)

Here's the code:

import java.util.Queue;

public class Sort {

    public static Queue<Integer> queueSort(Queue<Integer> queue) { 
                                                            
        int n = queue.size(); 

        if (queue.isEmpty()) {      
            System.out.println("Invalid Input");

        } else {

            for (int i = 0; i < n; i++) {  

                int minimumIndex = -1;
                int minimumValue = Integer.MAX_VALUE; 
                                                      

                for (int j = 0; j < n; j++) {        
                    int currentValue = queue.poll();

                    if (currentValue < minimumValue && j < (n - i)) {  
                        minimumValue = currentValue;                  
                        minimumIndex = j;            
                    }                                
                    queue.add(currentValue);        
                }

                for (int j = 0; j < n; j++) {     
                    int currentValue = queue.poll();

                    if (j != minimumIndex) {
                        queue.add(currentValue);
                    }
                }

                queue.add(minimumValue);      
            }

        }
        return queue;
    }
}

The test class:

    import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;
import java.util.Queue;

class SortTest {

    Queue<Integer> Q1 = new LinkedList<>();
    Queue<Integer> expectedQ1 = new LinkedList<>();

    Queue<Integer> Q2 = new LinkedList<>();
    Queue<Integer> expectedQ2 = new LinkedList<>();

    Queue<Integer> Q3 = new LinkedList<>();

    void testInputs() {
        Q1.add(10); Q1.add(-1); Q1.add(8); Q1.add(7); Q1.add(2);
        expectedQ1.add(-1); expectedQ1.add(2); expectedQ1.add(7); expectedQ1.add(8); expectedQ1.add(10);

        Q2.add(6000); Q2.add(5999);
        expectedQ2.add(5999); expectedQ2.add(6000);

    }


    @Test
    void queueSortTest1() {
        Assertions.assertEquals(expectedQ1, Sort.queueSort(Q1));
    }

    @Test
    void queueSortTest2() {
        Assertions.assertEquals(expectedQ2, Sort.queueSort(Q2));
    }

    @Test
    void queueSortTest3() {
        Assertions.assertEquals("Invalid Input", Sort.queueSort(Q3));
    }
}
Sk_Hssn
  • 45
  • 8

3 Answers3

1

You should use org.junit.Assert.assertEquals for Queue equality check.

0

If you want to consider the order in your equality check, you can do such trick:

Assertions.assertTrue(Arrays.equals(q1.toArray(), queueSort(q1).toArray()));

To make this code more beautiful, you can implement a custom matcher (for example, using hamcrest ).

Pay your attention, that your code is changing an initial queue (looks like a bug :) ).

So

Queue<Integer> sortedq1 = Sort.queueSort(q1);
Assertions.assertTrue(Arrays.equals(q1.toArray(), sortedq1.toArray()));

will always pass (as q1 was already sorted).This post can help to understand the reason.

I see that you edited the question. So adding the System.out validation as well:

@Test
void queueSort() {
    final PrintStream standardOut = System.out;
    final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outputStreamCaptor));
    Queue<Integer> q1 = new LinkedList<>();
    Queue<Integer> sortedq1 = Sort.queueSort(q1);
    Assertions.assertEquals("Invalid Input", outputStreamCaptor.toString().trim());
}
Eduard Dubilyer
  • 991
  • 2
  • 10
  • 21
  • Hi, thanks for the feedback. I edited the test class but still getting the same result. How should I handle an empty queue? Printing "Invalid Input" is not working with the tests. :( – Sk_Hssn Mar 08 '21 at 10:31
  • Added to my answer an example of System.out validation. – Eduard Dubilyer Mar 08 '21 at 10:42
0
Queue<Integer> res = ...;
Queue<Integer> expected = ...;

assertArrayEquals(expected.toArray(), res.toArray());
Kirill Ch
  • 5,496
  • 4
  • 44
  • 65