Skip to content

Instantly share code, notes, and snippets.

@lawrencejones
Created November 8, 2013 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawrencejones/7379517 to your computer and use it in GitHub Desktop.
Save lawrencejones/7379517 to your computer and use it in GitHub Desktop.
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import ic.doc.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@RunWith(JUnit4.class)
public class Tests {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
int sleepTime = 500;
Function<Integer> square = new Function<Integer>() {
@Override
public Integer applyTo(Integer elem) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
System.out.println("Interrupted.");
}
return elem*elem;
}
};
@Test
public void mapTest() {
MappableList<Integer> mlist = new MappableList<Integer>(list);
long start = System.nanoTime();
List<Integer> res = mlist.map(square);
long end = System.nanoTime();
for (int i = 0; i < list.size(); i++)
{
assertEquals("Mapping failed", Integer.valueOf(list.get(i)*list.get(i)), res.get(i));
}
long duration = (end - start)/1000000;
assertTrue("Mapping completed before expected.", (list.size()*sleepTime) < duration);
}
@Test
public void mapConcurrentTest() throws Exception {
MappableList<Integer> mclist = new MappableList<Integer>(list);
long start = System.nanoTime();
List<Integer> res = mclist.mapConcurrent(square);
long end = System.nanoTime();
for (int i = 0; i < list.size(); i++)
{
assertEquals("Mapping failed", Integer.valueOf(list.get(i)*list.get(i)), res.get(i));
}
long duration = (end - start)/1000000;
assertTrue("Concurrent mapping duration is unexpected.", (duration/10) == (sleepTime/10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment