Created
November 8, 2013 23:54
-
-
Save lawrencejones/7379517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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