dimanche 7 mars 2021

How to Test a Queue in JUnit 5?

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!

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 {

    @Test
    void queueSort() {

        Queue<Integer> q1 = new LinkedList<>();
        Queue<Integer> sortedq1 = Sort.queueSort(q1);

        q1.add(10);
        q1.add(7);
        q1.add(2);
        q1.add(8);
        q1.add(6);

        Assertions.assertArrayEquals(q1,sortedq1);
    }
}

Aucun commentaire:

Enregistrer un commentaire