Package de.danielkullmann.hackvm

Examples of de.danielkullmann.hackvm.HackVM$Trace


import de.danielkullmann.hackvm.VMState;

public class Test3 extends TestCase {

  public void testFibonacci() {
    HackVM vm = new HackVM();
    VMState result = vm.execute("1p 48*P 1p 48*P 2p 48*P 3p 48*P 5p", "", "");
    assertNull( result.getError() );
    assertEquals( result.getOutput(), "1 1 2 3 5" );
  }
View Full Code Here


    assertNull( result.getError() );
    assertEquals( result.getOutput(), "1 1 2 3 5" );
  }

  public void testReverseList() {
    HackVM vm = new HackVM();
    //                           setup   loop loop end?      copy one
    VMState result = vm.execute("0^2v+1- 1^1^ 1^1^:0^1-*55*? <1v<2^>2^> 1v1+ 1v1- 8$!", "5, 1024", "1024=5,6,7,8,9");
    HashMap<Integer, Integer> m = HackVM.parseMemory( "1024=9,8,7,6,5" );
    assertNull( result.getError() );
    assertEquals( result.getMemory(), m );
  }
View Full Code Here

public class AddTwoAndPrint implements SolutionCheck {

  @Override
  public boolean check(String program) {
    Random r = new Random();
    HackVM vm = new HackVM();
    for (int i = 0; i < 100; i++) {
      int n1 = r.nextInt();
      int n2 = r.nextInt();
      Stack<Integer> stack = HackVM.parseStack( n1 + " " + n2 );
      VMState result = vm.execute(program, stack, null);
      if ( ! result.getOutput().equals( "" + (n1+n2) ) ) return false;
    }
    return true;
  }
View Full Code Here

  public static void main(String[] args) {
    new ReverseList().run();
  }

  private void run() {
    HackVM vm = new HackVM();
    //          setup   loop loop end?      copy one
    vm.addObserver( this );
    VMState result = vm.execute("0^2v+1- 1^1^ 1^1^:0^1-*55*? <1v<2^>2^> 1v1+ 1v1- 8$!", "5, 1024", "1024=5,6,7,8,9");
    System.out.println( result.getError() );
    System.out.println( result.getMemory() );
  }
View Full Code Here

import de.danielkullmann.hackvm.VMState;

public class Test1 extends TestCase {

  public void test1() {
    HackVM vm = new HackVM();

    VMState result = vm.execute( " ", "", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );

    result = vm.execute( "p", "1", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );
    assertTrue( result.getOutput().equals( "1" ) );

    result = vm.execute( "P", "65", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );
    assertTrue( result.getOutput().equals( "A" ) );

    for (int i = 0; i < 10; i++) {
      result = vm.execute( ""+i, "", null );
      assertNull( result.getError() );
      assertTrue( result.getStack().size() == 1 );
      assertTrue( result.getStack().pop() == i );
    }

    result = vm.execute( "+", "7, 8", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 15 );

    result = vm.execute( "-", "7, 8", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == -1 );

    result = vm.execute( "*", "7, 8", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 56 );

    result = vm.execute( "/", "6, 3", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 2 );
   
    result = vm.execute( ":", "6, 3", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 1 );
   
    result = vm.execute( ":", "3, 6", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == -1 );
   
    result = vm.execute( ":", "8, 8", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 0 );
   
    result = vm.execute( "g!1", "2", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 1 );

    result = vm.execute( "g!1", "1", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );

    result = vm.execute( "?!1", "0, 2", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 1 );

    result = vm.execute( "?!1", "1, 2", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );

    result = vm.execute( "      c1!", "8", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 6 );

    result = vm.execute( "      $1!", "8", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );

    result = vm.execute( "<", "1", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 0 );

    result = vm.execute( "><", "1, 2, 1", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 2 );
   
    result = vm.execute( "^", "4, 0", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 2 );
    assertTrue( result.getStack().pop() == 4 );
    assertTrue( result.getStack().pop() == 4 );

    result = vm.execute( "^", "1, 2, 3, 4, 2", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 5 );
    assertTrue( result.getStack().pop() == 2 );
    assertTrue( result.getStack().pop() == 4 );
    assertTrue( result.getStack().pop() == 3 );
    assertTrue( result.getStack().pop() == 2 );
    assertTrue( result.getStack().pop() == 1 );

    result = vm.execute( "v", "4, 0", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 1 );
    assertTrue( result.getStack().peek() == 4 );

    result = vm.execute( "v", "1, 2, 3, 4, 2", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 4 );
    assertTrue( result.getStack().pop() == 2 );
    assertTrue( result.getStack().pop() == 4 );
    assertTrue( result.getStack().pop() == 3 );
    assertTrue( result.getStack().pop() == 1 );
 
    result = vm.execute( "d", "1", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );

    result = vm.execute( "ddddd", "1, 2, 3, 4, 5, 6, 7, 8, 9", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 4 );
    assertTrue( result.getStack().pop() == 4 );
    assertTrue( result.getStack().pop() == 3 );
    assertTrue( result.getStack().pop() == 2 );
    assertTrue( result.getStack().pop() == 1 );

    result = vm.execute( "!11111", "", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 0 );

    result = vm.execute( "11!111", "", null );
    assertNull( result.getError() );
    assertTrue( result.getStack().size() == 2 );

  }
View Full Code Here

TOP

Related Classes of de.danielkullmann.hackvm.HackVM$Trace

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.