PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/test/kilim/test/TestYield.java

http://github.com/kilim/kilim
Java | 96 lines | 73 code | 18 blank | 5 comment | 7 complexity | 3694ae8416b0d73c77fa2973a24fc4b7 MD5 | raw file
 1/* Copyright (c) 2006, Sriram Srinivasan
 2 *
 3 * You may distribute this software under the terms of the license 
 4 * specified in the file "License"
 5 */
 6
 7package kilim.test;
 8
 9import junit.framework.TestCase;
10import kilim.ExitMsg;
11import kilim.Mailbox;
12import kilim.Continuation;
13import kilim.Scheduler;
14import kilim.Task;
15import kilim.test.ex.ExYieldBase;
16
17public class TestYield extends TestCase {
18    
19    public void testStackBottom_st() throws Exception {
20        runTask(new kilim.test.ex.ExYieldStack(0));
21    }
22
23    public void testStackBottom_v() throws Exception {
24        runTask(new kilim.test.ex.ExYieldStack(1));
25    }
26    
27    public void testStackBottom_av() throws Exception {
28        runTask(new kilim.test.ex.ExYieldStack(2));
29    }
30    
31    public void testFactorial_st() throws Exception {
32        runTask(new kilim.test.ex.ExYieldStack(3));
33    }
34
35    public void testFactorial_av() throws Exception {
36        runTask(new kilim.test.ex.ExYieldStack(4));
37    }
38    
39    public void testDupsInVars() throws Exception {
40        runTask(new kilim.test.ex.ExYieldDups(0));
41    }
42
43    public void testDupsInStack() throws Exception {
44        runTask(new kilim.test.ex.ExYieldDups(1));
45    }
46
47    public void testLongArgs() throws Exception {
48        runTask(new kilim.test.ex.ExYieldDups(2));
49    }
50
51    public void testConstantsInStack() throws Exception {
52        runTask(new kilim.test.ex.ExYieldConstants(0));
53    }
54    
55    public void testLoop() throws Exception {
56         kilim.test.ex.ExLoop ex = new kilim.test.ex.ExLoop();
57         runTask(ex);
58         assertTrue(ex.verify());
59    }
60
61
62    public static void runTask(String taskClassName, int testCase) throws Exception {
63        ExYieldBase task;
64        
65        task = (ExYieldBase) (Class.forName(taskClassName).newInstance());
66        task.testCase = testCase;
67        runTask(task);
68    }
69
70    public static void runTask(Task task) throws Exception {
71        Mailbox<ExitMsg> exitmb = new Mailbox<ExitMsg>();
72        Scheduler s = Scheduler.make(1);
73        task.informOnExit(exitmb);
74        task.setScheduler(s); 
75        task.start();
76        
77        ExitMsg m = exitmb.getb();
78        if (m == null) {
79            fail("Timed Out");
80        } else {
81            Object res = m.result;
82            if (res instanceof Throwable) {
83                ((Throwable)res).printStackTrace();
84                fail(m.toString());
85            }
86        }
87        s.shutdown();
88    }
89    public static void runPure(Continuation pure) throws Exception {
90        while (!pure.run()) {}
91        if (pure.ex() != null) {
92            pure.ex().printStackTrace();
93            fail(pure.ex().toString());
94        }
95    }
96}