Advertisement
Guest User

teste bom pra ordenacao

a guest
Dec 5th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. package sorting.test;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5.  
  6. import org.junit.Assert;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9.  
  10. import sorting.AbstractSorting;
  11. import sorting.variationsOfBubblesort.CombSort;
  12. import sorting.variationsOfBubblesort.GnomeSort;
  13.  
  14. public class StudentSortingTest {
  15.     private Integer[] vetorTamPar;
  16.     private Integer[] vetorTamImpar;
  17.     private Integer[] vetorVazio = {};
  18.     private Integer[] vetorValoresRepetidos;
  19.     private Integer[] vetorValoresIguais;
  20.  
  21.     public AbstractSorting<Integer> implementation;
  22.  
  23.     @Before
  24.     public void setUp() {
  25.         populaVetorTamanhoPar(new Integer[] { 30, 28, 7, 29, 11, 26, 4, 22, 23,
  26.                 31 });
  27.         populaVetorTamanhoImpar(new Integer[] { 6, 41, 32, 7, 26, 4, 37, 49,
  28.                 11, 18, 36 });
  29.         populaVetorRepetido(new Integer[] { 4, 9, 3, 4, 0, 5, 1, 4 });
  30.         populaVetorIgual(new Integer[] { 6, 6, 6, 6, 6, 6 });
  31.         populaVetor();
  32.  
  33.         getImplementation();
  34.     }
  35.  
  36.     // // MÉTODOS AUXILIARES DA INICIALIZAÇÃO
  37.  
  38.     /**
  39.      * Método que inicializa a implementação a ser testada com a implementação
  40.      * do aluno
  41.      */
  42.     private void getImplementation() {
  43.         // TODO O aluno deve instanciar sua implementação abaixo ao invés de
  44.         // null
  45.         this.implementation = new CombSort<Integer>();
  46.     }
  47.  
  48.     public void populaVetorTamanhoPar(Integer[] arrayPadrao) {
  49.         this.vetorTamPar = Arrays.copyOf(arrayPadrao, arrayPadrao.length);
  50.     }
  51.  
  52.     public void populaVetorTamanhoImpar(Integer[] arrayPadrao) {
  53.         this.vetorTamImpar = Arrays.copyOf(arrayPadrao, arrayPadrao.length);
  54.     }
  55.  
  56.     public void populaVetorRepetido(Integer[] arrayPadrao) {
  57.         this.vetorValoresRepetidos = Arrays.copyOf(arrayPadrao,
  58.                 arrayPadrao.length);
  59.     }
  60.  
  61.     public void populaVetorIgual(Integer[] arrayPadrao) {
  62.         this.vetorValoresIguais = Arrays
  63.                 .copyOf(arrayPadrao, arrayPadrao.length);
  64.     }
  65.  
  66.     // FIM DOS METODOS AUXILIARES DA INICIALIZAÇÃO
  67.  
  68.     // MÉTODOS DE TESTE
  69.  
  70.     public void genericTest(Integer[] array) {
  71.         Integer[] copy1 = Arrays.copyOf(array, array.length);
  72.         Integer[] copy2 = Arrays.copyOf(array, array.length);
  73.         Integer[] copy3 = Arrays.copyOf(array, array.length);
  74.         implementation.sort(array);
  75.         Arrays.sort(copy1);
  76.         Assert.assertArrayEquals(copy1, array);
  77.  
  78.         if (copy3.length > 0) {
  79.             int i = getRandomNumberInRange(0, array.length / 2);
  80.             implementation.sort(copy2, 0, i);
  81.             Arrays.sort(copy3, 0, i + 1);
  82.         }
  83.         Assert.assertArrayEquals(copy2, copy3);
  84.     }
  85.  
  86.     @Test
  87.     public void testSort01() {
  88.         genericTest(vetorTamPar);
  89.     }
  90.  
  91.     @Test
  92.     public void testSort02() {
  93.         genericTest(vetorTamImpar);
  94.     }
  95.  
  96.     @Test
  97.     public void testSort03() {
  98.         genericTest(vetorVazio);
  99.     }
  100.  
  101.     @Test
  102.     public void testSort04() {
  103.         genericTest(vetorValoresIguais);
  104.     }
  105.  
  106.     @Test
  107.     public void testSort05() {
  108.         genericTest(vetorValoresRepetidos);
  109.     }
  110.  
  111.     // MÉTODOS QUE OS ALUNOS PODEM CRIAR
  112.     /**
  113.      * O ALUNO PODE IMPLEMENTAR METODOS DE ORDENAÇÃO TESTANDO O SORT COM TRES
  114.      * ARGUMENTOS PARA TESTAR A ORDENACAO EM UM PEDAÇO DO ARRAY. DICA: PROCUREM
  115.      * SEGUIR A ESTRUTURA DOS MÉTODOS DE TESTE ACIMA DESCRITOS, ORDENANDO APENAS
  116.      * UMA PARTE DO ARRAY.
  117.      */
  118.     private Integer[] vetor;
  119.  
  120.     private void populaVetor() {
  121.         int size = getRandomNumberInRange(0, 10000);
  122.         vetor = new Integer[size];
  123.         for (int i = 0; i < size; i++) {
  124.             vetor[i] = getRandomNumberInRange(-100000, 100000);
  125.         }
  126.     }
  127.  
  128.     public void genericTest2(Integer[] array) {
  129.         populaVetor();
  130.         // Pega dois indices, i ate no maximo a metade, para otimizar testes
  131.         int i = getRandomNumberInRange(0, array.length / 2);
  132.         int j = getRandomNumberInRange(i, array.length);
  133.         // Copiando o array
  134.         Integer[] copy1 = Arrays.copyOf(array, array.length);
  135.         // Ordenando uma parte e comparando
  136.         implementation.sort(array, i, j);
  137.         Arrays.sort(copy1, i, j + 1);
  138.         Assert.assertArrayEquals(copy1, array);
  139.         // Ordenando ele completo (o restante) e comparando
  140.         implementation.sort(array);
  141.         Arrays.sort(copy1);
  142.         Assert.assertArrayEquals(copy1, array);
  143.     }
  144.  
  145.     public void genericTest2(Integer[] array, int i, int j) {
  146.         populaVetor();
  147.         Integer[] copy1 = Arrays.copyOf(array, array.length);
  148.         implementation.sort(array, i, j);
  149.         if (i < 0 || j >= array.length)
  150.             return;    
  151.         Arrays.sort(copy1, i, j + 1);
  152.         Assert.assertArrayEquals(copy1, array);
  153.     }
  154.  
  155.     @Test
  156.     public void testSort10() {
  157.         genericTest2(vetor);
  158.     }
  159.  
  160.     @Test
  161.     public void testSort11() {
  162.         genericTest2(vetor);
  163.     }
  164.  
  165.     @Test
  166.     public void testSort12() {
  167.         genericTest2(vetor);
  168.     }
  169.  
  170.     @Test
  171.     public void testSort13() {
  172.         genericTest2(vetor);
  173.     }
  174.  
  175.     @Test
  176.     public void testSort14() {
  177.         genericTest2(vetor);
  178.     }
  179.  
  180.     @Test
  181.     public void testSort06() {
  182.         genericTest2(vetor);
  183.     }
  184.  
  185.     @Test
  186.     public void testSort07() {
  187.         genericTest2(vetor);
  188.     }
  189.  
  190.     @Test
  191.     public void testSort08() {
  192.         genericTest2(vetor);
  193.     }
  194.  
  195.     @Test
  196.     public void testSort15() {
  197.         genericTest2(vetor, -1, 0);
  198.     }
  199.  
  200.     @Test
  201.     public void testSort16() {
  202.         genericTest2(vetor, 0, 1000000);
  203.     }
  204.  
  205.     @Test
  206.     public void testSort17() {
  207.         genericTest2(vetor, 1, 0);
  208.     }
  209.  
  210.     @Test
  211.     public void testSort18() {
  212.         genericTest2(vetor, -1, 1000000);
  213.     }
  214.    
  215.     @Test
  216.     public void testSortNull() {
  217.         implementation.sort(null, 0, 1);
  218.         implementation.sort(null, -1, 1);
  219.         implementation.sort(null, 1, 0);
  220.         implementation.sort(null, 1, 1000000);
  221.     }
  222.  
  223.     private static int getRandomNumberInRange(int min, int max) {
  224.         if (min > max) {
  225.             throw new IllegalArgumentException("max must be greater than min");
  226.         }
  227.         Random r = new Random();
  228.         return r.nextInt((max - min) + 1) + min;
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement