Algorithms implementation using TDD.
๐ Selection Sort
Business Rules:
@Test
@DisplayName("It should find the smallest price.")
void findSmallest(){
int indexExpected = 3;
Products[] products = {
new Products("TV", 900.00),
new Products("Notebook", 1200.00),
new Products("Tablet", 450.00),
new Products("HD Case", 80.90) // Smallest price
};
int smallest = SelectionSort.findSmallest(products, 0, products.length - 1);
assertEquals(indexExpected, smallest);
assertEquals("HD Case", products[smallest].getModel());
}
@Test
@DisplayName("Should swap positions")
void swapPostions(){
Products[] products = {
new Products("TV", 900.00),
new Products("Notebook", 1200.00),
new Products("Tablet", 450.00),
new Products("HD Case", 80.90),
};
Products[] expectedProductsSwaped = {
new Products("TV", 900.00),
new Products("HD Case", 80.90), // Notebook - swaped
new Products("Tablet", 450.00),
new Products("Notebook", 1200.00), // HD Case - swaped
};
SelectionSort.swapPositions(products, 1, 3);
assertArrayEquals(expectedProductsSwaped, products);
}
@Test
@DisplayName("Should sort array")
void sortArray(){
Products[] arrayInput = {
new Products("TV", 900.00),
new Products("Notebook", 1200.00),
new Products("Tablet", 450.00),
new Products("HD Case", 80.90),
};
Products[] arrayExpected = {
new Products("HD Case", 80.90), // Notebook - swaped
new Products("Tablet", 450.00),
new Products("TV", 900.00),
new Products("Notebook", 1200.00), // HD Case - swaped
};
Products[] arraySorted = SelectionSort.sort(arrayInput);
assertArrayEquals(arrayExpected, arraySorted);
}
๐ Insertion Sort
Business Rules:
@Test
@DisplayName("Should swap positions")
void swapPostions(){
Products[] products = {
new Products("TV", 900.00),
new Products("Notebook", 1200.00),
new Products("Tablet", 450.00),
new Products("HD Case", 80.90),
};
Products[] expectedProductsSwaped = {
new Products("HD Case", 80.90), // Notebook - swaped
new Products("Tablet", 450.00),
new Products("TV", 900.00),
new Products("Notebook", 1200.00), // HD Case - swaped
};
InsertionSort.sort(products);
assertArrayEquals(expectedProductsSwaped, products);
}
- Maven โ๏ธ
- Java 8+ โ
Run the command:
mvn clean install