diff --git a/.gitignore b/.gitignore index 254bdcf..cb50d2d 100644 --- a/.gitignore +++ b/.gitignore @@ -54,4 +54,4 @@ dkms.conf main -result.txt \ No newline at end of file +*result.txt \ No newline at end of file diff --git a/MMS24-25.c b/MMS24-25.c index e210532..1f08442 100644 --- a/MMS24-25.c +++ b/MMS24-25.c @@ -17,12 +17,39 @@ #include #include "MMS24-25.h" #include +#include -// NULL Pointer +// Define NULL if not already provided #ifndef NULL #define NULL 0 #endif +void _error(char* fmt,...) { + printf("<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'"); + + va_list args; + va_start(args, fmt); + printf(fmt, args); + va_end(args); + + printf("'\x1B[0m >\n"); + + exit(-1); +} + +void _debug(char* fmt,...) { + printf("[\x1B[31;1m DEBUG \x1B[0m] \x1B[34m'"); + + va_list args; + va_start(args, fmt); + printf(fmt, args); + va_end(args); + + printf("'\x1B[0m\n"); + + exit(-1); +} + double interpolateLine( double dX1, double dY1, @@ -30,13 +57,18 @@ double interpolateLine( double dY2, double dXq ) { - double y = ( - (dY2 - dY1) / (dX2 - dX1) - ) * (dXq + dX1); + if (dX2 == dX1) + _error("interpolateLine: dX1 and dX2 must differ!"); + + double y = dY1 * ( + (dX2 - dXq) / (dX2 - dX1) + ) + dY2 * ( + (dXq - dX1) / (dX2 - dX1) + ); #ifdef _DEBUG_ // DEBUG: print the parameters and result - printf("interpolateLine(%lf, %lf, %lf, %lf, %lf) -> %lf\n", + _debug("interpolateLine(%lf, %lf, %lf, %lf, %lf) -> %lf\n", dX1,dY1,dX2,dY2,dXq,y); #endif @@ -77,10 +109,8 @@ double *generateSineValues( double* sineArray = NULL; sineArray = (double*) malloc(sizeof(double) * iNumValues); - if (sineArray == NULL) { - printf("Failed to allocate memory for SineArray\n"); - exit(-1); - } + if (sineArray == NULL) + _error("*generateSineValues: Failed to allocate memory for Sine Array\n"); for (int i=0; i -#include - - -int main (int iArgC, char** ppcArgV){ - int iRes = -1; - - if (iArgC != 6){ - printf("Number of Arguments != 6\n"); - printf("Usage testInterpolation \n"); - exit(iRes); - }; - - double dX1 = atof(ppcArgV[1]); - double dY1 = atof(ppcArgV[2]); - double dX2 = atof(ppcArgV[3]); - double dY2 = atof(ppcArgV[4]); - double dX = atof(ppcArgV[5]); - - for(int i=0; i<4; i++){ - double dRes = interpolateLine(dX1, dY1, dX2, dY2, dX+(double)i); - printf ("%f\n",dRes); - } - - return iRes; -} diff --git a/tests b/tests new file mode 100755 index 0000000..0b0af51 Binary files /dev/null and b/tests differ diff --git a/tests.c b/tests.c new file mode 100644 index 0000000..f45de54 --- /dev/null +++ b/tests.c @@ -0,0 +1,77 @@ +// +// Created by frederik on 1/11/25. +// + +#include "MMS24-25.h" +#include +#include +#include +#include + +void info(char* fmt,...) { + printf("[\x1B[37;1m INFO \x1B[0m]\t\x1B[34m'"); + + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + + printf("'\x1B[0m\n"); +} + +void success(char* fmt,...) { + printf("[\x1B[32;1m SUCCESS \x1B[0m]\t\x1B[34m'"); + + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + + printf("'\x1B[0m\n"); +} + +void error(const char* fmt, ...) { + va_list args; + + printf("<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'"); + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + printf("'\x1B[0m >\n"); + exit(-1); +} + +void testInterpolation() { + info("testInterpolation();"); + + double dRangeMin = -10; + double dRangeMax = 10; + double dRangeStep = 0.5; + + for (double dX1=dRangeMin;dX1 (%lf, %lf); %lf", dX1, dY1, dX2, dY2, dX, dR1, dR2); + } + } + } + } + } + + success("testInterpolation();"); +} + +int main (int iArgC, char** ppcArgV){ + testInterpolation(); + + return 0; +}