/test/kilim/test/ex/ExYieldStack.java
Java | 129 lines | 92 code | 21 blank | 16 comment | 19 complexity | 916d8020dc734b17cc2af0561a1f63f5 MD5 | raw file
1package kilim.test.ex; 2 3import kilim.Pausable; 4import kilim.Task; 5 6public class ExYieldStack extends ExYieldBase { 7 8 public ExYieldStack(int test) { 9 testCase = test; 10 } 11 12 public void execute() throws Pausable { 13 doPause = false; 14 test(); 15 doPause = true; 16 test(); 17 } 18 19 private void test() throws Pausable { 20 switch (testCase) { 21 case 0: testStackBottom_st(); break; 22 case 1: testStackBottom_v(); break; 23 case 2: testStackBottom_av(); break; 24 case 3: testFactorial_st(); break; 25 case 4: testFactorial_av(); break; 26 default: throw new IllegalStateException("Unknown testCase " + testCase); 27 } 28 } 29 30 // load stack with a number of types, call a static pausable method 31 // The call to verify ensures that the stack is loaded with 32 // a few elements before the call to pausable_x is made. 33 void testStackBottom_st() throws Pausable { 34 verify(fd, fs, fl, fa, pausable_st(10, doPause)); 35 } 36 37 // load stack with a number of types, call a virtual pausable method 38 void testStackBottom_v() throws Pausable { 39 verify(fd, fs, pausable_v(fl), fa, ff); 40 } 41 42 // load stack with a number of types, call a virtual pausable method 43 // on another object 44 void testStackBottom_av() throws Pausable { 45 verify(fd, fs, fl, fa, new ExYieldStack(testCase).pausable_v(10)); 46 } 47 48 // test a deep hierarchy that makes the state stack grow, have 49 // long parameters and return values, and pause in the middle 50 // of the computation so it is saves longs on the stack 51 void testFactorial_st() throws Pausable { 52 long n = fact_st(15L, doPause); 53 if (n != 1307674368000L) { 54 throw new RuntimeException("Incorrect factorial, n =" + n); 55 } 56 } 57 58 static long fact_st(long n, boolean doPause) throws Pausable { 59// System.out.println("n = " + n + ", doPause = " + doPause); 60 if (n == 1) { 61 if (doPause) Task.sleep(10); 62 return 1L; 63 } 64 if (n == 10) { 65 // Initial state stack is 10 elements long, so it is worth 66 // testing a pause here. 67 if (doPause) Task.sleep(10); 68 } 69 return n * fact_st(n-1, doPause); 70 } 71 72 void testFactorial_av() throws Pausable { 73 long n = new ExYieldStack(testCase).fact_av(15L, doPause); 74 if (n != 1307674368000L) { 75 throw new RuntimeException("Incorrect factorial, n =" + n); 76 } 77 } 78 79 long fact_av(long n, boolean doPause) throws Pausable { 80 if (n == 1) { 81 if (doPause) Task.sleep(50); 82 return 1L; 83 } 84 if (n == 10) { 85 // Initial state stack is 10 elements long, so it is worth 86 // testing a pause here. 87 if (doPause) Task.sleep(50); 88 } 89 return n * new ExYieldStack(testCase).fact_av(n-1, doPause); 90 } 91 92 static float pausable_st(int i, boolean doPause) throws Pausable { 93 if (doPause) { 94 Task.sleep(50); 95 } 96 return 11.0f; 97 } 98 99 long pausable_v(long l) throws Pausable { 100 if (doPause) { 101 Task.sleep(50); 102 } 103 return l; 104 } 105 106 static void verify(double ad, Object as, long al, Object aa, float aii) { 107 verify(ad); 108 verify((String) as); 109 verify(al); 110 verify((String[][]) aa); 111 } 112 113 void dummySyncTest() throws Pausable { 114 // just making sure that the weaver doesn't barf if we call 115 // call a pausable function from _outside_ a synchronized block. 116 synchronized(this) { 117 notify(); // dummy non-pausable method in a sync block 118 } 119 120 pausable_st(0, false); 121 122 synchronized(this) { 123 notify(); // dummy non-pausable method in a sync block 124 } 125 126 } 127 128 129}