Skip to content

Added Test for the Quicksort Algorithm #3008

Closed
@Hambalieu

Description

@Hambalieu

We saw that there was no test writing for the Quicksort Algorithm and We decided to write some test for the quicksort algorithm.Below is the code that we wrote for it.

`package com.thealgorithms.sorts;

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

import static org.junit.jupiter.api.Assertions.*;

/**

class QuickSortTest {

@Test
@DisplayName("Empty Array")
void quickSortEmptyArray()
{
    QuickSort sut = new QuickSort();
    Integer[] array = {};
    Integer[] sorted = sut.sort(array);
    Integer[] expected = {};
    assertArrayEquals(expected, sorted);
}

@Test
@DisplayName("Array has a single Value")
void quickSortSingleValueArray()
{
    QuickSort sut = new QuickSort();
    Integer[] array = {4};
    Integer[] sorted = sut.sort(array);
    Integer[] expected = {4};
    assertArrayEquals(expected, sorted);
}

@Test
@DisplayName("merge sort array of integers")
void quickSort()
{
    QuickSort sut = new QuickSort();
    Integer[] array = {8, 4, 23, 42, 16, 15};
    Integer[] sorted = sut.sort(array);
    Integer[] expected = {4, 8, 15, 16, 23, 42};
    assertArrayEquals(expected, sorted);
}

@Test
@DisplayName("sort array with negative values")
void quickSortNegativeValue()
{
    QuickSort sut = new QuickSort();
    Integer[] array = {8, -4, 23, -42, 16, 15};
    Integer[] sorted = sut.sort(array);
    Integer[] expected = {-42, -4, 8, 15, 16, 23};
    assertArrayEquals(expected, sorted);
}

@Test
@DisplayName("array has duplicate values")
void quickSortDuplicateValues()
{
    QuickSort sut = new QuickSort();
    Integer[] array = {8, 4, 23, 42, 4, 15};
    Integer[] sorted = sut.sort(array);
    Integer[] expected = {4, 4, 8, 15, 23, 42};
    assertArrayEquals(expected, sorted);
}

}

`

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions