Add readDoubleArray

This commit is contained in:
Frederik Beimgraben 2025-01-11 19:19:41 +01:00
parent 840d5b0c4f
commit 500aba94d2
2 changed files with 85 additions and 36 deletions

View File

@ -19,11 +19,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
// Define NULL if not already provided
#ifndef NULL
#define NULL 0
#endif
void _error(char *fmt, ...) { void _error(char *fmt, ...) {
fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'"); fprintf(stderr, "<\x1B[31;1m ERROR \x1B[0m: \x1B[34m'");
@ -35,7 +30,7 @@ void _error(char* fmt, ...) {
fprintf(stderr, "'\x1B[0m >\n"); fprintf(stderr, "'\x1B[0m >\n");
#ifndef _TESTS_NONSTOP #ifndef _TESTS_NONSTOP
exit(-1); exit(EXIT_FAILURE);
#endif #endif
} }
@ -145,13 +140,45 @@ 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 *file = fopen(pcOutName, "w");
if (datei == NULL) if (file == NULL)
_error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName); _error("writeDoubleArray: Error while opening file '%s'.\n", pcOutName);
for (int i = 0; i < iLen; i++) { for (int i = 0; i < iLen; i++) {
fprintf(datei, "%f\n", pdOut[i]); fprintf(file, "%f\n", pdOut[i]);
} }
fclose(datei); fclose(file);
}
int readDoubleArray(char *pcInName, double **ppdIn) {
char *line = NULL;
size_t len = 0;
ssize_t read;
int i = 0;
*ppdIn = NULL;
FILE *file = fopen(pcInName, "r");
if (file == NULL)
_error("readDoubleArray: Error while opening file '%s'.\n", pcInName);
while ((read = getline(&line, &len, file)) != -1) {
if (*ppdIn == NULL) {
*ppdIn = (double*) malloc(sizeof(double));
} else {
*ppdIn = (double*) realloc(*ppdIn, sizeof(double) *(i+1));
}
if (*ppdIn == NULL)
_error("readDoubleArray: Failed to allocate or re-allocate memory for pdIn\n");
(*ppdIn)[i] = atof(line);
i++;
}
free(line);
fclose(file);
return i;
} }

42
tests.c
View File

@ -70,7 +70,7 @@ void testInterpolation() {
success("testInterpolation();"); success("testInterpolation();");
} }
void testSineAndWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) { void testSineAndReadWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) {
info("Trying: %d, %d, %lf", nValues, dPLen, dAmp); info("Trying: %d, %d, %lf", nValues, dPLen, dAmp);
char* fileName = NULL; char* fileName = NULL;
@ -82,35 +82,57 @@ void testSineAndWriteDoubleArraySingle(int nValues, int dPLen, double dAmp) {
sprintf(fileName, "sine_%d_%d_%lf.results.txt", nValues, dPLen, dAmp); sprintf(fileName, "sine_%d_%d_%lf.results.txt", nValues, dPLen, dAmp);
writeDoubleArray( double *pdSineValues = generateSineValues(
generateSineValues(
nValues, nValues,
dPLen, dPLen,
dAmp dAmp
), );
writeDoubleArray(
pdSineValues,
nValues, nValues,
fileName fileName
); );
success("Wrote %d values to %s", nValues, fileName);
double *pdReadValues = NULL;
int iReadValues = readDoubleArray(
fileName,
&pdReadValues
);
if (iReadValues != nValues) {
error("Failed to read the correct number of values: %d != %d", iReadValues, nValues);
}
success("Read %d values from %s", iReadValues, fileName);
for (int i=0; i<nValues; i++) {
if (round(pdSineValues[i]*1e6) != round(pdReadValues[i]*1e6)) {
error("Failed to read the correct value at index %d: %lf != %lf", i, pdSineValues[i], pdReadValues[i]);
}
}
info("use `plot \"%s\", \"%s\" with lines` to view.", fileName, fileName); info("use `plot \"%s\", \"%s\" with lines` to view.", fileName, fileName);
free(fileName); free(fileName);
} }
void testSineAndWriteDoubleArray() { void testSineAndReadWriteDoubleArray() {
info("testSineAndWriteDoubleArray"); info("testSineAndWriteDoubleArray");
testSineAndWriteDoubleArraySingle(1000, 300, 1); testSineAndReadWriteDoubleArraySingle(1000, 300, 1);
testSineAndWriteDoubleArraySingle(10, 10, 0.5); testSineAndReadWriteDoubleArraySingle(10, 10, 0.5);
testSineAndWriteDoubleArraySingle(10, -10, 0.5); testSineAndReadWriteDoubleArraySingle(10, -10, 0.5);
testSineAndWriteDoubleArraySingle(10, 10, 0); testSineAndReadWriteDoubleArraySingle(10, 10, 0);
success("testSineAndWriteDoubleArray"); success("testSineAndWriteDoubleArray");
} }
int main (int iArgC, char** ppcArgV){ int main (int iArgC, char** ppcArgV){
testInterpolation(); testInterpolation();
testSineAndWriteDoubleArray(); testSineAndReadWriteDoubleArray();
return 0; return 0;
} }