Revision: 15735
http://jikesrvm.svn.sourceforge.net/jikesrvm/?rev=15735&view=revAuthor: rgarner
Date: 2009-07-23 01:46:59 +0000 (Thu, 23 Jul 2009)
Log Message:
-----------
Unit tests for the MMTk Harness Scheduler
Added Paths:
-----------
rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/
rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests.java
rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests2.java
rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/JavaSchedulerTest.java
rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/SchedulerTestCases.java
rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/TestMutator.java
Added: rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests.java
===================================================================
--- rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests.java (rev 0)
+++ rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests.java 2009-07-23 01:46:59 UTC (rev 15735)
@@ -0,0 +1,52 @@
+package org.mmtk.harness.scheduler;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mmtk.harness.Harness;
+
+/**
+ * Deterministic scheduler test cases, with maximum interleaving
+ */
+public class DeterministicTests {
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ Harness.init("scheduler=DETERMINISTIC","schedulerPolicy=FIXED","yieldInterval=1");
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @Test
+ public void testOneThreadOneItem() {
+ assertEquals(Arrays.asList("a"),
+ new SchedulerTestCases().testOneThreadOneItem("a"));
+ }
+
+ @Test
+ public void testOneThreadTwoItems() {
+ assertEquals(Arrays.asList("a","b"),
+ new SchedulerTestCases().testOneThreadTwoItems("a","b"));
+ }
+
+ @Test
+ public void testTwoThreadsOneItem() {
+ assertEquals(Arrays.asList("a","b"),
+ new SchedulerTestCases().testTwoThreadsOneItem("a","b"));
+ }
+
+ @Test
+ public void testTwoThreadsTwoItems() {
+ Object a = new Object();
+ Object b = new Object();
+ Object c = new Object();
+ Object d = new Object();
+ assertEquals(Arrays.asList("a","c","b","d"),
+ new SchedulerTestCases().testTwoThreadsTwoItems("a","b","c","d"));
+ }
+}
Added: rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests2.java
===================================================================
--- rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests2.java (rev 0)
+++ rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/DeterministicTests2.java 2009-07-23 01:46:59 UTC (rev 15735)
@@ -0,0 +1,52 @@
+package org.mmtk.harness.scheduler;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mmtk.harness.Harness;
+
+/**
+ * Deterministic scheduler test cases, with minimum interleaving
+ */
+public class DeterministicTests2 {
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ Harness.init("scheduler=DETERMINISTIC","schedulerPolicy=NEVER");
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @Test
+ public void testOneThreadOneItem() {
+ assertEquals(Arrays.asList("a"),
+ new SchedulerTestCases().testOneThreadOneItem("a"));
+ }
+
+ @Test
+ public void testOneThreadTwoItems() {
+ assertEquals(Arrays.asList("a","b"),
+ new SchedulerTestCases().testOneThreadTwoItems("a","b"));
+ }
+
+ @Test
+ public void testTwoThreadsOneItem() {
+ assertEquals(Arrays.asList("a","b"),
+ new SchedulerTestCases().testTwoThreadsOneItem("a","b"));
+ }
+
+ @Test
+ public void testTwoThreadsTwoItems() {
+ Object a = new Object();
+ Object b = new Object();
+ Object c = new Object();
+ Object d = new Object();
+ assertEquals(Arrays.asList("a","b","c","d"),
+ new SchedulerTestCases().testTwoThreadsTwoItems("a","b","c","d"));
+ }
+}
Added: rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/JavaSchedulerTest.java
===================================================================
--- rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/JavaSchedulerTest.java (rev 0)
+++ rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/JavaSchedulerTest.java 2009-07-23 01:46:59 UTC (rev 15735)
@@ -0,0 +1,63 @@
+package org.mmtk.harness.scheduler;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mmtk.harness.Harness;
+
+/**
+ * Test the plain Java scheduler
+ */
+public class JavaSchedulerTest {
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ Harness.init("scheduler=JAVA"/* ,"trace=SCHEDULER" */);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ @Test
+ public void testOneThreadOneItem() {
+ assertEquals(Arrays.asList("a"),new SchedulerTestCases().testOneThreadOneItem("a"));
+ }
+
+ @Test
+ public void testOneThreadTwoItems() {
+ assertEquals(Arrays.asList("a","b"),
+ new SchedulerTestCases().testOneThreadTwoItems("a","b"));
+ }
+
+ @Test
+ public void testTwoThreadsOneItem() {
+ assertEquals(setOf("a","b"),
+ setOf(new SchedulerTestCases().testTwoThreadsOneItem("a", "b")));
+ }
+
+ @Test
+ public void testTwoThreadsTwoItems() {
+ List<Object> result = new SchedulerTestCases().testTwoThreadsTwoItems("a", "b", "c", "d");
+ assertEquals(setOf("a","b","c","d"),setOf(result));
+ assertTrue(result.indexOf("a") < result.indexOf("b"));
+ assertTrue(result.indexOf("c") < result.indexOf("d"));
+ }
+
+ private <T> Set<T> setOf(T...items) {
+ return new HashSet<T>(Arrays.asList(items));
+ }
+
+ private <T> Set<T> setOf(Collection<T> items) {
+ return new HashSet<T>(items);
+ }
+
+}
Added: rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/SchedulerTestCases.java
===================================================================
--- rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/SchedulerTestCases.java (rev 0)
+++ rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/SchedulerTestCases.java 2009-07-23 01:46:59 UTC (rev 15735)
@@ -0,0 +1,78 @@
+package org.mmtk.harness.scheduler;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Common code for the scheduler tests. Not actually a Junit test suite,
+ * but used by several others.
+ */
+public class SchedulerTestCases {
+
+ /**
+ * One thread inserts a single item into the result list
+ *
+ * Expected result: A list containing the single item.
+ *
+ * @param a The item
+ * @return The result list
+ */
+ public List<Object> testOneThreadOneItem(Object a) {
+ List<Object> results = new ArrayList<Object>(1);
+ Scheduler.scheduleMutator(new TestMutator(results,Arrays.asList(a)));
+ Scheduler.schedule();
+ return results;
+ }
+
+ /**
+ * One thread inserts two items into the result list
+ *
+ * Expected result: A list containing the two items in order.
+ *
+ * @param a The item
+ * @return The result list
+ */
+ public List<Object> testOneThreadTwoItems(Object a, Object b) {
+ List<Object> results = new ArrayList<Object>(2);
+ Scheduler.scheduleMutator(new TestMutator(results,Arrays.asList(a,b)));
+ Scheduler.schedule();
+ return results;
+ }
+
+ /**
+ * Two threads insert a single item each into the result list
+ *
+ * Expected result: A list containing the two items in a
+ * policy/scheduler-dependent order.
+ *
+ * @param a The item inserted by thread 1
+ * @param b The item inserted by thread 2
+ * @return The result list
+ */
+ public List<Object> testTwoThreadsOneItem(Object a, Object b) {
+ List<Object> results = Collections.synchronizedList(new ArrayList<Object>(2));
+ Scheduler.scheduleMutator(new TestMutator(results,Arrays.asList(a)));
+ Scheduler.scheduleMutator(new TestMutator(results,Arrays.asList(b)));
+ Scheduler.schedule();
+ return results;
+ }
+
+ /**
+ * Two threads insert two items each into the result list
+ *
+ * Expected result: A list containing the four items in a
+ * policy/scheduler-dependent order.
+ * @param items The four items.
+ * @return The result list
+ */
+ public List<Object> testTwoThreadsTwoItems(Object...items) {
+ List<Object> results = Collections.synchronizedList(new ArrayList<Object>(4));
+ Scheduler.scheduleMutator(new TestMutator(results,Arrays.asList(items[0],items[1])));
+ Scheduler.scheduleMutator(new TestMutator(results,Arrays.asList(items[2],items[3])));
+ Scheduler.schedule();
+ return results;
+ }
+
+}
Added: rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/TestMutator.java
===================================================================
--- rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/TestMutator.java (rev 0)
+++ rvmroot/trunk/testing/tests/mmtk/src/org/mmtk/harness/scheduler/TestMutator.java 2009-07-23 01:46:59 UTC (rev 15735)
@@ -0,0 +1,37 @@
+package org.mmtk.harness.scheduler;
+
+import java.util.Collection;
+
+import org.mmtk.harness.lang.Env;
+
+public class TestMutator implements Schedulable {
+
+ private final Collection<Object> resultPool;
+
+ private final Collection<Object> items;
+
+
+ /**
+ * Create a test mutator that inserts 'results' into 'resultpool' when it is scheduled,
+ * with a yield point between every insertion.
+ * @param resultPool
+ * @param result
+ */
+ TestMutator(Collection<Object> resultPool, Collection<Object> items) {
+ super();
+ this.resultPool = resultPool;
+ this.items = items;
+ }
+
+ /**
+ * @see org.mmtk.harness.scheduler.Schedulable#execute(org.mmtk.harness.lang.Env)
+ */
+ @Override
+ public void execute(Env env) {
+ for (Object item : items) {
+ resultPool.add(item);
+ Scheduler.yield();
+ }
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
------------------------------------------------------------------------------
_______________________________________________
Jikesrvm-commits mailing list
Jikesrvm-commits@...
https://lists.sourceforge.net/lists/listinfo/jikesrvm-commits