Commit 8ccf53d6 authored by Chris Müller's avatar Chris Müller
Browse files

add unittest modules for tdd development in crystal

parent fe26cd2d
cmake_minimum_required(VERSION 2.8)
project(crystal)
include(${PROJECT_SOURCE_DIR}/macros/standard.cmake)
include_directories(src)
add_subdirectory(src)
add_subdirectory(test)
......@@ -4,11 +4,13 @@ set(VERSION "0.1")
set(SOURCES
matrix.c
unittest.c
)
set(HEADER
standard.h
matrix.h
unittest.h
)
add_library(${LIB} SHARED ${SOURCES})
......
#include <stdlib.h>
#include <stdio.h>
#include "matrix.h"
CrySingleMatrix
struct CrySingleMatrix
cry_singlematrix_new(unsigned int rows, unsigned int cols, float value)
{
float* p = 0;
float* data = cry_calloc(float, rows * cols);
CrySingleMatrix matrix = { .data = data, .rows = rows, .cols = cols };
struct CrySingleMatrix matrix = { .data = data, .rows = rows, .cols = cols };
for(p = data; p < data + rows * cols; ++p)
*p = value;
......@@ -16,12 +17,12 @@ cry_singlematrix_new(unsigned int rows, unsigned int cols, float value)
}
CryDoubleMatrix
struct CryDoubleMatrix
cry_doublematrix_new(unsigned int rows, unsigned int cols, double value)
{
double* p = 0;
double* data = cry_calloc(double, rows * cols);
CryDoubleMatrix matrix = { .data = data, .rows = rows, .cols = cols };
struct CryDoubleMatrix matrix = { .data = data, .rows = rows, .cols = cols };
for(p = data; p < data + rows * cols; ++p)
*p = value;
......@@ -29,6 +30,38 @@ cry_doublematrix_new(unsigned int rows, unsigned int cols, double value)
return matrix;
}
void
cry_singlematrix_print(FILE* io, struct CrySingleMatrix matrix)
{
float* p = 0;
for(p = matrix.data; p < matrix.data + matrix.rows * matrix.cols; ++p) {
size_t offset = ((p - matrix.data) / sizeof(float));
fprintf(io, "%#f9.3");
if(offset % matrix.cols >= matrix.cols - 1)
fprintf(io, "\n");
}
}
void
cry_doublematrix_print(FILE* io, struct CryDoubleMatrix matrix)
{
double* p = 0;
for(p = matrix.data; p < matrix.data + matrix.rows * matrix.cols; ++p) {
size_t offset = ((p - matrix.data) / sizeof(double));
fprintf(io, "%#lf9.3");
if(offset % matrix.cols >= matrix.cols - 1)
fprintf(io, "\n");
}
}
/*
CrySingleMatrix
cry_singlematrix_identity_new(unsigned int dim)
......@@ -61,17 +94,6 @@ cry_doublematrix_from_io(FILE* io)
}
void
cry_singlematrix_print(FILE* io, CrySingleMatrix matrix)
{
}
void
cry_doublematrix_print(FILE* io, CryDoubleMatrix matrix)
{
}
void
cry_singlematrix_free(CrySingleMatrix matrix)
......
......@@ -3,18 +3,28 @@
#include "standard.h"
struct CryMatrix {
pointer data;
struct CrySingleMatrix {
float* data;
unsigned int rows;
unsigned int cols;
};
typedef struct CryMatrix CrySingleMatrix;
typedef struct CryMatrix CryDoubleMatrix;
crydefine__tuple(struct CryMatrix, tuple_matrix);
struct CryDoubleMatrix {
double* data;
unsigned int rows;
unsigned int cols;
};
crydefine__tuple(struct CrySingleMatrix, tuple_singlematrix);
crydefine__tuple(struct CryDoubleMatrix, tuple_doublematrix);
struct CrySingleMatrix cry_singlematrix_new(unsigned int rows, unsigned int cols, float value);
struct CryDoubleMatrix cry_doublematrix_new(unsigned int rows, unsigned int cols, double value);
void cry_singlematrix_print(FILE* io, struct CrySingleMatrix matrix);
void cry_doublematrix_print(FILE* io, struct CryDoubleMatrix matrix);
CrySingleMatrix cry_singlematrix_new(unsigned int rows, unsigned int cols, float value);
CryDoubleMatrix cry_doublematrix_new(unsigned int rows, unsigned int cols, double value);
/*
CrySingleMatrix cry_singlematrix_identity_new(unsigned int dim);
......@@ -23,10 +33,7 @@ CryDoubleMatrix cry_doublematrix_identity_new(unsigned int dim);
CrySingleMatrix cry_singlematrix_from_io(FILE* io);
CryDoubleMatrix cry_doublematrix_from_io(FILE* io);
void cry_singlematrix_print(FILE* io, CrySingleMatrix matrix);
void cry_doublematrix_print(FILE* io, CryDoubleMatrix matrix);
void cry_singlematrix_free(CrySingleMatrix matrix);
cry_singlematrix_free(CrySingleMatrix matrix);
void cry_doublematrix_free(CryDoubleMatrix matrix);
*/
......
#include "unittest.h"
#include <unistd.h>
#include <sys/signal.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
static int monitor_testprocess(pid_t worker_pid, unsigned int timeout);
static void print_testprocess_status(const char* desc, int return_code, suseconds_t time);
void
cry_unittest_run(const char* desc, cry_modulechecker handler, unsigned int timeout)
{
pid_t process_pid = fork();
struct timeval start, end;
if(process_pid > 0) {
gettimeofday(&start, 0);
// Spawning monitoring and timeout process for test case
int status = monitor_testprocess(process_pid, timeout);
gettimeofday(&end, 0);
time_t diff_s = end.tv_sec - start.tv_sec;
suseconds_t diff_ms = end.tv_usec - start.tv_usec;
print_testprocess_status(desc, status, diff_s * 1000000L + diff_ms);
} else {
// Process for running test case
handler();
exit(EXIT_SUCCESS);
}
}
int
monitor_testprocess(pid_t worker_pid, unsigned int timeout)
{
pid_t timeout_pid = fork();
if(timeout_pid > 0) {
int status = 0;
pid_t pid = waitpid(worker_pid, &status, 0);
if(pid == worker_pid) {
kill(timeout_pid, SIGTERM);
return status;
} else if(pid == EINTR) {
return SIGINT;
} else
return SIGILL;
} else {
sleep(timeout);
kill(worker_pid, SIGINT);
exit(EXIT_SUCCESS);
}
}
void
print_testprocess_status(const char* desc, int return_code, suseconds_t time)
{
const char buffer[128];
snprintf(buffer, 128, "%s (%.3lf ms)", desc, time / 1000.0);
printf("%-50s ", buffer);
if(return_code == SIGINT)
printf("TIMEOUT");
else if(WIFEXITED(return_code))
printf("OK");
else if(WCOREDUMP(return_code))
printf("SEGFAULT");
else
printf("FAIL (%d)", return_code);
printf("\n");
}
#ifndef CRYSTAL_UNITTEST_H
#define CRYSTAL_UNITTEST_H
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
typedef void (*cry_modulechecker)(void);
void cry_unittest_run(const char* desc, cry_modulechecker handler, unsigned int timeout);
#endif // CRYSTAL_UNITTEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment