<commit_before>/******************************************************************************/
/**
@file
@author		<NAME>
@brief		A simple unit testing framework that emits JSON
			as well as human readable results, optionally.
@details	For more information, please see @ref planck_unit.h.
@copyright	Copyright 2015 <NAME>
@license	Licensed under the Apache License, Version 2.0 (the "License");
			you may not use this file except in compliance with the License.
			You may obtain a copy of the License at
			http://www.apache.org/licenses/LICENSE-2.0

@par
			Unless required by applicable law or agreed to in writing,
			software distributed under the License is distributed on an
			"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
			either express or implied. See the License for the specific
			language governing permissions and limitations under the
			License.
*/
/******************************************************************************/

#include "planck_unit.h"

jmp_buf planck_unit_longjmp_env;

/**
@brief		If possible, flush all output so far.
*/
#define PLANCK_UNIT_FLUSH fflush(stdout);

/**
@brief		Print a string.
@param		s
			A null-terminated character string (a pointer
			to the character array describing the string).
*/
#define PLANCK_UNIT_PRINT_STR(s) printf(s);PLANCK_UNIT_FLUSH;

/**
@brief		Print a comma.
*/
#define PLANCK_UNIT_PRINT_COMMA		printf(",");PLANCK_UNIT_FLUSH;

/**
@brief		Print a newline.
*/
#define PLANCK_UNIT_PRINT_NEWLINE	printf("\n");PLANCK_UNIT_FLUSH;

/* Output style depends on build-time arguments. For now, these will be in @ref planck_unit.h. */
#if defined(PLANCK_UNIT_OUTPUT_STYLE_JSON)

void
planck_unit_print_result_json(
	planck_unit_test_t *state
) {
	printf("{\"error_at_line\":%d,\"file\":\"%s\",\"test name\":\"%s\",\"time\":\"%lu\",\"message\":\"%s\"}", state->line, state->file, state->base_name, state->total_time, state->message);
	PLANCK_UNIT_FLUSH;

	if (NULL != state->next) {
		PLANCK_UNIT_PRINT_COMMA;
	}
}

void
planck_unit_print_preamble_json(
	planck_unit_suite_t *suite
) {
	PLANCK_UNIT_PRINT_STR("{\"results\":[");
}

void
planck_unit_print_postamble_json(
	planck_unit_suite_t *suite
) {
	printf("],\"total_tests\":%d,\"total_passed\":%d}\n", suite->total_tests, suite->total_passed);
	PLANCK_UNIT_FLUSH;
}

planck_unit_print_funcs_t planck_unit_print_funcs_json = {
	planck_unit_print_result_json, planck_unit_print_preamble_json, planck_unit_print_postamble_json
};

#endif
#if defined(PLANCK_UNIT_OUTPUT_STYLE_HUMAN)

void
planck_unit_print_result_human(
	planck_unit_test_t *state
) {
	if (PLANCK_UNIT_SUCCESS == state->result) {
		return;
	}

	printf("FAILURE: in function '%s', at %s:%d: %s, time: %lu ms\n", state->base_name, state->file, state->line, state->message, state->total_time);
}

void
planck_unit_print_preamble_none(
	planck_unit_suite_t *suite
) {
	/* Mark this param unused */
	(void) suite;
}

void
planck_unit_print_postamble_summary(
	planck_unit_suite_t *suite
) {
	planck_unit_test_t *state;

	PLANCK_UNIT_PRINT_STR("\n");
	state = suite->head;

	while (NULL != state) {
		if (PLANCK_UNIT_SUCCESS == state->result) {
			PLANCK_UNIT_PRINT_STR("*");
		}
		else {
			PLANCK_UNIT_PRINT_STR("F");
		}

		state = state->next;
	}

	printf("\n\nTotal Passed:\t%d\n", suite->total_passed);
	PLANCK_UNIT_FLUSH;

	printf("Total Tests:\t%d\n", suite->total_tests);
	PLANCK_UNIT_FLUSH;
}

planck_unit_print_funcs_t planck_unit_print_funcs_human = {
	planck_unit_print_result_human, planck_unit_print_preamble_none, planck_unit_print_postamble_summary
};

#endif
#if defined(PLANCK_UNIT_OUTPUT_STYLE_XML)

