TU-Programmieren_2/exercise10/task3.test.c
2025-04-09 10:22:44 +02:00

89 lines
2.4 KiB
C

/// @file
/// @brief Task3: tests of output files generated by other tests/commands using command line arguments for task3_main
#include "modules/iue-io/ccsv.h" // iueio_loadtxt
#include "modules/iue-num/numerics.h" // iuenum_isclose
#include <assert.h> // assert
#include <stdbool.h> // bool, true, false
#include <stdio.h> // printf
bool isclose(const struct Table* a, const struct Table* b) {
if (a->n != b->n)
return false;
for (size_t r = 0; r != a->n; ++r) {
if (a->rows[r].n != a->rows[r].n)
return false;
for (size_t c = 0; c != a->rows[r].n; ++c)
if (!iuenum_isclose(a->rows[r].values[c], b->rows[r].values[c]))
return false;
}
return true;
}
int main() {
{ // testing result of this command:
// "./build/task3_main --left rrev4x4.csv --right matrix4x2.csv --out matrix4x2_rrow.csv"
double data_expected[4][2] = {
{41, 42},
{31, 32},
{21, 22},
{11, 12},
};
struct Table expected = {NULL, 0};
table_append_copy(&expected, data_expected[0], 2);
table_append_copy(&expected, data_expected[1], 2);
table_append_copy(&expected, data_expected[2], 2);
table_append_copy(&expected, data_expected[3], 2);
const char filepath[] = "matrix4x2_rrow.csv";
struct Table table = {NULL, 0};
if (iueio_loadtxt(filepath, &table, ';', '#') != 0) {
fprintf(stderr, "error loading file %s\n", filepath);
exit(EXIT_FAILURE);
}
assert(isclose(&table, &expected));
table_clear(&table);
table_clear(&expected);
}
{ // testing result of this command:
// "./build/task3_main --left matrix4x2.csv --right rcol2x2.csv --out matrix4x2_rcols.csv"
double data_expected[4][2] = {
{12, 11},
{22, 21},
{32, 31},
{42, 41},
};
struct Table expected = {NULL, 0};
table_append_copy(&expected, data_expected[0], 2);
table_append_copy(&expected, data_expected[1], 2);
table_append_copy(&expected, data_expected[2], 2);
table_append_copy(&expected, data_expected[3], 2);
const char filepath[] = "matrix4x2_rcols.csv";
struct Table table = {NULL, 0};
if (iueio_loadtxt(filepath, &table, ';', '#') != 0) {
fprintf(stderr, "error loading file %s\n", filepath);
exit(EXIT_FAILURE);
}
assert(isclose(&table, &expected));
table_clear(&table);
table_clear(&expected);
}
printf("task3.test.c: all asserts passed\n");
return 0;
}