Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / mcr-test / src / edu / tamu / aser / rvtest / allocationvector / AllocationTest.java
diff --git a/JMCR-Stable/mcr-test/src/edu/tamu/aser/rvtest/allocationvector/AllocationTest.java b/JMCR-Stable/mcr-test/src/edu/tamu/aser/rvtest/allocationvector/AllocationTest.java
new file mode 100644 (file)
index 0000000..204935e
--- /dev/null
@@ -0,0 +1,162 @@
+package edu.tamu.aser.rvtest.allocationvector;\r
+\r
+import junit.framework.Assert;\r
+\r
+import org.junit.Test;\r
+import org.junit.internal.runners.statements.Fail;\r
+import org.junit.runner.RunWith;\r
+\r
+import edu.tamu.aser.reexecution.JUnit4MCRRunner;\r
+\r
+@RunWith(JUnit4MCRRunner.class)\r
+public class AllocationTest {\r
+\r
+    public static void main(String[] args) throws InterruptedException {\r
+        AllocationTest allocationTest = new AllocationTest();\r
+        allocationTest.testOneTenthAllocation();\r
+        allocationTest.testOneTenthFreeing();\r
+        allocationTest.testOneTenthAllocationAndFree();\r
+        allocationTest.testHalfAllocation();\r
+        allocationTest.testHalfFreeing();\r
+        allocationTest.testHalfAllocationAndFree();\r
+        allocationTest.testFullAllocation();\r
+        allocationTest.testFullFreeing();\r
+        allocationTest.testFullAllocationAndFree();\r
+    }\r
+\r
+//    @Test\r
+    public void testOneTenthAllocation() throws InterruptedException {\r
+        allocateAndEnsureNoErrors(80, 2, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testOneTenthFreeing() throws InterruptedException {\r
+        freeAndEnsureNoErrors(80, 2, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testOneTenthAllocationAndFree() throws InterruptedException {\r
+        allocateAndFreeAndEnsureNoErrors(80, 2, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testHalfAllocation() throws InterruptedException {\r
+        allocateAndEnsureNoErrors(80, 10, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testHalfFreeing() throws InterruptedException {\r
+        freeAndEnsureNoErrors(80, 10, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testHalfAllocationAndFree() throws InterruptedException {\r
+        allocateAndFreeAndEnsureNoErrors(80, 10, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testFullAllocation() throws InterruptedException {\r
+        allocateAndEnsureNoErrors(80, 10, 4);\r
+    }\r
+\r
+//    @Test\r
+    public void testFullFreeing() throws InterruptedException {\r
+        freeAndEnsureNoErrors(80, 10, 8);\r
+    }\r
+\r
+    @Test\r
+    public void testFullAllocationAndFree() throws InterruptedException {\r
+        allocateAndFreeAndEnsureNoErrors(80, 10, 8);\r
+    }\r
+\r
+    private AllocationVector vector;\r
+\r
+    public void allocateAndFreeAndEnsureNoErrors(int vectorSize, int allocationSize, int numThreads) throws InterruptedException {\r
+\r
+        vector = new AllocationVector(vectorSize);\r
+        AllocateAndFreeThread[] threads = new AllocateAndFreeThread[numThreads];\r
+        int[][] threadResults = new int[numThreads][allocationSize];\r
+\r
+        for (int i = 0; i < numThreads; i++) {\r
+            threads[i] = new AllocateAndFreeThread(vector, threadResults[i], 2);\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.start();\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.join();\r
+        }\r
+\r
+        checkForErrors(threadResults);\r
+    }\r
+\r
+    public void allocateAndEnsureNoErrors(int vectorSize, int allocationSize, int numThreads) throws InterruptedException {\r
+\r
+        vector = new AllocationVector(vectorSize);\r
+        AllocateAndFreeThread[] threads = new AllocateAndFreeThread[numThreads];\r
+        int[][] threadResults = new int[numThreads][allocationSize];\r
+\r
+        for (int i = 0; i < numThreads; i++) {\r
+            threads[i] = new AllocateAndFreeThread(vector, threadResults[i], 0);\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.start();\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.join();\r
+        }\r
+\r
+        checkForErrors(threadResults);\r
+    }\r
+\r
+    public void freeAndEnsureNoErrors(int vectorSize, int allocationSize, int numThreads) throws InterruptedException {\r
+\r
+        vector = new AllocationVector(vectorSize);\r
+        AllocateAndFreeThread[] threads = new AllocateAndFreeThread[numThreads];\r
+        int[][] threadResults = new int[numThreads][allocationSize];\r
+\r
+        // Perform allocation only to set up data for freeing\r
+        for (int i = 0; i < numThreads; i++) {\r
+            threads[i] = new AllocateAndFreeThread(vector, threadResults[i], 0);\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.start();\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.join();\r
+        }\r
+\r
+        checkForErrors(threadResults);\r
+\r
+        // Perform freeing only\r
+        for (int i = 0; i < numThreads; i++) {\r
+            threads[i] = new AllocateAndFreeThread(vector, threadResults[i], 1);\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.start();\r
+        }\r
+\r
+        for (AllocateAndFreeThread thread : threads) {\r
+            thread.join();\r
+        }\r
+\r
+        checkForErrors(threadResults);\r
+    }\r
+\r
+    private void checkForErrors(int[][] threadResults) {\r
+        for (int[] threadResult : threadResults) {\r
+            if (threadResult[0] == -2 || threadResult[0] == -3) {      \r
+                Assert.fail("Tried to allocate a block which was already allocated or free a block that was already free");\r
+                System.out.println("Tried to allocate a block which was already allocated or free a block that was already free");\r
+            }\r
+        }\r
+    }\r
+\r
+}\r