<commit_before>#include <cgreen/assertions.h>
#include <cgreen/boxed_double.h>
#include <cgreen/constraint_syntax_helpers.h>
#include <cgreen/message_formatting.h>
#include <cgreen/reporter.h>
#include <cgreen/string_comparison.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
namespace cgreen {
#endif

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

static double accuracy(int significant_figures, double largest);

static int significant_figures = 8;

void assert_that_(const char *file, int line, const char *actual_string, intptr_t actual, Constraint* constraint) {
    if (NULL != constraint && is_not_comparing(constraint)) {
        (*get_test_reporter()->assert_true)(
                get_test_reporter(),
                file,
                line,
                false,
                "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.",
                constraint->name);

        constraint->destroy(constraint);

        return;
    }

	char message[512] = {'\0'};

	if (parameters_are_not_valid_for(constraint, actual)) {
		format_validation_failure_message_for(message, sizeof(message), "", constraint, actual);

	    (*get_test_reporter()->assert_true)(
	    		get_test_reporter(),
	            file,
	            line,
	            false,
	            message);
		return;
	}

	format_expectation_failure_message_for(message, sizeof(message) - 1,
            constraint, actual_string, actual);

    (*get_test_reporter()->assert_true)(
        get_test_reporter(),
        file,
        line,
        (*constraint->compare)(constraint, actual),
        message
    );
  
    constraint->destroy(constraint);
}

void assert_that_double_(const char *file, int line, const char *actual_string, double actual, Constraint* constraint) {
    if (NULL != constraint && is_not_comparing(constraint)) {
        (*get_test_reporter()->assert_true)(
                get_test_reporter(),
                file,
                line,
                false,
                "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.",
                constraint->name);

        constraint->destroy(constraint);

        return;
    }

    BoxedDouble* boxed_actual = (BoxedDouble*)box_double(actual);

    (*get_test_reporter()->assert_true)(get_test_reporter(), file, line, (*constraint->compare)(constraint, (intptr_t)boxed_actual),
            "Expected [%s] to [%s] [%s] within [%d] significant figures\n"
    		"\t\tactual value:\t%08f\n"
    		"\t\texpected value:\t%08f",
            actual_string,
            constraint->name,
            constraint->expected_value_name,
            significant_figures,
            actual,
            as_double(constraint->expected_value));

    free(boxed_actual);
    constraint->destroy(constraint);
}

void assert_equal_(const char *file, int line, intptr_t tried, intptr_t expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            (tried == expected),
            "[%d] should match [%d]", tried, expected);
}

void assert_not_equal_(const char *file, int line, intptr_t tried, intptr_t expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            (tried != expected),
            "[%d] should not match [%d]", tried, expected);
}

void assert_double_equal_(const char *file, int line, double tried, double expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            doubles_are_equal(tried, expected),
            "[%f] should match [%f] within %d significant figures", tried, expected, significant_figures);
}

void assert_double_not_equal_(const char *file, int line, double tried, double expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            ! doubles_are_equal(tried, expected),
            "[%f] should not match [%f] within %d significant figures", tried, expected, significant_figures);
}

void assert_string_equal_(const char *file, int line, const char *tried, const char *expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            strings_are_equal(tried, expected),
            "[%s] should match [%s]", show_null_as_the_string_null(tried), show_null_as_the_string_null(expected));
}

void assert_string_not_equal_(const char *file, int line, const char *tried, const char *expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            ! strings_are_equal(tried, expected),
            "[%s] should not match [%s]", show_null_as_the_string_null(tried), show_null_as_the_string_null(expected));
}

void significant_figures_for_assert_double_are(int figures) {
    significant_figures = figures;
}

const char *show_null_as_the_string_null(const char *string) {
    return (string == NULL ? "NULL" : string);
}

int doubles_are_equal(double tried, double expected) {
    return max(tried, expected) - min(tried, expected) < accuracy(significant_figures, max(tried, expected));
}

static double accuracy(int figures, double largest) {
    return pow(10, 1 + (int)log10(largest) - figures);
}

#ifdef __cplusplus
} // namespace cgreen
#endif

