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;
    }
}