The Fourth and Fifth tasks are done
This commit is contained in:
parent
69ff8c30a1
commit
a6c451d866
28 changed files with 1077 additions and 15 deletions
8
.gitignore
vendored
8
.gitignore
vendored
|
@ -1,9 +1,15 @@
|
||||||
|
# Executable files
|
||||||
*.exe
|
*.exe
|
||||||
*~
|
|
||||||
*.out
|
*.out
|
||||||
*.o
|
*.o
|
||||||
|
|
||||||
|
# Vim files
|
||||||
|
*~
|
||||||
.*.sw*
|
.*.sw*
|
||||||
|
|
||||||
*output*
|
*output*
|
||||||
|
|
||||||
|
# Clion folders and files
|
||||||
cmake-build-debug/
|
cmake-build-debug/
|
||||||
.idea/
|
.idea/
|
||||||
CMakeLists.txt
|
CMakeLists.txt
|
|
@ -1,4 +0,0 @@
|
||||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
|
||||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
|
||||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
|
||||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
|
|
@ -18,6 +18,85 @@
|
||||||
"k": 3,
|
"k": 3,
|
||||||
"n": 3,
|
"n": 3,
|
||||||
"p": 3
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Basic Trace)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Negative Numbers)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "-1 -2 -3\n-4 -5 -6\n-7 -8 -9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Mixed Positive and Negative)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1 -2 3\n-4 5 -6\n7 -8 9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Floating Point Values)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1.1 2.2 3.3\n4.4 5.5 6.6\n7.7 8.8 9.9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=1)",
|
||||||
|
"k": 1,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=2)",
|
||||||
|
"k": 2,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=3)",
|
||||||
|
"k": 3,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=4, Fractional Values)",
|
||||||
|
"k": 4,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "1x1 Matrix (Single Element)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 1,
|
||||||
|
"p": 1,
|
||||||
|
"matrix": "42"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "2x2 Matrix",
|
||||||
|
"k": 0,
|
||||||
|
"n": 2,
|
||||||
|
"p": 2,
|
||||||
|
"matrix": "1 2\n3 4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "4x4 Identity Matrix",
|
||||||
|
"k": 0,
|
||||||
|
"n": 4,
|
||||||
|
"p": 4,
|
||||||
|
"matrix": "1 0 0 0\n0 1 0 0\n0 0 1 0\n0 0 0 1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "1000x1000 Large Matrix",
|
||||||
|
"k": 1,
|
||||||
|
"n": 1000,
|
||||||
|
"p": 5
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,12 +87,27 @@ def format_matrix(matrix):
|
||||||
formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split()))
|
formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split()))
|
||||||
return "\n".join(formatted)
|
return "\n".join(formatted)
|
||||||
|
|
||||||
def parse_matrix_output(output, label):
|
def parse_matrix_output(output, label, end_label):
|
||||||
"""Extracts and formats a labeled matrix from program output"""
|
"""Extracts and formats a labeled matrix from program output"""
|
||||||
parts = output.split(f"{label}:\n")
|
parts = output.split(f"{label}:\n")
|
||||||
|
if len(parts) > 1:
|
||||||
|
matrix_lines = parts[1].strip().split(end_label)[0].split("\n")
|
||||||
|
return format_matrix("\n".join(matrix_lines)) # Убираем Task = ... строку
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def result_matrix_output(output):
|
||||||
|
parts = output.split("Result matrix:\n")
|
||||||
if len(parts) > 1:
|
if len(parts) > 1:
|
||||||
matrix_lines = parts[1].strip().split("\n")
|
matrix_lines = parts[1].strip().split("\n")
|
||||||
return format_matrix("\n".join(matrix_lines[:-1])) # Убираем Task = ... строку
|
return format_matrix("\n".join(matrix_lines[:-1]))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def initial_matrix_output(output):
|
||||||
|
parts = output.split("Initial matrix:\n")
|
||||||
|
if len(parts) > 1:
|
||||||
|
parts = parts[1].split("Result")
|
||||||
|
matrix_lines = parts[0].strip().split("\n")
|
||||||
|
return format_matrix("\n".join(matrix_lines))
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def transpose_matrix(matrix):
|
def transpose_matrix(matrix):
|
||||||
|
@ -121,8 +136,8 @@ def run_test(test_suite, test):
|
||||||
result = run_command(cmd)
|
result = run_command(cmd)
|
||||||
|
|
||||||
# Extract both initial and result matrices
|
# Extract both initial and result matrices
|
||||||
initial_matrix = parse_matrix_output(result.stdout, "Initial matrix")
|
initial_matrix = initial_matrix_output(result.stdout)
|
||||||
result_matrix = parse_matrix_output(result.stdout, "Result matrix")
|
result_matrix = result_matrix_output(result.stdout)
|
||||||
|
|
||||||
# Compute expected transposed matrix
|
# Compute expected transposed matrix
|
||||||
expected_transposed = transpose_matrix(initial_matrix)
|
expected_transposed = transpose_matrix(initial_matrix)
|
||||||
|
@ -135,12 +150,14 @@ def run_test(test_suite, test):
|
||||||
|
|
||||||
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
||||||
|
|
||||||
|
if test.k == 0:
|
||||||
# Cleanup test file
|
# Cleanup test file
|
||||||
try:
|
try:
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
except (FileNotFoundError, PermissionError):
|
except (FileNotFoundError, PermissionError):
|
||||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||||
run_command("make clean", exit_on_error=True)
|
run_command("make clean", exit_on_error=True)
|
||||||
|
|
19
2025.03.07/4Ex/Makefile
Normal file
19
2025.03.07/4Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||||
|
|
||||||
|
a04.exe: main.o array_io.o solve.o init_f.o
|
||||||
|
gcc main.o solve.o array_io.o init_f.o -o a04.exe -lssp
|
||||||
|
|
||||||
|
main.o: main.c
|
||||||
|
gcc $(CFLAGS) -c main.c
|
||||||
|
|
||||||
|
solve.o: solve.c
|
||||||
|
gcc $(FLAGS) -c solve.c
|
||||||
|
|
||||||
|
array_io.o: array_io.c
|
||||||
|
gcc $(CFLAGS) -c array_io.c
|
||||||
|
|
||||||
|
init_f.o: init_f.c
|
||||||
|
gcc $(CFLAGS) -c init_f.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
del *.o *.exe
|
39
2025.03.07/4Ex/array_io.c
Normal file
39
2025.03.07/4Ex/array_io.c
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_sq_matrix(double *a, int n, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * n + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_sq_matrix(const double *a, int n, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < np; j++)
|
||||||
|
printf(" %10.3e", a[i * n + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_sq_matrix(double *a, int n, int k)
|
||||||
|
{
|
||||||
|
double (*q)(int, int, int, int);
|
||||||
|
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
|
||||||
|
int i, j;
|
||||||
|
q = f[k-1];
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
a[i * n + j] = q(n, n, i+1, j+1);
|
||||||
|
}
|
11
2025.03.07/4Ex/array_io.h
Normal file
11
2025.03.07/4Ex/array_io.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef ARRAY_IO_H
|
||||||
|
#define ARRAY_IO_H
|
||||||
|
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "init_f.h"
|
||||||
|
|
||||||
|
io_status read_sq_matrix(double *a, int n, const char *name);
|
||||||
|
void print_sq_matrix(const double *a, int n, int p);
|
||||||
|
void init_sq_matrix(double *a, int n, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.07/4Ex/init_f.c
Normal file
30
2025.03.07/4Ex/init_f.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include "init_f.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define MAX(n, m) (n < m ? m : n)
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.07/4Ex/init_f.h
Normal file
9
2025.03.07/4Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
14
2025.03.07/4Ex/io_status.h
Normal file
14
2025.03.07/4Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef IO_STATUS_H
|
||||||
|
#define IO_STATUS_H
|
||||||
|
|
||||||
|
#define LEN 1234
|
||||||
|
|
||||||
|
typedef enum _io_status
|
||||||
|
{
|
||||||
|
SUCCESS,
|
||||||
|
ERROR_OPEN,
|
||||||
|
ERROR_READ,
|
||||||
|
ERROR_MEM
|
||||||
|
} io_status;
|
||||||
|
|
||||||
|
#endif
|
65
2025.03.07/4Ex/main.c
Normal file
65
2025.03.07/4Ex/main.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "solve.h"
|
||||||
|
|
||||||
|
/* ./a.out n p k [filename] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a;
|
||||||
|
int n, p, k, task = 4;
|
||||||
|
char *name = 0;
|
||||||
|
|
||||||
|
if (!((argc == 4 || argc == 5) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &k) == 1 &&
|
||||||
|
k >= 0 && k <= 4 && (!(k == 0 && argc != 5))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n p k [filename]\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (argc == 5) name = argv[4];
|
||||||
|
|
||||||
|
a = (double *)malloc(n * n * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_sq_matrix(a, n, name);
|
||||||
|
do {
|
||||||
|
switch (ret)
|
||||||
|
{
|
||||||
|
case SUCCESS:
|
||||||
|
continue;
|
||||||
|
case ERROR_OPEN:
|
||||||
|
printf("Cannot open %s\n", name);
|
||||||
|
break;
|
||||||
|
case ERROR_READ:
|
||||||
|
printf("Cannot read %s\n", name);
|
||||||
|
}
|
||||||
|
free(a);
|
||||||
|
return 3;
|
||||||
|
} while (0);
|
||||||
|
} else init_sq_matrix(a, n, k);
|
||||||
|
|
||||||
|
printf("Initial matrix:\n");
|
||||||
|
print_sq_matrix(a, n, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
t4_solve(a, n);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf("Result matrix:\n");
|
||||||
|
print_sq_matrix(a, n, p);
|
||||||
|
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
15
2025.03.07/4Ex/solve.c
Normal file
15
2025.03.07/4Ex/solve.c
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
void t4_solve(double *a, int n)
|
||||||
|
{
|
||||||
|
double temp;
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = i; j < n; j++)
|
||||||
|
if (i != j) {
|
||||||
|
temp = a[i * n + j];
|
||||||
|
a[i * n + j] = (a[j * n + i] + temp) / 2;
|
||||||
|
a[j * n + i] = (a[j * n + i] + temp) / 2;
|
||||||
|
}
|
||||||
|
}
|
6
2025.03.07/4Ex/solve.h
Normal file
6
2025.03.07/4Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
void t4_solve(double *a, int n);
|
||||||
|
|
||||||
|
#endif
|
4
2025.03.07/4Ex/t.txt
Normal file
4
2025.03.07/4Ex/t.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1 2 3 4
|
||||||
|
1 2 3 4
|
||||||
|
1 2 3 4
|
||||||
|
1 2 3 4
|
102
2025.03.07/4Ex/test_cases.json
Normal file
102
2025.03.07/4Ex/test_cases.json
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
{
|
||||||
|
"exe": "a04.exe",
|
||||||
|
"filename": "matrix.txt",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "File Matrix Test",
|
||||||
|
"k": 0,
|
||||||
|
"matrix": "1 2 3 4\n1 2 3 4\n1 2 3 4\n1 2 3 4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=1)",
|
||||||
|
"k": 1,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=3)",
|
||||||
|
"k": 3,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Basic Trace)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Negative Numbers)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "-1 -2 -3\n-4 -5 -6\n-7 -8 -9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Mixed Positive and Negative)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1 -2 3\n-4 5 -6\n7 -8 9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Floating Point Values)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1.1 2.2 3.3\n4.4 5.5 6.6\n7.7 8.8 9.9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=1)",
|
||||||
|
"k": 1,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=2)",
|
||||||
|
"k": 2,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=3)",
|
||||||
|
"k": 3,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=4, Fractional Values)",
|
||||||
|
"k": 4,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "1x1 Matrix (Single Element)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 1,
|
||||||
|
"p": 1,
|
||||||
|
"matrix": "42"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "2x2 Matrix",
|
||||||
|
"k": 0,
|
||||||
|
"n": 2,
|
||||||
|
"p": 2,
|
||||||
|
"matrix": "1 2\n3 4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "4x4 Identity Matrix",
|
||||||
|
"k": 0,
|
||||||
|
"n": 4,
|
||||||
|
"p": 4,
|
||||||
|
"matrix": "1 0 0 0\n0 1 0 0\n0 0 1 0\n0 0 0 1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "1000x1000 Large Matrix",
|
||||||
|
"k": 1,
|
||||||
|
"n": 1000,
|
||||||
|
"p": 5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
168
2025.03.07/4Ex/test_runner.py
Normal file
168
2025.03.07/4Ex/test_runner.py
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
import re
|
||||||
|
import signal
|
||||||
|
from colorama import Fore, Style, init
|
||||||
|
|
||||||
|
# Enable color support in Windows
|
||||||
|
init(autoreset=True)
|
||||||
|
|
||||||
|
def color_text(text, color):
|
||||||
|
"""Returns colored text"""
|
||||||
|
return color + text + Style.RESET_ALL
|
||||||
|
|
||||||
|
def cleanup_and_exit():
|
||||||
|
"""Handles cleanup on Ctrl+C or forced exit"""
|
||||||
|
print(color_text("\n[ABORT] Operation interrupted. Cleaning up...", Fore.RED))
|
||||||
|
run_command("make clean")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
# Register Ctrl+C handler
|
||||||
|
signal.signal(signal.SIGINT, lambda sig, frame: cleanup_and_exit())
|
||||||
|
|
||||||
|
class TestCase:
|
||||||
|
"""Represents a single test case"""
|
||||||
|
def __init__(self, k, matrix=None, n=None, p=None, debug=False, name=None):
|
||||||
|
self.k = k
|
||||||
|
self.matrix = matrix
|
||||||
|
self.n = n
|
||||||
|
self.p = p
|
||||||
|
self.debug = debug
|
||||||
|
self.name = name if name else f"Test k={k}, n={n if n else 'auto'}, p={p if p else 'auto'}"
|
||||||
|
|
||||||
|
# Compute `n` if missing and `k == 0`
|
||||||
|
if self.k == 0 and not self.n and self.matrix:
|
||||||
|
self.n = len(self.matrix.strip().split("\n"))
|
||||||
|
|
||||||
|
# Compute `p`
|
||||||
|
self.p = self.p if self.p else self.n
|
||||||
|
|
||||||
|
def validate_inputs(self):
|
||||||
|
"""Ensures input values are valid"""
|
||||||
|
if self.k < 0 or (self.k == 0 and not self.matrix):
|
||||||
|
print(color_text(f"[ERROR] Invalid test parameters: {self.name}", Fore.RED))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
class TestSuite:
|
||||||
|
"""Handles loading and running test cases"""
|
||||||
|
def __init__(self, config_file):
|
||||||
|
self.config = self.load_config(config_file)
|
||||||
|
self.exe = self.config["exe"]
|
||||||
|
self.filename = self.config["filename"]
|
||||||
|
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_config(filename):
|
||||||
|
"""Loads test cases from JSON"""
|
||||||
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
def run_command(cmd, exit_on_error=False):
|
||||||
|
"""Runs a shell command and handles errors"""
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||||
|
return result
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||||
|
print(e.stderr)
|
||||||
|
if exit_on_error:
|
||||||
|
exit(1)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def wait_for_executable(exe):
|
||||||
|
"""Waits for the executable file to appear after compilation"""
|
||||||
|
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||||
|
while not os.path.exists(exe):
|
||||||
|
time.sleep(0.1) # Reduce CPU usage
|
||||||
|
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||||
|
|
||||||
|
def format_matrix(matrix):
|
||||||
|
"""Formats a matrix to match `printf("%10.3e")` output"""
|
||||||
|
formatted = []
|
||||||
|
for row in matrix.strip().split("\n"):
|
||||||
|
formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split()))
|
||||||
|
return "\n".join(formatted)
|
||||||
|
|
||||||
|
def result_matrix_output(output):
|
||||||
|
parts = output.split("Result matrix:\n")
|
||||||
|
if len(parts) > 1:
|
||||||
|
matrix_lines = parts[1].strip().split("\n")
|
||||||
|
return format_matrix("\n".join(matrix_lines[:-1]))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def initial_matrix_output(output):
|
||||||
|
parts = output.split("Initial matrix:\n")
|
||||||
|
if len(parts) > 1:
|
||||||
|
parts = parts[1].split("Result")
|
||||||
|
matrix_lines = parts[0].strip().split("\n")
|
||||||
|
return format_matrix("\n".join(matrix_lines))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def symmetric_matrix(matrix):
|
||||||
|
rows = [list(map(float, row.split())) for row in matrix.strip().split("\n")]
|
||||||
|
symmetrize = [[((rows[i][j] + rows[j][i]) / 2) for j in range(len(rows[i]))] for i in range(len(rows))]
|
||||||
|
return "\n".join(" ".join(f"{float(num):10.3e}" for num in row) for row in symmetrize)
|
||||||
|
|
||||||
|
def run_test(test_suite, test):
|
||||||
|
"""Runs the program and checks its result"""
|
||||||
|
if not test.validate_inputs():
|
||||||
|
return
|
||||||
|
|
||||||
|
exe, filename = test_suite.exe, test_suite.filename
|
||||||
|
|
||||||
|
# If matrix is given, write it to the file
|
||||||
|
if test.k == 0 and test.matrix:
|
||||||
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
|
f.write(format_matrix(test.matrix.strip()) + "\n")
|
||||||
|
|
||||||
|
cmd = [exe, str(test.n), str(test.p), str(test.k)]
|
||||||
|
if test.k == 0:
|
||||||
|
cmd.append(filename)
|
||||||
|
|
||||||
|
# Run the program
|
||||||
|
result = run_command(cmd)
|
||||||
|
|
||||||
|
# Extract both initial and result matrices
|
||||||
|
initial_matrix = initial_matrix_output(result.stdout)
|
||||||
|
result_matrix = result_matrix_output(result.stdout)
|
||||||
|
|
||||||
|
expected_transposed = symmetric_matrix(initial_matrix)
|
||||||
|
|
||||||
|
if result_matrix.strip() != expected_transposed.strip():
|
||||||
|
print(color_text(f"[FAIL] Test '{test.name}' matrix mismatch.", Fore.RED))
|
||||||
|
print(f"Expected:\n{expected_transposed}")
|
||||||
|
print(f"Got:\n{result_matrix}")
|
||||||
|
return
|
||||||
|
|
||||||
|
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
||||||
|
|
||||||
|
if test.k == 0:
|
||||||
|
# Cleanup test file
|
||||||
|
try:
|
||||||
|
os.remove(filename)
|
||||||
|
except (FileNotFoundError, PermissionError):
|
||||||
|
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||||
|
run_command("make clean", exit_on_error=True)
|
||||||
|
|
||||||
|
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||||
|
run_command("make", exit_on_error=True)
|
||||||
|
|
||||||
|
test_suite = TestSuite("test_cases.json")
|
||||||
|
wait_for_executable(test_suite.exe)
|
||||||
|
|
||||||
|
for test in test_suite.tests:
|
||||||
|
run_test(test_suite, test)
|
||||||
|
|
||||||
|
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||||
|
run_command("make clean")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
19
2025.03.07/5Ex/Makefile
Normal file
19
2025.03.07/5Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||||
|
|
||||||
|
a05.exe: main.o array_io.o solve.o init_f.o
|
||||||
|
gcc main.o solve.o array_io.o init_f.o -o a05.exe -lssp
|
||||||
|
|
||||||
|
main.o: main.c
|
||||||
|
gcc $(CFLAGS) -c main.c
|
||||||
|
|
||||||
|
solve.o: solve.c
|
||||||
|
gcc $(FLAGS) -c solve.c
|
||||||
|
|
||||||
|
array_io.o: array_io.c
|
||||||
|
gcc $(CFLAGS) -c array_io.c
|
||||||
|
|
||||||
|
init_f.o: init_f.c
|
||||||
|
gcc $(CFLAGS) -c init_f.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
del *.o *.exe
|
39
2025.03.07/5Ex/array_io.c
Normal file
39
2025.03.07/5Ex/array_io.c
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_sq_matrix(double *a, int n, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * n + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_sq_matrix(const double *a, int n, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < np; j++)
|
||||||
|
printf(" %10.3e", a[i * n + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_sq_matrix(double *a, int n, int k)
|
||||||
|
{
|
||||||
|
double (*q)(int, int, int, int);
|
||||||
|
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
|
||||||
|
int i, j;
|
||||||
|
q = f[k-1];
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
a[i * n + j] = q(n, n, i+1, j+1);
|
||||||
|
}
|
11
2025.03.07/5Ex/array_io.h
Normal file
11
2025.03.07/5Ex/array_io.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef ARRAY_IO_H
|
||||||
|
#define ARRAY_IO_H
|
||||||
|
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "init_f.h"
|
||||||
|
|
||||||
|
io_status read_sq_matrix(double *a, int n, const char *name);
|
||||||
|
void print_sq_matrix(const double *a, int n, int p);
|
||||||
|
void init_sq_matrix(double *a, int n, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.07/5Ex/init_f.c
Normal file
30
2025.03.07/5Ex/init_f.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include "init_f.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define MAX(n, m) (n < m ? m : n)
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.07/5Ex/init_f.h
Normal file
9
2025.03.07/5Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
14
2025.03.07/5Ex/io_status.h
Normal file
14
2025.03.07/5Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef IO_STATUS_H
|
||||||
|
#define IO_STATUS_H
|
||||||
|
|
||||||
|
#define LEN 1234
|
||||||
|
|
||||||
|
typedef enum _io_status
|
||||||
|
{
|
||||||
|
SUCCESS,
|
||||||
|
ERROR_OPEN,
|
||||||
|
ERROR_READ,
|
||||||
|
ERROR_MEM
|
||||||
|
} io_status;
|
||||||
|
|
||||||
|
#endif
|
65
2025.03.07/5Ex/main.c
Normal file
65
2025.03.07/5Ex/main.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "solve.h"
|
||||||
|
|
||||||
|
/* ./a.out n p k [filename] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a;
|
||||||
|
int n, p, k, task = 5;
|
||||||
|
char *name = 0;
|
||||||
|
|
||||||
|
if (!((argc == 4 || argc == 5) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &k) == 1 &&
|
||||||
|
k >= 0 && k <= 4 && (!(k == 0 && argc != 5))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n p k [filename]\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (argc == 5) name = argv[4];
|
||||||
|
|
||||||
|
a = (double *)malloc(n * n * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_sq_matrix(a, n, name);
|
||||||
|
do {
|
||||||
|
switch (ret)
|
||||||
|
{
|
||||||
|
case SUCCESS:
|
||||||
|
continue;
|
||||||
|
case ERROR_OPEN:
|
||||||
|
printf("Cannot open %s\n", name);
|
||||||
|
break;
|
||||||
|
case ERROR_READ:
|
||||||
|
printf("Cannot read %s\n", name);
|
||||||
|
}
|
||||||
|
free(a);
|
||||||
|
return 3;
|
||||||
|
} while (0);
|
||||||
|
} else init_sq_matrix(a, n, k);
|
||||||
|
|
||||||
|
printf("Initial matrix:\n");
|
||||||
|
print_sq_matrix(a, n, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
t5_solve(a, n);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf("Result matrix:\n");
|
||||||
|
print_sq_matrix(a, n, p);
|
||||||
|
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
15
2025.03.07/5Ex/solve.c
Normal file
15
2025.03.07/5Ex/solve.c
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
void t5_solve(double *a, int n)
|
||||||
|
{
|
||||||
|
double temp;
|
||||||
|
int i, j;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = i; j < n; j++)
|
||||||
|
if (i != j) {
|
||||||
|
temp = a[i * n + j];
|
||||||
|
a[i * n + j] = (temp - a[j * n + i]) / 2;
|
||||||
|
a[j * n + i] = (a[j * n + i] - temp) / 2;
|
||||||
|
} else a[i * n + j] = 0;
|
||||||
|
}
|
6
2025.03.07/5Ex/solve.h
Normal file
6
2025.03.07/5Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
void t5_solve(double *a, int n);
|
||||||
|
|
||||||
|
#endif
|
4
2025.03.07/5Ex/t.txt
Normal file
4
2025.03.07/5Ex/t.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1 2 3 4
|
||||||
|
1 2 3 4
|
||||||
|
1 2 3 4
|
||||||
|
1 2 3 4
|
102
2025.03.07/5Ex/test_cases.json
Normal file
102
2025.03.07/5Ex/test_cases.json
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
{
|
||||||
|
"exe": "a05.exe",
|
||||||
|
"filename": "matrix.txt",
|
||||||
|
"tests": [
|
||||||
|
{
|
||||||
|
"name": "File Matrix Test",
|
||||||
|
"k": 0,
|
||||||
|
"matrix": "1 2 3 4\n1 2 3 4\n1 2 3 4\n1 2 3 4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=1)",
|
||||||
|
"k": 1,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=3)",
|
||||||
|
"k": 3,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Basic Trace)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Negative Numbers)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "-1 -2 -3\n-4 -5 -6\n-7 -8 -9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Mixed Positive and Negative)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1 -2 3\n-4 5 -6\n7 -8 9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "File Matrix (Floating Point Values)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3,
|
||||||
|
"matrix": "1.1 2.2 3.3\n4.4 5.5 6.6\n7.7 8.8 9.9"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=1)",
|
||||||
|
"k": 1,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=2)",
|
||||||
|
"k": 2,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=3)",
|
||||||
|
"k": 3,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Generated Matrix (k=4, Fractional Values)",
|
||||||
|
"k": 4,
|
||||||
|
"n": 3,
|
||||||
|
"p": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "1x1 Matrix (Single Element)",
|
||||||
|
"k": 0,
|
||||||
|
"n": 1,
|
||||||
|
"p": 1,
|
||||||
|
"matrix": "42"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "2x2 Matrix",
|
||||||
|
"k": 0,
|
||||||
|
"n": 2,
|
||||||
|
"p": 2,
|
||||||
|
"matrix": "1 2\n3 4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "4x4 Identity Matrix",
|
||||||
|
"k": 0,
|
||||||
|
"n": 4,
|
||||||
|
"p": 4,
|
||||||
|
"matrix": "1 0 0 0\n0 1 0 0\n0 0 1 0\n0 0 0 1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "1000x1000 Large Matrix",
|
||||||
|
"k": 1,
|
||||||
|
"n": 1000,
|
||||||
|
"p": 5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
168
2025.03.07/5Ex/test_runner.py
Normal file
168
2025.03.07/5Ex/test_runner.py
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import platform
|
||||||
|
import re
|
||||||
|
import signal
|
||||||
|
from colorama import Fore, Style, init
|
||||||
|
|
||||||
|
# Enable color support in Windows
|
||||||
|
init(autoreset=True)
|
||||||
|
|
||||||
|
def color_text(text, color):
|
||||||
|
"""Returns colored text"""
|
||||||
|
return color + text + Style.RESET_ALL
|
||||||
|
|
||||||
|
def cleanup_and_exit():
|
||||||
|
"""Handles cleanup on Ctrl+C or forced exit"""
|
||||||
|
print(color_text("\n[ABORT] Operation interrupted. Cleaning up...", Fore.RED))
|
||||||
|
run_command("make clean")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
# Register Ctrl+C handler
|
||||||
|
signal.signal(signal.SIGINT, lambda sig, frame: cleanup_and_exit())
|
||||||
|
|
||||||
|
class TestCase:
|
||||||
|
"""Represents a single test case"""
|
||||||
|
def __init__(self, k, matrix=None, n=None, p=None, debug=False, name=None):
|
||||||
|
self.k = k
|
||||||
|
self.matrix = matrix
|
||||||
|
self.n = n
|
||||||
|
self.p = p
|
||||||
|
self.debug = debug
|
||||||
|
self.name = name if name else f"Test k={k}, n={n if n else 'auto'}, p={p if p else 'auto'}"
|
||||||
|
|
||||||
|
# Compute `n` if missing and `k == 0`
|
||||||
|
if self.k == 0 and not self.n and self.matrix:
|
||||||
|
self.n = len(self.matrix.strip().split("\n"))
|
||||||
|
|
||||||
|
# Compute `p`
|
||||||
|
self.p = self.p if self.p else self.n
|
||||||
|
|
||||||
|
def validate_inputs(self):
|
||||||
|
"""Ensures input values are valid"""
|
||||||
|
if self.k < 0 or (self.k == 0 and not self.matrix):
|
||||||
|
print(color_text(f"[ERROR] Invalid test parameters: {self.name}", Fore.RED))
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
class TestSuite:
|
||||||
|
"""Handles loading and running test cases"""
|
||||||
|
def __init__(self, config_file):
|
||||||
|
self.config = self.load_config(config_file)
|
||||||
|
self.exe = self.config["exe"]
|
||||||
|
self.filename = self.config["filename"]
|
||||||
|
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_config(filename):
|
||||||
|
"""Loads test cases from JSON"""
|
||||||
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
def run_command(cmd, exit_on_error=False):
|
||||||
|
"""Runs a shell command and handles errors"""
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||||
|
return result
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||||
|
print(e.stderr)
|
||||||
|
if exit_on_error:
|
||||||
|
exit(1)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def wait_for_executable(exe):
|
||||||
|
"""Waits for the executable file to appear after compilation"""
|
||||||
|
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||||
|
while not os.path.exists(exe):
|
||||||
|
time.sleep(0.1) # Reduce CPU usage
|
||||||
|
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||||
|
|
||||||
|
def format_matrix(matrix):
|
||||||
|
"""Formats a matrix to match `printf("%10.3e")` output"""
|
||||||
|
formatted = []
|
||||||
|
for row in matrix.strip().split("\n"):
|
||||||
|
formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split()))
|
||||||
|
return "\n".join(formatted)
|
||||||
|
|
||||||
|
def result_matrix_output(output):
|
||||||
|
parts = output.split("Result matrix:\n")
|
||||||
|
if len(parts) > 1:
|
||||||
|
matrix_lines = parts[1].strip().split("\n")
|
||||||
|
return format_matrix("\n".join(matrix_lines[:-1]))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def initial_matrix_output(output):
|
||||||
|
parts = output.split("Initial matrix:\n")
|
||||||
|
if len(parts) > 1:
|
||||||
|
parts = parts[1].split("Result")
|
||||||
|
matrix_lines = parts[0].strip().split("\n")
|
||||||
|
return format_matrix("\n".join(matrix_lines))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def asymmetric_matrix(matrix):
|
||||||
|
rows = [list(map(float, row.split())) for row in matrix.strip().split("\n")]
|
||||||
|
symmetrize = [[((rows[i][j] - rows[j][i]) / 2) for j in range(len(rows[i]))] for i in range(len(rows))]
|
||||||
|
return "\n".join(" ".join(f"{float(num):10.3e}" for num in row) for row in symmetrize)
|
||||||
|
|
||||||
|
def run_test(test_suite, test):
|
||||||
|
"""Runs the program and checks its result"""
|
||||||
|
if not test.validate_inputs():
|
||||||
|
return
|
||||||
|
|
||||||
|
exe, filename = test_suite.exe, test_suite.filename
|
||||||
|
|
||||||
|
# If matrix is given, write it to the file
|
||||||
|
if test.k == 0 and test.matrix:
|
||||||
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
|
f.write(format_matrix(test.matrix.strip()) + "\n")
|
||||||
|
|
||||||
|
cmd = [exe, str(test.n), str(test.p), str(test.k)]
|
||||||
|
if test.k == 0:
|
||||||
|
cmd.append(filename)
|
||||||
|
|
||||||
|
# Run the program
|
||||||
|
result = run_command(cmd)
|
||||||
|
|
||||||
|
# Extract both initial and result matrices
|
||||||
|
initial_matrix = initial_matrix_output(result.stdout)
|
||||||
|
result_matrix = result_matrix_output(result.stdout)
|
||||||
|
|
||||||
|
expected_transposed = asymmetric_matrix(initial_matrix)
|
||||||
|
|
||||||
|
if result_matrix.strip() != expected_transposed.strip():
|
||||||
|
print(color_text(f"[FAIL] Test '{test.name}' matrix mismatch.", Fore.RED))
|
||||||
|
print(f"Expected:\n{expected_transposed}")
|
||||||
|
print(f"Got:\n{result_matrix}")
|
||||||
|
return
|
||||||
|
|
||||||
|
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
||||||
|
|
||||||
|
if test.k == 0:
|
||||||
|
# Cleanup test file
|
||||||
|
try:
|
||||||
|
os.remove(filename)
|
||||||
|
except (FileNotFoundError, PermissionError):
|
||||||
|
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||||
|
run_command("make clean", exit_on_error=True)
|
||||||
|
|
||||||
|
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||||
|
run_command("make", exit_on_error=True)
|
||||||
|
|
||||||
|
test_suite = TestSuite("test_cases.json")
|
||||||
|
wait_for_executable(test_suite.exe)
|
||||||
|
|
||||||
|
for test in test_suite.tests:
|
||||||
|
run_test(test_suite, test)
|
||||||
|
|
||||||
|
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||||
|
run_command("make clean")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Add table
Add a link
Reference in a new issue