From a6c451d86628b6ef2c21a0d4417215dde7f29fff Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Mon, 3 Mar 2025 12:13:04 +0300 Subject: [PATCH] The Fourth and Fifth tasks are done --- .gitignore | 8 +- 2025.03.07/3Ex/matrix.txt | 4 - 2025.03.07/3Ex/test_cases.json | 79 ++++++++++++++++ 2025.03.07/3Ex/test_runner.py | 37 ++++++-- 2025.03.07/4Ex/Makefile | 19 ++++ 2025.03.07/4Ex/array_io.c | 39 ++++++++ 2025.03.07/4Ex/array_io.h | 11 +++ 2025.03.07/4Ex/init_f.c | 30 ++++++ 2025.03.07/4Ex/init_f.h | 9 ++ 2025.03.07/4Ex/io_status.h | 14 +++ 2025.03.07/4Ex/main.c | 65 +++++++++++++ 2025.03.07/4Ex/solve.c | 15 +++ 2025.03.07/4Ex/solve.h | 6 ++ 2025.03.07/4Ex/t.txt | 4 + 2025.03.07/4Ex/test_cases.json | 102 ++++++++++++++++++++ 2025.03.07/4Ex/test_runner.py | 168 +++++++++++++++++++++++++++++++++ 2025.03.07/5Ex/Makefile | 19 ++++ 2025.03.07/5Ex/array_io.c | 39 ++++++++ 2025.03.07/5Ex/array_io.h | 11 +++ 2025.03.07/5Ex/init_f.c | 30 ++++++ 2025.03.07/5Ex/init_f.h | 9 ++ 2025.03.07/5Ex/io_status.h | 14 +++ 2025.03.07/5Ex/main.c | 65 +++++++++++++ 2025.03.07/5Ex/solve.c | 15 +++ 2025.03.07/5Ex/solve.h | 6 ++ 2025.03.07/5Ex/t.txt | 4 + 2025.03.07/5Ex/test_cases.json | 102 ++++++++++++++++++++ 2025.03.07/5Ex/test_runner.py | 168 +++++++++++++++++++++++++++++++++ 28 files changed, 1077 insertions(+), 15 deletions(-) delete mode 100644 2025.03.07/3Ex/matrix.txt create mode 100644 2025.03.07/4Ex/Makefile create mode 100644 2025.03.07/4Ex/array_io.c create mode 100644 2025.03.07/4Ex/array_io.h create mode 100644 2025.03.07/4Ex/init_f.c create mode 100644 2025.03.07/4Ex/init_f.h create mode 100644 2025.03.07/4Ex/io_status.h create mode 100644 2025.03.07/4Ex/main.c create mode 100644 2025.03.07/4Ex/solve.c create mode 100644 2025.03.07/4Ex/solve.h create mode 100644 2025.03.07/4Ex/t.txt create mode 100644 2025.03.07/4Ex/test_cases.json create mode 100644 2025.03.07/4Ex/test_runner.py create mode 100644 2025.03.07/5Ex/Makefile create mode 100644 2025.03.07/5Ex/array_io.c create mode 100644 2025.03.07/5Ex/array_io.h create mode 100644 2025.03.07/5Ex/init_f.c create mode 100644 2025.03.07/5Ex/init_f.h create mode 100644 2025.03.07/5Ex/io_status.h create mode 100644 2025.03.07/5Ex/main.c create mode 100644 2025.03.07/5Ex/solve.c create mode 100644 2025.03.07/5Ex/solve.h create mode 100644 2025.03.07/5Ex/t.txt create mode 100644 2025.03.07/5Ex/test_cases.json create mode 100644 2025.03.07/5Ex/test_runner.py diff --git a/.gitignore b/.gitignore index 67455ea..cf83f7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,15 @@ +# Executable files *.exe -*~ *.out *.o + +# Vim files +*~ .*.sw* + *output* + +# Clion folders and files cmake-build-debug/ .idea/ CMakeLists.txt \ No newline at end of file diff --git a/2025.03.07/3Ex/matrix.txt b/2025.03.07/3Ex/matrix.txt deleted file mode 100644 index 7e4ee86..0000000 --- a/2025.03.07/3Ex/matrix.txt +++ /dev/null @@ -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 diff --git a/2025.03.07/3Ex/test_cases.json b/2025.03.07/3Ex/test_cases.json index c4811a1..8b7d856 100644 --- a/2025.03.07/3Ex/test_cases.json +++ b/2025.03.07/3Ex/test_cases.json @@ -18,6 +18,85 @@ "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 } ] } diff --git a/2025.03.07/3Ex/test_runner.py b/2025.03.07/3Ex/test_runner.py index 44229c0..18f468b 100644 --- a/2025.03.07/3Ex/test_runner.py +++ b/2025.03.07/3Ex/test_runner.py @@ -87,12 +87,27 @@ def format_matrix(matrix): formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split())) 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""" 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: 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 "" def transpose_matrix(matrix): @@ -121,8 +136,8 @@ def run_test(test_suite, test): result = run_command(cmd) # Extract both initial and result matrices - initial_matrix = parse_matrix_output(result.stdout, "Initial matrix") - result_matrix = parse_matrix_output(result.stdout, "Result matrix") + initial_matrix = initial_matrix_output(result.stdout) + result_matrix = result_matrix_output(result.stdout) # Compute expected transposed matrix expected_transposed = transpose_matrix(initial_matrix) @@ -134,13 +149,15 @@ def run_test(test_suite, test): 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)) - # 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) diff --git a/2025.03.07/4Ex/Makefile b/2025.03.07/4Ex/Makefile new file mode 100644 index 0000000..f82bcf8 --- /dev/null +++ b/2025.03.07/4Ex/Makefile @@ -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 diff --git a/2025.03.07/4Ex/array_io.c b/2025.03.07/4Ex/array_io.c new file mode 100644 index 0000000..df278b5 --- /dev/null +++ b/2025.03.07/4Ex/array_io.c @@ -0,0 +1,39 @@ +#include +#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); +} diff --git a/2025.03.07/4Ex/array_io.h b/2025.03.07/4Ex/array_io.h new file mode 100644 index 0000000..8a106c3 --- /dev/null +++ b/2025.03.07/4Ex/array_io.h @@ -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 diff --git a/2025.03.07/4Ex/init_f.c b/2025.03.07/4Ex/init_f.c new file mode 100644 index 0000000..cf325c3 --- /dev/null +++ b/2025.03.07/4Ex/init_f.c @@ -0,0 +1,30 @@ +#include "init_f.h" +#include + +#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); +} diff --git a/2025.03.07/4Ex/init_f.h b/2025.03.07/4Ex/init_f.h new file mode 100644 index 0000000..691aace --- /dev/null +++ b/2025.03.07/4Ex/init_f.h @@ -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 diff --git a/2025.03.07/4Ex/io_status.h b/2025.03.07/4Ex/io_status.h new file mode 100644 index 0000000..a51376d --- /dev/null +++ b/2025.03.07/4Ex/io_status.h @@ -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 diff --git a/2025.03.07/4Ex/main.c b/2025.03.07/4Ex/main.c new file mode 100644 index 0000000..2b0381b --- /dev/null +++ b/2025.03.07/4Ex/main.c @@ -0,0 +1,65 @@ +#include +#include +#include +#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; +} diff --git a/2025.03.07/4Ex/solve.c b/2025.03.07/4Ex/solve.c new file mode 100644 index 0000000..b57ad36 --- /dev/null +++ b/2025.03.07/4Ex/solve.c @@ -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; + } +} diff --git a/2025.03.07/4Ex/solve.h b/2025.03.07/4Ex/solve.h new file mode 100644 index 0000000..88db99c --- /dev/null +++ b/2025.03.07/4Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +void t4_solve(double *a, int n); + +#endif diff --git a/2025.03.07/4Ex/t.txt b/2025.03.07/4Ex/t.txt new file mode 100644 index 0000000..bbba707 --- /dev/null +++ b/2025.03.07/4Ex/t.txt @@ -0,0 +1,4 @@ +1 2 3 4 +1 2 3 4 +1 2 3 4 +1 2 3 4 diff --git a/2025.03.07/4Ex/test_cases.json b/2025.03.07/4Ex/test_cases.json new file mode 100644 index 0000000..e0d8204 --- /dev/null +++ b/2025.03.07/4Ex/test_cases.json @@ -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 + } + ] +} diff --git a/2025.03.07/4Ex/test_runner.py b/2025.03.07/4Ex/test_runner.py new file mode 100644 index 0000000..27fc9ba --- /dev/null +++ b/2025.03.07/4Ex/test_runner.py @@ -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() diff --git a/2025.03.07/5Ex/Makefile b/2025.03.07/5Ex/Makefile new file mode 100644 index 0000000..53339c6 --- /dev/null +++ b/2025.03.07/5Ex/Makefile @@ -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 diff --git a/2025.03.07/5Ex/array_io.c b/2025.03.07/5Ex/array_io.c new file mode 100644 index 0000000..df278b5 --- /dev/null +++ b/2025.03.07/5Ex/array_io.c @@ -0,0 +1,39 @@ +#include +#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); +} diff --git a/2025.03.07/5Ex/array_io.h b/2025.03.07/5Ex/array_io.h new file mode 100644 index 0000000..8a106c3 --- /dev/null +++ b/2025.03.07/5Ex/array_io.h @@ -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 diff --git a/2025.03.07/5Ex/init_f.c b/2025.03.07/5Ex/init_f.c new file mode 100644 index 0000000..cf325c3 --- /dev/null +++ b/2025.03.07/5Ex/init_f.c @@ -0,0 +1,30 @@ +#include "init_f.h" +#include + +#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); +} diff --git a/2025.03.07/5Ex/init_f.h b/2025.03.07/5Ex/init_f.h new file mode 100644 index 0000000..691aace --- /dev/null +++ b/2025.03.07/5Ex/init_f.h @@ -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 diff --git a/2025.03.07/5Ex/io_status.h b/2025.03.07/5Ex/io_status.h new file mode 100644 index 0000000..a51376d --- /dev/null +++ b/2025.03.07/5Ex/io_status.h @@ -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 diff --git a/2025.03.07/5Ex/main.c b/2025.03.07/5Ex/main.c new file mode 100644 index 0000000..7427c9c --- /dev/null +++ b/2025.03.07/5Ex/main.c @@ -0,0 +1,65 @@ +#include +#include +#include +#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; +} diff --git a/2025.03.07/5Ex/solve.c b/2025.03.07/5Ex/solve.c new file mode 100644 index 0000000..170d21a --- /dev/null +++ b/2025.03.07/5Ex/solve.c @@ -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; +} diff --git a/2025.03.07/5Ex/solve.h b/2025.03.07/5Ex/solve.h new file mode 100644 index 0000000..33d7e92 --- /dev/null +++ b/2025.03.07/5Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +void t5_solve(double *a, int n); + +#endif diff --git a/2025.03.07/5Ex/t.txt b/2025.03.07/5Ex/t.txt new file mode 100644 index 0000000..bbba707 --- /dev/null +++ b/2025.03.07/5Ex/t.txt @@ -0,0 +1,4 @@ +1 2 3 4 +1 2 3 4 +1 2 3 4 +1 2 3 4 diff --git a/2025.03.07/5Ex/test_cases.json b/2025.03.07/5Ex/test_cases.json new file mode 100644 index 0000000..23efa52 --- /dev/null +++ b/2025.03.07/5Ex/test_cases.json @@ -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 + } + ] +} diff --git a/2025.03.07/5Ex/test_runner.py b/2025.03.07/5Ex/test_runner.py new file mode 100644 index 0000000..a59c370 --- /dev/null +++ b/2025.03.07/5Ex/test_runner.py @@ -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()