/* vim: set ts=4 sw=4 et cindent: */<commit_msg>Clarified which was the actual and expected values in assert message<commit_after>#include <cgreen/assertions.h>
#include <cgreen/boxed_double.h>
#include <cgreen/constraint_syntax_helpers.h>
#include <cgreen/message_formatting.h>
#include <cgreen/reporter.h>
#include <cgreen/string_comparison.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
namespace cgreen {
#endif

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

static double accuracy(int significant_figures, double largest);

static int significant_figures = 8;

void assert_that_(const char *file, int line, const char *actual_string, intptr_t actual, Constraint* constraint) {
    if (NULL != constraint && is_not_comparing(constraint)) {
        (*get_test_reporter()->assert_true)(
                get_test_reporter(),
                file,
                line,
                false,
                "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.",
                constraint->name);

        constraint->destroy(constraint);

        return;
    }

	char message[512] = {'\0'};

	if (parameters_are_not_valid_for(constraint, actual)) {
		format_validation_failure_message_for(message, sizeof(message), "", constraint, actual);

	    (*get_test_reporter()->assert_true)(
	    		get_test_reporter(),
	            file,
	            line,
	            false,
	            message);
		return;
	}

	format_expectation_failure_message_for(message, sizeof(message) - 1,
            constraint, actual_string, actual);

    (*get_test_reporter()->assert_true)(
        get_test_reporter(),
        file,
        line,
        (*constraint->compare)(constraint, actual),
        message
    );
  
    constraint->destroy(constraint);
}

void assert_that_double_(const char *file, int line, const char *actual_string, double actual, Constraint* constraint) {
    if (NULL != constraint && is_not_comparing(constraint)) {
        (*get_test_reporter()->assert_true)(
                get_test_reporter(),
                file,
                line,
                false,
                "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.",
                constraint->name);

        constraint->destroy(constraint);

        return;
    }

    BoxedDouble* boxed_actual = (BoxedDouble*)box_double(actual);

    (*get_test_reporter()->assert_true)(get_test_reporter(), file, line, (*constraint->compare)(constraint, (intptr_t)boxed_actual),
            "Expected [%s] to [%s] [%s] within [%d] significant figures\n"
    		"\t\tactual value:\t%08f\n"
    		"\t\texpected value:\t%08f",
            actual_string,
            constraint->name,
            constraint->expected_value_name,
            significant_figures,
            actual,
            as_double(constraint->expected_value));

    free(boxed_actual);
    constraint->destroy(constraint);
}

void assert_equal_(const char *file, int line, intptr_t tried, intptr_t expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            (tried == expected),
            "[%d] should be [%d]", tried, expected);
}

void assert_not_equal_(const char *file, int line, intptr_t tried, intptr_t expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            (tried != expected),
            "[%d] should not be [%d]", tried, expected);
}

void assert_double_equal_(const char *file, int line, double tried, double expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            doubles_are_equal(tried, expected),
            "[%f] should be [%f] within %d significant figures", tried, expected, significant_figures);
}

void assert_double_not_equal_(const char *file, int line, double tried, double expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            ! doubles_are_equal(tried, expected),
            "[%f] should not be [%f] within %d significant figures", tried, expected, significant_figures);
}

void assert_string_equal_(const char *file, int line, const char *tried, const char *expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            strings_are_equal(tried, expected),
            "[%s] should be [%s]", show_null_as_the_string_null(tried), show_null_as_the_string_null(expected));
}

void assert_string_not_equal_(const char *file, int line, const char *tried, const char *expected) {
    (*get_test_reporter()->assert_true)(
            get_test_reporter(),
            file,
            line,
            ! strings_are_equal(tried, expected),
            "[%s] should not be [%s]", show_null_as_the_string_null(tried), show_null_as_the_string_null(expected));
}

void significant_figures_for_assert_double_are(int figures) {
    significant_figures = figures;
}

const char *show_null_as_the_string_null(const char *string) {
    return (string == NULL ? "NULL" : string);
}

int doubles_are_equal(double tried, double expected) {
    return max(tried, expected) - min(tried, expected) < accuracy(significant_figures, max(tried, expected));
}

static double accuracy(int figures, double largest) {
    return pow(10, 1 + (int)log10(largest) - figures);
}

#ifdef __cplusplus
} // namespace cgreen
#endif

/* vim: set ts=4 sw=4 et cindent: */
{'fraction_non_alphanumeric': 0.12203148625811616, 'fraction_numerical': 0.003024103886862937, 'mean_word_length': 2.867905056759546, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3551517', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3334, 'n_words': 790}