void
planck_unit_print_result_xml(
	planck_unit_test_t *state
) {
	printf("<test>name:\"");
	printf("%s", state->base_name);
	printf("\",line:\"");
	printf("%d", state->line);
	printf("\",file:\"");
	printf("%s", state->file);
	printf("\",time:\"");
	printf("%lu", state->total_time);
	printf("\",message:\"");
	printf("%s", state->message);
	printf("\"</test>\n");
	PLANCK_UNIT_FLUSH;
}

void
planck_unit_print_preamble_xml(
	planck_unit_suite_t *suite
) {
	planck_unit_test_t	*state;
	int					test_count = 0;

	PLANCK_UNIT_PRINT_STR("<planckmeta>\n");

	state = suite->head;

	while (NULL != state) {
		test_count++;
		printf("<testname>");
		printf("%s", state->base_name);
		printf("</testname>\n");
		PLANCK_UNIT_FLUSH;

		state = state->next;
	}

	printf("<testcount>");
	printf("%d", test_count);
	printf("</testcount>\n");
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("</planckmeta>\n");
	PLANCK_UNIT_FLUSH;
}

void
planck_unit_print_postamble_xml(
	planck_unit_suite_t *suite
) {
	printf("<summary>total_tests:\"");
	printf("%d", suite->total_tests);
	printf("\",total_passed:\"");
	printf("%d", suite->total_passed);
	printf("\"</summary>\n");
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("</suite>\n");
	PLANCK_UNIT_FLUSH;
}

planck_unit_print_funcs_t planck_unit_print_funcs_xml = {
	planck_unit_print_result_xml, planck_unit_print_preamble_xml, planck_unit_print_postamble_xml
};

#endif

#if defined(PLANCK_UNIT_OUTPUT_STYLE_CONCISE)

void
planck_unit_print_result_concise(
	planck_unit_test_t *state
) {
	printf("[");
	printf("%s", state->base_name);
	printf(": ");<commit_msg>Add fflush statements after all printf<commit_after>/******************************************************************************/
/**
@file
@author		<NAME>
@brief		A simple unit testing framework that emits JSON
			as well as human readable results, optionally.
@details	For more information, please see @ref planck_unit.h.
@copyright	Copyright 2015 <NAME>
@license	Licensed under the Apache License, Version 2.0 (the "License");
			you may not use this file except in compliance with the License.
			You may obtain a copy of the License at
			http://www.apache.org/licenses/LICENSE-2.0

@par
			Unless required by applicable law or agreed to in writing,
			software distributed under the License is distributed on an
			"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
			either express or implied. See the License for the specific
			language governing permissions and limitations under the
			License.
*/
/******************************************************************************/

#include "planck_unit.h"

jmp_buf planck_unit_longjmp_env;

/**
@brief		If possible, flush all output so far.
*/
#define PLANCK_UNIT_FLUSH fflush(stdout);

/**
@brief		Print a string.
@param		s
			A null-terminated character string (a pointer
			to the character array describing the string).
*/
#define PLANCK_UNIT_PRINT_STR(s) printf(s);PLANCK_UNIT_FLUSH;

/**
@brief		Print a comma.
*/
#define PLANCK_UNIT_PRINT_COMMA		printf(",");PLANCK_UNIT_FLUSH;

/**
@brief		Print a newline.
*/
#define PLANCK_UNIT_PRINT_NEWLINE	printf("\n");PLANCK_UNIT_FLUSH;

/* Output style depends on build-time arguments. For now, these will be in @ref planck_unit.h. */
#if defined(PLANCK_UNIT_OUTPUT_STYLE_JSON)

void
planck_unit_print_result_json(
	planck_unit_test_t *state
) {
	printf("{\"error_at_line\":%d,\"file\":\"%s\",\"test name\":\"%s\",\"time\":\"%lu\",\"message\":\"%s\"}", state->line, state->file, state->base_name, state->total_time, state->message);
	PLANCK_UNIT_FLUSH;

	if (NULL != state->next) {
		PLANCK_UNIT_PRINT_COMMA;
	}
}

void
planck_unit_print_preamble_json(
	planck_unit_suite_t *suite
) {
	PLANCK_UNIT_PRINT_STR("{\"results\":[");
}

void
planck_unit_print_postamble_json(
	planck_unit_suite_t *suite
) {
	printf("],\"total_tests\":%d,\"total_passed\":%d}\n", suite->total_tests, suite->total_passed);
	PLANCK_UNIT_FLUSH;
}

planck_unit_print_funcs_t planck_unit_print_funcs_json = {
	planck_unit_print_result_json, planck_unit_print_preamble_json, planck_unit_print_postamble_json
};

#endif
#if defined(PLANCK_UNIT_OUTPUT_STYLE_HUMAN)

void
planck_unit_print_result_human(
	planck_unit_test_t *state
) {
	if (PLANCK_UNIT_SUCCESS == state->result) {
		return;
	}

	printf("FAILURE: in function '%s', at %s:%d: %s, time: %lu ms\n", state->base_name, state->file, state->line, state->message, state->total_time);
}

void
planck_unit_print_preamble_none(
	planck_unit_suite_t *suite
) {
	/* Mark this param unused */
	(void) suite;
}

void
planck_unit_print_postamble_summary(
	planck_unit_suite_t *suite
) {
	planck_unit_test_t *state;

	PLANCK_UNIT_PRINT_STR("\n");
	state = suite->head;

	while (NULL != state) {
		if (PLANCK_UNIT_SUCCESS == state->result) {
			PLANCK_UNIT_PRINT_STR("*");
		}
		else {
			PLANCK_UNIT_PRINT_STR("F");
		}

		state = state->next;
	}

	printf("\n\nTotal Passed:\t%d\n", suite->total_passed);
	PLANCK_UNIT_FLUSH;

	printf("Total Tests:\t%d\n", suite->total_tests);
	PLANCK_UNIT_FLUSH;
}

planck_unit_print_funcs_t planck_unit_print_funcs_human = {
	planck_unit_print_result_human, planck_unit_print_preamble_none, planck_unit_print_postamble_summary
};

#endif
#if defined(PLANCK_UNIT_OUTPUT_STYLE_XML)

void
planck_unit_print_result_xml(
	planck_unit_test_t *state
) {
	PLANCK_UNIT_PRINT_STR("<test>name:\"");
	printf("%s", state->base_name);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\",line:\"");
	printf("%d", state->line);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\",file:\"");
	printf("%s", state->file);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\",time:\"");
	printf("%lu", state->total_time);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\",message:\"");
	printf("%s", state->message);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\"</test>\n");
}

void
planck_unit_print_preamble_xml(
	planck_unit_suite_t *suite
) {
	planck_unit_test_t	*state;
	int					test_count = 0;

	PLANCK_UNIT_PRINT_STR("<planckmeta>\n");

	state = suite->head;

	while (NULL != state) {
		test_count++;
		PLANCK_UNIT_PRINT_STR("<testname>");
		printf("%s", state->base_name);
		PLANCK_UNIT_FLUSH;
		PLANCK_UNIT_PRINT_STR("</testname>\n");

		state = state->next;
	}

	PLANCK_UNIT_PRINT_STR("<testcount>");
	printf("%d", test_count);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("</testcount>\n");
	PLANCK_UNIT_PRINT_STR("</planckmeta>\n");
}

void
planck_unit_print_postamble_xml(
	planck_unit_suite_t *suite
) {
	PLANCK_UNIT_PRINT_STR("<summary>total_tests:\"");
	printf("%d", suite->total_tests);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\",total_passed:\"");
	printf("%d", suite->total_passed);
	PLANCK_UNIT_FLUSH;
	PLANCK_UNIT_PRINT_STR("\"</summary>\n");
	PLANCK_UNIT_PRINT_STR("</suite>\n");
}

planck_unit_print_funcs_t planck_unit_print_funcs_xml = {
	planck_unit_print_result_xml, planck_unit_print_preamble_xml, planck_unit_print_postamble_xml
};

#endif

#if defined(PLANCK_UNIT_OUTPUT_STYLE_CONCISE)

void
planck_unit_print_result_concise(
	planck_unit_test_t *state
) {
	printf("[");
	printf("%s", state->base_name);
	printf(": ");
{'fraction_non_alphanumeric': 0.16741530975107619, 'fraction_numerical': 0.0016844469399213925, 'mean_word_length': 5.957682291666667, 'pattern_counts': {'":': 18, '<': 29, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, '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': '5350292', 'n_tokens_mistral': 4200, 'n_tokens_neox': 4027, 'n_words': 740}