Princeton1/module5/src/test/java/com/hithomelabs/princeton1/module5/SortTest.java
hitanshu310 a4ccbee99c
All checks were successful
sample gradle build and test / build (pull_request) Successful in 1m14s
sample gradle build and test / build (push) Successful in 4m10s
Added a lot of stuff
2025-02-02 01:12:22 +05:30

63 lines
1.7 KiB
Java

package com.hithomelabs.princeton1.module5;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
class SortTest {
private ArrayList<Apple> apples;
private AbstractCustomSorts<Apple> sortingAlgorithm;
private Random random;
@BeforeEach
void setUp() {
apples = new ArrayList<Apple>();
//sortingAlgorithm = new Selection<Apple>();
random = new Random();
}
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
for (int i = 0; i < 100; i++)
apples.add(new Apple(random.nextInt(100)));
Apple[] arr = apples.toArray(new Apple[apples.size()]);
apples.sort(null);
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
sortingAlgorithm.sort(arr);
assertArrayEquals(sorted, arr);
}
@Test
@DisplayName("Testing Insertion sort functionality")
public void testInsertionSort() {
sortingAlgorithm = new Insertion<Apple>();
testSort(sortingAlgorithm);
}
@Test
@DisplayName("Testing Selection sort functionality")
public void testSelectionSort() {
sortingAlgorithm = new Selection<Apple>();
testSort(sortingAlgorithm);
}
@Test
@DisplayName("Testing Shell sort functionality")
public void testShellSort() {
sortingAlgorithm = new Shell<>();
testSort(sortingAlgorithm);
}
@AfterEach
void tearDown() {
sortingAlgorithm = null;
apples = null;
}
}