/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. package kilim.test;
  7. import junit.framework.TestCase;
  8. import kilim.ExitMsg;
  9. import kilim.Mailbox;
  10. import kilim.Continuation;
  11. import kilim.Scheduler;
  12. import kilim.Task;
  13. import kilim.test.ex.ExYieldBase;
  14. public class TestYield extends TestCase {
  15. public void testStackBottom_st() throws Exception {
  16. runTask(new kilim.test.ex.ExYieldStack(0));
  17. }
  18. public void testStackBottom_v() throws Exception {
  19. runTask(new kilim.test.ex.ExYieldStack(1));
  20. }
  21. public void testStackBottom_av() throws Exception {
  22. runTask(new kilim.test.ex.ExYieldStack(2));
  23. }
  24. public void testFactorial_st() throws Exception {
  25. runTask(new kilim.test.ex.ExYieldStack(3));
  26. }
  27. public void testFactorial_av() throws Exception {
  28. runTask(new kilim.test.ex.ExYieldStack(4));
  29. }
  30. public void testDupsInVars() throws Exception {
  31. runTask(new kilim.test.ex.ExYieldDups(0));
  32. }
  33. public void testDupsInStack() throws Exception {
  34. runTask(new kilim.test.ex.ExYieldDups(1));
  35. }
  36. public void testLongArgs() throws Exception {
  37. runTask(new kilim.test.ex.ExYieldDups(2));
  38. }
  39. public void testConstantsInStack() throws Exception {
  40. runTask(new kilim.test.ex.ExYieldConstants(0));
  41. }
  42. public void testLoop() throws Exception {
  43. kilim.test.ex.ExLoop ex = new kilim.test.ex.ExLoop();
  44. runTask(ex);
  45. assertTrue(ex.verify());
  46. }
  47. public static void runTask(String taskClassName, int testCase) throws Exception {
  48. ExYieldBase task;
  49. task = (ExYieldBase) (Class.forName(taskClassName).newInstance());
  50. task.testCase = testCase;
  51. runTask(task);
  52. }
  53. public static void runTask(Task task) throws Exception {
  54. Mailbox<ExitMsg> exitmb = new Mailbox<ExitMsg>();
  55. Scheduler s = Scheduler.make(1);
  56. task.informOnExit(exitmb);
  57. task.setScheduler(s);
  58. task.start();
  59. ExitMsg m = exitmb.getb();
  60. if (m == null) {
  61. fail("Timed Out");
  62. } else {
  63. Object res = m.result;
  64. if (res instanceof Throwable) {
  65. ((Throwable)res).printStackTrace();
  66. fail(m.toString());
  67. }
  68. }
  69. s.shutdown();
  70. }
  71. public static void runPure(Continuation pure) throws Exception {
  72. while (!pure.run()) {}
  73. if (pure.ex() != null) {
  74. pure.ex().printStackTrace();
  75. fail(pure.ex().toString());
  76. }
  77. }
  78. }