Add benchmarks, change project structure
This commit is contained in:
@@ -1,13 +1,30 @@
|
||||
# Project and C standard configuration
|
||||
project(bhunit LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 90)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
# Disable extensions
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
# Library code
|
||||
set(BHUNIT_SOURCE
|
||||
src/Unit.c
|
||||
)
|
||||
|
||||
set(BHUNIT_HEADER
|
||||
include/BH/Unit.h
|
||||
)
|
||||
|
||||
# Library
|
||||
add_library(BHUnit STATIC ${BHUNIT_SOURCE} ${BHUNIT_HEADER})
|
||||
target_include_directories(BHUnit PUBLIC include)
|
||||
|
||||
# Enable testing
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
|
||||
# Search files
|
||||
file(GLOB TEST_FILES "src/*.c")
|
||||
file(GLOB TEST_FILES "tests/*.c")
|
||||
|
||||
foreach(TEST_FILENAME ${TEST_FILES})
|
||||
# Add test
|
||||
|
||||
53
test/include/BH/Unit.h
Normal file
53
test/include/BH/Unit.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef BH_UNIT_H
|
||||
#define BH_UNIT_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
typedef int (*BH_UnitCallback)(void);
|
||||
|
||||
#define BH_VERIFY(e) \
|
||||
do { \
|
||||
if (!(e)) { \
|
||||
printf("%s:%d\t%s\n", __FILE__, __LINE__, #e); \
|
||||
return -1; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
#define BH_FAIL(msg) \
|
||||
do { \
|
||||
printf("%s:%d\t%s\n", __FILE__, __LINE__, msg); \
|
||||
return -1; \
|
||||
} while(0)
|
||||
|
||||
|
||||
#define BH_VERIFY_DELTA(x, y, e) \
|
||||
do { \
|
||||
double BH_VERIFY_DELTA = (x)-(y); \
|
||||
if (BH_VERIFY_DELTA < 0.0) \
|
||||
BH_VERIFY_DELTA = -BH_VERIFY_DELTA; \
|
||||
if (BH_VERIFY_DELTA > (e)) { \
|
||||
printf("%s:%d\t%s (differs by %f)\n", \
|
||||
__FILE__, __LINE__, #x " == " #y, BH_VERIFY_DELTA); \
|
||||
return -1; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
#define BH_UNIT_TEST(name) \
|
||||
static int unit##name(void)
|
||||
|
||||
|
||||
#define BH_UNIT_ADD(name) \
|
||||
BH_UnitAdd(#name, unit##name)
|
||||
|
||||
|
||||
void BH_UnitAdd(const char *name,
|
||||
BH_UnitCallback cb);
|
||||
|
||||
|
||||
int BH_UnitRun(void);
|
||||
|
||||
|
||||
#endif /* BH_UNIT_H */
|
||||
80
test/src/Unit.c
Normal file
80
test/src/Unit.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <BH/Unit.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
typedef struct BH_Unit
|
||||
{
|
||||
struct BH_Unit *next;
|
||||
const char *name;
|
||||
BH_UnitCallback cb;
|
||||
} BH_Unit;
|
||||
|
||||
|
||||
static BH_Unit *root = NULL;
|
||||
|
||||
|
||||
static void cleanup(void)
|
||||
{
|
||||
BH_Unit *current;
|
||||
|
||||
current = root;
|
||||
while (current)
|
||||
{
|
||||
BH_Unit *next = current->next;
|
||||
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BH_UnitAdd(const char *name, BH_UnitCallback cb)
|
||||
{
|
||||
BH_Unit *unit, *current;
|
||||
|
||||
/* Allocate and fill new unit test entry */
|
||||
unit = malloc(sizeof(*unit));
|
||||
if (!unit)
|
||||
return;
|
||||
|
||||
unit->next = NULL;
|
||||
unit->name = name;
|
||||
unit->cb = cb;
|
||||
|
||||
/* Append unit test entry */
|
||||
current = root;
|
||||
while (current && current->next)
|
||||
current = current->next;
|
||||
|
||||
if (current)
|
||||
current->next = unit;
|
||||
else
|
||||
root = unit;
|
||||
}
|
||||
|
||||
|
||||
int BH_UnitRun(void)
|
||||
{
|
||||
BH_Unit *current;
|
||||
int result = 0;
|
||||
printf("Running tests...\n");
|
||||
|
||||
current = root;
|
||||
while (current)
|
||||
{
|
||||
printf("%s\n", current->name);
|
||||
if (current->cb())
|
||||
{
|
||||
printf("\tFAIL\n");
|
||||
result = -1;
|
||||
}
|
||||
else
|
||||
printf("\tPASS\n");
|
||||
|
||||
fflush(stdout);
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
cleanup();
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user