/test/kilim/test/ex/ExYieldStack.java

http://github.com/kilim/kilim · Java · 129 lines · 92 code · 21 blank · 16 comment · 19 complexity · 916d8020dc734b17cc2af0561a1f63f5 MD5 · raw file

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