Add more error handling and tests
This commit is contained in:
parent
1b52f92196
commit
86dd4be559
2
.gitignore
vendored
2
.gitignore
vendored
@ -54,4 +54,4 @@ dkms.conf
|
||||
|
||||
main
|
||||
|
||||
*result.txt
|
||||
*results.txt
|
||||
48
MMS24-25.c
48
MMS24-25.c
@ -24,30 +24,41 @@
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
void _error(char* fmt,...) {
|
||||
printf("<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
|
||||
void _error(char* fmt, ...) {
|
||||
fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printf(fmt, args);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
printf("'\x1B[0m >\n");
|
||||
fprintf(stderr, "'\x1B[0m >\n");
|
||||
|
||||
#ifndef _TESTS_NONSTOP
|
||||
exit(-1);
|
||||
#endif
|
||||
}
|
||||
|
||||
void _debug(char* fmt,...) {
|
||||
printf("[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'");
|
||||
void _debug(char* fmt, ...) {
|
||||
fprintf(stderr, "[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printf(fmt, args);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
printf("'\x1B[0m\n");
|
||||
fprintf(stderr, "'\x1B[0m\n");
|
||||
}
|
||||
|
||||
exit(-1);
|
||||
void _warn(char* fmt, ...) {
|
||||
fprintf(stderr, "[\x1B[33;1m WARN \x1B[0m] \x1B[34m'");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, "'\x1B[0m\n");
|
||||
}
|
||||
|
||||
double interpolateLine(
|
||||
@ -102,9 +113,22 @@ double *generateSineValues(
|
||||
) {
|
||||
/* Generate an array of `iNumValues` double value, so that the values
|
||||
* represent the Y-coordinates of a sine curve of which one full period
|
||||
* occupies `iNumSamplesPerPeriod` values
|
||||
* occupies `iNumSamplesPerPeriod` values.
|
||||
*/
|
||||
|
||||
if (iNumSamplesPerPeriod == 0)
|
||||
_error("*generateSineValues: iNumSamplesPerPeriod must be non-0!");
|
||||
|
||||
if (iNumSamplesPerPeriod < 0)
|
||||
_warn("*generateSineValues: iNumSamplesPerPeriod is less than 0!");
|
||||
|
||||
if (iNumValues < 0)
|
||||
_error("*generateSineValues: iNumValues must be equal to or greater than 0!");
|
||||
|
||||
if (iNumValues == 0)
|
||||
_warn("*generateSineValues: iNumValues is 0!");
|
||||
|
||||
|
||||
// Initialize the output array
|
||||
double* sineArray = NULL;
|
||||
sineArray = (double*) malloc(sizeof(double) * iNumValues);
|
||||
@ -120,9 +144,7 @@ double *generateSineValues(
|
||||
}
|
||||
|
||||
void writeDoubleArray(double *pdOut, int iLen, char *pcOutName) {
|
||||
/* Write a list of double values to a file
|
||||
*/
|
||||
|
||||
// Write a list of double values to a file
|
||||
FILE* datei = fopen(pcOutName, "w");
|
||||
if (datei == NULL)
|
||||
_error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName);
|
||||
|
||||
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
||||
CC=gcc
|
||||
SOURCE_FILE=MMS24-25.c
|
||||
OPTS=-lm
|
||||
OPTS=-lm -D _TESTS_NONSTOP
|
||||
|
||||
all: clean tests run
|
||||
|
||||
|
||||
65
tests.c
65
tests.c
@ -9,44 +9,44 @@
|
||||
#include <math.h>
|
||||
|
||||
void info(char* fmt,...) {
|
||||
printf("[\x1B[37;1m INFO \x1B[0m]\t\x1B[34m'");
|
||||
fprintf(stderr, "[\x1B[37;1m INFO \x1B[0m] \x1B[34m'");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
printf("'\x1B[0m\n");
|
||||
fprintf(stderr, "'\x1B[0m\n");
|
||||
}
|
||||
|
||||
void success(char* fmt,...) {
|
||||
printf("[\x1B[32;1m SUCCESS \x1B[0m]\t\x1B[34m'");
|
||||
fprintf(stderr, "[\x1B[32;1m PASS \x1B[0m] \x1B[34m'");
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
printf("'\x1B[0m\n");
|
||||
fprintf(stderr, "'\x1B[0m\n");
|
||||
}
|
||||
|
||||
void error(const char* fmt, ...) {
|
||||
va_list args;
|
||||
|
||||
printf("<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
|
||||
fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
printf("'\x1B[0m >\n");
|
||||
fprintf(stderr, "'\x1B[0m >\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
void testInterpolation() {
|
||||
info("testInterpolation();");
|
||||
info("testInterpolation");
|
||||
|
||||
double dRangeMin = -10;
|
||||
double dRangeMax = 10;
|
||||
double dRangeStep = 0.5;
|
||||
double dRangeMin = -3;
|
||||
double dRangeMax = 3;
|
||||
double dRangeStep = 0.33;
|
||||
|
||||
for (double dX1=dRangeMin;dX1<dRangeMax;dX1+=dRangeStep) {
|
||||
for (double dX2=dRangeMin;dX2<dRangeMax;dX2+=dRangeStep) {
|
||||
@ -70,8 +70,47 @@ void testInterpolation() {
|
||||
success("testInterpolation();");
|
||||
}
|
||||
|
||||
void testSineAndWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) {
|
||||
info("Trying: %d, %d, %lf", nValues, dPLen, dAmp);
|
||||
|
||||
char* fileName = NULL;
|
||||
fileName = malloc(100*sizeof(char));
|
||||
|
||||
if (fileName == NULL) {
|
||||
error("Could not allocate Memory for `fileName`!");
|
||||
}
|
||||
|
||||
sprintf(fileName, "sine_%d_%d_%lf.results.txt", nValues, dPLen, dAmp);
|
||||
|
||||
writeDoubleArray(
|
||||
generateSineValues(
|
||||
nValues,
|
||||
dPLen,
|
||||
dAmp
|
||||
),
|
||||
nValues,
|
||||
fileName
|
||||
);
|
||||
|
||||
info("use `plot \"%s\", \"%s\" with lines` to view.", fileName, fileName);
|
||||
|
||||
free(fileName);
|
||||
}
|
||||
|
||||
void testSineAndWriteDoubleArray() {
|
||||
info("testSineAndWriteDoubleArray");
|
||||
|
||||
testSineAndWriteDoubleArraySingle(1000, 300, 1);
|
||||
testSineAndWriteDoubleArraySingle(10, 10, 0.5);
|
||||
testSineAndWriteDoubleArraySingle(10, -10, 0.5);
|
||||
testSineAndWriteDoubleArraySingle(10, 10, 0);
|
||||
|
||||
success("testSineAndWriteDoubleArray");
|
||||
}
|
||||
|
||||
int main (int iArgC, char** ppcArgV){
|
||||
testInterpolation();
|
||||
testSineAndWriteDoubleArray();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user