Package de.danielkullmann.hackvm

Examples of de.danielkullmann.hackvm.HackVM$Command


    /**
     * Create an instance of {@link Command }
     *
     */
    public Command createCommand() {
        return new Command();
    }
View Full Code Here


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

        } else {
          throw new IOException("Unexpected character");
        }
      }
      try {
        out.add(new Command(bytes));
      } finally {
        bytes = null;
        arguments = 0;
      }
    } else if (in.readByte() == '*') {
      long l = readLong(in);
      if (l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("Java only supports arrays up to " + Integer.MAX_VALUE + " in size");
      }
      int numArgs = (int) l;
      if (numArgs < 0) {
        throw new RedisException("Invalid size: " + numArgs);
      }
      bytes = new byte[numArgs][];
      checkpoint();
      decode(ctx, in, out);
    } else {
      // Go backwards one
      in.readerIndex(in.readerIndex() - 1);
      // Read command -- can't be interupted
      byte[][] b = new byte[1][];
      b[0] = in.readBytes(in.bytesBefore((byte) '\r')).array();
      in.skipBytes(2);
      out.add(new Command(b, true));
    }
  }
View Full Code Here

        boolean isOff = _hasTrueInput(off);
        boolean isOn = _hasTrueInput(on);

        if ((brightLevel >= 0) && (brightLevel <= 100)) {
            _transmit(new Command((_destination), x10.Command.BRIGHT,
                    brightLevel));
        }

        if ((dimLevel >= 0) && (dimLevel <= 100)) {
            _transmit(new Command((_destination), x10.Command.DIM, dimLevel));
        }

        if (isOn) {
            _transmit(new Command((_destination), x10.Command.ON));
        }

        if (isOff) {
            _transmit(new Command((_destination), x10.Command.OFF));
        }
    }
View Full Code Here

    public void fire() throws IllegalActionException {
        super.fire();

        // Check whether a command is ready.
        if (_commandReady()) {
            Command command = _getCommand();
            receivedCommand.send(0, new StringToken(_commandToString(command)));
        } else {
            receivedCommand.send(0, _EMPTY_STRING);
        }
    }
View Full Code Here

    public void fire() throws IllegalActionException {
        super.fire();

        // Check whether a command is ready
        if (_commandReady()) {
            Command sensedCommand = _getCommand();
            byte function = sensedCommand.getFunctionByte();
            byte functionOfInterest = Command.BRIGHT;
            String commandValue = command.stringValue();

            if (!commandValue.equals("BRIGHT")) {
                functionOfInterest = Command.DIM;
            }

            String sensedHouseCode = "" + sensedCommand.getHouseCode();
            int sensedUnitCode = sensedCommand.getUnitCode();

            String houseCodeValue = houseCode.stringValue();
            int unitCodeValue = ((IntToken) unitCode.getToken()).intValue();

            if (sensedHouseCode.equals(houseCodeValue)
                    && (sensedUnitCode == unitCodeValue)
                    && (function == functionOfInterest)) {
                level.send(0, new IntToken(sensedCommand.getLevel()));
            } else {
                level.send(0, _NO_COMMAND_TOKEN);
            }
        } else {
            level.send(0, _NO_COMMAND_TOKEN);
View Full Code Here

TOP

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

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.