Example #1
0
  private static boolean typecheck(String test) {
    String[] parts = test.split("=>");
    String code = parts[0];
    boolean expectSuccess = Boolean.parseBoolean(parts[1]);

    Reader r = new StringReader(code);

    try {
      CMMTokenizer t = new CMMTokenizer(r);
      CMMParser p = new CMMParser(t);
      CMMASTNode n = p.parse();
      // n.accept(, null);

      //// TODO:
      if (false) {
        throw new RuntimeException("Type checking failed.");
      }

      // Retrieve the ouput ?
      String progOutput = output.toString().trim(); // .replaceAll("\n", "");
      output.delete(0, output.length());

      if (expectSuccess) {
        return true;
      } else {
        System.err.println(
            "For test (" + test + "), typechecking was succesful, while typing is incorrect.");
        System.err.println("Program output: " + progOutput);
        System.err.println();
        return false;
      }
    } catch (ExitTrappedException e) {
      System.err.println("Exception for test: " + test);
      System.err.println(e.getClass() + " - " + e.getMessage());

      if (expectSuccess) {
        System.err.println("TEST FAILED!");
      } else {
        System.err.println("Test succesful.");
      }

      System.err.println();
      return !expectSuccess;
    } catch (Exception e) {
      System.err.println("Exception for test: " + test);
      System.err.println(e.getClass() + " - " + e.getMessage());

      if (expectSuccess) {
        System.err.println("TEST FAILED!");
      } else {
        System.err.println("Test succesful.");
      }

      System.err.println();
      return !expectSuccess;
    }
  }
Example #2
0
  private static boolean test(String test) {
    String[] parts = test.split("=>");
    String code = parts[0];
    String expectedResult = parts[1];

    Reader r = new StringReader(code);

    try {
      CMMTokenizer t = new CMMTokenizer(r);
      CMMParser p = new CMMParser(t);
      CMMASTNode n = p.parse();
      n.accept(new CMMInterpreterVisitor(), new CMMEnvironment());

      // Retrieve the ouput
      String progOutput = output.toString().trim().replaceAll("\n", "").replaceAll("\"", "");
      output.delete(0, output.length());

      if (expectedResult.equals(progOutput)) {
        return true;
      } else {
        if (expectedResult.contains("+-")) {
          String[] parts2 = expectedResult.split("\\+-");
          double expected = java.lang.Double.parseDouble(parts2[0]);
          double deviation = java.lang.Double.parseDouble(parts2[1]);

          double actualResult = java.lang.Double.parseDouble(progOutput);

          if (Math.abs(expected - actualResult) < deviation) {
            return true;
          }
        }

        System.err.println(
            "For test ("
                + test
                + "), program output ("
                + progOutput
                + ") did not match expected output ("
                + expectedResult
                + ")");
        System.err.println();
        return false;
      }
    } catch (ExitTrappedException e) {
      System.err.println("ExitTrappedException for test: " + test);
      System.err.println();
      return false;
    } catch (Exception e) {
      System.err.println("Exception for test: " + test);
      System.err.println(e.getClass() + " - " + e.getMessage());
      System.err.println();
      return false;
    }
  }