|
| 1 | +// DL 2025.06.25 |
| 2 | +// Unit test: Converts a Matrix Market (MM) matrix to internal CRS format |
| 3 | +// and compares against expected output from disk. |
| 4 | +// Assumes a single MPI rank (no parallel communication). |
| 5 | + |
| 6 | +#include "../../src/comm.h" |
| 7 | +#include "../../src/matrix.h" |
| 8 | +#include "../common.h" |
| 9 | +#include <dirent.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string.h> |
| 13 | + |
| 14 | +/** |
| 15 | + * Runs conversion tests for all `.mtx` files in `dataDir/testMatrices/`. |
| 16 | + * |
| 17 | + * For each matrix: |
| 18 | + * 1. Builds full path to the file |
| 19 | + * 2. Loads MM matrix and converts it to GMatrix, and then CRS |
| 20 | + * 3. Dumps the converted result to a file |
| 21 | + * 4. Compares the dump against expected result (if present) |
| 22 | + * |
| 23 | + * Returns 0 on success, 1 if any test fails. |
| 24 | + */ |
| 25 | +int test_convertCRS(void* args, const char* dataDir) |
| 26 | +{ |
| 27 | + // Compose path to directory containing test matrices |
| 28 | + char matricesPath[STR_LEN]; |
| 29 | + snprintf(matricesPath, STR_LEN, "%s%s", dataDir, "testMatrices/"); |
| 30 | + |
| 31 | + // Open the directory and check for errors |
| 32 | + DIR* dir = opendir(matricesPath); |
| 33 | + if (dir == NULL) { |
| 34 | + perror("Error opening directory"); |
| 35 | + return 1; |
| 36 | + } |
| 37 | + |
| 38 | + // Read the directory entries |
| 39 | + struct dirent* entry; |
| 40 | + |
| 41 | + // Iterate through files in the directory |
| 42 | + while ((entry = readdir(dir)) != NULL) { |
| 43 | + // Only process files with ".mtx" extension |
| 44 | + if (strstr(entry->d_name, ".mtx") != NULL) { |
| 45 | + |
| 46 | + // Build full path to matrix file |
| 47 | + char matrixPath[STR_LEN]; |
| 48 | + snprintf(matrixPath, STR_LEN, "%s%s", matricesPath, entry->d_name); |
| 49 | + |
| 50 | + MMMatrix M; |
| 51 | + Args* arguments = (Args*)args; |
| 52 | + |
| 53 | + STRIP_MATRIX_FILE(entry) // Removes .mtx from file name |
| 54 | + |
| 55 | + // Path to expected output (".in" file) for this matrix |
| 56 | + char pathToExpectedData[STR_LEN]; |
| 57 | + snprintf(pathToExpectedData, |
| 58 | + STR_LEN, |
| 59 | + "data/expected/%s_CRS.in", |
| 60 | + entry->d_name); |
| 61 | + |
| 62 | + // Only test if expected file exists |
| 63 | + if (fopen(pathToExpectedData, "r")) { |
| 64 | + |
| 65 | + // Path to write reported output (".out" file) |
| 66 | + char pathToReportedData[STR_LEN]; |
| 67 | + snprintf(pathToReportedData, |
| 68 | + STR_LEN, |
| 69 | + "data/reported/%s_CRS.out", |
| 70 | + entry->d_name); |
| 71 | + |
| 72 | + // Open output file for writing results |
| 73 | + FILE* reportedData = fopen(pathToReportedData, "w"); |
| 74 | + if (reportedData == NULL) { |
| 75 | + perror("fopen failed for reportedData"); |
| 76 | + exit(EXIT_FAILURE); // Crash fast on I/O error |
| 77 | + } |
| 78 | + |
| 79 | + // Load matrix from Matrix Market file |
| 80 | + MMMatrixRead(&M, matrixPath); |
| 81 | + |
| 82 | + // Declare graph and algebraic matrices |
| 83 | + GMatrix m; |
| 84 | + Matrix A; |
| 85 | + Comm c; |
| 86 | + |
| 87 | + // Set single-rank defaults (no parallel distribution) |
| 88 | + M.startRow = 0; |
| 89 | + M.stopRow = M.nr; |
| 90 | + M.totalNr = M.nr; |
| 91 | + M.totalNnz = M.nnz; |
| 92 | + c.rank = 0; |
| 93 | + c.size = 1; |
| 94 | + c.logFile = reportedData; |
| 95 | + |
| 96 | + // Convert to internal formats |
| 97 | + matrixConvertfromMM(&M, &m); // MM → GMatrix |
| 98 | + convertMatrix(&A, &m); // GMatrix → Matrix |
| 99 | + commMatrixDump(&c, &A); // Output formatted result |
| 100 | + fclose(reportedData); |
| 101 | + |
| 102 | + // Diff against expected file — if mismatch, fail the test |
| 103 | + if (diff_files(pathToExpectedData, pathToReportedData, 0)) { |
| 104 | + closedir(dir); |
| 105 | + return 1; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Clean up and return success |
| 112 | + closedir(dir); |
| 113 | + return 0; |
| 114 | +} |
0 commit comments