<commit_before>package dr.xml.unittest;

import dr.xml.*;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <NAME>
 * @author <NAME>
 */

public class BeastUnitTest implements Reportable {

    private final String message;
    private final String actual;
    private final String expected;

    private Boolean pass;

    private final AssertType assertType;

    public BeastUnitTest(String message, String actual, String expected,
                         AssertType assertType) {
        this.message = message;
        this.actual = actual;
        this.expected = expected;
        this.assertType = assertType;
    }

    public void execute() {
        if (!assertType.equivalent(actual, expected)) {
            failCheck();
        }

        pass = true;
    }

    private void failCheck() {
        String string = formatName()
                + ": '" + actual + "' != '" + expected + "'";
        System.err.println(string);
        System.exit(-1);
    }

    private String formatName() {
        return "assert" + ((message != null) ? (" " + message) : "");
    }

    private String getPass() {
        return pass != null ?
                (pass ? "passed" : "fail") :
                "not executed";
    }

    @Override
    public String getReport() {
        return formatName() + ": " + getPass();
    }

    interface AssertType {

        boolean equivalent(String a, String b);

        class StringAssert implements AssertType {

            @Override
            public boolean equivalent(String a, String b) {
                return a.compareTo(b) == 0;
            }
        }

        class DoubleAssert implements AssertType {

            private final double tolerance;
            private final String stripChars;
            private final ToleranceType toleranceType;

            DoubleAssert(double tolerance, ToleranceType tolType, String stripChars) {
                this.tolerance = tolerance;
                this.stripChars = stripChars;
                this.toleranceType = tolType;
            }

            @Override
            public boolean equivalent(String a, String b) {

                double[] lhs = parseArray(a);
                double[] rhs = parseArray(b);

                if (lhs.length != rhs.length) {
                    return false;
                }

                for (int i = 0; i < lhs.length; ++i) {
                    if (!toleranceType.close(lhs[i], rhs[i], tolerance)) {
                        return false;
                    }
                }

                return true;
            }

            private double[] parseArray(String string) {
                string = string.replaceAll(",", " ");
                string = string.replaceAll("[" + stripChars + "]", " ");
                string = string.trim();
                String[] strings = string.split("\\s+");
                double[] reals = new double[strings.length];
                for (int i = 0; i < strings.length; ++i) {
                    reals[i] = Double.valueOf(strings[i]);<commit_msg>more informative error messages for BeastUnitTest<commit_after>package dr.xml.unittest;

import dr.xml.*;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <NAME>
 * @author <NAME>
 */

public class BeastUnitTest implements Reportable {

    private final String message;
    private final String actual;
    private final String expected;

    private Boolean pass;

    private final AssertType assertType;

    public BeastUnitTest(String message, String actual, String expected,
                         AssertType assertType) {
        this.message = message;
        this.actual = actual;
        this.expected = expected;
        this.assertType = assertType;
    }

    public void execute() {
        if (!assertType.equivalent(actual, expected)) {
            failCheck();
        }

        pass = true;
    }

    private void failCheck() {
        String string = formatName()
                + ": '" + actual + "' != '" + expected + "'";
        System.err.println(string);
        System.exit(-1);
    }

    private String formatName() {
        return "assert" + ((message != null) ? (" " + message) : "");
    }

    private String getPass() {
        return pass != null ?
                (pass ? "passed" : "fail") :
                "not executed";
    }

    @Override
    public String getReport() {
        return formatName() + ": " + getPass();
    }

    interface AssertType {

        boolean equivalent(String a, String b);

        class StringAssert implements AssertType {

            @Override
            public boolean equivalent(String a, String b) {
                return a.compareTo(b) == 0;
            }
        }

        class DoubleAssert implements AssertType {

            private final double tolerance;
            private final String stripChars;
            private final ToleranceType toleranceType;

            DoubleAssert(double tolerance, ToleranceType tolType, String stripChars) {
                this.tolerance = tolerance;
                this.stripChars = stripChars;
                this.toleranceType = tolType;
            }

            @Override
            public boolean equivalent(String a, String b) {

                double[] lhs = parseArray(a);
                double[] rhs = parseArray(b);

                if (lhs.length != rhs.length) {
                    System.err.println("The dimensions of the \"actual\" and \"expected\" values are not the same.");
                    return false;
                }

                for (int i = 0; i < lhs.length; ++i) {
                    if (!toleranceType.close(lhs[i], rhs[i], tolerance)) {
                        System.err.println("Dimension " + (i + 1) + " of \"actual\" does not match \" expected\". (" + lhs[i] + " != " + rhs[i] + ")");
                        return false;
                    }
                }

                return true;
            }

            private double[] parseArray(String string) {
                string = string.replaceAll(",", " ");
                string = string.replaceAll("[" + stripChars + "]", " ");
                string = string.trim();
                String[] strings = string.split("\\s+");
                double[] reals = new double[strings.length];
                for (int i = 0; i < strings.length; ++i) {
                    reals[i] = Double.valueOf(strings[i]);
{'fraction_non_alphanumeric': 0.11204782342121399, 'fraction_numerical': 0.0013795217657878603, 'mean_word_length': 1.6268115942028984, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7305684', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1593, 'n_words': 445}