Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame] | 1 | // Utilities for testing |
| 2 | |
| 3 | #ifndef OSQP_TESTER_H |
| 4 | #define OSQP_TESTER_H |
| 5 | |
| 6 | #ifndef EMBEDDED |
| 7 | |
| 8 | #define mu_assert(msg, pred) do { INFO(msg); REQUIRE(pred); } while((void)0, 0) |
| 9 | #define TESTS_TOL 1e-4 // Define tests tolerance |
| 10 | |
| 11 | c_float* csc_to_dns(csc *M) |
| 12 | { |
| 13 | c_int i, j = 0; // Predefine row index and column index |
| 14 | c_int idx; |
| 15 | |
| 16 | // Initialize matrix of zeros |
| 17 | c_float *A = (c_float *)c_calloc(M->m * M->n, sizeof(c_float)); |
| 18 | |
| 19 | // Allocate elements |
| 20 | for (idx = 0; idx < M->p[M->n]; idx++) |
| 21 | { |
| 22 | // Get row index i (starting from 1) |
| 23 | i = M->i[idx]; |
| 24 | |
| 25 | // Get column index j (increase if necessary) (starting from 1) |
| 26 | while (M->p[j + 1] <= idx) { |
| 27 | j++; |
| 28 | } |
| 29 | |
| 30 | // Assign values to A |
| 31 | A[j * (M->m) + i] = M->x[idx]; |
| 32 | } |
| 33 | return A; |
| 34 | } |
| 35 | |
| 36 | c_int is_eq_csc(csc *A, csc *B, c_float tol) { |
| 37 | c_int j, i; |
| 38 | |
| 39 | // If number of columns does not coincide, they are not equal. |
| 40 | if (A->n != B->n) return 0; |
| 41 | |
| 42 | for (j = 0; j < A->n; j++) { // Cycle over columns j |
| 43 | // if column pointer does not coincide, they are not equal |
| 44 | if (A->p[j] != B->p[j]) return 0; |
| 45 | |
| 46 | for (i = A->p[j]; i < A->p[j + 1]; i++) { // Cycle rows i in column j |
| 47 | if ((A->i[i] != B->i[i]) || // Different row indices |
| 48 | (c_absval(A->x[i] - B->x[i]) > tol)) { |
| 49 | return 0; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | #endif // #ifndef EMBEDDED |
| 57 | |
| 58 | #endif // #ifndef OSQP_TESTER_H |