The Ninth and Tenth tasks are done
This commit is contained in:
parent
5a81ed3cd8
commit
62be214e9d
26 changed files with 1338 additions and 0 deletions
19
2025.03.07/10Ex/Makefile
Normal file
19
2025.03.07/10Ex/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
|
||||
|
||||
a10.exe: main.o array_io.o solve.o init_f.o
|
||||
gcc main.o solve.o array_io.o init_f.o -o a10.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
|
2
2025.03.07/10Ex/a.txt
Normal file
2
2025.03.07/10Ex/a.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
1 2
|
||||
3 4
|
40
2025.03.07/10Ex/array_io.c
Normal file
40
2025.03.07/10Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include "array_io.h"
|
||||
|
||||
io_status read_matrix(double *a, int n, int m, 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 < m; j++)
|
||||
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||
{fclose(fp); return ERROR_READ;}
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_matrix(const double *a, int n, int m, int p)
|
||||
{
|
||||
int np = (n > p ? p : n);
|
||||
int mp = (m > p ? p : m);
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < np; i++)
|
||||
{
|
||||
for (j = 0; j < mp; j++)
|
||||
printf(" %10.3e", a[i * m + j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_matrix(double *a, int n, int m, 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 < m; j++)
|
||||
a[i * m + j] = q(n, m, i+1, j+1);
|
||||
}
|
11
2025.03.07/10Ex/array_io.h
Normal file
11
2025.03.07/10Ex/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_matrix(double *a, int n, int m, const char *name);
|
||||
void print_matrix(const double *a, int n, int m, int p);
|
||||
void init_matrix(double *a, int n, int m, int k);
|
||||
|
||||
#endif
|
2
2025.03.07/10Ex/b.txt
Normal file
2
2025.03.07/10Ex/b.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
1 0 2
|
||||
0 1 3
|
30
2025.03.07/10Ex/init_f.c
Normal file
30
2025.03.07/10Ex/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/10Ex/init_f.h
Normal file
9
2025.03.07/10Ex/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/10Ex/io_status.h
Normal file
14
2025.03.07/10Ex/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
|
131
2025.03.07/10Ex/main.c
Normal file
131
2025.03.07/10Ex/main.c
Normal file
|
@ -0,0 +1,131 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "array_io.h"
|
||||
#include "io_status.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a.out n m k p k_a [filename_a] k_b [filename_b] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a, *b, *c;
|
||||
int n, m, k, p, k_a, k_b, task = 10;
|
||||
char *name_a = 0, *name_b = 0;
|
||||
|
||||
// TODO: CHECK usage parameters
|
||||
if (!((argc == 7 || argc == 8 || argc == 9) &&
|
||||
sscanf(argv[1], "%d", &n) == 1 &&
|
||||
sscanf(argv[2], "%d", &m) == 1 &&
|
||||
sscanf(argv[3], "%d", &k) == 1 &&
|
||||
sscanf(argv[4], "%d", &p) == 1 &&
|
||||
sscanf(argv[5], "%d", &k_a) == 1 &&
|
||||
(k_a >= 0 && k_a <= 4) &&
|
||||
(!(k_a == 0 && argc == 7)) &&
|
||||
((k_a == 0 && sscanf(argv[7], "%d", &k_b) == 1) ||
|
||||
(k_a != 0 && sscanf(argv[6], "%d", &k_b) == 1)) &&
|
||||
(k_b >= 0 && k_b <= 4) &&
|
||||
(!(k_b == 0 && argc == 7)) &&
|
||||
(!((k_a == 0 && k_b == 0) && argc != 9))))
|
||||
{
|
||||
printf("Usage: %s n m k p k_a [filename_a] k_b [filename_b]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (argc != 7)
|
||||
{
|
||||
int i_b = 7;
|
||||
if (k_a == 0) { name_a = argv[6]; i_b++; }
|
||||
if (k_b == 0) name_b = argv[i_b];
|
||||
}
|
||||
|
||||
a = (double *)malloc(n * m * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
b = (double *)malloc(m * k * sizeof(double));
|
||||
if (!b)
|
||||
{
|
||||
free(a);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
c = (double *)malloc(n * k * sizeof(double));
|
||||
if (!c)
|
||||
{
|
||||
free(a);
|
||||
free(b);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name_a)
|
||||
{ /* Читаем матрицу A из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(a, n, m, name_a);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_a);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_a);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
return 3;
|
||||
} while (0);
|
||||
}
|
||||
else init_matrix(a, n, m, k_a);
|
||||
|
||||
if (name_b)
|
||||
{ /* Читаем матрицу B из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(b, m, k, name_b);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_b);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_b);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
return 3;
|
||||
} while (0);
|
||||
}
|
||||
else init_matrix(b, m, k, k_b);
|
||||
|
||||
printf("Initial matrix A:\n");
|
||||
print_matrix(a, n, m, p);
|
||||
printf("Initial vector b:\n");
|
||||
print_matrix(b, m, k, p);
|
||||
|
||||
t = clock();
|
||||
matrix_multiply(a, b, c, n, m, k);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Result vector c:\n");
|
||||
print_matrix(c, n, k, p);
|
||||
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
|
||||
return 0;
|
||||
}
|
15
2025.03.07/10Ex/solve.c
Normal file
15
2025.03.07/10Ex/solve.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "solve.h"
|
||||
#include "math.h"
|
||||
|
||||
void matrix_multiply(const double *A, const double *B, double *c, int n, int m, int l)
|
||||
{
|
||||
int i, j, k, index;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < l; j++)
|
||||
{
|
||||
index = i * l + j;
|
||||
c[index] = 0;
|
||||
for (k = 0; k < m; k++)
|
||||
c[index] += A[i * m + k] * B[k * l + j];
|
||||
}
|
||||
}
|
6
2025.03.07/10Ex/solve.h
Normal file
6
2025.03.07/10Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
void matrix_multiply(const double *A, const double *B, double *c, int n, int m, int l);
|
||||
|
||||
#endif
|
35
2025.03.07/10Ex/test_cases.json
Normal file
35
2025.03.07/10Ex/test_cases.json
Normal file
|
@ -0,0 +1,35 @@
|
|||
{
|
||||
"exe": "a10.exe",
|
||||
"fA": "matA.txt",
|
||||
"fB": "matB.txt",
|
||||
"tests": [
|
||||
{
|
||||
"name": "Gen A, Gen B",
|
||||
"n": 4,
|
||||
"m": 4,
|
||||
"k": 4,
|
||||
"p": 4,
|
||||
"kA": 1,
|
||||
"kB": 2
|
||||
},
|
||||
{
|
||||
"name": "File A, Gen B",
|
||||
"kA": 0,
|
||||
"kB": 4,
|
||||
"matrix_a": "1 2 3 4\n5 6 7 8\n9 0 1 2",
|
||||
"n": 3,
|
||||
"m": 4,
|
||||
"k": 3
|
||||
},
|
||||
{
|
||||
"name": "File A, File B",
|
||||
"kA": 0,
|
||||
"kB": 0,
|
||||
"matrix_a": "1 2\n3 4",
|
||||
"matrix_b": "1 0 2\n0 1 3",
|
||||
"n": 2,
|
||||
"m": 2,
|
||||
"k": 3
|
||||
}
|
||||
]
|
||||
}
|
442
2025.03.07/10Ex/test_runner.py
Normal file
442
2025.03.07/10Ex/test_runner.py
Normal file
|
@ -0,0 +1,442 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
import re
|
||||
import signal
|
||||
from colorama import Fore, Style, init
|
||||
import math
|
||||
|
||||
init(autoreset=True) # Enable color support in Windows
|
||||
|
||||
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 for matrix multiplication:
|
||||
n, m, k: dimensions
|
||||
p: number of displayed rows/columns
|
||||
kA: formula for matrix A (0 => read from file)
|
||||
kB: formula for matrix B (0 => read from file)
|
||||
matrix_a, matrix_b: text for input files if kA=0 or kB=0
|
||||
name: optional test name
|
||||
"""
|
||||
def __init__(self,
|
||||
n=None,
|
||||
m=None,
|
||||
k=None,
|
||||
p=None,
|
||||
kA=None,
|
||||
kB=None,
|
||||
matrix_a=None,
|
||||
matrix_b=None,
|
||||
name=None):
|
||||
|
||||
# Required parameters
|
||||
self.n = n
|
||||
self.m = m
|
||||
self.k = k
|
||||
self.kA = kA
|
||||
self.kB = kB
|
||||
|
||||
# Possibly missing p => p = max(n, m, k)
|
||||
self.p = p
|
||||
|
||||
# If kA=0 => matrix_a must be provided
|
||||
self.matrix_a = matrix_a
|
||||
# If kB=0 => matrix_b must be provided
|
||||
self.matrix_b = matrix_b
|
||||
|
||||
# If no name => auto
|
||||
self.name = name if name else f"Test (n={n}, m={m}, k={k}, kA={kA}, kB={kB})"
|
||||
|
||||
# Fix up the dimension logic:
|
||||
# n not required if kA=0 => read from matrix_a lines
|
||||
# m not required if kA=0 => read from matrix_a columns or lines from matrix_b
|
||||
# k not required if kB=0 => read from matrix_b columns
|
||||
# p => default = max(n,m,k)
|
||||
self._fix_dimensions()
|
||||
|
||||
def _fix_dimensions(self):
|
||||
"""Resolve missing n, m, k, p from matrix files if needed."""
|
||||
# If kA=0 => we must figure out n,m from matrix_a
|
||||
if self.kA == 0 and self.matrix_a:
|
||||
lines_a = self.matrix_a.strip().split("\n")
|
||||
_n = len(lines_a)
|
||||
_m = len(lines_a[0].split()) if _n > 0 else 0
|
||||
if not self.n:
|
||||
self.n = _n
|
||||
if not self.m:
|
||||
self.m = _m
|
||||
|
||||
# If kB=0 => figure out dimensions from matrix_b
|
||||
if self.kB == 0 and self.matrix_b:
|
||||
lines_b = self.matrix_b.strip().split("\n")
|
||||
_m2 = len(lines_b)
|
||||
_k = len(lines_b[0].split()) if _m2 > 0 else 0
|
||||
if not self.m:
|
||||
self.m = _m2
|
||||
if not self.k:
|
||||
self.k = _k
|
||||
|
||||
# If p not specified => p = max(n,m,k)
|
||||
if not self.p:
|
||||
self.p = max(self.n, self.m, self.k)
|
||||
|
||||
def validate_inputs(self):
|
||||
"""Check minimal validity for the test."""
|
||||
if self.kA is None or self.kB is None:
|
||||
print(color_text(f"[ERROR] Missing kA/kB in test '{self.name}'", Fore.RED))
|
||||
return False
|
||||
if self.kA == 0 and not self.matrix_a:
|
||||
print(color_text(f"[ERROR] kA=0 but no matrix_a provided: {self.name}", Fore.RED))
|
||||
return False
|
||||
if self.kB == 0 and not self.matrix_b:
|
||||
print(color_text(f"[ERROR] kB=0 but no matrix_b provided: {self.name}", Fore.RED))
|
||||
return False
|
||||
if any(x is None for x in [self.n, self.m, self.k]):
|
||||
print(color_text(f"[ERROR] Dimensions not resolved in test '{self.name}'", Fore.RED))
|
||||
return False
|
||||
return True
|
||||
|
||||
class TestSuite:
|
||||
"""Loads the config, builds TestCase objects, tracks exe / files."""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
# fA / fB are file names for matrix A and B input
|
||||
self.fA = self.config["fA"]
|
||||
self.fB = self.config["fB"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs shell command with errors captured."""
|
||||
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):
|
||||
"""Wait for the .exe 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)
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def format_matrix(text):
|
||||
"""
|
||||
Convert a raw matrix text (e.g. '1 2 3\n4 5 6') into a string with each
|
||||
value formatted as '%10.3e' to match the program's output.
|
||||
"""
|
||||
lines = text.strip().split("\n")
|
||||
out_lines = []
|
||||
for line in lines:
|
||||
nums = line.split()
|
||||
fmt_nums = [f"{float(n):10.3e}" for n in nums]
|
||||
out_lines.append(" ".join(fmt_nums))
|
||||
return "\n".join(out_lines)
|
||||
|
||||
def parse_matrix_output(output, label):
|
||||
"""
|
||||
Extracts a matrix from the test program's output, e.g.:
|
||||
"Initial matrix A:\n ... \n... \nInitial vector b:"
|
||||
If not found, returns ''.
|
||||
"""
|
||||
pattern = rf"{label}:\s*\n(.*?)\n[a-zA-Z]" # match until next label or next letter
|
||||
match = re.search(pattern, output, flags=re.DOTALL)
|
||||
if match:
|
||||
raw = match.group(1).strip()
|
||||
return raw
|
||||
return ""
|
||||
|
||||
def matrix_multiply(A, B, n, m, k):
|
||||
"""
|
||||
Multiply A(n×m) by B(m×k) => C(n×k).
|
||||
A, B are lists of lists (floats).
|
||||
Returns list of lists for C.
|
||||
"""
|
||||
# Initialize C
|
||||
C = [[0.0]*k for _ in range(n)]
|
||||
for i in range(n):
|
||||
for j in range(k):
|
||||
val = 0.0
|
||||
for z in range(m):
|
||||
val += A[i][z]*B[z][j]
|
||||
C[i][j] = val
|
||||
return C
|
||||
|
||||
def parse_matrix_lines(matrix_str):
|
||||
"""
|
||||
Convert matrix string (with ' %10.3e') lines => list of lists (floats).
|
||||
"""
|
||||
lines = matrix_str.strip().split("\n")
|
||||
mat = []
|
||||
for ln in lines:
|
||||
row = ln.strip().split()
|
||||
# row = ['1.000e+000','2.000e+000',...]
|
||||
mat.append([float(x) for x in row])
|
||||
return mat
|
||||
|
||||
def mat_to_str(mat):
|
||||
"""
|
||||
Convert a list-of-lists (floats) to the string with '%10.3e' format.
|
||||
"""
|
||||
lines = []
|
||||
for row in mat:
|
||||
lines.append(" ".join(f"{val:10.3e}" for val in row))
|
||||
return "\n".join(lines)
|
||||
|
||||
def generate_matrix(n, m, k_formula):
|
||||
"""
|
||||
If k_formula !=0 => generate an n×m matrix using formula f(k_formula, n, m, i, j).
|
||||
"""
|
||||
def f(kf, n, m, i, j):
|
||||
if kf == 1:
|
||||
return max(n, m) - max(i, j) + 1
|
||||
elif kf == 2:
|
||||
return max(i, j)
|
||||
elif kf == 3:
|
||||
return abs(i-j)
|
||||
elif kf == 4:
|
||||
return 1.0/(i+j-1) if (i+j-1)!=0 else 0
|
||||
return 0
|
||||
|
||||
mat = []
|
||||
for i in range(1, n+1):
|
||||
row = []
|
||||
for j in range(1, m+1):
|
||||
row.append(f(k_formula, n, m, i, j))
|
||||
mat.append(row)
|
||||
return mat
|
||||
|
||||
import math
|
||||
|
||||
def print_matrix_mismatch(expected, actual, matrix_name, test_name):
|
||||
"""
|
||||
Печатает подробную информацию о несовпадении двух матриц.
|
||||
expected, actual: list-of-lists (float values)
|
||||
matrix_name: как назвать матрицу (например, "Matrix A" или "Matrix B")
|
||||
test_name: имя теста или любая другая справочная информация
|
||||
"""
|
||||
print(f"[FAIL] {test_name} - mismatch in {matrix_name}")
|
||||
|
||||
# Полный вывод матриц
|
||||
print(f"\n{matrix_name} (EXPECTED):")
|
||||
print(_matrix_to_str(expected))
|
||||
print(f"\n{matrix_name} (ACTUAL):")
|
||||
print(_matrix_to_str(actual))
|
||||
|
||||
# Если есть разница в размере — выводим
|
||||
if len(expected) != len(actual) or any(len(e) != len(a) for e,a in zip(expected, actual)):
|
||||
print("\n[INFO] Shape mismatch.")
|
||||
return
|
||||
|
||||
# Вывод различий поэлементно
|
||||
print("\n[INFO] Differences (index, expected, got):")
|
||||
found_diff = False
|
||||
for i, (rowE, rowA) in enumerate(zip(expected, actual)):
|
||||
for j, (valE, valA) in enumerate(zip(rowE, rowA)):
|
||||
if not math.isclose(valE, valA, rel_tol=1e-3, abs_tol=1e-4):
|
||||
found_diff = True
|
||||
print(f" [{i},{j}] {valE:10.3e} != {valA:10.3e}")
|
||||
if not found_diff:
|
||||
print(" No elementwise differences found (shape was likely mismatched or zero-size).")
|
||||
|
||||
def _matrix_to_str(matrix):
|
||||
"""
|
||||
Вспомогательная функция для форматирования матрицы:
|
||||
печатает элементы в стиле '%10.3e' (как в тестируемой программе).
|
||||
"""
|
||||
lines = []
|
||||
for row in matrix:
|
||||
line = " ".join(f"{val:10.3e}" for val in row)
|
||||
lines.append(line)
|
||||
return "\n".join(lines)
|
||||
|
||||
def print_matrix_shape_mismatch(expected, actual, matrix_name, test_name):
|
||||
"""
|
||||
Выводит информацию при несовпадении размеров (shape) двух матриц.
|
||||
expected, actual: list-of-lists (float values)
|
||||
matrix_name: как назвать матрицу (например, "Matrix A" или "Matrix B")
|
||||
test_name: имя теста или любая другая справочная информация
|
||||
"""
|
||||
print(f"[FAIL] {test_name} - mismatch shape in {matrix_name}")
|
||||
|
||||
len_expected = len(expected)
|
||||
len_actual = len(actual)
|
||||
print(f" {matrix_name} (EXPECTED) shape: {len_expected} x {len(expected[0]) if len_expected>0 else 0}")
|
||||
print(f" {matrix_name} (ACTUAL) shape: {len_actual} x {len(actual[0]) if len_actual>0 else 0}")
|
||||
|
||||
# Если ещё неясно, где именно несоответствие,
|
||||
# можно вывести детальнее по каждой строке:
|
||||
if len_expected == len_actual:
|
||||
for i, (rowE, rowA) in enumerate(zip(expected, actual)):
|
||||
if len(rowE) != len(rowA):
|
||||
print(f" Row {i} length mismatch: {len(rowE)} != {len(rowA)}")
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Main test logic."""
|
||||
if not test.validate_inputs():
|
||||
return
|
||||
|
||||
# Prepare matrix A, B => write to fA, fB if kA=0 or kB=0
|
||||
# Or generate them if kA!=0 / kB!=0
|
||||
# 1) build/format matrix A => string => write to fA if kA=0
|
||||
# 2) build/format matrix B => string => write to fB if kB=0
|
||||
|
||||
fA = test_suite.fA
|
||||
fB = test_suite.fB
|
||||
|
||||
# Build A
|
||||
if test.kA == 0 and test.matrix_a:
|
||||
# Write matrix_a (formatted) to fA
|
||||
with open(fA, "w", encoding="utf-8") as fa:
|
||||
fa.write(format_matrix(test.matrix_a) + "\n")
|
||||
# Also parse it as 2D float array for local multiplication
|
||||
A_list = parse_matrix_lines(format_matrix(test.matrix_a))
|
||||
else:
|
||||
# generate
|
||||
A_list = generate_matrix(test.n, test.m, test.kA)
|
||||
|
||||
# Build B
|
||||
if test.kB == 0 and test.matrix_b:
|
||||
with open(fB, "w", encoding="utf-8") as fb:
|
||||
fb.write(format_matrix(test.matrix_b) + "\n")
|
||||
B_list = parse_matrix_lines(format_matrix(test.matrix_b))
|
||||
else:
|
||||
B_list = generate_matrix(test.m, test.k, test.kB)
|
||||
|
||||
# -- Build command line --
|
||||
# n m k p kA [fA_if kA=0] kB [fB_if kB=0]
|
||||
cmd_list = [test_suite.exe,
|
||||
str(test.n), str(test.m), str(test.k),
|
||||
str(test.p),
|
||||
str(test.kA)]
|
||||
if test.kA==0:
|
||||
cmd_list.append(fA)
|
||||
cmd_list.append(str(test.kB))
|
||||
if test.kB==0:
|
||||
cmd_list.append(fB)
|
||||
|
||||
cmd = " ".join(cmd_list)
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# 2) Extract matrix A => "Initial matrix A:"
|
||||
# matrix B => "Initial vector b:"
|
||||
# matrix C => "Result vector c:"
|
||||
output = result.stdout
|
||||
initA_str = parse_matrix_output(output, "Initial matrix A")
|
||||
initB_str = parse_matrix_output(output, "Initial vector b")
|
||||
resC_str = parse_matrix_output(output, "Result vector c")
|
||||
|
||||
# Convert them to list-of-lists
|
||||
initA_list = parse_matrix_lines(format_matrix(initA_str))
|
||||
initB_list = parse_matrix_lines(format_matrix(initB_str))
|
||||
resC_list = parse_matrix_lines(format_matrix(resC_str))
|
||||
|
||||
# 3) Compare initA_list with A_list (both 2D arrays)
|
||||
# Compare initB_list with B_list
|
||||
# Then multiply A_list * B_list => localC_list
|
||||
# Compare localC_list with resC_list
|
||||
# Make sure shapes match.
|
||||
|
||||
# Compare shapes for A
|
||||
if len(initA_list) != len(A_list) or any(len(rowA) != len(rowB) for rowA, rowB in zip(initA_list, A_list)):
|
||||
print(color_text(f"[FAIL] {test.name} - mismatch shape in A", Fore.RED))
|
||||
print_matrix_shape_mismatch(A_list, initA_list, "Matrix A", test.name)
|
||||
return
|
||||
|
||||
# Compare each element
|
||||
for rowA, rowB in zip(initA_list, A_list):
|
||||
for valA, valB in zip(rowA, rowB):
|
||||
if not math.isclose(valA, valB, rel_tol=1e-3, abs_tol=1e-4):
|
||||
print(color_text(f"[FAIL] {test.name} - mismatch in matrix A", Fore.RED))
|
||||
print_matrix_mismatch(A_list, initA_list, "Matrix A", test.name)
|
||||
return
|
||||
|
||||
# Compare shapes for B
|
||||
if len(initB_list) != len(B_list) or any(len(rA) != len(rB) for rA, rB in zip(initB_list, B_list)):
|
||||
print(color_text(f"[FAIL] {test.name} - mismatch shape in B", Fore.RED))
|
||||
print_matrix_shape_mismatch(B_list, initB_list, "Matrix B", test.name)
|
||||
return
|
||||
|
||||
for rowA, rowB in zip(initB_list, B_list):
|
||||
for valA, valB in zip(rowA, rowB):
|
||||
if not math.isclose(valA, valB, rel_tol=1e-3, abs_tol=1e-4):
|
||||
print(color_text(f"[FAIL] {test.name} - mismatch in matrix B", Fore.RED))
|
||||
print_matrix_mismatch(B_list, initB_list, "Matrix B", test.name)
|
||||
return
|
||||
|
||||
# Multiply local
|
||||
localC_list = matrix_multiply(A_list, B_list, test.n, test.m, test.k)
|
||||
|
||||
# Compare shape with resC_list
|
||||
if len(resC_list) != len(localC_list) or any(len(aRow) != len(bRow) for aRow,bRow in zip(resC_list, localC_list)):
|
||||
print(color_text(f"[FAIL] {test.name} - mismatch shape in C", Fore.RED))
|
||||
print_matrix_shape_mismatch(localC_list, resC_list, "Matrix A", test.name)
|
||||
return
|
||||
|
||||
# Compare each element
|
||||
for rC, lC in zip(resC_list, localC_list):
|
||||
for vC, vLoc in zip(rC, lC):
|
||||
if not math.isclose(vC, vLoc, rel_tol=1e-3, abs_tol=1e-4):
|
||||
print(color_text(f"[FAIL] {test.name} - mismatch in matrix C", Fore.RED))
|
||||
print_matrix_mismatch(resC_list, localC_list, "Matrix C", test.name)
|
||||
return
|
||||
|
||||
print(color_text(f"[PASS] {test.name}", Fore.GREEN))
|
||||
|
||||
# Cleanup
|
||||
if test.kA==0:
|
||||
try:
|
||||
os.remove(test_suite.fA)
|
||||
except:
|
||||
pass
|
||||
if test.kB==0:
|
||||
try:
|
||||
os.remove(test_suite.fB)
|
||||
except:
|
||||
pass
|
||||
|
||||
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/9Ex/Makefile
Normal file
19
2025.03.07/9Ex/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
|
||||
|
||||
a09.exe: main.o array_io.o solve.o init_f.o
|
||||
gcc main.o solve.o array_io.o init_f.o -o a09.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
|
5
2025.03.07/9Ex/a.txt
Normal file
5
2025.03.07/9Ex/a.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
1 2 3 4
|
||||
4 3 2 1
|
||||
1 2 3 4
|
||||
4 3 2 1
|
||||
1 2 3 4
|
40
2025.03.07/9Ex/array_io.c
Normal file
40
2025.03.07/9Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include "array_io.h"
|
||||
|
||||
io_status read_matrix(double *a, int n, int m, 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 < m; j++)
|
||||
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||
{fclose(fp); return ERROR_READ;}
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_matrix(const double *a, int n, int m, int p)
|
||||
{
|
||||
int np = (n > p ? p : n);
|
||||
int mp = (m > p ? p : m);
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < np; i++)
|
||||
{
|
||||
for (j = 0; j < mp; j++)
|
||||
printf(" %10.3e", a[i * m + j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_matrix(double *a, int n, int m, 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 < m; j++)
|
||||
a[i * m + j] = q(n, m, i+1, j+1);
|
||||
}
|
11
2025.03.07/9Ex/array_io.h
Normal file
11
2025.03.07/9Ex/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_matrix(double *a, int n, int m, const char *name);
|
||||
void print_matrix(const double *a, int n, int m, int p);
|
||||
void init_matrix(double *a, int n, int m, int k);
|
||||
|
||||
#endif
|
4
2025.03.07/9Ex/b.txt
Normal file
4
2025.03.07/9Ex/b.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1
|
||||
2
|
||||
3
|
||||
4
|
30
2025.03.07/9Ex/init_f.c
Normal file
30
2025.03.07/9Ex/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/9Ex/init_f.h
Normal file
9
2025.03.07/9Ex/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/9Ex/io_status.h
Normal file
14
2025.03.07/9Ex/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
|
124
2025.03.07/9Ex/main.c
Normal file
124
2025.03.07/9Ex/main.c
Normal file
|
@ -0,0 +1,124 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "array_io.h"
|
||||
#include "io_status.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a.out n m p k_a [filename_a] k_b [filename_b] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a, *b, *c;
|
||||
int n, m, p, k_a, k_b, task = 9;
|
||||
char *name_a = 0, *name_b = 0;
|
||||
// TODO: CHECK usage parametrs
|
||||
if (!((argc == 6 || argc == 7 || argc == 8) &&
|
||||
sscanf(argv[1], "%d", &n) == 1 &&
|
||||
sscanf(argv[2], "%d", &m) == 1 &&
|
||||
sscanf(argv[3], "%d", &p) == 1 &&
|
||||
sscanf(argv[4], "%d", &k_a) == 1 &&
|
||||
(k_a >= 0 && k_a <= 4) &&
|
||||
(!(k_a == 0 && argc == 6)) &&
|
||||
((k_a == 0 && sscanf(argv[6], "%d", &k_b) == 1) ||
|
||||
(k_a != 0 && sscanf(argv[5], "%d", &k_b) == 1)) &&
|
||||
(k_b >= 0 && k_b <= 4) &&
|
||||
(!(k_b == 0 && argc == 6)) &&
|
||||
(!((k_a == 0 && k_b == 0) && argc != 8))))
|
||||
{
|
||||
printf("Usage: %s n m p k_a [filename_a] k_b [filename_b]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc != 6)
|
||||
{
|
||||
int i_b = 6;
|
||||
if (k_a == 0) {name_a = argv[5];i_b++;}
|
||||
if (k_b == 0) name_b = argv[i_b];
|
||||
}
|
||||
|
||||
a = (double *)malloc(n * m * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
b = (double *)malloc(m * sizeof(double));
|
||||
if (!b)
|
||||
{
|
||||
free(a);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
c = (double *)malloc(n * sizeof(double));
|
||||
if (!c)
|
||||
{
|
||||
free(a);
|
||||
free(b);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name_a)
|
||||
{ /* из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(a, n, m, name_a);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_a);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_a);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(a, n, m, k_a);
|
||||
|
||||
if (name_b)
|
||||
{
|
||||
io_status ret;
|
||||
ret = read_matrix(b, m, 1, name_b);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_b);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_b);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(b, m, 1, k_b);
|
||||
|
||||
printf("Initial matrix A:\n");
|
||||
print_matrix(a, n, m, p);
|
||||
printf("Initial vector b:\n");
|
||||
print_matrix(b, m, 1, p);
|
||||
|
||||
t = clock();
|
||||
t9_solve(a, b, c, n, m);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Result vector c:\n");
|
||||
print_matrix(c, n, 1, p);
|
||||
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
|
||||
return 0;
|
||||
}
|
20
2025.03.07/9Ex/solve.c
Normal file
20
2025.03.07/9Ex/solve.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "solve.h"
|
||||
#include "math.h"
|
||||
|
||||
void t9_solve(const double *A, const double *b, double *c, int n, int m)
|
||||
{
|
||||
matrix_multiply(A, b, c, n, m, 1);
|
||||
}
|
||||
|
||||
void matrix_multiply(const double *A, const double *B, double *c, int n, int m, int l)
|
||||
{
|
||||
int i, j, k, index;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < l; j++)
|
||||
{
|
||||
index = i * l + j;
|
||||
c[index] = 0;
|
||||
for (k = 0; k < m; k++)
|
||||
c[index] += A[i * m + k] * B[k * l + j];
|
||||
}
|
||||
}
|
7
2025.03.07/9Ex/solve.h
Normal file
7
2025.03.07/9Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
void t9_solve(const double *A, const double *b, double *c, int n, int m);
|
||||
void matrix_multiply(const double *A, const double *B, double *c, int n, int m, int l);
|
||||
|
||||
#endif
|
123
2025.03.07/9Ex/test_cases.json
Normal file
123
2025.03.07/9Ex/test_cases.json
Normal file
|
@ -0,0 +1,123 @@
|
|||
{
|
||||
"exe": "a08.exe",
|
||||
"filename": "matrix.txt",
|
||||
"tests": [
|
||||
{
|
||||
"name": "File Matrix Test",
|
||||
"i": 1,
|
||||
"j": 2,
|
||||
"g": 2,
|
||||
"k": 0,
|
||||
"p": 5,
|
||||
"matrix": "1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16\n17 18 19 20"
|
||||
},
|
||||
{
|
||||
"name": "Basic Addition",
|
||||
"i": 1,
|
||||
"j": 2,
|
||||
"g": 2,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||
},
|
||||
{
|
||||
"name": "Negative Gamma",
|
||||
"i": 1,
|
||||
"j": 3,
|
||||
"g": -1,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||
},
|
||||
{
|
||||
"name": "Large 1000x1000 Matrix",
|
||||
"i": 500,
|
||||
"j": 999,
|
||||
"g": 1.5,
|
||||
"n": 1000,
|
||||
"m": 1000,
|
||||
"k": 1
|
||||
},
|
||||
{
|
||||
"name": "Floating Point Values",
|
||||
"i": 1,
|
||||
"j": 3,
|
||||
"g": 0.5,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "1.1 2.2 3.3\n4.4 5.5 6.6\n7.7 8.8 9.9"
|
||||
},
|
||||
{
|
||||
"name": "Negative Values",
|
||||
"i": 1,
|
||||
"j": 2,
|
||||
"g": -2,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "-1 -2 -3\n-4 -5 -6\n-7 -8 -9"
|
||||
},
|
||||
{
|
||||
"name": "Adding a Row to Itself (Should Double)",
|
||||
"i": 2,
|
||||
"j": 2,
|
||||
"g": 1,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||
},
|
||||
{
|
||||
"name": "Adding Row with Gamma = 0 (No Change)",
|
||||
"i": 1,
|
||||
"j": 3,
|
||||
"g": 0,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "1 2 3\n4 5 6\n7 8 9"
|
||||
},
|
||||
{
|
||||
"name": "Identity Matrix Transformation",
|
||||
"i": 2,
|
||||
"j": 3,
|
||||
"g": 1,
|
||||
"n": 3,
|
||||
"m": 3,
|
||||
"p": 3,
|
||||
"k": 0,
|
||||
"matrix": "1 0 0\n0 1 0\n0 0 1"
|
||||
},
|
||||
{
|
||||
"name": "All Zeroes",
|
||||
"i": 1,
|
||||
"j": 2,
|
||||
"g": 5,
|
||||
"n": 4,
|
||||
"m": 4,
|
||||
"p": 4,
|
||||
"k": 0,
|
||||
"matrix": "0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0"
|
||||
},
|
||||
{
|
||||
"name": "Different Row Sizes",
|
||||
"i": 2,
|
||||
"j": 3,
|
||||
"g": 3,
|
||||
"n": 4,
|
||||
"m": 5,
|
||||
"p": 5,
|
||||
"k": 0,
|
||||
"matrix": "10 20 30 40 50\n60 70 80 90 100\n110 120 130 140 150\n160 170 180 190 200"
|
||||
}
|
||||
]
|
||||
}
|
176
2025.03.07/9Ex/test_runner.py
Normal file
176
2025.03.07/9Ex/test_runner.py
Normal file
|
@ -0,0 +1,176 @@
|
|||
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, i, j, g, k, matrix=None, n=None, m=None, p=None, debug=False, name=None):
|
||||
self.i = i
|
||||
self.j = j
|
||||
self.g = g
|
||||
self.k = k
|
||||
self.matrix = matrix
|
||||
self.n = n
|
||||
self.m = m
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
self.name = name if name else f"Test k={k}, n={n if n else 'auto'}, m={m if m else 'auto'}, p={p if p else 'auto'}"
|
||||
|
||||
# Compute `n` if missing and `k == 0`
|
||||
if self.k == 0 and self.matrix:
|
||||
if not self.n:
|
||||
self.n = len(self.matrix.strip().split("\n"))
|
||||
if not self.m:
|
||||
self.m = len(self.matrix.strip().split("\n")[0].split())
|
||||
|
||||
# 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 ep1_matrix(matrix, i, j, g):
|
||||
rows = matrix.strip().split("\n")
|
||||
ep1 = [[float(num) for num in row.split()] for row in rows]
|
||||
for k in range(len(ep1[0])): ep1[j][k] += ep1[i][k] * g
|
||||
return "\n".join(" ".join(f"{num:10.3e}" for num in row) for row in ep1)
|
||||
|
||||
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.i), str(test.j), str(test.g), str(test.n), str(test.m), 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 = ep1_matrix(initial_matrix, test.i-1, test.j-1, test.g)
|
||||
|
||||
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