Open willmont1982 opened 4 years ago
import org.junit.Test;
import br.com.CompareOperation; import br.com.Operation; import br.com.SwapOperation;
public class BubbleSortAlgorithmTest { @Test public void doesItReturnTheNumbersInCorrectOrder() { Integer[] numbers = new Integer[] { 7, 3, 5, 1, 4, 2, 6, 8 }; Integer[] expectedNumbers = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Integer[] numbers = new Integer[] { 5, 2, 1, 4, 3 }; Integer[] expectedNumbers = new Integer[] { 1, 2, 3, 4, 5 }; SortAlgorithm sortingAlgorithm = new BubbleSortAlgorithm(); sortingAlgorithm.sort(numbers); assertArrayEquals(expectedNumbers, numbers); }
@Test
public void doesItReturnTheCorrectOperations() {
Integer[] unsortedNumbers = new Integer[] { 7, 3, 5, 1, 4, 2, 6, 8 };
Integer[] unsortedNumbers = new Integer[] { 5, 2, 1, 4, 3 };
SortAlgorithm sortingAlgorithm = new BubbleSortAlgorithm();
/*
* 7, 3, 5, 1, 4, 2, 6, 8 | Initial state
* 3, 7, 5, 1, 4, 2, 6, 8 | 0 <=> 1
* 3, 5, 7, 1, 4, 2, 6, 8 | 1 <=> 2
* 3, 5, 1, 7, 4, 2, 6, 8 | 2 <=> 3
* 3, 5, 1, 4, 7, 2, 6, 8 | 3 <=> 4
* 3, 5, 1, 4, 2, 7, 6, 8 | 4 <=> 5
* 3, 5, 1, 4, 2, 6, 7, 8 | 5 <=> 6
* 3, 1, 5, 4, 2, 6, 7, 8 | 1 <=> 2
* 3, 1, 4, 5, 2, 6, 7, 8 | 2 <=> 3
* 3, 1, 4, 2, 5, 6, 7, 8 | 3 <=> 4
* 1, 3, 4, 2, 5, 6, 7, 8 | 0 <=> 1
* 1, 3, 2, 4, 5, 6, 7, 8 | 2 <=> 3
* 1, 2, 3, 4, 5, 6, 7, 8 | 1 <=> 2
* 5, 2, 1, 4, 3 | Initial state
* 5, 2, 1, 4, 3 | COMPARE: (0, 1)
* 2, 5, 1, 4, 3 | SWAP: (0, 1)
* 2, 5, 1, 4, 3 | COMPARE: (1, 2)
* 2, 1, 5, 4, 3 | SWAP: (1, 2)
* 2, 1, 5, 4, 3 | COMPARE: (2, 3)
* 2, 1, 4, 5, 3 | SWAP: (2, 3)
* 2, 1, 4, 5, 3 | COMPARE: (3, 4)
* 2, 1, 4, 3, 5 | SWAP: (3, 4)
* 2, 1, 4, 3, 5 | COMPARE: (0, 1)
* 1, 2, 4, 3, 5 | SWAP: (0, 1)
* 1, 2, 4, 3, 5 | COMPARE: (1, 2)
* 1, 2, 4, 3, 5 | COMPARE: (2, 3)
* 1, 2, 3, 4, 5 | SWAP: (2, 3)
* 1, 2, 3, 4, 5 | COMPARE: (0, 1)
* 1, 2, 3, 4, 5 | COMPARE: (1, 2)
* 1, 2, 3, 4, 5 | COMPARE: (0, 1)
*/
List<Operation> expectedOperations = new ArrayList<>();
expectedOperations.add(new CompareOperation(0, 1));
expectedOperations.add(new SwapOperation(0, 1));
expectedOperations.add(new CompareOperation(1, 2));
expectedOperations.add(new SwapOperation(1, 2));
expectedOperations.add(new CompareOperation(2, 3));
expectedOperations.add(new SwapOperation(2, 3));
expectedOperations.add(new CompareOperation(3, 4));
expectedOperations.add(new SwapOperation(3, 4));
expectedOperations.add(new SwapOperation(4, 5));
expectedOperations.add(new SwapOperation(5, 6));
expectedOperations.add(new SwapOperation(1, 2));
expectedOperations.add(new SwapOperation(2, 3));
expectedOperations.add(new SwapOperation(3, 4));
expectedOperations.add(new CompareOperation(0, 1));
expectedOperations.add(new SwapOperation(0, 1));
expectedOperations.add(new CompareOperation(1, 2));
expectedOperations.add(new CompareOperation(2, 3));
expectedOperations.add(new SwapOperation(2, 3));
expectedOperations.add(new SwapOperation(1, 2));
expectedOperations.add(new CompareOperation(0, 1));
expectedOperations.add(new CompareOperation(1, 2));
expectedOperations.add(new CompareOperation(0, 1));
List<Operation> operations = sortingAlgorithm.sort(unsortedNumbers);
assertArrayEquals(expectedOperations.toArray(), operations.toArray());
}
package br.com.sortingservice.operations;
public class SwapOperation implements Operation { public class SwapOperation implements ExecutableOperation {