Add more error handling and tests

This commit is contained in:
Frederik Beimgraben 2025-01-11 18:49:44 +01:00
parent 1b52f92196
commit 86dd4be559
5 changed files with 89 additions and 28 deletions

2
.gitignore vendored
View File

@ -54,4 +54,4 @@ dkms.conf
main main
*result.txt *results.txt

View File

@ -24,30 +24,41 @@
#define NULL 0 #define NULL 0
#endif #endif
void _error(char* fmt,...) { void _error(char* fmt, ...) {
printf("<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'"); fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
printf(fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
printf("'\x1B[0m >\n"); fprintf(stderr, "'\x1B[0m >\n");
#ifndef _TESTS_NONSTOP
exit(-1); exit(-1);
#endif
} }
void _debug(char* fmt,...) { void _debug(char* fmt, ...) {
printf("[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'"); fprintf(stderr, "[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'");
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
printf(fmt, args); vfprintf(stderr, fmt, args);
va_end(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( double interpolateLine(
@ -102,9 +113,22 @@ double *generateSineValues(
) { ) {
/* Generate an array of `iNumValues` double value, so that the values /* Generate an array of `iNumValues` double value, so that the values
* represent the Y-coordinates of a sine curve of which one full period * 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 // Initialize the output array
double* sineArray = NULL; double* sineArray = NULL;
sineArray = (double*) malloc(sizeof(double) * iNumValues); sineArray = (double*) malloc(sizeof(double) * iNumValues);
@ -120,9 +144,7 @@ double *generateSineValues(
} }
void writeDoubleArray(double *pdOut, int iLen, char *pcOutName) { 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"); FILE* datei = fopen(pcOutName, "w");
if (datei == NULL) if (datei == NULL)
_error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName); _error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName);

View File

@ -1,6 +1,6 @@
CC=gcc CC=gcc
SOURCE_FILE=MMS24-25.c SOURCE_FILE=MMS24-25.c
OPTS=-lm OPTS=-lm -D _TESTS_NONSTOP
all: clean tests run all: clean tests run

BIN
tests

Binary file not shown.

65
tests.c
View File

@ -9,44 +9,44 @@
#include <math.h> #include <math.h>
void info(char* fmt,...) { 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_list args;
va_start(args, fmt); va_start(args, fmt);
vprintf(fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
printf("'\x1B[0m\n"); fprintf(stderr, "'\x1B[0m\n");
} }
void success(char* fmt,...) { 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_list args;
va_start(args, fmt); va_start(args, fmt);
vprintf(fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
printf("'\x1B[0m\n"); fprintf(stderr, "'\x1B[0m\n");
} }
void error(const char* fmt, ...) { void error(const char* fmt, ...) {
va_list args; 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); va_start(args, fmt);
vprintf(fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
printf("'\x1B[0m >\n"); fprintf(stderr, "'\x1B[0m >\n");
exit(-1); exit(-1);
} }
void testInterpolation() { void testInterpolation() {
info("testInterpolation();"); info("testInterpolation");
double dRangeMin = -10; double dRangeMin = -3;
double dRangeMax = 10; double dRangeMax = 3;
double dRangeStep = 0.5; double dRangeStep = 0.33;
for (double dX1=dRangeMin;dX1<dRangeMax;dX1+=dRangeStep) { for (double dX1=dRangeMin;dX1<dRangeMax;dX1+=dRangeStep) {
for (double dX2=dRangeMin;dX2<dRangeMax;dX2+=dRangeStep) { for (double dX2=dRangeMin;dX2<dRangeMax;dX2+=dRangeStep) {
@ -70,8 +70,47 @@ void testInterpolation() {
success("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){ int main (int iArgC, char** ppcArgV){
testInterpolation(); testInterpolation();
testSineAndWriteDoubleArray();
return 0; return 0;
} }