Princeton1/module5/src/test/java/com/hithomelabs/princeton1/module5/SortTest.java
2025-03-08 22:19:02 +05:30

96 lines
2.5 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 com.hithomelabs.princeton1.common.Apple;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SortTest {
private Apple[] apples, applesCopy;
private AbstractCustomSorts<Apple> sortingAlgorithm;
private Random random;
@BeforeEach
void setUp() {
apples = new Apple[100];
// applesCopy = new Apple[100];
//sortingAlgorithm = new Selection<Apple>();
random = new Random();
}
private void testSort() {
int value;
for (int i = 0; i < 100; i++) {
value = random.nextInt(100);
apples[i] = new Apple(value);
// applesCopy[i] = new Apple(value);
}
}
private boolean test(){
for(int i=0;i<99;i++){
if(apples[i].compareTo(apples[i+1]) > 0)
return false;
}
return true;
}
@Test
@DisplayName("Testing Insertion sort functionality")
public void testInsertionSort() {
sortingAlgorithm = new Insertion<Apple>();
//populates random apples
testSort();
//calling sort function
sortingAlgorithm.sort(apples);
//checking if sorted array has any element which is less than preceeding one
assertTrue(test());
}
@Test
@DisplayName("Testing Selection sort functionality")
public void testSelectionSort() {
sortingAlgorithm = new Selection<Apple>();
//populates random apples
testSort();
//calling sort function
sortingAlgorithm.sort(apples);
//checking if sorted array has any element which is less than preceeding one
assertTrue(test());
}
@Test
@DisplayName("Testing Shell sort functionality")
public void testShellSort() {
sortingAlgorithm = new Shell<Apple>();
//populates random apples
testSort();
//calling sort function
sortingAlgorithm.sort(apples);
//checking if sorted array has any element which is less than preceeding one
assertTrue(test());
}
@AfterEach
void tearDown() {
sortingAlgorithm = null;
apples = null;
applesCopy = null;
}
}