Population { public Chromosome[] selectBestKChromosome(int k) { if (fitnessCalculatedFlags.cardinality() == chromosomes.length) { Chromosome[] cp = Arrays.copyOf(chromosomes, chromosomes.length); Arrays.sort(cp); return Arrays.copyOfRange(cp, cp.length - k, cp.length); } return null; } Population(int size); Chromosome getChromosome(int idx); double calculateFitnessForChromosome(int idx, Function<Chromosome, Double> fitnessFunction); void calculateFitnessForAll(Function<Chromosome, Double> fitnessFunction); void setChromosome(int idx, Chromosome chromosome); Chromosome getChromosome(Integer idx); Chromosome[] selectBestKChromosome(int k); double getTotalFitness(); double getAverageFitness(); int size(); void setFitness(Integer idx, Double fitness);  }
@Test public void selectBestKChromosomeWithoutFitnessCalculation() { Assert.assertNull(population.selectBestKChromosome(K)); }
@Test public void selectBestKChromosome() { population.calculateFitnessForAll(fitnessFunction); Chromosome[] res = population.selectBestKChromosome(K); Assert.assertEquals(res[0].getFitness(), 180, PRECISION); }