mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add and test helper to easily get random routes and jobs from collection
This commit is contained in:
parent
b357bed413
commit
3402bb413e
2 changed files with 52 additions and 10 deletions
|
|
@ -12,20 +12,18 @@ import java.util.Random;
|
|||
public class RandomUtils {
|
||||
|
||||
public static VehicleRoute nextRoute(Collection<VehicleRoute> routes, Random random){
|
||||
int randomIndex = random.nextInt(routes.size());
|
||||
int count = 0;
|
||||
for(VehicleRoute route : routes){
|
||||
if(count <= randomIndex) return route;
|
||||
count++;
|
||||
}
|
||||
return null;
|
||||
return nextItem(routes,random);
|
||||
}
|
||||
|
||||
public static Job nextJob(Collection<Job> jobs, Random random){
|
||||
int randomIndex = random.nextInt(jobs.size());
|
||||
return nextItem(jobs,random);
|
||||
}
|
||||
|
||||
public static <T> T nextItem(Collection<T> items, Random random){
|
||||
int randomIndex = random.nextInt(items.size());
|
||||
int count = 0;
|
||||
for(Job job : jobs){
|
||||
if(count <= randomIndex) return job;
|
||||
for(T item : items){
|
||||
if(count == randomIndex) return item;
|
||||
count++;
|
||||
}
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
package jsprit.core.util;
|
||||
|
||||
import jsprit.core.problem.job.Job;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Created by schroeder on 14/01/15.
|
||||
*/
|
||||
public class RandomUtilsTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnSingleJob(){
|
||||
Job job = mock(Job.class);
|
||||
Collection<Job> jobs = Arrays.asList(job);
|
||||
Assert.assertEquals(job, RandomUtils.nextItem(jobs, RandomNumberGeneration.getRandom()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSingleJob_(){
|
||||
Job job = mock(Job.class);
|
||||
Collection<Job> jobs = Arrays.asList(job);
|
||||
Assert.assertEquals(job, RandomUtils.nextJob(jobs, RandomNumberGeneration.getRandom()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnJob3(){
|
||||
Job job3 = mock(Job.class);
|
||||
List<Job> jobs = Arrays.asList(mock(Job.class),mock(Job.class),job3);
|
||||
Random random = mock(Random.class);
|
||||
when(random.nextInt(jobs.size())).thenReturn(2);
|
||||
Assert.assertEquals(job3,RandomUtils.nextJob(jobs,random));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue