aboutsummaryrefslogtreecommitdiff
path: root/test/test_framework.c
blob: d56694427464c743cc02618149cb99fad7528771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdatomic.h>
#include <stdint.h>
#include "test_framework.h"


extern atomic_flag test_framework_assertion_failed;


void test_report_error(
		const char *type, const char *condition, const char *fname, int lineno) {
	printf("\x1B[31;1m%s:%d: Assertion failed:\n  %s(%s)\x1B[0m\n",
			fname, lineno, type, condition);
	atomic_flag_test_and_set(&test_framework_assertion_failed);
}

void print_buffer(FILE *stream, const void *buffer_, size_t length) {
	const uint8_t *buffer = (const uint8_t*)buffer_;
	fputc('"', stream);

	for (size_t i = 0; i < length; i++) {
		const uint8_t b = buffer[i];
		if (b == '"') fprintf(stream, "\\\"");
		else if (b == '\\') fprintf(stream, "\\\\");
		else if (32 <= b && b < 127) fputc(b, stream);
		else {
			fprintf(stream, "\\x%c%c",
					"0123456789abcdef"[b >> 4],
					"0123456789abcdef"[b & 0xf]);
		}
	}

	fprintf(stream, "\"[%zu]", length);
}