Compare commits

...

2 Commits

Author SHA1 Message Date
840d5b0c4f Add "tests" to gitignore 2025-01-11 18:50:04 +01:00
86dd4be559 Add more error handling and tests 2025-01-11 18:49:44 +01:00
5 changed files with 91 additions and 28 deletions

4
.gitignore vendored
View File

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

View File

@ -25,29 +25,40 @@
#endif
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_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'");
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);

View File

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

BIN
tests

Binary file not shown.

65
tests.c
View File

@ -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;
}