Added assertion & test statistics
This commit is contained in:
parent
b25ad2001b
commit
2e2b2bc2df
5 changed files with 85 additions and 8 deletions
62
include/assert.h
Normal file
62
include/assert.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
#ifndef CRITERION_ASSERT_H_
|
||||
# define CRITERION_ASSERT_H_
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <stdbool.h>
|
||||
# include "criterion.h"
|
||||
|
||||
enum criterion_assert_kind {
|
||||
NORMAL,
|
||||
FATAL
|
||||
};
|
||||
|
||||
struct criterion_assert_stat {
|
||||
enum criterion_assert_kind kind;
|
||||
const char *condition;
|
||||
const char *message;
|
||||
bool passed;
|
||||
unsigned line;
|
||||
|
||||
struct criterion_assert_stat *next;
|
||||
};
|
||||
|
||||
struct criterion_test_stats {
|
||||
struct criterion_test *test;
|
||||
struct criterion_assert_stat *asserts;
|
||||
unsigned failed;
|
||||
unsigned passed;
|
||||
};
|
||||
|
||||
extern struct criterion_test_stats g_current_test_stats;
|
||||
|
||||
# define assertImpl(Kind, Condition, ...) \
|
||||
do { \
|
||||
int passed = !!(Condition); \
|
||||
struct criterion_assert_stat *stat = \
|
||||
malloc(sizeof (struct criterion_assert_stat)); \
|
||||
*stat = (struct criterion_assert_stat) { \
|
||||
.kind = Kind, \
|
||||
.condition = #Condition, \
|
||||
.message = "" __VA_ARGS__, \
|
||||
.passed = passed, \
|
||||
.line = __LINE__, \
|
||||
.next = g_current_test_stats.asserts, \
|
||||
}; \
|
||||
g_current_test_stats.asserts = stat; \
|
||||
if (passed) \
|
||||
++g_current_test_stats.passed; \
|
||||
else \
|
||||
++g_current_test_stats.failed; \
|
||||
if (!passed && Kind == FATAL) \
|
||||
return; \
|
||||
} while (0)
|
||||
|
||||
# define expect(Condition, ...) assertImpl(NORMAL, Condition, __VA_ARGS__)
|
||||
# define assert(Condition, ...) assertImpl(FATAL, Condition, __VA_ARGS__)
|
||||
|
||||
# define assertArrayEquals(A, B, Size, ...) \
|
||||
assertImpl(NORMAL, !memcmp((A), (B), Size), __VA_ARGS__)
|
||||
# define expectArrayEquals(A, B, Size, ...) \
|
||||
assertImpl(FATAL, !memcmp((A), (B), Size), __VA_ARGS__)
|
||||
|
||||
#endif /* !CRITERION_ASSERT_H_ */
|
|
@ -24,8 +24,10 @@
|
|||
#ifndef CRITERION_H_
|
||||
# define CRITERION_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include "common.h"
|
||||
# include <stdbool.h>
|
||||
# include <stddef.h>
|
||||
# include "common.h"
|
||||
# include "assert.h"
|
||||
|
||||
struct criterion_test_extra_data {
|
||||
size_t sentinel_;
|
||||
|
|
|
@ -2,12 +2,15 @@
|
|||
#include <stdlib.h>
|
||||
#include "criterion.h"
|
||||
#include "report.h"
|
||||
|
||||
Test(misc, simple) {
|
||||
}
|
||||
#include "assert.h"
|
||||
|
||||
Test(misc, failing) {
|
||||
exit(1);
|
||||
assert(1);
|
||||
assert(0);
|
||||
}
|
||||
|
||||
Test(misc, simple) {
|
||||
expect(1);
|
||||
}
|
||||
|
||||
ReportHook(PRE_INIT) {
|
||||
|
|
|
@ -28,7 +28,11 @@ ReportHook(PRE_INIT) {
|
|||
}
|
||||
|
||||
ReportHook(POST_TEST) {
|
||||
struct criterion_test_stats *stats = data;
|
||||
|
||||
int success = stats->failed == 0;
|
||||
|
||||
printf("%s\n", success ? "SUCCESS" : "FAILURE");
|
||||
}
|
||||
|
||||
ReportHook(PRE_TEST) {}
|
||||
|
|
10
src/runner.c
10
src/runner.c
|
@ -27,6 +27,7 @@
|
|||
#include <stdio.h>
|
||||
#include "runner.h"
|
||||
#include "report.h"
|
||||
#include "assert.h"
|
||||
|
||||
static struct criterion_test * const g_section_start = &__start_criterion_tests;
|
||||
static struct criterion_test * const g_section_end = &__stop_criterion_tests;
|
||||
|
@ -38,14 +39,19 @@ static void map_tests(void (*fun)(struct criterion_test *)) {
|
|||
}
|
||||
|
||||
__attribute__ ((always_inline))
|
||||
static inline void nothing(void) {}
|
||||
static inline void nothing() {}
|
||||
|
||||
struct criterion_test_stats g_current_test_stats;
|
||||
|
||||
static void run_test_nofork(struct criterion_test *test) {
|
||||
report(PRE_INIT, test);
|
||||
(test->data->init ?: nothing)();
|
||||
report(PRE_TEST, test);
|
||||
|
||||
g_current_test_stats = (struct criterion_test_stats) { .test = test };
|
||||
(test->test ?: nothing)();
|
||||
report(POST_TEST, test);
|
||||
|
||||
report(POST_TEST, &g_current_test_stats);
|
||||
(test->data->fini ?: nothing)();
|
||||
report(POST_FINI, test);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue