You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 line
2.2 KiB

  1. package interpreter;
  2. import expression.Addition;
  3. import expression.Identifier;
  4. import expression.Int;
  5. import expression.Subtraction;
  6. import org.junit.Test;
  7. import program.Assignment;
  8. import program.Composition;
  9. import program.Loop;
  10. import program.Program;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import static org.junit.Assert.assertEquals;
  14. public class InterpreterTest {
  15. @Test
  16. public void testSem() {
  17. Program initialization = new Composition(
  18. new Composition(
  19. new Assignment(new Identifier("a"), new Int(2)),
  20. new Assignment(new Identifier("b"), new Int(4))),
  21. new Assignment(new Identifier("r"), new Int(0)));
  22. Program body = new Composition(
  23. new Assignment(new Identifier("r"), new Addition(new Identifier("r"), new Identifier("b"))),
  24. new Assignment(new Identifier("a"), new Subtraction(new Identifier("a"), new Int(1))));
  25. Program loop = new Loop(new Identifier("a"), body);
  26. Program program = new Composition(initialization, loop);
  27. Interpreter interpreter = new Interpreter(program);
  28. Map<String, Integer> valuation = interpreter.getValuation();
  29. assertEquals(8, valuation.get("r").intValue());
  30. assertEquals(0, valuation.get("a").intValue());
  31. assertEquals(4, valuation.get("b").intValue());
  32. }
  33. @Test
  34. public void testSemWithValuation() {
  35. Map<String, Integer> valuation = new HashMap<>();
  36. valuation.put("a", 2);
  37. valuation.put("b", 4);
  38. valuation.put("r", 0);
  39. Program body = new Composition(
  40. new Assignment(new Identifier("r"), new Addition(new Identifier("r"), new Identifier("b"))),
  41. new Assignment(new Identifier("a"), new Subtraction(new Identifier("a"), new Int(1))));
  42. Program loop = new Loop(new Identifier("a"), body);
  43. Interpreter interpreter = new Interpreter(loop, valuation);
  44. valuation = interpreter.getValuation();
  45. assertEquals(8, valuation.get("r").intValue());
  46. assertEquals(0, valuation.get("a").intValue());
  47. assertEquals(4, valuation.get("b").intValue());
  48. }
  49. }