1 package org.jruby.ast.executable; 2 3 import org.jruby.Ruby; 4 import org.jruby.ast.executable.YARVMachine.Instruction; 5 import org.jruby.parser.LocalStaticScope; 6 import org.jruby.parser.StaticScope; 7 import org.jruby.runtime.ThreadContext; 8 import org.jruby.runtime.builtin.IRubyObject; 9 10 import junit.framework.TestCase; 11 12 public class YARVMachineTest extends TestCase { 13 public static Instruction[] getSimpleTest(Ruby runtime) { 14 return new Instruction[] { 15 new Instruction(YARVInstructions.PUTSTRING, "Hello, YARV!"), 16 new Instruction(YARVInstructions.DUP), 17 new Instruction(YARVInstructions.SETLOCAL, 0), 18 new Instruction(YARVInstructions.GETLOCAL, 0), 19 new Instruction(YARVInstructions.POP), 20 new Instruction(YARVInstructions.SETLOCAL, 1), 21 new Instruction(YARVInstructions.PUTOBJECT, runtime.getTrue()), 22 new Instruction(YARVInstructions.BRANCHIF, 10), 23 new Instruction(YARVInstructions.PUTSTRING, "Wrong String"), 24 new Instruction(YARVInstructions.JUMP, 11), 25 new Instruction(YARVInstructions.GETLOCAL, 1), 26 new Instruction(YARVInstructions.PUTOBJECT, runtime.newFixnum(2)), 27 new Instruction(YARVInstructions.SEND, "*", 1, null, 0), 28 new Instruction(YARVInstructions.SEND, "to_s", 0, null, YARVInstructions.VCALL_FLAG | YARVInstructions.FCALL_FLAG), 29 new Instruction(YARVInstructions.SEND, "+", 1, null, 0) 30 }; 31 }; 32 33 public static Instruction[] getFib(Ruby runtime, int n){ 34 return new Instruction[] { 35 new Instruction(YARVInstructions.PUTOBJECT, runtime.newFixnum(n)), new Instruction(YARVInstructions.SETLOCAL, 0), 38 new Instruction(YARVInstructions.PUTOBJECT, runtime.newFixnum(0)), 41 new Instruction(YARVInstructions.SETLOCAL, 1), 42 new Instruction(YARVInstructions.PUTOBJECT, runtime.newFixnum(1)), 44 new Instruction(YARVInstructions.SETLOCAL, 2), 45 new Instruction(YARVInstructions.PUTOBJECT, runtime.newFixnum(1)), 47 new Instruction(YARVInstructions.SETLOCAL, 3), 48 new Instruction(YARVInstructions.GETLOCAL, 3), 50 new Instruction(YARVInstructions.GETLOCAL, 0), 51 new Instruction(YARVInstructions.SEND, "<=", 1, null, 0), 52 new Instruction(YARVInstructions.BRANCHUNLESS, 25), 53 new Instruction(YARVInstructions.GETLOCAL, 1), 55 new Instruction(YARVInstructions.SETLOCAL, 4), 56 new Instruction(YARVInstructions.GETLOCAL, 2), 58 new Instruction(YARVInstructions.SETLOCAL, 1), 59 new Instruction(YARVInstructions.GETLOCAL, 4), 61 new Instruction(YARVInstructions.GETLOCAL, 2), 62 new Instruction(YARVInstructions.SEND, "+", 1, null, 0), 63 new Instruction(YARVInstructions.SETLOCAL, 2), 64 new Instruction(YARVInstructions.GETLOCAL, 3), 66 new Instruction(YARVInstructions.PUTOBJECT, runtime.newFixnum(1)), 67 new Instruction(YARVInstructions.SEND, "+", 1, null, 0), 68 new Instruction(YARVInstructions.SETLOCAL, 3), 69 new Instruction(YARVInstructions.JUMP, 8), 71 new Instruction(YARVInstructions.GETLOCAL, 1) 73 }; 74 }; 75 76 public void testSimpleExecution() { 77 YARVMachine ym = new YARVMachine(); 78 79 Ruby runtime = Ruby.newInstance(System.in, System.out, System.err); 80 ThreadContext context = runtime.getCurrentContext(); 81 82 StaticScope scope = new LocalStaticScope(null); 83 scope.setVariables(new String [] { "zero", "one" }); 84 assertEquals("Hello, YARV!Hello, YARV!Object", ym.exec(context, runtime.getObject(), scope, getSimpleTest(runtime)).toString()); 85 } 86 87 public void testIterativeFib() { 88 YARVMachine ym = new YARVMachine(); 89 90 Ruby runtime = Ruby.newInstance(System.in, System.out, System.err); 91 ThreadContext context = runtime.getCurrentContext(); 92 93 StaticScope scope = new LocalStaticScope(null); 94 scope.setVariables(new String [] {"n", "i", "j", "cur", "k"}); 95 96 assertEquals("55", ym.exec(context, runtime.getObject(), scope, getFib(runtime,10)).toString()); 97 98 IRubyObject fib5k = ym.exec(context, runtime.getObject(), scope, getFib(runtime,5000)); 99 assertEquals("38789684543883256337019163083259053120821277146462451061605972148955501390440370" + 100 "9701082291646221066947929345285888297381348310200895498294036143015691147893836421656" + 101 "3944106910214505634133706558656238254656700712525929903854933813928836378347518908762" + 102 "9707120333370529231076930085180938498018038478139967488817655546537882916442689129803" + 103 "8461377896902150229308247566634622492307188332480328037503913035290330450584270114763" + 104 "5242270210934637699104006714174883298422891491273104054328753298044273676822977244987" + 105 "7498745556919077038806370468327948113589737399931101062193081490185708153978543791953" + 106 "0561751076105307568878376603366735544525884488624161921055345749367589784902798823435" + 107 "1023599844663934853256411952221859563060475364645470760330902420806382584929156452876" + 108 "2915757591423438091423029174910889841552098544324865940797935713168416928680395453095" + 109 "4538869811466508206686289742063932343848846524098874239587380197699382031717420893226" + 110 "5468879364002630797780058759129671389634214252579116872755600360311370547754724604639" + 111 "987588046985178408674382863125", fib5k.toString()); 112 } 113 114 public static void main(String [] args) { 115 new YARVMachineTest().testIterativeFib(); 116 } 117 } 118 | Popular Tags |