forked from Hithomelabs/Princeton1
Reviewed-on: Hithomelabs/Princeton1#17 Reviewed-by: kruti <krutis0201@gmail.com> Co-authored-by: hitanshu310 <hitanshu98@gmail.com> Co-committed-by: hitanshu310 <hitanshu98@gmail.com>
63 lines
1.9 KiB
Java
63 lines
1.9 KiB
Java
package com.hithomelabs.princeton1.module8;
|
|
|
|
import com.hithomelabs.princeton1.common.Apple;
|
|
import org.junit.jupiter.api.*;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Random;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
class HeapSortTest {
|
|
|
|
private HeapSort<Apple> heap;
|
|
private ArrayList<Apple> apples;
|
|
private Random random;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
heap = new HeapSort<Apple>();
|
|
apples = new ArrayList<Apple>();
|
|
random = new Random();
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Sort a heap of a 100 random sized apples")
|
|
public void testSort(){
|
|
for(int i = 0; i < 100; i++)
|
|
apples.add(new Apple(random.nextInt(1000)));
|
|
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
|
heap.sort(arr);
|
|
apples.sort(null);
|
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
|
Assertions.assertArrayEquals(sorted, arr);
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Sort a heap of a 100 random sized with Comparator")
|
|
public void testComparatorSort(){
|
|
for(int i = 0; i < 100; i++)
|
|
apples.add(new Apple(random.nextInt(1000)));
|
|
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
|
heap.sort(arr, Apple.COMPARE_BY_SIZE);
|
|
apples.sort(Apple.COMPARE_BY_SIZE);
|
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
|
Assertions.assertArrayEquals(sorted, arr);
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("Sort 100 apples in descending order")
|
|
public void testSortBorder(){
|
|
for(int i = 0; i < 100; i++)
|
|
apples.add(new Apple(100 - i));
|
|
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
|
heap.sort(arr);
|
|
apples.sort(null);
|
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
|
Assertions.assertArrayEquals(sorted, arr);
|
|
}
|
|
|
|
@AfterEach
|
|
void tearDown() {
|
|
}
|
|
} |